My current TCP client is building sockets using org.apache.http.conn.ssl.SSLSocketFactory
I am migrating to import javax.net.ssl.SSLSocketFactory;
However, I am not sure how to load the private key for the client certificate which is stored in the .p12 file that I already have, as the context for the new SSLSocketFactory?
Here is my existing code, using the deprecated SSLSocketFactory:
import org.apache.http.ssl.SSLContexts;
import org.apache.http.conn.ssl.SSLSocketFactory;
String ksPassphrase = "myKSpass";
String pkPassphrase = "myPKpass";
// Get a keystore instance of type PKCS12:
KeyStore keystore = KeyStore.getInstance("PKCS12");
// Load keystore file and password:
keystore.load(new FileInputStream("myFile.p12"), ksPassphrase.toCharArray());
// Create HTTPS connection socket factory context:
SSLContext context = SSLContexts.custom()
.loadKeyMaterial(keystore, pkPassphrase.toCharArray())
.build();
SSLSocketFactory sslScktFactory = new SSLSocketFactory(context);
Here is the best code I could manage from the docs:
import javax.net.ssl.*;
// not sure if this is the pass to KS or private key or, somehow both?
String passphrase = "myPassphrase"
SSLContext context = SSLContext.getInstance("TLS");
KeyManagerFactory keyMngFactory = KeyManagerFactory.getInstance("SunX509");
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream("myFile.jks"), passphrase.toCharArray());
keyMngFactory.init(keyStore, passphrase);
context.init(keyMngFactory.getKeyManagers(), null, null);
SSLSocketFactory sslScktFactory = context.getSocketFactory();
- I think the problem lies that I don't have a
.custom()
method for the context under the new library.
What to use for the new SSLSocketFactory? - Is it the case that JKS keystores don't have internal passwords for the private keys stored inside (I only used PKCS until now, not familiar with the JKS format) and that's why I can't see where the private key password is being referred to?