1 64 65 package com.jcorporate.expresso.core.security; 66 67 import com.jcorporate.expresso.core.misc.ByteArrayCounter; 68 import com.jcorporate.expresso.core.misc.ConfigManager; 69 import com.jcorporate.expresso.kernel.ComponentLifecycle; 70 import com.jcorporate.expresso.kernel.Configuration; 71 import com.jcorporate.expresso.kernel.exception.ChainedException; 72 import com.jcorporate.expresso.kernel.exception.ConfigurationException; 73 74 75 86 public class CryptoManager extends com.jcorporate.expresso.kernel.ComponentBase implements ComponentLifecycle { 87 88 static final private String thisClass = "com.jcorporate.expresso.core.security.CryptoManager"; 90 static private CryptoManager theManager = null; 91 static protected ByteArrayCounter ivCounter = new ByteArrayCounter(8); 92 protected AbstractRandomNumber randomGenerator = null; 93 protected AbstractStringEncryption stringEncryptor = null; 94 protected StringHash stringHash = null; 95 boolean initialized; 96 97 String encryptMode; 98 99 102 boolean strongCrypto = false; 103 104 107 private String cryptoKey; 108 private String randomSeed; 109 110 114 public CryptoManager() { 115 } 116 117 126 public static synchronized CryptoManager getInstance() 127 throws ChainedException { 128 if (theManager == null) { 129 theManager = new CryptoManager(); 130 boolean strongCrypto = false; 131 132 strongCrypto = ConfigManager.getConfig().strongCrypto(); 133 134 theManager.setEncryptMode(ConfigManager.getConfig().getEncryptMode()); 135 theManager.setCryptoKey(ConfigManager.getConfig().getCryptoKey()); 136 theManager.setRandomSeed(ConfigManager.getConfigDir()); 137 theManager.setStrongCrypto(strongCrypto); 138 139 if (!strongCrypto) { 141 theManager.loadClasses(false); 142 } else { 143 theManager.loadClasses(true); 144 } 145 } 146 147 return theManager; 148 } 149 150 151 154 public synchronized void destroy() { 155 stringEncryptor.destroy(); 156 randomGenerator = null; 157 stringHash = null; 158 stringEncryptor = null; 159 theManager = null; 160 161 } 162 163 168 public synchronized boolean isUsingStrongCrypto() { 169 return strongCrypto; 170 } 171 172 181 public synchronized AbstractRandomNumber getRandomGenerator() { 182 return randomGenerator; 183 } 184 185 195 public synchronized AbstractStringEncryption getStringEncryption() { 196 return stringEncryptor; 197 } 198 199 207 public synchronized StringHash getStringHash() { 208 return stringHash; 209 } 210 211 219 public synchronized void loadClasses(boolean useStrongCrypto) 220 throws ChainedException { 221 String packageName; 222 final String myName = thisClass + ".loadClasses"; 223 strongCrypto = useStrongCrypto; 224 225 if (useStrongCrypto == false) { 227 packageName = "com.jcorporate.expresso.core.security.weakencryption"; 228 } else { 229 230 packageName = "com.jcorporate.expresso.core.security.strongencryption"; 232 } 233 try { 235 randomGenerator = (AbstractRandomNumber) Class.forName(packageName + 236 ".RandomNumber").newInstance(); 237 stringEncryptor = (AbstractStringEncryption) Class.forName(packageName + 238 ".StringEncryption").newInstance(); 239 stringHash = new StringHash(); 240 241 randomGenerator.setCryptoManager(this); 242 stringEncryptor.setCryptoManager(this); 243 stringHash.setCryptoManager(this); 244 stringEncryptor.init(); 245 randomGenerator.init(); 246 } catch (ClassNotFoundException ex) { 247 throw new ChainedException(myName + 248 " Unable to load crypto class in package " + 249 packageName, ex); 250 } catch (IllegalAccessException ex) { 251 throw new ChainedException(myName + ":Package " + packageName, ex); 252 } catch (InstantiationException ex) { 253 throw new ChainedException(myName + 254 " Unable to instantiate a cryto class in package " + 255 packageName, ex); 256 } catch (ChainedException e) { 257 throw e; 258 } catch (Exception e) { 259 throw new ChainedException(myName + 260 ":Unable to load classes for crypto package " + 261 packageName, e); 262 } 263 } 264 265 public synchronized void initialize() { 266 theManager = this; 267 } 268 269 275 public synchronized void configure(Configuration newConfig) throws ConfigurationException { 276 Boolean strongCrypto = (Boolean ) newConfig.get("StrongCrypto"); 277 this.setStrongCrypto(strongCrypto.booleanValue()); 278 this.setEncryptMode((String ) newConfig.get("EncryptMode")); 279 this.setCryptoKey((String ) newConfig.get("CryptoKey")); 280 this.setRandomSeed((String ) newConfig.get("RandomSeed")); 281 282 283 try { 285 if (!strongCrypto.booleanValue()) { 286 theManager.loadClasses(false); 287 } else { 288 theManager.loadClasses(true); 289 } 290 } catch (ChainedException ex) { 291 throw new ConfigurationException("Error loading cryptographic manager.", ex); 292 } 293 } 294 295 301 public synchronized void reconfigure(Configuration newConfig) throws ConfigurationException { 302 destroy(); 303 setStrongCrypto(false); 304 setEncryptMode(null); 305 setCryptoKey(null); 306 setRandomSeed(null); 307 configure(newConfig); 308 } 309 310 public synchronized boolean isStrongCrypto() { 311 return strongCrypto; 312 } 313 314 public synchronized void setStrongCrypto(boolean strongCrypto) { 315 this.strongCrypto = strongCrypto; 316 } 317 318 public synchronized String getEncryptMode() { 319 return encryptMode; 320 } 321 322 public synchronized void setEncryptMode(String encryptMode) { 323 this.encryptMode = encryptMode; 324 } 325 326 public boolean isInitialized() { 327 return initialized; 328 } 329 330 public void setCryptoKey(String cryptoKey) { 331 this.cryptoKey = cryptoKey; 332 } 333 334 public String getCryptoKey() { 335 return cryptoKey; 336 } 337 338 public void setRandomSeed(String randomSeed) { 339 this.randomSeed = randomSeed; 340 } 341 342 public String getRandomSeed() { 343 return randomSeed; 344 } 345 346 347 } | Popular Tags |