2

I'm new to java and find it very difficult for HTTP communication. I'm trying to convert the below curl request in java which uses a .crt certificate along with a pem private key.

curl -X POST 'https://oauthasservices.cert.mydomain.com/oauth2/apitoken?grant_type=client_credentials'\
-H 'Accept: application/json' \
--cert my_cert.crt \
--key my-private-key.pem \
-d 'grant_type=client_credentials'
Enter PEM pass phrase: myphrase ==> access token retrieved .

I'm not able to understand how can I pass the private key and passphrase while creating SSL context/ key Store in Java.

    keyStore.load(new FileInputStream(keyStorePath), keystorePassword.toCharArray());
    KeyManagerFactory kmf = 
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, keystorePassword.toCharArray());
    SSLContext ctx = SSLContext.getInstance("TLS");// something else?
    ctx.init(kmf.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = ctx.getSocketFactory();

Please let me know What is the best way to deal with crt cert file along with private key?

Ankit
  • 51
  • 1
  • 6
  • 1
    I think it is easier if you take your .crt and your private key and convert to a keystore, then use the keystore in code. Example of creating keystore https://stackoverflow.com/questions/906402/how-to-import-an-existing-x-509-certificate-and-private-key-in-java-keystore-to Otherwise, if you want to do everything in code, please see https://stackoverflow.com/questions/42675033/how-to-build-a-sslsocketfactory-from-pem-certificate-and-key-without-converting – JCompetence Oct 26 '21 at 07:08
  • 1
    This might be helpful https://github.com/Hakky54/sslcontext-kickstart#using-pem-files it will take care of the low level coding and handling and parsing the pen files – Hakan54 Nov 01 '21 at 13:11

0 Answers0