1 22 23 package org.continuent.sequoia.common.net; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.security.GeneralSecurityException ; 29 import java.security.KeyStore ; 30 31 import javax.net.ServerSocketFactory; 32 import javax.net.SocketFactory; 33 import javax.net.ssl.SSLServerSocketFactory; 34 import javax.net.ssl.SSLSocketFactory; 35 36 import com.sun.net.ssl.KeyManager; 37 import com.sun.net.ssl.KeyManagerFactory; 38 import com.sun.net.ssl.SSLContext; 39 import com.sun.net.ssl.TrustManager; 40 import com.sun.net.ssl.TrustManagerFactory; 41 42 48 public class SocketFactoryFactory 49 { 50 51 58 public static ServerSocketFactory createServerFactory(SSLConfiguration config) 59 throws SSLException 60 { 61 try 62 { 63 64 if (config == null) 65 return ServerSocketFactory.getDefault(); 67 68 SSLContext context = createSSLContext(config); 69 SSLServerSocketFactory ssf = context.getServerSocketFactory(); 71 72 if (!config.isClientAuthenticationRequired()) 73 return ssf; 74 75 return new AuthenticatedServerSocketFactory(ssf); 76 } 77 catch (Exception e) 78 { 79 throw new SSLException(e); 80 } 81 } 82 83 90 public static SocketFactory createFactory(SSLConfiguration config) 91 throws Exception  92 { 93 if (config == null) 94 return SocketFactory.getDefault(); 96 97 SSLContext context = createSSLContext(config); 98 99 SSLSocketFactory ssf = context.getSocketFactory(); 101 102 if (!config.isClientAuthenticationRequired()) 103 return ssf; 104 105 return new AuthenticatedSocketFactory(ssf); 106 } 107 108 115 public static SSLContext createSSLContext(SSLConfiguration config) 116 throws Exception  117 { 118 119 KeyManager[] kms = getKeyManagers(config.getKeyStore(), config 120 .getKeyStorePassword(), config.getKeyStoreKeyPassword()); 121 122 TrustManager[] tms = getTrustManagers(config.getTrustStore(), config 123 .getTrustStorePassword()); 124 125 SSLContext context = SSLContext.getInstance("SSL"); 129 context.init(kms, tms, null); 130 return context; 131 } 132 133 protected static KeyManager[] getKeyManagers(File keyStore, 134 String keyStorePassword, String keyPassword) throws IOException , 135 GeneralSecurityException  136 { 137 String alg = KeyManagerFactory.getDefaultAlgorithm(); 139 KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg); 140 141 FileInputStream fis = new FileInputStream (keyStore); 144 KeyStore ks = KeyStore.getInstance("jks"); 145 146 char[] passwd = null; 147 if (keyStorePassword != null) 148 { 149 passwd = keyStorePassword.toCharArray(); 150 } 151 ks.load(fis, passwd); 152 fis.close(); 153 154 kmFact.init(ks, keyPassword.toCharArray()); 156 157 KeyManager[] kms = kmFact.getKeyManagers(); 159 return kms; 160 } 161 162 protected static TrustManager[] getTrustManagers(File trustStore, 163 String trustStorePassword) throws IOException , GeneralSecurityException  164 { 165 String alg = TrustManagerFactory.getDefaultAlgorithm(); 167 TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); 168 169 FileInputStream fis = new FileInputStream (trustStore); 172 KeyStore ks = KeyStore.getInstance("jks"); 173 ks.load(fis, trustStorePassword.toCharArray()); 174 fis.close(); 175 176 tmFact.init(ks); 178 179 TrustManager[] tms = tmFact.getTrustManagers(); 181 return tms; 182 } 183 } 184
| Popular Tags
|