Search code examples
javasslssl-certificatekeystoresslcontext

How to store KeyStore for 2 different domains


I would like to store keystore for 2 different domains programatically. Below is the code to load keystore for domain A. I would like to do it for domain B. Both Keystore would be used in the same application.

public static SSLContext createSSLContext() throws Exception{
    KeyStore clientStore = createKeyStore();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(clientStore, "password".toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(kms, null, new SecureRandom());

    return sslContext;
}
public static KeyStore createKeyStore() throws Exception{
    KeyStore clientStore = KeyStore.getInstance("PKCS12");
    try {
        clientStore.load(new ByteArrayInputStream("PKCS12 info"), "password".toCharArray());

    } catch(Exception e){
        e.printStackTrace();
    }
    return clientStore;
}

Solution

  • As dave-thompson-085 mentioned, I was missing TrustStore keys. Snippet from following post was helpful. Programmatically Import CA trust cert into existing keystore file without using keytool