1 24 25 package org.objectweb.cjdbc.common.net; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 import java.security.GeneralSecurityException ; 31 import java.security.KeyStore ; 32 33 import javax.net.ServerSocketFactory; 34 import javax.net.SocketFactory; 35 import javax.net.ssl.SSLServerSocketFactory; 36 import javax.net.ssl.SSLSocketFactory; 37 38 import com.sun.net.ssl.KeyManager; 39 import com.sun.net.ssl.KeyManagerFactory; 40 import com.sun.net.ssl.SSLContext; 41 import com.sun.net.ssl.TrustManager; 42 import com.sun.net.ssl.TrustManagerFactory; 43 44 50 public class SocketFactoryFactory 51 { 52 53 60 public static ServerSocketFactory createServerFactory(SSLConfiguration config) 61 throws SSLException 62 { 63 try 64 { 65 66 if (config == null) 67 return ServerSocketFactory.getDefault(); 69 70 SSLContext context = createSSLContext(config); 71 SSLServerSocketFactory ssf = context.getServerSocketFactory(); 73 74 if (!config.isClientAuthenticationRequired()) 75 return ssf; 76 77 return new AuthenticatedServerSocketFactory(ssf); 78 } 79 catch (Exception e) 80 { 81 throw new SSLException(e); 82 } 83 } 84 85 92 public static SocketFactory createFactory(SSLConfiguration config) 93 throws Exception 94 { 95 if (config == null) 96 return SocketFactory.getDefault(); 98 99 SSLContext context = createSSLContext(config); 100 101 SSLSocketFactory ssf = context.getSocketFactory(); 103 104 if (!config.isClientAuthenticationRequired()) 105 return ssf; 106 107 return new AuthenticatedSocketFactory(ssf); 108 } 109 110 117 public static SSLContext createSSLContext(SSLConfiguration config) 118 throws Exception 119 { 120 121 KeyManager[] kms = getKeyManagers(config.getKeyStore(), config 122 .getKeyStorePassword(), config.getKeyStoreKeyPassword()); 123 124 TrustManager[] tms = getTrustManagers(config.getTrustStore(), config 125 .getTrustStorePassword()); 126 127 SSLContext context = SSLContext.getInstance("SSL"); 131 context.init(kms, tms, null); 132 return context; 133 } 134 135 protected static KeyManager[] getKeyManagers(File keyStore, 136 String keyStorePassword, String keyPassword) throws IOException , 137 GeneralSecurityException 138 { 139 String alg = KeyManagerFactory.getDefaultAlgorithm(); 141 KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg); 142 143 FileInputStream fis = new FileInputStream (keyStore); 146 KeyStore ks = KeyStore.getInstance("jks"); 147 148 char[] passwd = null; 149 if (keyStorePassword != null) 150 { 151 passwd = keyStorePassword.toCharArray(); 152 } 153 ks.load(fis, passwd); 154 fis.close(); 155 156 kmFact.init(ks, keyPassword.toCharArray()); 158 159 KeyManager[] kms = kmFact.getKeyManagers(); 161 return kms; 162 } 163 164 protected static TrustManager[] getTrustManagers(File trustStore, 165 String trustStorePassword) throws IOException , GeneralSecurityException 166 { 167 String alg = TrustManagerFactory.getDefaultAlgorithm(); 169 TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); 170 171 FileInputStream fis = new FileInputStream (trustStore); 174 KeyStore ks = KeyStore.getInstance("jks"); 175 ks.load(fis, trustStorePassword.toCharArray()); 176 fis.close(); 177 178 tmFact.init(ks); 180 181 TrustManager[] tms = tmFact.getTrustManagers(); 183 return tms; 184 } 185 } 186 | Popular Tags |