I have a Java application that connects to another Java app through a socket with SSL, so my client JVM already has the -Djavax.net.ssl.keyStore
and -Djavax.net.ssl.trustStore
properties set.
This application needs to make some HTTP requests to a web server that requires client authentication. I can open the connection by using a URLConnection
in Java which returns an HTTPSURLConnectionImpl
.
The client certificate I want to present to the web server in the request is different than the one set as my JVM system property. Is there a way I can set a client cert. as a request property in the HTTPSURLConnectionImpl
?
Setting a SSL "client certificate" is not adequate directly through
HTTPSURLConnectionImpl
's request properties, because a digital signature is also required to prove you own the certificate. SSL already does all that automatically, so to makes sense to use that layer.You have two ways to solve your issue going forward.
Through configuration
You can add you client key and certificate to your JVM KeyStore, it should be picked up at Runtime when the server asks for your client-side SSL authentication. (SSL/TLS is designed for that : the server will ask for a client certificate that is signed by it's trusted authority, which allows the SSL Engine to choose the right certificate, even when your KeyStore holds many).
Through Code
You can roll you own
SSLContext
using custom madeKeyStore
/TrustStore
s. This is a bit complex (I won't elaborate on how to buildKeystore
instances in Java), but the gist of it is here :