use of javax.net.ssl.KeyManager in project apache-kafka-on-k8s by banzaicloud.
the class SslFactory method createSSLContext.
// package access for testing
SSLContext createSSLContext(SecurityStore keystore) throws GeneralSecurityException, IOException {
SSLContext sslContext;
if (provider != null)
sslContext = SSLContext.getInstance(protocol, provider);
else
sslContext = SSLContext.getInstance(protocol);
KeyManager[] keyManagers = null;
if (keystore != null) {
String kmfAlgorithm = this.kmfAlgorithm != null ? this.kmfAlgorithm : KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
KeyStore ks = keystore.load();
Password keyPassword = keystore.keyPassword != null ? keystore.keyPassword : keystore.password;
kmf.init(ks, keyPassword.value().toCharArray());
keyManagers = kmf.getKeyManagers();
}
String tmfAlgorithm = this.tmfAlgorithm != null ? this.tmfAlgorithm : TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
KeyStore ts = truststore == null ? null : truststore.load();
tmf.init(ts);
sslContext.init(keyManagers, tmf.getTrustManagers(), this.secureRandomImplementation);
if (keystore != null && keystore != this.keystore) {
if (this.keystore == null)
throw new ConfigException("Cannot add SSL keystore to an existing listener for which no keystore was configured.");
if (keystoreVerifiableUsingTruststore)
SSLConfigValidatorEngine.validate(this, sslContext);
if (!CertificateEntries.create(this.keystore.load()).equals(CertificateEntries.create(keystore.load()))) {
throw new ConfigException("Keystore DistinguishedName or SubjectAltNames do not match");
}
}
return sslContext;
}
use of javax.net.ssl.KeyManager in project Much-Assembly-Required by simon987.
the class SocketServer method getContext.
/**
* See https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/SSLServerLetsEncryptExample.java
*/
/*
* * Copyright (c) 2010-2017 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*/
private static SSLContext getContext(String pathTo) {
SSLContext context;
String password = "MAR";
try {
context = SSLContext.getInstance("TLS");
byte[] certBytes = parseDERFromPEM(getBytes(new File(pathTo + File.separator + "cert.pem")), "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----");
byte[] keyBytes = parseDERFromPEM(getBytes(new File(pathTo + File.separator + "privkey.pem")), "-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----");
X509Certificate cert = generateCertificateFromDER(certBytes);
RSAPrivateKey key = generatePrivateKeyFromDER(keyBytes);
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
keystore.setCertificateEntry("cert-alias", cert);
keystore.setKeyEntry("key-alias", key, password.toCharArray(), new Certificate[] { cert });
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keystore, password.toCharArray());
KeyManager[] km = kmf.getKeyManagers();
context.init(km, null, null);
} catch (Exception e) {
context = null;
}
return context;
}
use of javax.net.ssl.KeyManager in project runwar by cfmlprojects.
the class SSLUtil method createSSLContext.
private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, final char[] passphrase, final String[] addCertificatePaths) throws IOException {
KeyManager[] keyManagers;
try {
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, passphrase);
keyManagers = keyManagerFactory.getKeyManagers();
} catch (NoSuchAlgorithmException ex) {
throw new IOException("Unable to initialise KeyManager[]", ex);
} catch (UnrecoverableKeyException ex2) {
throw new IOException("Unable to initialise KeyManager[]", ex2);
} catch (KeyStoreException ex3) {
throw new IOException("Unable to initialise KeyManager[]", ex3);
}
if (addCertificatePaths != null && addCertificatePaths.length > 0) {
for (int length = addCertificatePaths.length, i = 0; i < length; ++i) {
addCertificate(keyStore, new File(addCertificatePaths[i]), "addedKey" + i);
}
}
TrustManager[] trustManagers;
try {
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
trustManagers = trustManagerFactory.getTrustManagers();
} catch (NoSuchAlgorithmException ex4) {
throw new IOException("Unable to initialise TrustManager[]", ex4);
} catch (KeyStoreException ex5) {
throw new IOException("Unable to initialise TrustManager[]", ex5);
}
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);
} catch (NoSuchAlgorithmException ex6) {
throw new IOException("Unable to create and initialise the SSLContext", ex6);
} catch (KeyManagementException ex7) {
throw new IOException("Unable to create and initialise the SSLContext", ex7);
}
Arrays.fill(passphrase, '*');
return sslContext;
}
use of javax.net.ssl.KeyManager in project Pix-Art-Messenger by kriztan.
the class XmppConnection method getTlsFactoryVerifier.
private TlsFactoryVerifier getTlsFactoryVerifier() throws NoSuchAlgorithmException, KeyManagementException, IOException {
final SSLContext sc = SSLSocketHelper.getSSLContext();
MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
KeyManager[] keyManager;
if (account.getPrivateKeyAlias() != null && account.getPassword().isEmpty()) {
keyManager = new KeyManager[] { new MyKeyManager() };
} else {
keyManager = null;
}
String domain = account.getJid().getDomainpart();
sc.init(keyManager, new X509TrustManager[] { mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain) }, mXmppConnectionService.getRNG());
final SSLSocketFactory factory = sc.getSocketFactory();
final DomainHostnameVerifier verifier = trustManager.wrapHostnameVerifier(new XmppDomainVerifier(), mInteractive);
return new TlsFactoryVerifier(factory, verifier);
}
use of javax.net.ssl.KeyManager in project incubator-pulsar by apache.
the class SecurityUtility method createSslContext.
public static SSLContext createSslContext(boolean allowInsecureConnection, Certificate[] trustCertficates, Certificate[] certificates, PrivateKey privateKey) throws GeneralSecurityException {
KeyStoreHolder ksh = new KeyStoreHolder();
TrustManager[] trustManagers = null;
KeyManager[] keyManagers = null;
trustManagers = setupTrustCerts(ksh, allowInsecureConnection, trustCertficates);
keyManagers = setupKeyManager(ksh, privateKey, certificates);
SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(keyManagers, trustManagers, new SecureRandom());
sslCtx.getDefaultSSLParameters();
return sslCtx;
}
Aggregations