Curl cacert equivalent in Java HttpClient

Viewed 2320

I am able to make the following curl command with no problem:

curl -s --cacert cert_ca.pem -X POST --data-urlencode name=abcde https://abcd.com/resource

Can you help me coming up with something on Java to achieve the same? I have looked at similar post but they talk about complete SSL handshake. Where as here, I am just presenting CA cert to use the specified certificate file to verify the peer.

Another question, is my understanding correct --> Here, I need to just configure truststore with the CA cert to verify the peer and not necessarily any keystore as I am not presenting any certificate on my behalf?

Thank you

I do have the jks file for CA cert as well

Upon trying Robert's suggestion with the following code:

    SSLContext sslContext = SSLContexts.custom()                          
         .loadTrustMaterial(new File("test_ca.jks")
         .build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    httpClient = HttpClients.custom()
         .setSSLSocketFactory(sslsf)
         .build();
    String openEndpoint = "URL";
    HttpPost httpPost = new HttpPost(openEndpoint);
    httpPost.setEntity(new UrlEncodedFormEntity(reqBodyParams)); 
    CloseableHttpResponse httpResponse = ( CloseableHttpResponse ) httpClient.execute(httpPost);

I am getting the following exception:

    javax.net.ssl.SSLException: Received fatal alert: unexpected_message
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1769)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:124)
at sun.security.ssl.Handshaker.sendChangeCipherSpec(Handshaker.java:1130)
at sun.security.ssl.ClientHandshaker.sendChangeCipherAndFinish(ClientHandshaker.java:1216)
at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:1128)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:348)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
1 Answers

You can specify a Java keystore file as "trust material" (trust store) when building the Apache Commons Http SSLContext:

    SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(new File("keystore.jks"), "trust-password".toCharArray()) // trust
            .build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
Related