I'm trying to create a SSL connection with certificates loaded from two files (.p12 and .p7b).
I have tried the following code to load the .p12 file
char []passwKey = "1234567".toCharArray();
KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("/home/user/Desktop/file.p12"), passwKey);
KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts,passwKey);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(tmf.getKeyManagers(), null, null);
SSLSocketFactory factory =sslContext.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(factory);
SSLSocket socket = (SSLSocket) factory.createSocket("www.host.com", 8883); // Create the ServerSocket
String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
socket.startHandshake();
but i receive exception:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I believe that I must create a .jks file form the .p12 and .p7b files (that contains the whole CA chain), but i'm a noob at this and I have no idea how to do that. Examples that I found were based on a single file/certificate.
UPDATE:
I used the certification files to create a single keystore (i believe i only needed the .p12 file) but with no luck. So I accessed the site directly and I exported the certificate as .pem and added it to a keystore. In my debug information I now receive "ServerHello" but at the end, I still get
handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
I tried several solutions, for ex. Java client certificates over HTTPS/SSL or Getting javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure Error with the certificate from the .p12 file received and the one exported from browser but none of them work...
UPDATE 2:
I tried this: https://stackoverflow.com/a/11908693/1215791 and managed to get to ServerHelloDone (and Found Trusted Certificate ...).
But, what i'm trying to do now is login with a SOAP request and i get this:
com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
at SoapT.login(SoapT.java:241)
at SoapT.main(SoapT.java:75)
I believe that is not a problem with the attached certificates, but an error when creating the soap request or an error (html) for the server.