1 16 17 package org.apache.tomcat.util.net.jsse; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.SocketException ; 23 import java.security.cert.Certificate ; 24 import java.security.cert.CertificateFactory ; 25 import java.security.cert.X509Certificate ; 26 27 import javax.net.ssl.HandshakeCompletedEvent; 28 import javax.net.ssl.HandshakeCompletedListener; 29 import javax.net.ssl.SSLException; 30 import javax.net.ssl.SSLSession; 31 import javax.net.ssl.SSLSocket; 32 33 34 47 48 class JSSE14Support extends JSSESupport { 49 50 private static org.apache.commons.logging.Log logger = 51 org.apache.commons.logging.LogFactory.getLog(JSSE14Support.class); 52 53 Listener listener = new Listener(); 54 55 public JSSE14Support(SSLSocket sock){ 56 super(sock); 57 sock.addHandshakeCompletedListener(listener); 58 } 59 60 protected void handShake() throws IOException { 61 if( ssl.getWantClientAuth() ) { 62 logger.debug("No client cert sent for want"); 63 } else { 64 ssl.setNeedClientAuth(true); 65 } 66 synchronousHandshake(ssl); 67 } 68 69 74 private void synchronousHandshake(SSLSocket socket) 75 throws IOException { 76 InputStream in = socket.getInputStream(); 77 int oldTimeout = socket.getSoTimeout(); 78 socket.setSoTimeout(1000); 79 byte[] b = new byte[0]; 80 listener.reset(); 81 socket.startHandshake(); 82 int maxTries = 60; for (int i = 0; i < maxTries; i++) { 84 if(logger.isTraceEnabled()) 85 logger.trace("Reading for try #" +i); 86 try { 87 int x = in.read(b); 88 } catch(SSLException sslex) { 89 logger.info("SSL Error getting client Certs",sslex); 90 throw sslex; 91 } catch (IOException e) { 92 } 94 if (listener.completed) { 95 break; 96 } 97 } 98 socket.setSoTimeout(oldTimeout); 99 if (listener.completed == false) { 100 throw new SocketException ("SSL Cert handshake timeout"); 101 } 102 } 103 104 107 protected X509Certificate [] getX509Certificates(SSLSession session) 108 throws IOException 109 { 110 Certificate [] certs=null; 111 try { 112 certs = session.getPeerCertificates(); 113 } catch( Throwable t ) { 114 logger.debug("Error getting client certs",t); 115 return null; 116 } 117 if( certs==null ) return null; 118 119 X509Certificate [] x509Certs = new X509Certificate [certs.length]; 120 for(int i=0; i < certs.length; i++) { 121 if( certs[i] instanceof X509Certificate ) { 122 x509Certs[i] = (X509Certificate )certs[i]; 124 } else { 125 try { 126 byte [] buffer = certs[i].getEncoded(); 127 CertificateFactory cf = 128 CertificateFactory.getInstance("X.509"); 129 ByteArrayInputStream stream = 130 new ByteArrayInputStream (buffer); 131 x509Certs[i] = (X509Certificate ) 132 cf.generateCertificate(stream); 133 } catch(Exception ex) { 134 logger.info("Error translating cert " + certs[i], ex); 135 return null; 136 } 137 } 138 if(logger.isTraceEnabled()) 139 logger.trace("Cert #" + i + " = " + x509Certs[i]); 140 } 141 if(x509Certs.length < 1) 142 return null; 143 return x509Certs; 144 } 145 146 147 private static class Listener implements HandshakeCompletedListener { 148 volatile boolean completed = false; 149 public void handshakeCompleted(HandshakeCompletedEvent event) { 150 completed = true; 151 } 152 void reset() { 153 completed = false; 154 } 155 } 156 157 } 158 159 | Popular Tags |