I've written the TLS socket but haven't been able to get around to
testing it yet - swamped.

The socket also has a certificate factory to enable certificates to be
read from a variety of sources.

I hope that the following server.xml snip correctly describes the
inclusion!



    <Connector
className="org.apache.catalina.connector.http.HttpConnector"
               port="8443" minProcessors="5" maxProcessors="75"
               enableLookups="true"
               acceptCount="10" debug="0" scheme="https" secure="true">
      <Factory
className="com.syntactics.server.net.PureTLSSocketFactory"
             

pureTLSCertificateFactory="com.syntactics.server.net.PureTLSTestCertificateFactory"
               clientAuth="false" protocol="TLS"/>
    </Connector>

If any anyone wants to repackage and test, they are most welcome. The
com.syntactics.server.net.PureTLSTestCertificateFactory is the
user-supplied class for digging out the certificate from the appropriate
location.

If nobody else has time, I should be able to do this over the next 4
weeks or so.
package com.syntactics.server.net;

import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import COM.claymoresystems.ptls.SSLContext;
import COM.claymoresystems.ptls.SSLContext;
import COM.claymoresystems.ptls.SSLSocket;
import COM.claymoresystems.ptls.SSLServerSocket;
import COM.claymoresystems.sslg.SSLPolicyInt;

/**
 * SSL server socket factory--wraps PureTLS
 *
 * @author Eric Rescorla
 *
 * some sections of this file cribbed from SSLSocketFactory
 * (the JSSE socket factory)
 *
 */

 
public class PureTLSSocketFactory 
    implements org.apache.catalina.net.ServerSocketFactory
{
    static String defaultProtocol = "TLS";
    static boolean defaultClientAuth = false;
    static String pureTLSCertificateFactoryName = 
"com.syntactics.server.net.PureTLSCertificateFactory";
    private PureTLSCertificateFactoryInterface pureTLSCertificateFactory = null;
    
    private SSLContext context=null;
    
    public PureTLSSocketFactory() {
    }

    /**
     * Should we require client authentication?
     */
    private boolean clientAuth = false;

    public boolean getClientAuth() {
        return (this.clientAuth);
    }

    public void setClientAuth(boolean clientAuth) {
        this.clientAuth = clientAuth;
    }
    
    public String getPureTLSCertificateFactory() {
        return pureTLSCertificateFactoryName;
    }
    
    public void setPureTLSCertificateFactory(String pureTLSCertificateFactory) {
        this.pureTLSCertificateFactoryName = pureTLSCertificateFactory;
    }

    public ServerSocket createSocket(int port)
        throws IOException
    {
        init();
        return new SSLServerSocket(context,port);
    }

    public ServerSocket createSocket(int port, int backlog)
        throws IOException
    {
        init();
        ServerSocket tmp;
        
        try {
            tmp=new SSLServerSocket(context,port,backlog);
        }
        catch (IOException e){
            throw e;
        }
        return tmp;
    }

    public ServerSocket createSocket(int port, int backlog,
                                     InetAddress ifAddress)
        throws IOException
    {
        init();
        return new SSLServerSocket(context,port,backlog,ifAddress);
    }

    private void init()
        throws IOException//, ClassNotFoundException, IllegalAccessException, 
InstantiationException
    {
        try {
            pureTLSCertificateFactory = 
(PureTLSCertificateFactoryInterface)Class.forName(pureTLSCertificateFactoryName).newInstance();
        } catch (ClassNotFoundException cnfe) {
            throw new 
IOException(cnfe.getMessage());//ClassNotFoundException(cnfe.getMessage());
        } catch (IllegalAccessException iae) {
            throw new 
IOException(iae.getMessage());//IllegalAccessException(iae.getMessage());
        } catch (InstantiationException ie) {
            throw new 
IOException(ie.getMessage());//InstantiationException(ie.getMessage());
        }
        
        if(context!=null)
            return;

        try {
            String keyStoreFile=null;
            if(keyStoreFile==null) 
keyStoreFile=pureTLSCertificateFactory.getKeyStoreFile();
            InputStream keyStoreStream = pureTLSCertificateFactory.getKeyStoreStream();
            
            String keyPass=null;
            if(keyPass==null) keyPass=pureTLSCertificateFactory.getKeyPassword();
            
            String rootFile=null;
            if(rootFile==null) rootFile=pureTLSCertificateFactory.getRootFile();
            InputStream rootStream = pureTLSCertificateFactory.getRootStream();

            String randomFile=null;
            if(randomFile==null) randomFile=pureTLSCertificateFactory.getRandomFile();
            
            String protocol=defaultProtocol;

            SSLContext tmpContext=new SSLContext();
            if(clientAuth){
                if (rootStream == null)
                    tmpContext.loadRootCertificates(rootFile);
                else
                    tmpContext.loadRootCertificates(rootStream);
            }
            if (keyStoreStream == null)
                tmpContext.loadEAYKeyFile(keyStoreFile,keyPass);
            else
                tmpContext.loadEAYKeyFile(keyStoreStream,keyPass);
            tmpContext.useRandomnessFile(randomFile,keyPass);
            
            if (rootStream!=null) rootStream.close();
            if (keyStoreStream!=null) keyStoreStream.close();
            
            SSLPolicyInt policy=new SSLPolicyInt();
            policy.requireClientAuth(clientAuth);
            policy.handshakeOnConnect(false);
            policy.waitOnClose(false);
            tmpContext.setPolicy(policy);
            context=tmpContext;
        } catch (Exception e){
            throw new IOException(e.getMessage());
        }
    }

//    public Socket acceptSocket(ServerSocket socket)
//      throws IOException
//    {
//      try {
//          Socket sock=socket.accept();
//          return sock;
//      } catch (SSLException e){
//            throw new SocketException("SSL handshake error" + e.toString());
//      }
//    }
//
    public void handshake(Socket sock)
         throws IOException
    {
        ((SSLSocket)sock).handshake();
    }
    
}

    
    


