//TODO for performance reasons we should cache the KeymanagerFactory and TrustManagerFactory
if ((keyStorePassword != null) && (keyPassword != null) && (!keyStorePassword.equals(keyPassword))) {
LogUtils.log(LOG, Level.WARNING, "KEY_PASSWORD_NOT_SAME_KEYSTORE_PASSWORD");
}
try {
SSLContext sslctx = SSLContext.getInstance(secureSocketProtocol);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(keystoreKeyManagerFactoryAlgorithm);
KeyStore ks = KeyStore.getInstance(keyStoreType);
FileInputStream fis = new FileInputStream(keyStoreLocation);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
KeyManager[] keystoreManagers = null;
if (keyStorePassword != null) {
try {
ks.load(bin, keyStorePassword.toCharArray());
kmf.init(ks, keyStorePassword.toCharArray());
keystoreManagers = kmf.getKeyManagers();
LogUtils.log(LOG, Level.INFO, "LOADED_KEYSTORE", new Object[]{keyStoreLocation});
} catch (Exception e) {
LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_KEYSTORE",
new Object[]{keyStoreLocation, e.getMessage()});
}
}
if ((keyStorePassword == null) && (keyStoreLocation != null)) {
LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_KEYSTORE_NULL_PASSWORD",
new Object[]{keyStoreLocation});
}
// ************************* Load Trusted CA file *************************
TrustManager[] trustStoreManagers = null;
KeyStore trustedCertStore = KeyStore.getInstance(trustStoreType);
trustedCertStore.load(new FileInputStream(trustStoreLocation), null);
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(trustStoreKeyManagerFactoryAlgorithm);
try {
tmf.init(trustedCertStore);
trustStoreManagers = tmf.getTrustManagers();
LogUtils.log(LOG, Level.INFO, "LOADED_TRUST_STORE", new Object[]{trustStoreLocation});
} catch (Exception e) {
LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_TRUST_STORE",
new Object[]{trustStoreLocation, e.getMessage()});
}
sslctx.init(keystoreManagers, trustStoreManagers, null);
httpsConnection.setSSLSocketFactory(new SSLSocketFactoryWrapper(sslctx.getSocketFactory(),
cipherSuites));
} catch (Exception e) {