use of javax.net.ssl.KeyManager in project fabric8 by fabric8io.
the class WebClients method configureClientCert.
public static void configureClientCert(WebClient webClient, String clientCertData, File clientCertFile, String clientKeyData, File clientKeyFile, String clientKeyAlgo, char[] clientKeyPassword) {
try {
KeyStore keyStore = createKeyStore(clientCertData, clientCertFile, clientKeyData, clientKeyFile, clientKeyAlgo, clientKeyPassword);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, clientKeyPassword);
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
TLSClientParameters params = conduit.getTlsClientParameters();
if (params == null) {
params = new TLSClientParameters();
conduit.setTlsClientParameters(params);
}
KeyManager[] existingKeyManagers = params.getKeyManagers();
if (existingKeyManagers != null && existingKeyManagers.length > 0) {
List<KeyManager> list = new ArrayList<>();
list.addAll(Arrays.asList(existingKeyManagers));
list.addAll(Arrays.asList(keyManagers));
keyManagers = list.toArray(new KeyManager[list.size()]);
}
params.setKeyManagers(keyManagers);
} catch (Exception e) {
LOG.error("Could not create key manager for " + clientCertFile + " (" + clientKeyFile + ")", e);
}
}
use of javax.net.ssl.KeyManager in project ballerina by ballerina-lang.
the class OpenSecureSocket method getSslContext.
private SSLContext getSslContext(BStruct options) throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
if (algorithm == null) {
algorithm = "SunX509";
}
if (log.isDebugEnabled()) {
log.debug("SSL algorithm : " + algorithm);
}
String keyStorePath = options.getStringField(SocketConstants.KEY_STORE_OPTION_FIELD_INDEX);
String keyStorePass = options.getStringField(SocketConstants.KEY_STORE_PASS_OPTION_FIELD_INDEX);
String trustStorePath = options.getStringField(SocketConstants.TRUST_STORE_OPTION_FIELD_INDEX);
String trustStorePass = options.getStringField(SocketConstants.TRUST_STORE_PASS_OPTION_FIELD_INDEX);
String certPassword = options.getStringField(SocketConstants.CERT_PASS_OPTION_FIELD_INDEX);
String protocol = options.getStringField(SocketConstants.SSL_PROTOCOL_OPTION_FIELD_INDEX);
String sslProtocol = (protocol == null || protocol.isEmpty()) ? SocketConstants.DEFAULT_SSL_PROTOCOL : options.getStringField(SocketConstants.SSL_PROTOCOL_OPTION_FIELD_INDEX);
if (log.isDebugEnabled()) {
log.debug("KeyStore path: " + keyStorePath);
log.debug("TrustStore path: " + trustStorePath);
log.debug("Protocol: " + sslProtocol);
}
KeyManager[] keyManagers = getKeyManagers(certPassword, keyStorePath, keyStorePass, algorithm);
TrustManager[] trustManagers = getTrustManagers(trustStorePath, trustStorePass, algorithm);
SSLContext sslContext = SSLContext.getInstance(sslProtocol);
sslContext.init(keyManagers, trustManagers, null);
return sslContext;
}
use of javax.net.ssl.KeyManager in project activemq-artemis by apache.
the class FailoverStaticNetworkTest method setUp.
@Before
public void setUp() throws Exception {
KeyManager[] km = SslBrokerServiceTest.getKeyManager();
TrustManager[] tm = SslBrokerServiceTest.getTrustManager();
sslContext = new SslContext(km, tm, null);
}
use of javax.net.ssl.KeyManager in project tomcat70 by apache.
the class JSSESocketFactory method getKeyManagers.
/**
* Gets the initialized key managers.
*/
protected KeyManager[] getKeyManagers(String keystoreType, String keystoreProvider, String algorithm, String keyAlias) throws Exception {
KeyManager[] kms = null;
String keystorePass = getKeystorePassword();
KeyStore ks = getKeystore(keystoreType, keystoreProvider, keystorePass);
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
String keyPass = endpoint.getKeyPass();
if (keyPass == null) {
keyPass = keystorePass;
}
kmf.init(ks, keyPass.toCharArray());
kms = kmf.getKeyManagers();
if (keyAlias != null) {
String alias = keyAlias;
if (JSSESocketFactory.defaultKeystoreType.equals(keystoreType)) {
alias = alias.toLowerCase(Locale.ENGLISH);
}
for (int i = 0; i < kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager) kms[i], alias);
}
}
return kms;
}
use of javax.net.ssl.KeyManager in project k-9 by k9mail.
the class DefaultTrustedSocketFactory method createSocket.
public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
TrustManager[] trustManagers = new TrustManager[] { trustManagerFactory.getTrustManagerForDomain(host, port) };
KeyManager[] keyManagers = null;
if (!TextUtils.isEmpty(clientCertificateAlias)) {
keyManagers = new KeyManager[] { new KeyChainKeyManager(context, clientCertificateAlias) };
}
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
Socket trustedSocket;
if (socket == null) {
trustedSocket = socketFactory.createSocket();
} else {
trustedSocket = socketFactory.createSocket(socket, host, port, true);
}
SSLSocket sslSocket = (SSLSocket) trustedSocket;
hardenSocket(sslSocket);
setSniHost(socketFactory, sslSocket, host);
return trustedSocket;
}
Aggregations