package com.syntactics.server.net;

import java.io.InputStream;

public class PureTLSCertificateFactory
    implements PureTLSCertificateFactoryInterface {
    
    static String defaultKeyStoreFile = "server.pem";
    static String defaultKeyPass = "password";    
    static String defaultRootFile = "root.pem";
    static String defaultRandomFile = "random.pem";
    
    static String pureTLSCertificateFactory = null;
    static String keyPassword = null;
    static String keyStoreFile = null;
    static String rootFile = null;
    static String randomFile = null;
    static InputStream keyStoreStream = null;
    static InputStream rootStream = null;
    static InputStream randomStream = null;
    
    public PureTLSCertificateFactory() {
    }
    
    public String getKeyPassword() {
        if (keyPassword == null)
            return defaultKeyPass;
        else
            return keyPassword;
    }
    
    public String getKeyStoreFile() {
        if (keyStoreFile == null)
            return defaultKeyStoreFile;
        else
            return keyStoreFile;
    }
    
    public InputStream getKeyStoreStream() {
        if (keyStoreStream == null)
            return null;
        else
            return keyStoreStream;
    }
    
    public String getPureTLSCertificateFactory() {
        if (pureTLSCertificateFactory == null)
            return null;
        else
            return pureTLSCertificateFactory;
    }
    
    public String getRandomFile() {
        if (randomFile == null)
            return defaultRandomFile;
        else
            return randomFile;
    }
    
    public InputStream getRandomStream() {
        if (randomStream == null)
            return null;
        else
            return randomStream;
    }
    
    public String getRootFile() {
        if (rootFile == null)
            return defaultRootFile;
        else
            return rootFile;
    }
    
    public InputStream getRootStream() {
        if (rootStream == null)
            return null;
        else
            return rootStream;
    }
    
    public void setKeyPassword(String password) {
        this.keyPassword = password;
    }
    
    public void setKeyStoreFile(String keyStoreFile) {
        this.keyStoreFile = keyStoreFile;
    }
    
    public void setKeyStoreStream(InputStream keyStoreStream) {
        this.keyStoreStream = keyStoreStream;
    }
    
    public void setPureTLSCertificateFactory(String pureTLSCertificateFactory) {
        this.pureTLSCertificateFactory = pureTLSCertificateFactory;
    }
    
    public void setRandomFile(String randomFile) {
        this.randomFile = randomFile;
    }
    
    public void setRandomStream(InputStream randomStream) {
        this.randomStream = randomStream;
    }
    
    public void setRootFile(String rootFile) {
        this.rootFile = rootFile;
    }
    
    public void setRootStream(InputStream rootStream) {
        this.rootStream = rootStream;
    }
    
}
package com.syntactics.server.net;

import java.io.InputStream;

public interface PureTLSCertificateFactoryInterface {
    
    public String getKeyPassword();
    public void setKeyPassword(String password);
    
    // Keys from files
    
    public String getKeyStoreFile();
    public void setKeyStoreFile(String keyStoreFile);
    
    public String getRootFile();
    public void setRootFile(String rootFile);
    
    public String getRandomFile();
    public void setRandomFile(String randomFile);
    
    // Keys from streams
    
    public InputStream getKeyStoreStream();
    public void setKeyStoreStream(InputStream keyStoreFile);
    
    public InputStream getRootStream();
    public void setRootStream(InputStream rootFile);
    
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to