00001
00025
package org.objectweb.cjdbc.common.net;
00026
00027
import java.io.File;
00028
import java.io.FileInputStream;
00029
import java.io.IOException;
00030
import java.security.GeneralSecurityException;
00031
import java.security.KeyStore;
00032
00033
import javax.net.ServerSocketFactory;
00034
import javax.net.SocketFactory;
00035
import javax.net.ssl.SSLServerSocketFactory;
00036
import javax.net.ssl.SSLSocketFactory;
00037
00038
import com.sun.net.ssl.KeyManager;
00039
import com.sun.net.ssl.KeyManagerFactory;
00040
import com.sun.net.ssl.SSLContext;
00041
import com.sun.net.ssl.TrustManager;
00042
import com.sun.net.ssl.TrustManagerFactory;
00043
00050 public class SocketFactoryFactory
00051 {
00052
00060 public static ServerSocketFactory
createServerFactory(
SSLConfiguration config)
00061
throws SSLException
00062 {
00063
try
00064 {
00065
00066
if (config == null)
00067
00068
return ServerSocketFactory.getDefault();
00069
00070 SSLContext context =
createSSLContext(config);
00071
00072 SSLServerSocketFactory ssf = context.getServerSocketFactory();
00073
00074
if (!config.isClientAuthenticationRequired())
00075
return ssf;
00076
00077
return new AuthenticatedServerSocketFactory(ssf);
00078 }
00079
catch (Exception e)
00080 {
00081
throw new SSLException(e);
00082 }
00083 }
00084
00092 public static SocketFactory
createFactory(
SSLConfiguration config)
00093
throws Exception
00094 {
00095
if (config == null)
00096
00097
return SocketFactory.getDefault();
00098
00099 SSLContext context =
createSSLContext(config);
00100
00101
00102 SSLSocketFactory ssf = context.getSocketFactory();
00103
00104
if (!config.isClientAuthenticationRequired())
00105
return ssf;
00106
00107
return new AuthenticatedSocketFactory(ssf);
00108 }
00109
00117 public static SSLContext
createSSLContext(
SSLConfiguration config)
00118
throws Exception
00119 {
00120
00121 KeyManager[] kms =
getKeyManagers(config.getKeyStore(), config
00122 .getKeyStorePassword(), config.getKeyStoreKeyPassword());
00123
00124 TrustManager[] tms =
getTrustManagers(config.getTrustStore(), config
00125 .getTrustStorePassword());
00126
00127
00128
00129
00130 SSLContext context = SSLContext.getInstance(
"SSL");
00131 context.init(kms, tms, null);
00132
return context;
00133 }
00134
00135 protected static KeyManager[]
getKeyManagers(File keyStore,
00136 String keyStorePassword, String keyPassword)
throws IOException,
00137 GeneralSecurityException
00138 {
00139
00140 String alg = KeyManagerFactory.getDefaultAlgorithm();
00141 KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg);
00142
00143
00144
00145 FileInputStream fis =
new FileInputStream(keyStore);
00146 KeyStore ks = KeyStore.getInstance(
"jks");
00147
00148
char[] passwd = null;
00149
if (keyStorePassword != null)
00150 {
00151 passwd = keyStorePassword.toCharArray();
00152 }
00153 ks.load(fis, passwd);
00154 fis.close();
00155
00156
00157 kmFact.init(ks, keyPassword.toCharArray());
00158
00159
00160 KeyManager[] kms = kmFact.getKeyManagers();
00161
return kms;
00162 }
00163
00164 protected static TrustManager[]
getTrustManagers(File trustStore,
00165 String trustStorePassword)
throws IOException, GeneralSecurityException
00166 {
00167
00168 String alg = TrustManagerFactory.getDefaultAlgorithm();
00169 TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
00170
00171
00172
00173 FileInputStream fis =
new FileInputStream(trustStore);
00174 KeyStore ks = KeyStore.getInstance(
"jks");
00175 ks.load(fis, trustStorePassword.toCharArray());
00176 fis.close();
00177
00178
00179 tmFact.init(ks);
00180
00181
00182 TrustManager[] tms = tmFact.getTrustManagers();
00183
return tms;
00184 }
00185 }