1 7 package org.jboss.security.ssl; 8 9 import java.io.IOException ; 10 import java.io.Serializable ; 11 import java.rmi.server.RMIClientSocketFactory ; 12 import java.security.cert.Certificate ; 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 27 public class RMISSLClientSocketFactory implements HandshakeCompletedListener, 28 RMIClientSocketFactory , Serializable 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 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 64 public java.net.Socket createSocket(String host, int port) 65 throws IOException 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 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 cipher = event.getCipherSuite(); 89 SSLSession session = event.getSession(); 90 String peerHost = session.getPeerHost(); 91 Certificate [] localCerts = event.getLocalCertificates(); 92 Certificate [] peerCerts = null; 93 try 94 { 95 peerCerts = event.getPeerCertificates(); 96 } 97 catch(Exception 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 |