1 17 package org.apache.servicemix.jbi.security.keystore.impl; 18 19 import java.security.KeyManagementException ; 20 import java.security.KeyStoreException ; 21 import java.security.NoSuchAlgorithmException ; 22 import java.security.NoSuchProviderException ; 23 import java.security.SecureRandom ; 24 import java.security.UnrecoverableKeyException ; 25 26 import javax.net.ssl.SSLContext; 27 import javax.net.ssl.SSLServerSocketFactory; 28 import javax.net.ssl.SSLSocketFactory; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.servicemix.jbi.security.keystore.KeyIsLocked; 33 import org.apache.servicemix.jbi.security.keystore.KeystoreInstance; 34 import org.apache.servicemix.jbi.security.keystore.KeystoreIsLocked; 35 import org.apache.servicemix.jbi.security.keystore.KeystoreManager; 36 37 42 public class BaseKeystoreManager implements KeystoreManager { 43 44 protected final Log log = LogFactory.getLog(getClass()); 45 46 protected KeystoreInstance[] keystores; 47 48 51 public KeystoreInstance[] getKeystores() { 52 return keystores; 53 } 54 55 58 public void setKeystores(KeystoreInstance[] keystores) { 59 this.keystores = keystores; 60 } 61 62 99 public SSLSocketFactory createSSLFactory( 100 String provider, 101 String protocol, 102 String algorithm, 103 String keyStore, 104 String keyAlias, 105 String trustStore) throws KeystoreIsLocked, KeyIsLocked, 106 NoSuchAlgorithmException , UnrecoverableKeyException , KeyStoreException , KeyManagementException , 107 NoSuchProviderException { 108 KeystoreInstance keyInstance = null; 110 if (keyStore != null) { 111 keyInstance = getKeystore(keyStore); 112 if (keyInstance.isKeystoreLocked()) { 113 throw new KeystoreIsLocked("Keystore '" + keyStore 114 + "' is locked; please use the keystore page in the admin console to unlock it"); 115 } 116 if (keyInstance.isKeyLocked(keyAlias)) { 117 throw new KeystoreIsLocked("Key '" + keyAlias + "' in keystore '" + keyStore 118 + "' is locked; please use the keystore page in the admin console to unlock it"); 119 } 120 } 121 KeystoreInstance trustInstance = trustStore == null ? null : getKeystore(trustStore); 122 if (trustInstance != null && trustInstance.isKeystoreLocked()) { 123 throw new KeystoreIsLocked("Keystore '" + trustStore 124 + "' is locked; please use the keystore page in the admin console to unlock it"); 125 } 126 127 try { 130 145 SSLContext context; 146 if (provider == null) { 147 context = SSLContext.getInstance(protocol); 148 } else { 149 context= SSLContext.getInstance(protocol, provider); 150 } 151 context.init(keyInstance == null ? null : keyInstance.getKeyManager(algorithm, keyAlias), 152 trustInstance == null ? null : trustInstance.getTrustManager(algorithm), 153 new SecureRandom ()); 154 return context.getSocketFactory(); 155 } catch (Exception e) { 156 log.error("Unable to dynamically load", e); 157 return null; 158 } 159 } 160 161 192 public SSLServerSocketFactory createSSLServerFactory( 193 String provider, 194 String protocol, 195 String algorithm, 196 String keyStore, 197 String keyAlias, 198 String trustStore) throws KeystoreIsLocked, 199 KeyIsLocked, NoSuchAlgorithmException , UnrecoverableKeyException , KeyStoreException , 200 KeyManagementException , NoSuchProviderException { 201 KeystoreInstance keyInstance = getKeystore(keyStore); 202 if (keyInstance.isKeystoreLocked()) { 203 throw new KeystoreIsLocked("Keystore '" + keyStore 204 + "' is locked; please use the keystore page in the admin console to unlock it"); 205 } 206 if (keyInstance.isKeyLocked(keyAlias)) { 207 throw new KeystoreIsLocked("Key '" + keyAlias + "' in keystore '" + keyStore 208 + "' is locked; please use the keystore page in the admin console to unlock it"); 209 } 210 KeystoreInstance trustInstance = trustStore == null ? null : getKeystore(trustStore); 211 if (trustInstance != null && trustInstance.isKeystoreLocked()) { 212 throw new KeystoreIsLocked("Keystore '" + trustStore 213 + "' is locked; please use the keystore page in the admin console to unlock it"); 214 } 215 216 try { 219 234 SSLContext context; 235 if (provider == null) { 236 context = SSLContext.getInstance(protocol); 237 } else { 238 context= SSLContext.getInstance(protocol, provider); 239 } 240 context.init(keyInstance == null ? null : keyInstance.getKeyManager(algorithm, keyAlias), 241 trustInstance == null ? null : trustInstance.getTrustManager(algorithm), 242 new SecureRandom ()); 243 return context.getServerSocketFactory(); 244 } catch (Exception e) { 245 log.error("Unable to dynamically load", e); 246 return null; 247 } 248 } 249 250 public KeystoreInstance getKeystore(String name) { 251 if (keystores != null) { 252 for (int i = 0; i < keystores.length; i++) { 253 if (name.equals(keystores[i].getName())) { 254 return keystores[i]; 255 } 256 } 257 } 258 return null; 259 } 260 261 } 262 | Popular Tags |