KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > ssl > DomainSocketFactory


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.ssl;
8
9 import java.io.IOException JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.Socket JavaDoc;
12 import java.net.UnknownHostException JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.net.SocketFactory;
16 import javax.net.ssl.HandshakeCompletedEvent;
17 import javax.net.ssl.HandshakeCompletedListener;
18 import javax.net.ssl.SSLContext;
19 import javax.net.ssl.SSLSession;
20 import javax.net.ssl.SSLSocket;
21 import javax.net.ssl.SSLSocketFactory;
22
23 import org.jboss.logging.Logger;
24 import org.jboss.security.SecurityDomain;
25
26 /**
27  * An implementation of SocketFactory that creates SSL sockets using the
28  * JSSE SSLContext and a JBossSX SecurityDomain for the KeyManagerFactory
29  * and TrustManagerFactory objects.
30  *
31  * @see javax.net.ssl.SSLContext
32  * @see org.jboss.security.SecurityDomain
33  *
34  * @author Scott.Stark@jboss.org
35  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
36  *
37  * @version $Revision: 1.5.4.2 $
38  */

39 public class DomainSocketFactory
40    extends SSLSocketFactory
41    implements HandshakeCompletedListener
42 {
43    private static Logger log = Logger.getLogger(DomainSocketFactory.class);
44    private transient SecurityDomain securityDomain;
45    private transient SSLContext sslCtx = null;
46    private boolean wantsClientAuth = true;
47    private boolean needsClientAuth = false;
48
49    /**
50     * A default constructor for use when created by Class.newInstance. The
51     * factory is not usable until its SecurityDomain has been established.
52     */

53    public DomainSocketFactory()
54    {
55    }
56
57    /**
58     * Create a socket factory instance that uses the given SecurityDomain
59     * as the source for the SSL KeyManagerFactory and TrustManagerFactory.
60     */

61    public DomainSocketFactory(SecurityDomain securityDomain)
62       throws IOException JavaDoc
63    {
64       if( securityDomain == null )
65          throw new IOException JavaDoc("The securityDomain may not be null");
66       this.securityDomain = securityDomain;
67    }
68
69    public SecurityDomain getSecurityDomain()
70    {
71       return securityDomain;
72    }
73
74    public void setSecurityDomain(SecurityDomain securityDomain)
75    {
76       this.securityDomain = securityDomain;
77    }
78
79    public boolean isWantsClientAuth()
80    {
81       return wantsClientAuth;
82    }
83    public void setWantsClientAuth(boolean wantsClientAuth)
84    {
85       this.wantsClientAuth = wantsClientAuth;
86    }
87
88    public boolean isNeedsClientAuth()
89    {
90       return needsClientAuth;
91    }
92    public void setNeedsClientAuth(boolean needsClientAuth)
93    {
94       this.needsClientAuth = needsClientAuth;
95    }
96
97    // SSLSocketFactory methods --------------------------------------
98

99    /**
100     * Create a client socket connected to the specified host and port.
101     *
102     * @param serverHost - the host name
103     * @param serverPort - the port number
104     * @return a socket connected to the specified host and port.
105     * @exception IOException if an I/O error occurs during socket creation.
106     */

107    public Socket JavaDoc createSocket(String JavaDoc serverHost, int serverPort)
108       throws IOException JavaDoc, UnknownHostException JavaDoc
109    {
110       InetAddress JavaDoc serverAddr = InetAddress.getByName(serverHost);
111       return this.createSocket(serverAddr, serverPort);
112    }
113
114    public Socket JavaDoc createSocket(String JavaDoc serverHost, int serverPort,
115                               InetAddress JavaDoc clientAddr, int clientPort)
116       throws IOException JavaDoc, UnknownHostException JavaDoc
117    {
118       InetAddress JavaDoc serverAddr = InetAddress.getByName(serverHost);
119       return this.createSocket(serverAddr, serverPort, clientAddr, clientPort);
120    }
121
122    public Socket JavaDoc createSocket(InetAddress JavaDoc serverAddr, int serverPort)
123       throws IOException JavaDoc
124    {
125       return this.createSocket(serverAddr, serverPort, null, 0);
126    }
127
128    public Socket JavaDoc createSocket(InetAddress JavaDoc serverAddr, int serverPort,
129                               InetAddress JavaDoc clientAddr, int clientPort)
130       throws IOException JavaDoc
131    {
132       initSSLContext();
133       SSLSocketFactory factory = sslCtx.getSocketFactory();
134       SSLSocket socket =
135          (SSLSocket)factory.createSocket(serverAddr, serverPort,
136                                          clientAddr, clientPort);
137       String JavaDoc[] supportedProtocols = socket.getSupportedProtocols();
138       log.debug("Supported protocols: " + Arrays.asList(supportedProtocols));
139       String JavaDoc[] protocols = supportedProtocols; // {"SSLv3"};
140
socket.setEnabledProtocols(protocols);
141       socket.addHandshakeCompletedListener(this);
142       socket.setNeedClientAuth(needsClientAuth);
143       socket.setWantClientAuth(wantsClientAuth);
144       return socket;
145    }
146
147    public Socket JavaDoc createSocket(Socket JavaDoc s, String JavaDoc host,
148                               int port, boolean autoClose)
149       throws IOException JavaDoc
150    {
151       initSSLContext();
152       SSLSocketFactory factory = sslCtx.getSocketFactory();
153       SSLSocket socket =
154          (SSLSocket)factory.createSocket(s, host, port, autoClose);
155       socket.addHandshakeCompletedListener(this);
156       return socket;
157    }
158
159    public String JavaDoc[] getDefaultCipherSuites()
160    {
161       String JavaDoc[] cipherSuites = {};
162       try
163       {
164          initSSLContext();
165          SSLSocketFactory factory = sslCtx.getSocketFactory();
166          cipherSuites = factory.getDefaultCipherSuites();
167       }
168       catch(IOException JavaDoc e)
169       {
170          log.error("Failed to get default SSLSocketFactory", e);
171       }
172       return cipherSuites;
173    }
174    
175    public String JavaDoc[] getSupportedCipherSuites()
176    {
177       String JavaDoc[] cipherSuites = {};
178       try
179       {
180          initSSLContext();
181          SSLSocketFactory factory = sslCtx.getSocketFactory();
182          cipherSuites = factory.getSupportedCipherSuites();
183       }
184       catch(IOException JavaDoc e)
185       {
186          log.error("Failed to get default SSLSocketFactory", e);
187       }
188       return cipherSuites;
189    }
190    
191    /**
192     * The default SocketFactory which looks to the java:/jaas/other
193     * security domain configuration.
194     */

195    public static SocketFactory getDefault()
196    {
197       DomainSocketFactory ssf = null;
198       try
199       {
200          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
201          SecurityDomain sd = (SecurityDomain)iniCtx.lookup("java:/jaas/other");
202          ssf = new DomainSocketFactory(sd);
203       }
204       catch(Exception JavaDoc e)
205       {
206          log.error("Failed to create default SocketFactory", e);
207       }
208       return ssf;
209    }
210    
211    // HandshakeCompletedListener method -----------------------------
212

213    public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent)
214    {
215       Logger log = Logger.getLogger(ClientSocketFactory.class);
216       if( log.isTraceEnabled() )
217       {
218          String JavaDoc cipher = handshakeCompletedEvent.getCipherSuite();
219          SSLSession session = handshakeCompletedEvent.getSession();
220          String JavaDoc peerHost = session.getPeerHost();
221          log.debug("SSL handshakeCompleted, cipher="+cipher
222             +", peerHost="+peerHost);
223       }
224    }
225
226    // Private method ------------------------------------------------
227

228    private void initSSLContext()
229       throws IOException JavaDoc
230    {
231       if( sslCtx != null )
232          return;
233       sslCtx = Context.forDomain(securityDomain);
234    }
235
236 }
237
Popular Tags