0

I want to upload a file to a HTTPS-server using Java. The server is not a open URL hence I need a client certificates to establish a connection. I am having .pem , .jks , .pkcs12 client certificate files.

Can any one suggest me how to use this certificate files in my application to establish communication ? Do I need to use all 3 certificate files ?

Matthias
  • 3,458
  • 4
  • 27
  • 46
Ganesh Rao B
  • 461
  • 2
  • 8
  • 23
  • 1
    Possible duplicate of [Java client certificates over HTTPS/SSL](http://stackoverflow.com/questions/875467/java-client-certificates-over-https-ssl) – Matthias Nov 08 '16 at 10:28

1 Answers1

1

The .jks file is the Java Keystore. It should contain the correct client certificates (and maybe also the intermediate certificates from the certificate chain).

I assume you are going to write a client that uploads the file to the HTTPS server? Then you should use the .jks file with the client certificate with the (let's say apache) HttpClient.

You need to create a SSLContext and load the keystore

SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File("keystore", "yourPassword".toCharArray(), new TrustSelfSignedStrategy()).build();

Then you have to put the sslContextin a SSLConnectionSocketFactory

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

And then finally build the HttpClient

HttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

After all these steps the httpClient should use your client certificate from the keystore for your desired request.

smsnheck
  • 1,563
  • 3
  • 21
  • 33