Search in sources :

Example 61 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project xDrip-plus by jamorham.

the class SSLServerSocketHelper method makeSSLSocketFactory.

static SSLServerSocketFactory makeSSLSocketFactory(InputStream keystoreStream, char[] passphrase) throws IOException {
    try {
        // bks should be supported - stream needs to be in this format
        final KeyStore keystore = KeyStore.getInstance("BKS");
        keystore.load(keystoreStream, passphrase);
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, passphrase);
        return makeSSLSocketFactory(keystore, keyManagerFactory);
    } catch (Exception e) {
        // simplify exception handling
        throw new IOException(e.getMessage());
    }
}
Also used : IOException(java.io.IOException) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 62 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project iaf by ibissource.

the class AuthSSLProtocolSocketFactory method createKeyManagers.

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password, String algorithm) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }
    log.debug("Initializing key manager");
    if (StringUtils.isEmpty(algorithm)) {
        algorithm = KeyManagerFactory.getDefaultAlgorithm();
        log.debug("using default KeyManager algorithm [" + algorithm + "]");
    } else {
        log.debug("using configured KeyManager algorithm [" + algorithm + "]");
    }
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(algorithm);
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}
Also used : KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 63 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project BaseProject by fly803.

the class HttpsUtils method prepareKeyManager.

private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) {
    try {
        if (bksFile == null || password == null)
            return null;
        KeyStore clientKeyStore = KeyStore.getInstance("BKS");
        clientKeyStore.load(bksFile, password.toCharArray());
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(clientKeyStore, password.toCharArray());
        return keyManagerFactory.getKeyManagers();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 64 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project async-http-client by AsyncHttpClient.

the class TestUtils method createKeyManagers.

private static KeyManager[] createKeyManagers() throws GeneralSecurityException, IOException {
    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keyStoreStream = TestUtils.class.getClassLoader().getResourceAsStream("ssltest-cacerts.jks")) {
        char[] keyStorePassword = "changeit".toCharArray();
        ks.load(keyStoreStream, keyStorePassword);
    }
    assert (ks.size() > 0);
    // Set up key manager factory to use our key store
    char[] certificatePassword = "changeit".toCharArray();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, certificatePassword);
    // Initialize the SSLContext to work with our key managers.
    return kmf.getKeyManagers();
}
Also used : InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 65 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project ranger by apache.

the class NiFiRegistryConnectionMgr method createSslContext.

private static SSLContext createSslContext(final String keystore, final char[] keystorePasswd, final String keystoreType, final String truststore, final char[] truststorePasswd, final String truststoreType, final String protocol) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
    // prepare the keystore
    final KeyStore keyStore = KeyStore.getInstance(keystoreType);
    try (final InputStream keyStoreStream = new FileInputStream(keystore)) {
        keyStore.load(keyStoreStream, keystorePasswd);
    }
    final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, keystorePasswd);
    // prepare the truststore
    final KeyStore trustStore = KeyStore.getInstance(truststoreType);
    try (final InputStream trustStoreStream = new FileInputStream(truststore)) {
        trustStore.load(trustStoreStream, truststorePasswd);
    }
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    // initialize the ssl context
    final SSLContext sslContext = SSLContext.getInstance(protocol);
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
    return sslContext;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

KeyManagerFactory (javax.net.ssl.KeyManagerFactory)439 KeyStore (java.security.KeyStore)322 SSLContext (javax.net.ssl.SSLContext)218 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)203 FileInputStream (java.io.FileInputStream)135 IOException (java.io.IOException)122 InputStream (java.io.InputStream)106 KeyManager (javax.net.ssl.KeyManager)104 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)79 TrustManager (javax.net.ssl.TrustManager)76 KeyStoreException (java.security.KeyStoreException)62 SecureRandom (java.security.SecureRandom)58 CertificateException (java.security.cert.CertificateException)57 UnrecoverableKeyException (java.security.UnrecoverableKeyException)54 KeyManagementException (java.security.KeyManagementException)51 File (java.io.File)37 X509Certificate (java.security.cert.X509Certificate)33 GeneralSecurityException (java.security.GeneralSecurityException)31 X509TrustManager (javax.net.ssl.X509TrustManager)29 Certificate (java.security.cert.Certificate)28