I generated a CSR using OpenSSL:
openssl req -out MyCompanyCsr.csr -new -newkey rsa:2048 -nodes -keyout MyCompanyPrivateKey.key
So starting out, we have:
- MyCompanyPrivateKey.key
- MyCompanyCsr.csr
Then I sent it to our integration partner, who responded with 3 files:
- PartnerIntermediateCa.crt
- PartnerRootCa.crt
- MyCompanyCsr.crt
Now I need to connect to their web service using mutual SSL. To do this, I know I need to set the truststore and keystore in my SSLSocketFactory for JAXB.
I'm instantiating the keystore and truststore in Java using:
KeyStore trustStore = KeyStore.getInstance("JKS");
InputStream tsis = ClassLoader.getSystemResourceAsStream(trustStorePath);
trustStore.load(tsis, "mypassword".toCharArray());
tsis.close();
KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream ksis = ClassLoader.getSystemResourceAsStream(keyStorePath);
keyStore.load(ksis, "mypassword".toCharArray());
if (ksis != null) {
ksis.close();
}
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, "mypassword".toCharArray());
However, attempting to use this code in connecting to the server throws a SSLHandshakeException
with the message http.client.failed
:
com.sun.xml.ws.client.ClientTransportException: HTTP transport error:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
The keystore
and truststore
I'm using were exported from my browser, with the Client private key as a PKCS
and the Server cert as a x509 Cert PKCS#7 w/ Chain'. Then opened them up in Portecle and exported them both as
JKS` files.
Assuming the Java code is legit, how can I be sure I have correctly created the keystore
and truststore
?
Thanks very much.
I finally figured this out. I used FireFox and Portecle.
Install the Server Certs and Private Key in the browser.
Note: One always confusing point: both the "truststore" and "keystore" are keystores as far as Portecle / Java goes. The only difference is the one we use as our keystore in the client is going to have our private keys in addition to the public certs.
TrustStore built with the server certificates:
Save somewhere as ffTestServerCert.crt
Open in Portecle via: Examine menu > Examine Certificate > select ffTestServerCert.crt
Save as .pem file on disk (for this example, say I have caCert1.pem, caCert2.pem, caCert3.pem)
Create new Keystore in Portecle: File > New Keystore > JKS
Repeat for any other certs you want to import (I did all 3).
Save keystore in Portecle:
Congrats, that's the valid truststore.
KeyStore built with the private key and server certificates:
Should say they were exported, click Ok
Open keys in Portecle
Enter password chosen above
Convert to JKS using Portecle
password
. Hit Ok
Save keystore:
Finished
Now you have the correctly-configured clientTrustStore.jks and clientKeyStore.jks for authenticating your client.
To see an example of how these can now be used, you can check out: SOAP with mutual SSL - how to send over credentials?