KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource EJB server
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.io.Serializable JavaDoc;
11 import java.rmi.server.RMIClientSocketFactory JavaDoc;
12 import java.security.cert.Certificate JavaDoc;
13 import javax.net.ssl.HandshakeCompletedEvent;
14 import javax.net.ssl.HandshakeCompletedListener;
15 import javax.net.ssl.SSLSession;
16 import javax.net.ssl.SSLSocketFactory;
17 import javax.net.ssl.SSLSocket;
18
19 import org.jboss.logging.Logger;
20
21 /** An implementation of RMIClientSocketFactory that uses the JSSE
22  default SSLSocketFactory to create a client SSLSocket.
23  *
24  * @author Scott.Stark@jboss.org
25  * @version $Revision: 1.5.6.2 $
26  */

27 public class RMISSLClientSocketFactory implements HandshakeCompletedListener,
28    RMIClientSocketFactory JavaDoc, Serializable JavaDoc
29 {
30    private static Logger log = Logger.getLogger(RMISSLClientSocketFactory.class);
31    private static final long serialVersionUID = -6412485012870705607L;
32    private boolean wantsClientAuth = true;
33    private boolean needsClientAuth = false;
34
35    /** Creates new RMISSLClientSocketFactory */
36    public RMISSLClientSocketFactory()
37    {
38    }
39
40    public boolean isWantsClientAuth()
41    {
42       return wantsClientAuth;
43    }
44    public void setWantsClientAuth(boolean wantsClientAuth)
45    {
46       this.wantsClientAuth = wantsClientAuth;
47    }
48
49    public boolean isNeedsClientAuth()
50    {
51       return needsClientAuth;
52    }
53    public void setNeedsClientAuth(boolean needsClientAuth)
54    {
55       this.needsClientAuth = needsClientAuth;
56    }
57
58    /** Create a client socket connected to the specified host and port.
59    * @param host - the host name
60    * @param port - the port number
61    * @return a socket connected to the specified host and port.
62    * @exception IOException if an I/O error occurs during socket creation.
63    */

64    public java.net.Socket JavaDoc createSocket(String JavaDoc host, int port)
65       throws IOException JavaDoc
66    {
67       SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
68       SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
69       socket.addHandshakeCompletedListener(this);
70       socket.setWantClientAuth(wantsClientAuth);
71       socket.setNeedClientAuth(needsClientAuth);
72       log.debug("createSocket, host="+host+", port="+port
73          +",needsClientAuth="+needsClientAuth+", wantsClientAuth="+wantsClientAuth);
74       return socket;
75    }
76
77    public boolean equals(Object JavaDoc obj)
78    {
79       return obj instanceof RMISSLClientSocketFactory;
80    }
81    public int hashCode()
82    {
83       return getClass().getName().hashCode();
84    }
85
86    public void handshakeCompleted(HandshakeCompletedEvent event)
87    {
88       String JavaDoc cipher = event.getCipherSuite();
89       SSLSession session = event.getSession();
90       String JavaDoc peerHost = session.getPeerHost();
91       Certificate JavaDoc[] localCerts = event.getLocalCertificates();
92       Certificate JavaDoc[] peerCerts = null;
93       try
94       {
95          peerCerts = event.getPeerCertificates();
96       }
97       catch(Exception JavaDoc e)
98       {
99          log.debug("Failed to retrieve peer certs", e);
100       }
101       log.debug("SSL handshakeCompleted, cipher="+cipher
102          +", peerHost="+peerHost);
103       int count = localCerts != null ? localCerts.length : 0;
104       log.debug("ClientCertChain length: "+count);
105       for(int n = 0; n < count; n ++)
106          log.debug("Cert["+n+"]="+localCerts[n]);
107       count = peerCerts != null ? peerCerts.length : 0;
108       log.debug("PeerCertChain length: "+count);
109       for(int n = 0; n < count; n ++)
110          log.debug("Cert["+n+"]="+peerCerts[n]);
111    }
112    
113 }
114
Popular Tags