FileDocCategorySizeDatePackage
JSSESocketFactory.javaAPI DocGlassfish v2 API12990Fri May 04 22:33:16 BST 2007org.apache.tomcat.util.net.jsse

JSSESocketFactory

public abstract class JSSESocketFactory extends org.apache.tomcat.util.net.ServerSocketFactory
SSL server socket factory. It _requires_ a valid RSA key and JSSE.
author
Harish Prabandham
author
Costin Manolache
author
Stefan Freyr Stefansson
author
EKR -- renamed to JSSESocketFactory

Fields Summary
static String
defaultProtocol
static String
defaultAlgorithm
static boolean
defaultClientAuth
private static final String
defaultKeyPass
protected static final com.sun.org.apache.commons.logging.Log
log
protected boolean
initialized
protected boolean
clientAuth
protected SSLServerSocketFactory
sslProxy
protected String[]
enabledCiphers
Constructors Summary
public JSSESocketFactory()


       
    
Methods Summary
public java.net.SocketacceptSocket(java.net.ServerSocket socket)

        SSLSocket asock = null;
        try {
             asock = (SSLSocket)socket.accept();
             asock.setNeedClientAuth(clientAuth);
        } catch (SSLException e){
          throw new SocketException("SSL handshake error" + e.toString());
        }
        return asock;
    
public java.net.ServerSocketcreateSocket(int port)

        if (!initialized) init();
        ServerSocket socket = sslProxy.createServerSocket(port);
        initServerSocket(socket);
        return socket;
    
public java.net.ServerSocketcreateSocket(int port, int backlog)

        if (!initialized) init();
        ServerSocket socket = sslProxy.createServerSocket(port, backlog);
        initServerSocket(socket);
        return socket;
    
public java.net.ServerSocketcreateSocket(int port, int backlog, java.net.InetAddress ifAddress)

   
        if (!initialized) init();
        ServerSocket socket = sslProxy.createServerSocket(port, backlog,
                                                          ifAddress);
        initServerSocket(socket);
        return socket;
    
protected java.lang.String[]getEnabledCiphers(java.lang.String requestedCiphers, java.lang.String[] supportedCiphers)


        String[] enabledCiphers = null;

        if (requestedCiphers != null) {
            Vector vec = null;
            String cipher = requestedCiphers;
            int index = requestedCiphers.indexOf(',");
            if (index != -1) {
                int fromIndex = 0;
                while (index != -1) {
                    cipher = requestedCiphers.substring(fromIndex, index).trim();
                    if (cipher.length() > 0) {
                        /*
                         * Check to see if the requested cipher is among the
                         * supported ciphers, i.e., may be enabled
                         */
                        for (int i=0; supportedCiphers != null
                                     && i<supportedCiphers.length; i++) {
                            if (supportedCiphers[i].equals(cipher)) {
                                if (vec == null) {
                                    vec = new Vector();
                                }
                                vec.addElement(cipher);
                                break;
                            }
                        }
                    }
                    fromIndex = index+1;
                    index = requestedCiphers.indexOf(',", fromIndex);
                } // while
                cipher = requestedCiphers.substring(fromIndex);
            }

            if (cipher != null) {
                cipher = cipher.trim();
                if (cipher.length() > 0) {
                    /*
                     * Check to see if the requested cipher is among the
                     * supported ciphers, i.e., may be enabled
                     */
                    for (int i=0; supportedCiphers != null
                                 && i<supportedCiphers.length; i++) {
                        if (supportedCiphers[i].equals(cipher)) {
                            if (vec == null) {
                                vec = new Vector();
                            }
                            vec.addElement(cipher);
                            break;
                        }
                    }
                }
            }           

            if (vec != null) {
                enabledCiphers = new String[vec.size()];
                vec.copyInto(enabledCiphers);
            }
        }

        return enabledCiphers;
    
protected abstract java.lang.String[]getEnabledProtocols(javax.net.ssl.SSLServerSocket socket, java.lang.String requestedProtocols)

protected java.security.KeyStoregetKeystore(java.lang.String pass)


        String keystoreFile = (String)attributes.get("keystore");
        if (log.isDebugEnabled()) {
            log.debug("Keystore file= " + keystoreFile);
        }

        String keystoreType = (String)attributes.get("keystoreType");
        if (log.isDebugEnabled()) {
            log.debug("Keystore type= " + keystoreType);
        }

        return getStore(keystoreType, keystoreFile, pass);
    
protected java.lang.StringgetKeystorePassword()

        String keyPass = (String)attributes.get("keypass");
        if (keyPass == null) {
            keyPass = defaultKeyPass;
        }
        String keystorePass = (String)attributes.get("keystorePass");
        if (keystorePass == null) {
            keystorePass = keyPass;
        }
        return keystorePass;
    
private java.security.KeyStoregetStore(java.lang.String type, java.lang.String path, java.lang.String pass)


        KeyStore ks = null;
        InputStream istream = null;
        try {
            ks = KeyStore.getInstance(type);
            File keyStoreFile = new File(path);
            if (!keyStoreFile.isAbsolute()) {
                keyStoreFile = new File(System.getProperty("catalina.base"),
                                        path);
            }
            istream = new FileInputStream(keyStoreFile);

            ks.load(istream, pass.toCharArray());
            istream.close();
            istream = null;
        } catch (FileNotFoundException fnfe) {
            throw fnfe;
        } catch (IOException ioe) {
            throw ioe;      
        } catch(Exception ex) {
            ex.printStackTrace();
            throw new IOException("Exception trying to load keystore " +
                                  path + ": " + ex.getMessage() );
        } finally {
            if (istream != null) {
                try {
                    istream.close();
                } catch (IOException ioe) {
                    // Do nothing
                }
            }
        }

        return ks;
    
protected java.security.KeyStoregetTrustStore()


        String truststore = (String)attributes.get("truststore");
        if (log.isDebugEnabled()) {
            log.debug("Truststore file= " + truststore);
        }

        String truststoreType = (String)attributes.get("truststoreType");
        if (log.isDebugEnabled()) {
            log.debug("Truststore type= " + truststoreType);
        }

        String truststorePassword = System.getProperty(
                                    "javax.net.ssl.trustStorePassword");
        if (truststorePassword == null) {
            truststorePassword = getKeystorePassword();
        }

        return getStore(truststoreType, truststore, truststorePassword);
    
public voidhandshake(java.net.Socket sock)

        ((SSLSocket)sock).startHandshake();
    
public abstract voidinit()
Reads the keystore and initializes the SSL socket factory. Place holder method to initialize the KeyStore, etc.

private voidinitServerSocket(java.net.ServerSocket ssocket)
Configures the given SSL server socket with the requested cipher suites, protocol versions, and need for client authentication


        SSLServerSocket socket = (SSLServerSocket) ssocket;

        if (attributes.get("ciphers") != null) {
            socket.setEnabledCipherSuites(enabledCiphers);
        }

        String requestedProtocols = (String) attributes.get("protocols");
        setEnabledProtocols(socket, getEnabledProtocols(socket, 
                                                         requestedProtocols));

        // we don't know if client auth is needed -
        // after parsing the request we may re-handshake
        socket.setNeedClientAuth(clientAuth);
    
protected abstract voidsetEnabledProtocols(javax.net.ssl.SSLServerSocket socket, java.lang.String[] protocols)
Set the SSL protocol variants to be enabled.

param
socket the SSLServerSocket.
param
protocols the protocols to use.