Search in sources :

Example 66 with KeyManager

use of javax.net.ssl.KeyManager in project undertow by undertow-io.

the class Http2Server method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore) throws Exception {
    KeyManager[] keyManagers;
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password("key"));
    keyManagers = keyManagerFactory.getKeyManagers();
    TrustManager[] trustManagers;
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    trustManagers = trustManagerFactory.getTrustManagers();
    SSLContext sslContext;
    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    return sslContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager)

Example 67 with KeyManager

use of javax.net.ssl.KeyManager in project undertow by undertow-io.

the class DefaultServer method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, String protocol, boolean client) throws IOException {
    final KeyManager[] keyManagers;
    try {
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, STORE_PASSWORD);
        keyManagers = keyManagerFactory.getKeyManagers();
    } catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) {
        throw new IOException("Unable to initialise KeyManager[]", e);
    }
    final TrustManager[] trustManagers;
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        trustManagers = trustManagerFactory.getTrustManagers();
    } catch (NoSuchAlgorithmException | KeyStoreException e) {
        throw new IOException("Unable to initialise TrustManager[]", e);
    }
    final SSLContext sslContext;
    try {
        if (openssl && !client) {
            sslContext = SSLContext.getInstance("openssl.TLS");
        } else {
            sslContext = SSLContext.getInstance(protocol);
        }
        sslContext.init(keyManagers, trustManagers, null);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new IOException("Unable to create and initialise the SSLContext", e);
    }
    if (!client) {
        SNIContextMatcher matcher = new SNIContextMatcher.Builder().setDefaultContext(sslContext).addMatch("localhost", sslContext).build();
        return new SNISSLContext(matcher);
    } else {
        return sslContext;
    }
}
Also used : SNIContextMatcher(io.undertow.protocols.ssl.SNIContextMatcher) SNISSLContext(io.undertow.protocols.ssl.SNISSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) SNISSLContext(io.undertow.protocols.ssl.SNISSLContext) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) UnrecoverableKeyException(java.security.UnrecoverableKeyException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager)

Example 68 with KeyManager

use of javax.net.ssl.KeyManager in project Conversations by siacs.

the class XmppConnection method getSSLSocketFactory.

private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    final SSLContext sc = SSLSocketHelper.getSSLContext();
    final MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
    final KeyManager[] keyManager;
    if (account.getPrivateKeyAlias() != null) {
        keyManager = new KeyManager[] { new MyKeyManager() };
    } else {
        keyManager = null;
    }
    final String domain = account.getServer();
    sc.init(keyManager, new X509TrustManager[] { mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain) }, mXmppConnectionService.getRNG());
    return sc.getSocketFactory();
}
Also used : MemorizingTrustManager(eu.siacs.conversations.services.MemorizingTrustManager) SSLContext(javax.net.ssl.SSLContext) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 69 with KeyManager

use of javax.net.ssl.KeyManager in project jmeter by apache.

the class Proxy method getWrappedKeyManagers.

/**
 * Return the key managers, wrapped to return a specific alias
 */
private KeyManager[] getWrappedKeyManagers(final String keyAlias) throws GeneralSecurityException, IOException {
    if (!keyStore.containsAlias(keyAlias)) {
        throw new IOException("Keystore does not contain alias " + keyAlias);
    }
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYMANAGERFACTORY);
    kmf.init(keyStore, keyPassword.toCharArray());
    final KeyManager[] keyManagers = kmf.getKeyManagers();
    // Check if alias is suitable here, rather than waiting for connection to fail
    final int keyManagerCount = keyManagers.length;
    final KeyManager[] wrappedKeyManagers = new KeyManager[keyManagerCount];
    for (int i = 0; i < keyManagerCount; i++) {
        wrappedKeyManagers[i] = new ServerAliasKeyManager(keyManagers[i], keyAlias);
    }
    return wrappedKeyManagers;
}
Also used : IOException(java.io.IOException) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 70 with KeyManager

use of javax.net.ssl.KeyManager in project ribbon by Netflix.

the class AbstractSslContextFactory method createKeyManagers.

/**
 * Creates the key managers to be used by the factory from the associated key store and password.
 *
 * @return the newly created array of key managers
 * @throws ClientSslSocketFactoryException if an exception is detected in loading the key store
 */
private KeyManager[] createKeyManagers() throws ClientSslSocketFactoryException {
    final KeyManagerFactory factory;
    try {
        factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        factory.init(this.keyStore, this.keyStorePassword.toCharArray());
    } catch (NoSuchAlgorithmException e) {
        throw new ClientSslSocketFactoryException(String.format("Failed to create the key store because the algorithm %s is not supported. ", KeyManagerFactory.getDefaultAlgorithm()), e);
    } catch (UnrecoverableKeyException e) {
        throw new ClientSslSocketFactoryException("Unrecoverable Key Exception initializing key manager factory; this is probably fatal", e);
    } catch (KeyStoreException e) {
        throw new ClientSslSocketFactoryException("KeyStore exception initializing key manager factory; this is probably fatal", e);
    }
    KeyManager[] managers = factory.getKeyManagers();
    LOGGER.debug("Key managers are initialized. Total {} managers. ", managers.length);
    return managers;
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

KeyManager (javax.net.ssl.KeyManager)210 SSLContext (javax.net.ssl.SSLContext)127 TrustManager (javax.net.ssl.TrustManager)127 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)103 KeyStore (java.security.KeyStore)95 IOException (java.io.IOException)59 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)59 SecureRandom (java.security.SecureRandom)54 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)54 KeyManagementException (java.security.KeyManagementException)46 X509TrustManager (javax.net.ssl.X509TrustManager)45 KeyStoreException (java.security.KeyStoreException)42 X509KeyManager (javax.net.ssl.X509KeyManager)40 InputStream (java.io.InputStream)33 UnrecoverableKeyException (java.security.UnrecoverableKeyException)32 FileInputStream (java.io.FileInputStream)31 CertificateException (java.security.cert.CertificateException)30 GeneralSecurityException (java.security.GeneralSecurityException)24 X509Certificate (java.security.cert.X509Certificate)23 File (java.io.File)15