1 20 package org.apache.mina.integration.spring.ssl; 21 22 import java.security.KeyStore ; 23 import java.security.SecureRandom ; 24 25 import javax.net.ssl.KeyManager; 26 import javax.net.ssl.KeyManagerFactory; 27 import javax.net.ssl.SSLContext; 28 import javax.net.ssl.TrustManager; 29 import javax.net.ssl.TrustManagerFactory; 30 import javax.net.ssl.ManagerFactoryParameters; 31 32 import org.springframework.beans.factory.config.AbstractFactoryBean; 33 import org.springframework.util.Assert; 34 35 59 public class SSLContextFactoryBean extends AbstractFactoryBean { 60 private String protocol = "TLS"; 61 62 private String provider = null; 63 64 private SecureRandom secureRandom = null; 65 66 private KeyStore keyManagerFactoryKeyStore = null; 67 68 private char[] keyManagerFactoryKeyStorePassword = null; 69 70 private KeyManagerFactory keyManagerFactory = null; 71 72 private String keyManagerFactoryAlgorithm = null; 73 74 private String keyManagerFactoryProvider = null; 75 76 private boolean keyManagerFactoryAlgorithmUseDefault = false; 77 78 private KeyStore trustManagerFactoryKeyStore = null; 79 80 private TrustManagerFactory trustManagerFactory = null; 81 82 private String trustManagerFactoryAlgorithm = null; 83 84 private String trustManagerFactoryProvider = null; 85 86 private boolean trustManagerFactoryAlgorithmUseDefault = false; 87 88 private ManagerFactoryParameters trustManagerFactoryParameters = null; 89 90 protected Object createInstance() throws Exception { 91 KeyManagerFactory kmf = this.keyManagerFactory; 92 TrustManagerFactory tmf = this.trustManagerFactory; 93 94 if (kmf == null) { 95 String algorithm = keyManagerFactoryAlgorithm; 96 if (algorithm == null && keyManagerFactoryAlgorithmUseDefault) { 97 algorithm = KeyManagerFactory.getDefaultAlgorithm(); 98 } 99 if (algorithm != null) { 100 if (keyManagerFactoryProvider == null) { 101 kmf = KeyManagerFactory.getInstance(algorithm); 102 } else { 103 kmf = KeyManagerFactory.getInstance(algorithm, 104 keyManagerFactoryProvider); 105 } 106 } 107 } 108 109 if (tmf == null) { 110 String algorithm = trustManagerFactoryAlgorithm; 111 if (algorithm == null && trustManagerFactoryAlgorithmUseDefault) { 112 algorithm = TrustManagerFactory.getDefaultAlgorithm(); 113 } 114 if (algorithm != null) { 115 if (trustManagerFactoryProvider == null) { 116 tmf = TrustManagerFactory.getInstance(algorithm); 117 } else { 118 tmf = TrustManagerFactory.getInstance(algorithm, 119 trustManagerFactoryProvider); 120 } 121 } 122 } 123 124 KeyManager[] keyManagers = null; 125 if (kmf != null) { 126 kmf.init(keyManagerFactoryKeyStore, 127 keyManagerFactoryKeyStorePassword); 128 keyManagers = kmf.getKeyManagers(); 129 } 130 TrustManager[] trustManagers = null; 131 if (tmf != null) { 132 if (trustManagerFactoryParameters != null) { 133 tmf.init(trustManagerFactoryParameters); 134 } else { 135 tmf.init(trustManagerFactoryKeyStore); 136 } 137 trustManagers = tmf.getTrustManagers(); 138 } 139 140 SSLContext context = null; 141 if (provider == null) { 142 context = SSLContext.getInstance(protocol); 143 } else { 144 context = SSLContext.getInstance(protocol, provider); 145 } 146 147 context.init(keyManagers, trustManagers, secureRandom); 148 149 return context; 150 } 151 152 public Class getObjectType() { 153 return SSLContext.class; 154 } 155 156 164 public void setProtocol(String protocol) { 165 Assert.notNull(protocol, "Property 'protocol' may not be null"); 166 this.protocol = protocol; 167 } 168 169 178 public void setKeyManagerFactoryAlgorithmUseDefault(boolean useDefault) { 179 this.keyManagerFactoryAlgorithmUseDefault = useDefault; 180 } 181 182 191 public void setTrustManagerFactoryAlgorithmUseDefault(boolean useDefault) { 192 this.trustManagerFactoryAlgorithmUseDefault = useDefault; 193 } 194 195 202 public void setKeyManagerFactory(KeyManagerFactory factory) { 203 this.keyManagerFactory = factory; 204 } 205 206 224 public void setKeyManagerFactoryAlgorithm(String algorithm) { 225 this.keyManagerFactoryAlgorithm = algorithm; 226 } 227 228 245 public void setKeyManagerFactoryProvider(String provider) { 246 this.keyManagerFactoryProvider = provider; 247 } 248 249 256 public void setKeyManagerFactoryKeyStore(KeyStore keyStore) { 257 this.keyManagerFactoryKeyStore = keyStore; 258 } 259 260 267 public void setKeyManagerFactoryKeyStorePassword(String password) { 268 if (password != null) { 269 this.keyManagerFactoryKeyStorePassword = password.toCharArray(); 270 } else { 271 this.keyManagerFactoryKeyStorePassword = null; 272 } 273 } 274 275 282 public void setTrustManagerFactory(TrustManagerFactory factory) { 283 this.trustManagerFactory = factory; 284 } 285 286 304 public void setTrustManagerFactoryAlgorithm(String algorithm) { 305 this.trustManagerFactoryAlgorithm = algorithm; 306 } 307 308 319 public void setTrustManagerFactoryKeyStore(KeyStore keyStore) { 320 this.trustManagerFactoryKeyStore = keyStore; 321 } 322 323 330 public void setTrustManagerFactoryParameters( 331 ManagerFactoryParameters parameters) { 332 this.trustManagerFactoryParameters = parameters; 333 } 334 335 352 public void setTrustManagerFactoryProvider(String provider) { 353 this.trustManagerFactoryProvider = provider; 354 } 355 356 364 public void setSecureRandom(SecureRandom secureRandom) { 365 this.secureRandom = secureRandom; 366 } 367 368 } 369 | Popular Tags |