Search in sources :

Example 16 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project netty-socketio by mrniko.

the class SocketIOChannelInitializer method createSSLContext.

private SSLContext createSSLContext(Configuration configuration) throws Exception {
    TrustManager[] managers = null;
    if (configuration.getTrustStore() != null) {
        KeyStore ts = KeyStore.getInstance(configuration.getTrustStoreFormat());
        ts.load(configuration.getTrustStore(), configuration.getTrustStorePassword().toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        managers = tmf.getTrustManagers();
    }
    KeyStore ks = KeyStore.getInstance(configuration.getKeyStoreFormat());
    ks.load(configuration.getKeyStore(), configuration.getKeyStorePassword().toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(configuration.getKeyManagerFactoryAlgorithm());
    kmf.init(ks, configuration.getKeyStorePassword().toCharArray());
    SSLContext serverContext = SSLContext.getInstance(configuration.getSSLProtocol());
    serverContext.init(kmf.getKeyManagers(), managers, null);
    return serverContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 17 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project ninja by ninjaframework.

the class StandaloneHelper method createSSLContext.

public static SSLContext createSSLContext(URI keystoreUri, char[] keystorePassword, URI truststoreUri, char[] truststorePassword) throws Exception {
    // load keystore
    KeyStore keystore = loadKeyStore(keystoreUri, keystorePassword);
    KeyManager[] keyManagers;
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keystore, keystorePassword);
    keyManagers = keyManagerFactory.getKeyManagers();
    // load truststore
    KeyStore truststore = loadKeyStore(truststoreUri, truststorePassword);
    TrustManager[] trustManagers;
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.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) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager)

Example 18 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project robovm by robovm.

the class HandshakeCompletedEventTest method getKeyManagers.

/**
     * Loads a keystore from a base64-encoded String. Returns the KeyManager[]
     * for the result.
     */
private KeyManager[] getKeyManagers(String keys) throws Exception {
    byte[] bytes = Base64.decode(keys.getBytes());
    InputStream inputStream = new ByteArrayInputStream(bytes);
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(inputStream, PASSWORD.toCharArray());
    inputStream.close();
    String algorithm = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm);
    keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
    return keyManagerFactory.getKeyManagers();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 19 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project robovm by robovm.

the class HttpsURLConnectionTest method getContext.

/**
     * Builds and returns the context used for secure socket creation.
     */
private static SSLContext getContext() throws Exception {
    String type = KeyStore.getDefaultType();
    String keyStore = getKeyStoreFileName();
    File keyStoreFile = new File(keyStore);
    FileInputStream fis = new FileInputStream(keyStoreFile);
    KeyStore ks = KeyStore.getInstance(type);
    ks.load(fis, KS_PASSWORD.toCharArray());
    fis.close();
    if (DO_LOG && false) {
        TestKeyStore.dump("HttpsURLConnection.getContext", ks, KS_PASSWORD.toCharArray());
    }
    String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
    kmf.init(ks, KS_PASSWORD.toCharArray());
    KeyManager[] keyManagers = kmf.getKeyManagers();
    String tmfAlgorthm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorthm);
    tmf.init(ks);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    if (DO_LOG) {
        trustManagers = TestTrustManager.wrap(trustManagers);
    }
    SSLContext ctx = SSLContext.getInstance("TLSv1");
    ctx.init(keyManagers, trustManagers, null);
    return ctx;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) File(java.io.File) TestKeyStore(libcore.java.security.TestKeyStore) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TestTrustManager(libcore.javax.net.ssl.TestTrustManager)

Example 20 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project robovm by robovm.

the class KeyManagerFactoryTest method test_KeyManagerFactory_getInstance.

public void test_KeyManagerFactory_getInstance() throws Exception {
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if (!type.equals("KeyManagerFactory")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            try {
                {
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
                    assertEquals(algorithm, kmf.getAlgorithm());
                    test_KeyManagerFactory(kmf);
                }
                {
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm, provider);
                    assertEquals(algorithm, kmf.getAlgorithm());
                    assertEquals(provider, kmf.getProvider());
                    test_KeyManagerFactory(kmf);
                }
                {
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm, provider.getName());
                    assertEquals(algorithm, kmf.getAlgorithm());
                    assertEquals(provider, kmf.getProvider());
                    test_KeyManagerFactory(kmf);
                }
            } catch (Exception e) {
                throw new Exception("Problem with algorithm " + algorithm, e);
            }
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Provider(java.security.Provider) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

KeyManagerFactory (javax.net.ssl.KeyManagerFactory)156 KeyStore (java.security.KeyStore)114 SSLContext (javax.net.ssl.SSLContext)80 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)69 FileInputStream (java.io.FileInputStream)44 KeyManager (javax.net.ssl.KeyManager)31 TrustManager (javax.net.ssl.TrustManager)30 IOException (java.io.IOException)29 InputStream (java.io.InputStream)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)28 KeyStoreException (java.security.KeyStoreException)21 SecureRandom (java.security.SecureRandom)18 UnrecoverableKeyException (java.security.UnrecoverableKeyException)18 KeyManagementException (java.security.KeyManagementException)17 CertificateException (java.security.cert.CertificateException)17 File (java.io.File)10 Certificate (java.security.cert.Certificate)10 NoSuchProviderException (java.security.NoSuchProviderException)9 SSLEngine (javax.net.ssl.SSLEngine)9 X509TrustManager (javax.net.ssl.X509TrustManager)9