use of javax.net.ssl.KeyManagerFactory in project simple-java-mail by bbottema.
the class SSLConfiguration method getSSLSocketFactory.
public SSLSocketFactory getSSLSocketFactory() throws SocksException {
MiscUtil.checkNotNull(trustKeyStoreInfo, "trustKeyStoreInfo may not be null");
FileInputStream s1 = null;
FileInputStream s2 = null;
try {
final SSLContext context = SSLContext.getInstance("SSL");
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
final KeyStore trustKeyStore = KeyStore.getInstance(trustKeyStoreInfo.getType());
trustKeyStore.load(s1 = new FileInputStream(trustKeyStoreInfo.getKeyStorePath()), trustKeyStoreInfo.getPassword().toCharArray());
trustManagerFactory.init(trustKeyStore);
KeyStore keyStore = null;
if (keyStoreInfo != null && keyStoreInfo.getKeyStorePath() != null) {
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance(keyStoreInfo.getType());
keyStore.load(s2 = new FileInputStream(keyStoreInfo.getKeyStorePath()), keyStoreInfo.getPassword().toCharArray());
keyManagerFactory.init(keyStore, keyStoreInfo.getPassword().toCharArray());
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
} else {
context.init(null, trustManagerFactory.getTrustManagers(), null);
}
if (keyStore != null) {
LOGGER.info("SSL: Key store:{}", keyStoreInfo.getKeyStorePath());
}
LOGGER.info("SSL: Trust key store:{}", trustKeyStoreInfo.getKeyStorePath());
return context.getSocketFactory();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new SocksException(e.getMessage());
} finally {
tryCloseStream(s1);
tryCloseStream(s2);
}
}
use of javax.net.ssl.KeyManagerFactory in project nifi-registry by apache.
the class NiFiRegistryClientConfig method getSslContext.
public SSLContext getSslContext() {
if (sslContext != null) {
return sslContext;
}
final KeyManagerFactory keyManagerFactory;
if (keystoreFilename != null && keystorePass != null && keystoreType != null) {
try {
// prepare the keystore
final KeyStore keyStore = KeyStoreUtils.getKeyStore(keystoreType.name());
try (final InputStream keyStoreStream = new FileInputStream(new File(keystoreFilename))) {
keyStore.load(keyStoreStream, keystorePass.toCharArray());
}
keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
if (keyPass == null) {
keyManagerFactory.init(keyStore, keystorePass.toCharArray());
} else {
keyManagerFactory.init(keyStore, keyPass.toCharArray());
}
} catch (final Exception e) {
throw new IllegalStateException("Failed to load Keystore", e);
}
} else {
keyManagerFactory = null;
}
final TrustManagerFactory trustManagerFactory;
if (truststoreFilename != null && truststorePass != null && truststoreType != null) {
try {
// prepare the truststore
final KeyStore trustStore = KeyStoreUtils.getTrustStore(truststoreType.name());
try (final InputStream trustStoreStream = new FileInputStream(new File(truststoreFilename))) {
trustStore.load(trustStoreStream, truststorePass.toCharArray());
}
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
} catch (final Exception e) {
throw new IllegalStateException("Failed to load Truststore", e);
}
} else {
trustManagerFactory = null;
}
if (keyManagerFactory != null || trustManagerFactory != null) {
try {
// initialize the ssl context
KeyManager[] keyManagers = keyManagerFactory != null ? keyManagerFactory.getKeyManagers() : null;
TrustManager[] trustManagers = trustManagerFactory != null ? trustManagerFactory.getTrustManagers() : null;
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom());
sslContext.getDefaultSSLParameters().setNeedClientAuth(true);
return sslContext;
} catch (final Exception e) {
throw new IllegalStateException("Created keystore and truststore but failed to initialize SSLContext", e);
}
} else {
return null;
}
}
use of javax.net.ssl.KeyManagerFactory in project nifi-registry by apache.
the class SslContextFactory method createSslContext.
/**
* Creates a SSLContext instance using the given information.
*
* @param keystore the full path to the keystore
* @param keystorePasswd the keystore password
* @param keystoreType the type of keystore (e.g., PKCS12, JKS)
* @param protocol the protocol to use for the SSL connection
*
* @return a SSLContext instance
* @throws KeyStoreException if any issues accessing the keystore
* @throws IOException for any problems loading the keystores
* @throws NoSuchAlgorithmException if an algorithm is found to be used but is unknown
* @throws CertificateException if there is an issue with the certificate
* @throws UnrecoverableKeyException if the key is insufficient
* @throws KeyManagementException if unable to manage the key
*/
public static SSLContext createSslContext(final String keystore, final char[] keystorePasswd, final char[] keyPasswd, final String keystoreType, final String protocol) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
// prepare the keystore
final KeyStore keyStore = KeyStoreUtils.getKeyStore(keystoreType);
try (final InputStream keyStoreStream = new FileInputStream(keystore)) {
keyStore.load(keyStoreStream, keystorePasswd);
}
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
if (keyPasswd == null) {
keyManagerFactory.init(keyStore, keystorePasswd);
} else {
keyManagerFactory.init(keyStore, keyPasswd);
}
// initialize the ssl context
final SSLContext ctx = SSLContext.getInstance(protocol);
ctx.init(keyManagerFactory.getKeyManagers(), new TrustManager[0], new SecureRandom());
return ctx;
}
use of javax.net.ssl.KeyManagerFactory in project nzbhydra2 by theotherp.
the class SSLConfiguration method getSSLServerSocketFactory.
public SSLServerSocketFactory getSSLServerSocketFactory() throws SSLConfigurationException {
checkNotNull(keyStoreInfo, "keyStoreInfo may not be null");
String KEY_STORE_PASSWORD = getKeyStoreInfo().getPassword();
String KEY_STORE_PATH = getKeyStoreInfo().getKeyStorePath();
KeyStore keyStore = null;
KeyStore trustKeyStore = null;
try {
SSLContext ctx = SSLContext.getInstance("SSL");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(KEY_STORE_PATH), KEY_STORE_PASSWORD.toCharArray());
keyManagerFactory.init(keyStore, KEY_STORE_PASSWORD.toCharArray());
if (needClientAuth && trustKeyStoreInfo != null) {
String TRUST_KEY_STORE_PATH = getTrustKeyStoreInfo().getKeyStorePath();
String TRUST_KEY_STORE_PASSWORD = getTrustKeyStoreInfo().getPassword();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustKeyStore = KeyStore.getInstance("JKS");
trustKeyStore.load(new FileInputStream(TRUST_KEY_STORE_PATH), TRUST_KEY_STORE_PASSWORD.toCharArray());
trustManagerFactory.init(trustKeyStore);
ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
} else {
ctx.init(keyManagerFactory.getKeyManagers(), null, null);
}
logger.info("SSL: Key store:{}", keyStoreInfo.getKeyStorePath());
if (trustKeyStore != null) {
logger.info("SSL: Trust key store:{}", trustKeyStoreInfo.getKeyStorePath());
}
logger.info("SSL: Client authentication:{}", needClientAuth);
;
return ctx.getServerSocketFactory();
} catch (Exception e) {
throw new SSLConfigurationException(e.getMessage());
}
}
use of javax.net.ssl.KeyManagerFactory in project nzbhydra2 by theotherp.
the class SSLConfiguration method getSSLSocketFactory.
public SSLSocketFactory getSSLSocketFactory() throws SSLConfigurationException {
checkNotNull(trustKeyStoreInfo, "trustKeyStoreInfo may not be null");
KeyStore keyStore = null;
KeyStore trustKeyStore = null;
try {
SSLContext context = SSLContext.getInstance("SSL");
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustKeyStore = KeyStore.getInstance(trustKeyStoreInfo.getType());
trustKeyStore.load(new FileInputStream(trustKeyStoreInfo.getKeyStorePath()), trustKeyStoreInfo.getPassword().toCharArray());
trustManagerFactory.init(trustKeyStore);
if (keyStoreInfo != null && keyStoreInfo.getKeyStorePath() != null) {
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance(keyStoreInfo.getType());
keyStore.load(new FileInputStream(keyStoreInfo.getKeyStorePath()), keyStoreInfo.getPassword().toCharArray());
keyManagerFactory.init(keyStore, keyStoreInfo.getPassword().toCharArray());
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
} else {
context.init(null, trustManagerFactory.getTrustManagers(), null);
}
if (keyStore != null) {
logger.info("SSL: Key store:{}", keyStoreInfo.getKeyStorePath());
}
logger.info("SSL: Trust key store:{}", trustKeyStoreInfo.getKeyStorePath());
return context.getSocketFactory();
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new SSLConfigurationException(e.getMessage());
}
}
Aggregations