0

I'm new to certificate concept can any one help how to send the certificate in https request. My CURL command is below:

curl -d "grant_type=password&client_id=SmartRest&client_secret=594a27f3-4432-4d37-9196-2ba49de52758&username=user123&password=welcome123" https://xxxxxxx.xxx.in:8543/auth/realms/restapi/protocol/openid-connect/token --cacert ./ca_bundle.crt

Same thing i need to send in my java code, i have only one .crt file, I don't have keypass or anything.

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
  • Why you need to send a .crt certificate to a openid endpoint?Probably you need it to validate the server certificate – pedrofb Dec 18 '18 at 06:39
  • @pedrofb - that url shared by client, i need to call that only. that curl is working(host change) – Rajesh Narravula Dec 18 '18 at 06:40
  • `--cacert` verifies the server certificate. It does not send the certificate to the server. To transform curl to Java you need to use a keystore (JKS or PKCS12) and include the certificate into it. You can find many examples here in SO – pedrofb Dec 18 '18 at 07:10
  • you forgot to provide link – Rajesh Narravula Dec 18 '18 at 07:27
  • try https://stackoverflow.com/questions/5871279/java-ssl-and-cert-keystore or https://stackoverflow.com/questions/25084104/https-certificate-validation-fails-when-using-a-truststore – pedrofb Dec 18 '18 at 10:37

1 Answers1

0

I trusted the certificate by below keytool command.

keytool -import -trustcacerts -file "ca_bundle.crt" -alias "alias" -keystore  "C:\Program Files\Java\jdk1.8.0_131\jre\lib\security\cacerts"

Below code works fine, no need to skip ssl validation.

public static void main(String[] args) throws Exception {

   HttpsURLConnection con = (HttpsURLConnection) new URL("https://xxxx.xxx.xx:8543/auth/realms/restapi/protocol/openid-connect/token").openConnection();
   con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   String urlParameters = "grant_type=password&client_id=SmartRest&client_secret=594a27f3-4432-4d37-9196-2ba49de52758&username=user123&password=welcome123";

   con.setDoOutput(true);
   DataOutputStream wr = new DataOutputStream(con.getOutputStream());
   wr.writeBytes(urlParameters);
   wr.flush();
   wr.close();

   BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
   String inputLine;
   StringBuffer response = new StringBuffer();

   while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
   }
   in.close();

   //print result
   System.out.println(response.toString());

}

i got the result.

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54