Using multiple SSL client certificates with the same host in Java


cwick:

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 a different certificate is that the remote site uses the userid attribute embedded in the certificate to identify the client.

This is a server application running on 3 different OS 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 if that can help me, though, unless there is a way to tell Java which certificate from the keystore to use.

Erickson:

SSL can prompt the client for a certificate to display. This might allow you to use one keystore with multiple identities, but, unfortunately, most servers don't use this hinting feature. So it will be more robust if you specify a client certificate to use for each connection.

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

Initialize the required context once, then reuse the correct context for each connection. If you want to establish multiple connections, you can 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 the socket directly:

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

Or, if you're using a URLclass, you can specify SSLSocketFactorywhich to use when making HTTPS requests:

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

Java 6 has some additional APIs that make it easier to configure sockets based on your preferences for cipher suites etc.

Related


Using multiple SSL client certificates with the same host in Java

cwick: 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 a different certificate is that the remote site uses the userid attribute embedded in the certificate to identify

Multiple SSL certificates in virtual host?

alavona I have a server running Ubuntu 12.04.3 LTS and two servers running Ubuntu 14.04.1 LTS with SSL certificates in them. All are bitnami stacks, so their apache2 config files are at: /opt/bitnami/apache2/conf/bitnami The requirement is to www.have a total

Multiple SSL certificates in virtual host?

alavona I have a server running Ubuntu 12.04.3 LTS and two servers running Ubuntu 14.04.1 LTS with SSL certificates in them. All are bitnami stacks, so their apache2 config files are at: /opt/bitnami/apache2/conf/bitnami The requirement is to www.have a total

Are Java code signing certificates the same as SSL certificates?

Expect: I'm looking for a Java code signing certificate so my Java applet doesn't throw such scary security warnings. However, I found that all the places they offered were charging too much (I think) like over $200 a year. While doing research, code signing c

Are Java code signing certificates the same as SSL certificates?

Expect: I'm looking for a Java code signing certificate so my Java applet doesn't throw such scary security warnings. However, I find that all the places they offer are charging too much (I think) like over $200 a year. While doing research, code signing certi

Nginx client authentication using multiple client certificates

Jonathan Nagin I'm trying to setup NGINX to perform client authentication for multiple clients. My problem is that those clients will have different certificates, basically different root CAs: [clientA.crt] ClientA > IntermediateA > RootA [clientB.crt] ClientB

Nginx client authentication using multiple client certificates

Jonathan Nagin I'm trying to setup NGINX to perform client authentication for multiple clients. My problem is that those clients will have different certificates, basically different root CAs: [clientA.crt] ClientA > IntermediateA > RootA [clientB.crt] ClientB

Nginx client authentication using multiple client certificates

Jonathan Nagin I'm trying to setup NGINX to perform client authentication for multiple clients. My problem is that those clients will have different certificates, basically different root CAs: [clientA.crt] ClientA > IntermediateA > RootA [clientB.crt] ClientB

How to have multiple SSL certificates for Java server

Lawrence Dol: I have an internal HTTP server written in Java; the full source code is at my disposal. The HTTP server can be configured with any number of websites, each with a separate listening socket, which is created with the following command: skt=SSLServ

How to load multiple SSL certificates in Java KeyStore?

Peter I have two SSL certificate files. The first is labeled "OU=Certificate Authority" and the second is labeled "OU=Root Certificate". Our C++ application loads both certificates for proper client/server handshake. Now, I need to use these certificates in my

How to load multiple SSL certificates in Java KeyStore?

Peter I have two SSL certificate files. The first is labeled "OU=Certificate Authority" and the second is labeled "OU=Root Certificate". Our C++ application loads both certificates for proper client/server handshake. Now, I need to use these certificates in my

How to have multiple SSL certificates for Java server

Lawrence Dol: I have an internal HTTP server written in Java; the full source code is at my disposal. The HTTP server can be configured with any number of websites, each with a separate listening socket, which is created with the following command: skt=SSLServ

How to load multiple SSL certificates in Java KeyStore?

Peter I have two SSL certificate files. The first is labeled "OU=Certificate Authority" and the second is labeled "OU=Root Certificate". Our C++ application loads both certificates for proper client/server handshake. Now, I need to use these certificates in my

How to have multiple SSL certificates for Java server

Lawrence Dol: I have an internal HTTP server written in Java; the full source code is at my disposal. The HTTP server can be configured with any number of websites, each with a separate listening socket, which is created with the following command: skt=SSLServ