KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > CustomSSLSocketFactory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28 import java.security.KeyManagementException JavaDoc;
29 import java.security.KeyStore JavaDoc;
30 import java.security.KeyStoreException JavaDoc;
31 import java.security.NoSuchAlgorithmException JavaDoc;
32 import java.security.UnrecoverableKeyException JavaDoc;
33 import java.security.cert.CertPath JavaDoc;
34 import java.security.cert.CertPathValidator JavaDoc;
35 import java.security.cert.CertPathValidatorResult JavaDoc;
36 import java.security.cert.Certificate JavaDoc;
37 import java.security.cert.CertificateException JavaDoc;
38 import java.security.cert.CertificateFactory JavaDoc;
39 import java.security.cert.PKIXCertPathValidatorResult JavaDoc;
40 import java.security.cert.PKIXParameters JavaDoc;
41 import java.security.cert.TrustAnchor JavaDoc;
42 import java.security.cert.X509Certificate JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.Enumeration JavaDoc;
45
46 import javax.net.SocketFactory;
47 import javax.net.ssl.KeyManager;
48 import javax.net.ssl.KeyManagerFactory;
49 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.SSLSocket;
51 import javax.net.ssl.SSLSocketFactory;
52 import javax.net.ssl.TrustManager;
53 import javax.net.ssl.X509TrustManager;
54
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 /**
59  */

60 public class CustomSSLSocketFactory extends SocketFactory {
61
62     private static final Log log = LogFactory.getLog(CustomSSLSocketFactory.class);
63     private static SocketFactory instance;
64     private static Class JavaDoc socketFactoryImpl = CustomSSLSocketFactory.class;
65
66
67     /**
68      */

69     public CustomSSLSocketFactory() {
70
71     }
72
73     public static SocketFactory getDefault() {
74         try {
75             return instance == null ? instance = (SocketFactory) socketFactoryImpl.newInstance() : instance;
76         } catch (Exception JavaDoc e) {
77             log.error("Could not create instance of class " + socketFactoryImpl.getCanonicalName(), e);
78             return instance == null ? instance = new CustomSSLSocketFactory() : instance;
79         }
80     }
81
82     /**
83      * @param socketFactoryImpl
84      */

85     public static void setFactoryImpl(Class JavaDoc socketFactoryImpl) {
86         CustomSSLSocketFactory.socketFactoryImpl = socketFactoryImpl;
87     }
88     
89     public Socket JavaDoc createSocket() throws IOException JavaDoc {
90         SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket();
91         return theSocket;
92     }
93
94     public Socket JavaDoc createSocket(String JavaDoc hostname, int port) throws IOException JavaDoc, UnknownHostException JavaDoc {
95         SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(hostname, port);
96         return theSocket;
97     }
98
99     public Socket JavaDoc createSocket(String JavaDoc hostname, int port, InetAddress JavaDoc arg2, int arg3) throws IOException JavaDoc, UnknownHostException JavaDoc {
100         SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(hostname, port, arg2, arg3);
101         return theSocket;
102     }
103
104     public Socket JavaDoc createSocket(InetAddress JavaDoc arg0, int arg1) throws IOException JavaDoc {
105         SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(arg0, arg1);
106         return theSocket;
107     }
108
109     public Socket JavaDoc createSocket(InetAddress JavaDoc arg0, int arg1, InetAddress JavaDoc arg2, int arg3) throws IOException JavaDoc {
110         SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(arg0, arg1, arg2, arg3);
111         return theSocket;
112     }
113
114     private SSLSocketFactory getSocketFactory() throws IOException JavaDoc {
115         try {
116             SSLContext sslCtx = SSLContext.getInstance("SSL");
117             KeyManager[] aKM = SSLKeyManager.getKeyManagerArray();
118             TrustManager[] aTM = SSLTrustManager.getTrustManagerArray();
119             sslCtx.init(aKM, aTM, null);
120             SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
121             return socketFactory;
122         } catch (KeyManagementException JavaDoc e) {
123             log.error("Cannot create SSL socket", e);
124             throw new IOException JavaDoc("Cannot create SSL socket: " + e.getMessage());
125         } catch (NoSuchAlgorithmException JavaDoc e) {
126             log.error("Cannot create SSL socket", e);
127             throw new IOException JavaDoc("Cannot create SSL socket: " + e.getMessage());
128         }
129     }
130
131
132
133     
134 }
Popular Tags