Search code examples
javasslssl-certificatekeystore

Java: Loading SSL Keystore via a resource


If I have:

System.setProperty("javax.net.ssl.keyStore", '/etc/certificates/fdms/WS1001237590._.1.ks');
System.setProperty("javax.net.ssl.keyStorePassword", 'DV8u4xRVDq');
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

I'm able to open a secure connection without a problem.

However, I'd like to have the certificates stored directly in the war, so I use: (The file input stream will eventually become a resource stream, but I'm doing this to get it to work.)

System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("/etc/certificates/fdms/WS1001237590._.1.ks"), "DV8u4xRVDq".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "DV8u4xRVDq".toCharArray());
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);

Now, if I open the same connection, I get: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure


Solution

  • For posterity's sake, all of this was far too complicated, and we pretty much just had a check in the static block:

    if( environment == 'production') {
        System.setProperty("javax.net.ssl.keyStore",                    '/etc/certificates/prod/keystore.ks');
        System.setProperty("javax.net.ssl.keyStorePassword",            'password');
        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
    } else {
        System.setProperty("javax.net.ssl.keyStore",                    '/etc/certificates/test/keystore.ks');
        System.setProperty("javax.net.ssl.keyStorePassword",            'password');
        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
    }