1 20 21 package org.snmp4j.security; 22 23 import java.io.Serializable ; 24 import org.snmp4j.smi.OID; 25 import java.io.InputStream ; 26 import java.util.Properties ; 27 import java.util.Enumeration ; 28 import org.snmp4j.log.*; 29 import java.io.IOException ; 30 import java.util.Hashtable ; 31 import org.snmp4j.smi.OctetString; 32 import org.snmp4j.SNMP4JSettings; 33 34 46 public class SecurityProtocols implements Serializable { 47 48 private static final long serialVersionUID = 3800474900139635836L; 49 50 private java.util.Hashtable authProtocols; 51 private java.util.Hashtable privProtocols; 52 53 public static final String SECURITY_PROTOCOLS_PROPERTIES = 54 "org.snmp4j.securityProtocols"; 55 private static final String SECURITY_PROTOCOLS_PROPERTIES_DEFAULT = 56 "SecurityProtocols.properties"; 57 private static final LogAdapter logger = LogFactory.getLogger(SecurityProtocols.class); 58 59 private static SecurityProtocols instance = null; 60 private int maxAuthDigestLength = 0; 61 private int maxPrivDecryptParamsLength = 0; 62 63 protected SecurityProtocols() { 64 authProtocols = new Hashtable (5); 65 privProtocols = new Hashtable (5); 66 } 67 68 73 public static SecurityProtocols getInstance() { 74 if (instance == null) { 75 instance = new SecurityProtocols(); 76 } 77 return instance; 78 } 79 80 84 public static void setSecurityProtocols(SecurityProtocols securityProtocols) { 85 SecurityProtocols.instance = securityProtocols; 86 } 87 88 96 public synchronized void addDefaultProtocols() { 97 if (SNMP4JSettings.isExtensibilityEnabled()) { 98 String secProtocols = 99 System.getProperty(SECURITY_PROTOCOLS_PROPERTIES, 100 SECURITY_PROTOCOLS_PROPERTIES_DEFAULT); 101 InputStream is = 102 SecurityProtocols.class.getResourceAsStream(secProtocols); 103 if (is == null) { 104 throw new InternalError ("Could not read '" + secProtocols + 105 "' from classpath!"); 106 } 107 Properties props = new Properties (); 108 try { 109 props.load(is); 110 for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) { 111 String className = (String ) en.nextElement(); 112 try { 113 Class c = Class.forName(className); 114 Object proto = c.newInstance(); 115 if (proto instanceof AuthenticationProtocol) { 116 addAuthenticationProtocol((AuthenticationProtocol) proto); 117 } 118 else if (proto instanceof PrivacyProtocol) { 119 addPrivacyProtocol((PrivacyProtocol) proto); 120 } 121 else { 122 logger.error( 123 "Failed to register security protocol because it does " + 124 "not implement required interfaces: " + className); 125 } 126 } 127 catch (Exception cnfe) { 128 logger.error(cnfe); 129 throw new InternalError (cnfe.toString()); 130 } 131 } 132 } 133 catch (IOException iox) { 134 String txt = "Could not read '" + secProtocols + "': " + 135 iox.getMessage(); 136 logger.error(txt); 137 throw new InternalError (txt); 138 } 139 finally { 140 try { 141 is.close(); 142 } 143 catch (IOException ex) { 144 logger.warn(ex); 146 } 147 } 148 } 149 else { 150 addAuthenticationProtocol(new AuthMD5()); 151 addAuthenticationProtocol(new AuthSHA()); 152 addPrivacyProtocol(new PrivDES()); 153 addPrivacyProtocol(new PrivAES128()); 154 addPrivacyProtocol(new PrivAES192()); 155 addPrivacyProtocol(new PrivAES256()); 156 } 157 } 158 159 168 public synchronized void addAuthenticationProtocol(AuthenticationProtocol auth) { 169 if (authProtocols.get(auth.getID()) == null) { 170 authProtocols.put(auth.getID(), auth); 171 if (auth.getDigestLength() > maxAuthDigestLength) { 172 maxAuthDigestLength = auth.getDigestLength(); 173 } 174 } 175 } 176 177 186 public AuthenticationProtocol getAuthenticationProtocol(OID id) { 187 if (id == null) { 188 return null; 189 } 190 return (AuthenticationProtocol)authProtocols.get(id); 191 } 192 193 198 public void removeAuthenticationProtocol(AuthenticationProtocol auth) { 199 authProtocols.remove(auth.getID()); 200 } 201 202 211 public synchronized void addPrivacyProtocol(PrivacyProtocol priv) { 212 if (privProtocols.get(priv.getID()) == null) { 213 privProtocols.put(priv.getID(), priv); 214 if (priv.getDecryptParamsLength() > maxPrivDecryptParamsLength) { 215 maxPrivDecryptParamsLength = priv.getDecryptParamsLength(); 216 } 217 } 218 } 219 220 229 public PrivacyProtocol getPrivacyProtocol(OID id) { 230 if (id == null) { 231 return null; 232 } 233 return (PrivacyProtocol)privProtocols.get(id); 234 } 235 236 241 public void removePrivacyProtocol(PrivacyProtocol priv) { 242 privProtocols.remove(priv.getID()); 243 } 244 245 246 260 public byte[] passwordToKey(OID authProtocolID, 261 OctetString passwordString, 262 byte[] engineID) { 263 264 AuthenticationProtocol protocol = 265 (AuthenticationProtocol)authProtocols.get(authProtocolID); 266 if (protocol == null) { 267 return null; 268 } 269 return protocol.passwordToKey(passwordString, engineID); 270 } 271 272 288 public byte[] passwordToKey(OID privProtocolID, 289 OID authProtocolID, 290 OctetString passwordString, 291 byte[] engineID) { 292 293 AuthenticationProtocol authProtocol = 294 (AuthenticationProtocol)authProtocols.get(authProtocolID); 295 if (authProtocol == null) { 296 return null; 297 } 298 PrivacyProtocol privProtocol = 299 (PrivacyProtocol)privProtocols.get(privProtocolID); 300 if (privProtocol == null) { 301 return null; 302 } 303 byte[] key = authProtocol.passwordToKey(passwordString, engineID); 304 305 if (key == null) { 306 return null; 307 } 308 if (key.length >= privProtocol.getMinKeyLength()) { 309 if (key.length > privProtocol.getMaxKeyLength()) { 310 byte[] truncatedKey = new byte[privProtocol.getMaxKeyLength()]; 312 System.arraycopy(key, 0, truncatedKey, 0, privProtocol.getMaxKeyLength()); 313 return truncatedKey; 314 } 315 return key; 316 } 317 byte[] extKey = new byte[privProtocol.getMinKeyLength()]; 321 int length = key.length; 322 for (int i=0; i<length; i++) { 323 extKey[i] = key[i]; 324 } 325 326 while (length < extKey.length) 327 { 328 byte[] hash = authProtocol.hash(extKey, 0, length); 329 330 if (hash == null) { 331 return null; 332 } 333 int bytesToCopy = extKey.length - length; 334 if (bytesToCopy > authProtocol.getDigestLength()) { 335 bytesToCopy = authProtocol.getDigestLength(); 336 } 337 for (int i=0; i<bytesToCopy; i++) { 338 extKey[length + i] = hash[i]; 339 } 340 341 length += bytesToCopy; 342 } 343 return extKey; 344 } 345 346 354 public int getMaxAuthDigestLength() { 355 return maxAuthDigestLength; 356 } 357 358 366 public int getMaxPrivDecryptParamsLength() { 367 return maxPrivDecryptParamsLength; 368 } 369 } 370 371 | Popular Tags |