views:

1106

answers:

2

I have an in-house HTTP server written in Java; full source code at my disposal. The HTTP server can configure any number of web sites, each of which will have a separate listen socket created with:

skt=SSLServerSocketFactory.getDefault().createServerSocket(prt,bcklog,adr);

Using a standard key store created with the Java keytool, I cannot for the life of me work out how to get different certificates associated with different listen sockets so that each configured web site has it's own certificate.

I'm in a time pinch for this now, so some code samples that illustrate would be most appreciated. But as much I would appreciate any good overview on how JSSE hangs together in this regard (I have searched Sun's JSSE doco until my brain hurts (literally; though it might be as much caffeine withdrawal)).

Edit

Is there no simple way to use the alias to associate the server certificates in a key store with the listen sockets? So that:

  • The customer has one key store to many for all certificates, and
  • There is no need to fiddle around with multiple key stores, etc.

I was getting the impression (earlier this afternoon) that I could write a simple KeyManager, with only chooseServerAlias(...) returning non-null, that being the name of the alias I wanted - anyone have any thoughts on that line of reasoning?

Solution

The solution I used, built from slyvarking's answer was to create a temporary key store and populate it with the desired key/cert extracted from the singular external key store. Code follows for any who are interested (svrctfals is my "server certificate alias" value):

    SSLServerSocketFactory              ssf;                                    // server socket factory
    SSLServerSocket                     skt;                                    // server socket

    // LOAD EXTERNAL KEY STORE
    KeyStore mstkst;
    try {
        String   kstfil=GlobalSettings.getString("javax.net.ssl.keyStore"        ,System.getProperty("javax.net.ssl.keyStore"        ,""));
        String   ksttyp=GlobalSettings.getString("javax.net.ssl.keyStoreType"    ,System.getProperty("javax.net.ssl.keyStoreType"    ,"jks"));
        char[]   kstpwd=GlobalSettings.getString("javax.net.ssl.keyStorePassword",System.getProperty("javax.net.ssl.keyStorePassword","")).toCharArray();

        mstkst=KeyStore.getInstance(ksttyp);
        mstkst.load(new FileInputStream(kstfil),kstpwd);
        }
    catch(java.security.GeneralSecurityException thr) {
        throw new IOException("Cannot load keystore ("+thr+")");
        }

    // CREATE EPHEMERAL KEYSTORE FOR THIS SOCKET USING DESIRED CERTIFICATE
    try {
        SSLContext        ctx=SSLContext.getInstance("TLS");
        KeyManagerFactory kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore          sktkst;
        char[]            blkpwd=new char[0];

        sktkst=KeyStore.getInstance("jks");
        sktkst.load(null,blkpwd);
        sktkst.setKeyEntry(svrctfals,mstkst.getKey(svrctfals,blkpwd),blkpwd,mstkst.getCertificateChain(svrctfals));
        kmf.init(sktkst,blkpwd);
        ctx.init(kmf.getKeyManagers(),null,null);
        ssf=ctx.getServerSocketFactory();
        }
    catch(java.security.GeneralSecurityException thr) {
        throw new IOException("Cannot create secure socket ("+thr+")");
        }

    // CREATE AND INITIALIZE SERVER SOCKET
    skt=(SSLServerSocket)ssf.createServerSocket(prt,bcklog,adr);
    ...
    return skt;
+1  A: 

You won't be able to use the default SSLServerSocketFactory.

Instead, initialize a different SSLContext for each site, each using a KeyManagerFactory configured with a key store containing a key entry with correct server certificate. (After initializing the KeyManagerFactory, pass its key managers to the init method of the SSLContext.)

After the SSLContext is initalized, get its SSLServerSocketFactory, and use that to create your listener.

KeyStore identity = KeyStore.getInstance(KeyStore.getDefaultType());
/* Load the keystore (a different one for each site). */
...
SSLContext ctx = SSLContext.getInstance("TLS");
KeyManagerFactory kmf = 
  KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(identity, password);
