[Q#537040][A#573468] How to connect to a secure website using SSL in Java with a pkcs12 file?
https://stackoverflow.com/q/537040
I have a pkcs12 file. I need to use this to connect to a webpage using https protocol. I came across some code where in order to connect to a secure web page i need to set the following system properties: I have the p12(pkcs12) file. All I need is a truststore file. I extracted the certificates using: Now converted the cert PEM file to der Now adding the der file to a keystore Now I have the truststore, but when I use it, I get the following error Update: After removing certain properties and setting only the "trustStore", "trustStorePassword" and "trustStoreType" property, I got the following exception Please Help.
Answer
https://stackoverflow.com/a/573468
For anyone encountering a similar situation I was able to solve the issue above as follows: Regenerate your pkcs12 file as follows: Import the CA certificate from server into a TrustStore ( either your own, or the java keystore in $JAVA_HOME/jre/lib/security/cacerts, password: changeit). Set the following system properties: Test ur url. Courtesy@ http://forums.sun.com/thread.jspa?threadID=5296333
APIzation
openssl pkcs12 -in oldpkcs.p12 -out keys -passout pass:tmp
openssl pkcs12 -in keys -export -out new.p12 -passin pass:tmp -passout pass:newpasswd
System.setProperty("javax.net.ssl.trustStore", "myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.keyStore", "new.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "newpasswd");
package com.stackoverflow.api;
public class Human573468 {
public static void setJavaxProperties() {
System.setProperty("javax.net.ssl.trustStore", "myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.keyStore", "new.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "newpasswd");
}
}
package com.stackoverflow.api;
/**
* How to connect to a secure website using SSL in Java with a pkcs12 file?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/573468">https://stackoverflow.com/a/573468</a>
*/
public class APIzator573468 {
public static void connect() throws Exception {
System.setProperty("javax.net.ssl.trustStore", "myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.keyStore", "new.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "newpasswd");
}
}