If you want to connect somewhere using an ssl-connection you will need an SSLContext object. I had some trouble creating one with a client certificate because it rejected my .p12 file or other things broke.
Just c&p and adjust the following snippet to your needs…
private static SSLContext getSSLContext(File pKeyFile, String pKeyPassword) throws Exception { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); KeyStore keyStore = KeyStore.getInstance("PKCS12"); InputStream keyInput = new FileInputStream(pKeyFile); keyStore.load(keyInput, pKeyPassword.toCharArray()); keyInput.close(); keyManagerFactory.init(keyStore, pKeyPassword.toCharArray()); SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom()); return context; }
… and call it with something like this:
SSLContext sslcontext = getSSLContext(new File("ssl/myKeyFile.p12"), "VerySecurePassword");
You can create everything you need with it, so have fun 🙂