views:

2352

answers:

3

In my Java application, I need to connect to the same host using SSL, but using a different certificate each time. The reason I need to use different certificates is that the remote site uses a user ID property embedded in the certificate to identify the client.

This is a server application that runs on 3 different operating systems, and I need to be able to switch certificates without restarting the process.

Another user suggested importing multiple certificates into the same keystore. I'm not sure that helps me, though, unless there is a way to tell Java which certificate in the keystore to use.

A: 

Have you tried resetting the keyStore System property in between requests?

System.setProperty("javax.net.ssl.keyStore", pathToKeyStore1);
...<set other ssl properties>
...make request to server

System.setProperty("javax.net.ssl.keyStore", pathToKeyStore2);
...<set other ssl properties>
...make request to server
Gandalf
Yes, unfortunately, it didn't work. Java seems to only read the javax.net.ssl.keyStore once, when you first make an SSL connection.
cwick
Plus it would create chaos in any multi-threaded application.
erickson
+2  A: 

SSL can provide hints to the client about which certificate to present. This might allow you to use one key store with multiple identities in it, but, unfortunately, most servers don't use this hinting feature. So, it will be more robust if you specify the client certificate to use on for each connection.

Here is sample code to set up one SSLContext with specified identity and trust stores. You can repeat these steps to create multiple contexts, one for each client certificate you want to use. Each SSLContext would probably use the same trust store, but a different identity store (containing the single client key entry to be used in that context).

Initialize the contexts that you will need one time, and reuse the the correct one for each connection. If you are making multiple connections, this will allow you to take advantage of SSL sessions.

KeyManagerFactory kmf = 
  KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(identityStore, password);
TrustManagerFactory tmf =
  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

Later, you can create a socket directly:

SSLSocketFactory factory = ctx.getSocketFactory();
Socket socket = factory.createSocket(host, port);

Or, if you are using the URL class, you can specify the SSLSocketFactory to use when making HTTPS requests:

HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(ctx.getSocketFactory());

Java 6 has some additional API that makes it easier to configure sockets according to your preferences for cipher suites, etc.

erickson
"Java 6 has some additional API that makes it easier to configure sockets according to your preferences for cipher suites" Can you point me to further documentation/discussion on these configurations?
Tazzy531
@Tazzy531 - Java 6 added [`SSLParameters`,](http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/net/ssl/class-use/SSLParameters.html) which you can set on an `SSLEngine` or a new `SSLSocket` in a single operation.
erickson
A: 

There is a solution here for dynamically choosing the client certificate used for SSL Authentication from an Axis Client.

Matt