Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SocketFactoryFactory.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Marc Wick.
00022  * Contributor(s): ______________________.
00023  */
00024 
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 
00044 /**
00045  * This class defines a SocketFactory
00046  * 
00047  * @author <a href="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
00048  * @version 1.0
00049  */
00050 public class SocketFactoryFactory
00051 {
00052 
00053   /**
00054    * create a server socket factory with the specified configuration
00055    * 
00056    * @param config - the ssl configuration
00057    * @return - the socket factory
00058    * @throws SSLException - could not create factory
00059    */
00060   public static ServerSocketFactory createServerFactory(SSLConfiguration config)
00061       throws SSLException
00062   {
00063     try
00064     {
00065 
00066       if (config == null)
00067         // nothing todo return default SocketFactory
00068         return ServerSocketFactory.getDefault();
00069 
00070       SSLContext context = createSSLContext(config);
00071       // Finally, we get a SocketFactory
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 
00085   /**
00086    * create a socket factory with the specified configuration
00087    * 
00088    * @param config - the ssl configuration
00089    * @return - the socket factory
00090    * @throws Exception - could not create factory
00091    */
00092   public static SocketFactory createFactory(SSLConfiguration config)
00093       throws Exception
00094   {
00095     if (config == null)
00096       // nothing todo return default SocketFactory
00097       return SocketFactory.getDefault();
00098 
00099     SSLContext context = createSSLContext(config);
00100 
00101     // Finally, we get a SocketFactory
00102     SSLSocketFactory ssf = context.getSocketFactory();
00103 
00104     if (!config.isClientAuthenticationRequired())
00105       return ssf;
00106 
00107     return new AuthenticatedSocketFactory(ssf);
00108   }
00109 
00110   /**
00111    * create a ssl context
00112    * 
00113    * @param config - ssl config
00114    * @return - the ssl context
00115    * @throws Exception - problems initializing the content
00116    */
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     // Now construct a SSLContext using these KeyManagers. We
00128     // specify a null SecureRandom, indicating that the
00129     // defaults should be used.
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     // First, get the default KeyManagerFactory.
00140     String alg = KeyManagerFactory.getDefaultAlgorithm();
00141     KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg);
00142 
00143     // Next, set up the KeyStore to use. We need to load the file into
00144     // a KeyStore instance.
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     // Now we initialize the TrustManagerFactory with this KeyStore
00157     kmFact.init(ks, keyPassword.toCharArray());
00158 
00159     // And now get the TrustManagers
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     // First, get the default TrustManagerFactory.
00168     String alg = TrustManagerFactory.getDefaultAlgorithm();
00169     TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
00170 
00171     // Next, set up the TrustStore to use. We need to load the file into
00172     // a KeyStore instance.
00173     FileInputStream fis = new FileInputStream(trustStore);
00174     KeyStore ks = KeyStore.getInstance("jks");
00175     ks.load(fis, trustStorePassword.toCharArray());
00176     fis.close();
00177 
00178     // Now we initialize the TrustManagerFactory with this KeyStore
00179     tmFact.init(ks);
00180 
00181     // And now get the TrustManagers
00182     TrustManager[] tms = tmFact.getTrustManagers();
00183     return tms;
00184   }
00185 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1