Search in sources :

Example 36 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project UltimateAndroid by cymcsg.

the class HttpsUtils method getKeyManagerFactory.

private static KeyManagerFactory getKeyManagerFactory(InputStream key, String keyPassword) {
    KeyManagerFactory kmf = null;
    try {
        String keyStoreType = "BKS";
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(key, keyPassword.toCharArray());
        String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
        kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
        kmf.init(keyStore, keyPassword.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return kmf;
}
Also used : KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 37 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project android_frameworks_base by AOSPA.

the class SSLSocketFactory method createKeyManagers.

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}
Also used : KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 38 with KeyManagerFactory

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

the class DefaultServer method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, boolean client) throws IOException {
    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);
    }
    TrustManager[] trustManagers = null;
    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);
    }
    SSLContext sslContext;
    try {
        if (openssl && !client) {
            sslContext = SSLContext.getInstance("openssl.TLS");
        } else {
            sslContext = SSLContext.getInstance("TLS");
        }
        sslContext.init(keyManagers, trustManagers, null);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new IOException("Unable to create and initialise the SSLContext", e);
    }
    return sslContext;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) 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 39 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory 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 40 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project eiger by wlloyd.

the class SSLFactory method createSSLContext.

private static SSLContext createSSLContext(EncryptionOptions options) throws IOException {
    FileInputStream tsf = new FileInputStream(options.truststore);
    FileInputStream ksf = new FileInputStream(options.keystore);
    SSLContext ctx;
    try {
        ctx = SSLContext.getInstance(options.protocol);
        TrustManagerFactory tmf;
        KeyManagerFactory kmf;
        tmf = TrustManagerFactory.getInstance(options.algorithm);
        KeyStore ts = KeyStore.getInstance(options.store_type);
        ts.load(tsf, options.truststore_password.toCharArray());
        tmf.init(ts);
        kmf = KeyManagerFactory.getInstance(options.algorithm);
        KeyStore ks = KeyStore.getInstance(options.store_type);
        ks.load(ksf, options.keystore_password.toCharArray());
        kmf.init(ks, options.keystore_password.toCharArray());
        ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    } catch (Exception e) {
        throw new IOException("Error creating the initializing the SSL Context", e);
    } finally {
        FileUtils.closeQuietly(tsf);
        FileUtils.closeQuietly(ksf);
    }
    return ctx;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

KeyManagerFactory (javax.net.ssl.KeyManagerFactory)183 KeyStore (java.security.KeyStore)134 SSLContext (javax.net.ssl.SSLContext)90 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)81 FileInputStream (java.io.FileInputStream)48 IOException (java.io.IOException)39 KeyManager (javax.net.ssl.KeyManager)37 InputStream (java.io.InputStream)36 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)34 TrustManager (javax.net.ssl.TrustManager)33 KeyStoreException (java.security.KeyStoreException)26 KeyManagementException (java.security.KeyManagementException)23 UnrecoverableKeyException (java.security.UnrecoverableKeyException)23 CertificateException (java.security.cert.CertificateException)23 SecureRandom (java.security.SecureRandom)21 File (java.io.File)12 Certificate (java.security.cert.Certificate)11 X509KeyManager (javax.net.ssl.X509KeyManager)11 URL (java.net.URL)10 X509TrustManager (javax.net.ssl.X509TrustManager)10