It looks like you are trying to use HTTPS with client certificate authentication. I assume that your server is configured to request this (since the client certificate can only be requested by the server).
In Java, java.security.cert.X509Certificate is really just a certificate (public key certificate without private key). What you need on the client side is to configure a private key with it. Assuming that your private key and certificate are in the keystore (to simplify, I assume there is only one suitable certificate with its private key, possibly with other certificates in the chain, in this keystore) and that you use the default trust store:
KeyStore ks = ... KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, password); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(kmf.getKeyManagers(), null, null); URL url = new URL("https://example/"); HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); connection.setSSLSocketFactory(sslContext.getSSLSocketFactory());
Other libraries will allow you to set up SSLContext or KeyStore slightly different way, but the principles should be the same.
(You can also use the system properties of javax.net.ssl.keyStore , if necessary.)
Bruno
source share