I'm working with JRE 1.8.0_51 (I cannot change this), which does not include the root certificate for Let's Encrypt in lib/security/cacerts
(it is added in 1.8.0_141
)
I need to add the certificate at runtime, and I have found this code to do that:
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keyStoreStream = getClass().getClassLoader().getResourceAsStream("j51_le_ca_cert.jks");
keyStore.load(keyStoreStream, "changeit".toCharArray());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustManagers, null);
SSLContext.setDefault(sc);
} catch (Exception e) {
e.printStackTrace();
}
This runs without throwing any error, but causes one of following things to happen:
- The LE certificate is correctly installed, but overwrites all existing certificates
- The LE certificate is not installed and all existing certificates remain
Which of those two events happens varies, seemingly arbitrarily, based on the system and environment in which it is running.
How can I reliably install the certificate alongside the existing certificates?