ctx.init(kmf.getKeyManagers(), null, null);
SSLServerSocketFactory factory = ctx.getServerSocketFactory();
ServerSocket server = factory.createSocket(port);
erickson
And how does one get a different SSLContext associated with the listen socket?
Software Monkey
Which of these objects will I need to provide a custom implementation for, and which will use stock objects provided by JSSE?
Software Monkey
They can all be stock JSSE. Your main task is to build a distinct key store for each site. See my update for an example. I didn't use an IDE so some of the methods names might be off.
erickson
Do I need to do this for algorithm "SSL" as well; or does TLS cover SSL too. I confess I really don't get the use of "algorithm" in this context - does it really mean "protocol".
Software Monkey
It could vary by provider, but Sun's TLS will support SSL clients. If you are concerned about a variety of client configurations, install the OpenSSL package and use its `openssl s_client` with the options you want to test.
erickson
Also, I don't see how `identity` ends up associated with the listen socket, in the example above.
Software Monkey
Presumably too, I can load one key store with multiple certificates, extract a cert by alias, insert it into a new key store and then proceed as above?
Software Monkey
I skipped a step; I'll fix that up. Yes, you could do that from one key store. The mapping from alias to site would be part of your application config.
erickson
I edited the question with a thought about using a custom KeyManager - any thoughts on that?
Software Monkey
Thanks so much for your assistance.
Software Monkey
+1  A: 

The easiest way to do this is to use a single certificate for all your domain names. Put all other site names in SAN (Subject Alternative Name).

If you prefer one certificate for each domain name, you can write your own key manager and use alias to identify the domain so you can use a single keystore. In our system, we make a convention that keystore alias always equals the CN in the certificate. So we can do something like this,

        SSLContext sctx1 = SSLContext.getInstance("SSLv3");
        sctx1.init(new X509KeyManager[] { 
          new MyKeyManager("/config/master.jks", 
            "changeme".toCharArray(), "site1.example.com") },
                      null, null);
        SSLServerSocketFactory ssf = 
         (SSLServerSocketFactory) sctx1.getServerSocketFactory();
  ServerSocket ss1 = ssf.createServerSocket(1234);

...

        SSLContext sctx2 = SSLContext.getInstance("SSLv3");
        sctx2.init(new X509KeyManager[] { 
          new MyKeyManager("/config/master.jks", 
            "changeme".toCharArray(), "site2.example.com") },
                      null, null);
        ssf = 
         (SSLServerSocketFactory) sctx2.getServerSocketFactory();
  ServerSocket ss2 = ssf.createServerSocket(5678);

...

public static class MyKeyManager implements X509KeyManager {
    private KeyStore keyStore;
    private String alias;
    private char[] password;

    MyKeyManager(String keyStoreFile, char[] password, String alias)
        throws IOException, GeneralSecurityException
    {
        this.alias = alias;
        this.password = password;
        InputStream stream = new FileInputStream(keyStoreFile);
        keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(stream, password);
    }

    public PrivateKey getPrivateKey(String alias) {
        try {
   return (PrivateKey) keyStore.getKey(alias, password);
  } catch (Exception e) {
   return null;
  }
    }

    public X509Certificate[] getCertificateChain(String alias) {
        try {
            java.security.cert.Certificate[] certs = keyStore.getCertificateChain(alias);
            if (certs == null || certs.length == 0)
             return null;
            X509Certificate[] x509 = new X509Certificate[certs.length];
            for (int i = 0; i < certs.length; i++)
             x509[i] = (X509Certificate)certs[i];
   return x509;
  } catch (Exception e) {
   return null;
  }          
    }

    public String chooseServerAlias(String keyType, Principal[] issuers,
                                    Socket socket) {
        return alias;
    }

    public String[] getClientAliases(String parm1, Principal[] parm2) {
        throw new UnsupportedOperationException("Method getClientAliases() not yet implemented.");
    }

    public String chooseClientAlias(String keyTypes[], Principal[] issuers, Socket socket) {
        throw new UnsupportedOperationException("Method chooseClientAlias() not yet implemented.");
    }

    public String[] getServerAliases(String parm1, Principal[] parm2) {
        return new String[] { alias };
    }

    public String chooseServerAlias(String parm1, Principal[] parm2) {
        return alias;
    }
}
ZZ Coder
Ahh. This is exactly what I was suspecting could be done yesterday... Would have preferred this to my solution, as it's logically cleaner, I think.
Software Monkey
None of the tools I have seen have had a way to set alternative subject names - do you have any recommendations?
Software Monkey
OpenSSL can create CSR with SANs. You can't use the command line. You need to add SANs in [alt_names] section in config file. See http://therowes.net/~greg/2008/01/08/creating-a-certificate-with-multiple-hostnames/
ZZ Coder