Search in sources :

Example 6 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 7 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project elasticsearch by elastic.

the class GceDiscoverTests method getSSLContext.

private static SSLContext getSSLContext() throws Exception {
    char[] passphrase = "keypass".toCharArray();
    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream stream = GceDiscoverTests.class.getResourceAsStream("/test-node.jks")) {
        assertNotNull("can't find keystore file", stream);
        ks.load(stream, passphrase);
    }
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, passphrase);
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);
    SSLContext ssl = SSLContext.getInstance("TLS");
    ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    return ssl;
}
Also used : InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 8 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project vert.x by eclipse.

the class KeyStoreHelper method getKeyMgrFactory.

public KeyManagerFactory getKeyMgrFactory(VertxInternal vertx) throws Exception {
    KeyManagerFactory fact = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    fact.getProvider();
    KeyStore ks = loadStore(vertx);
    fact.init(ks, password != null ? password.toCharArray() : null);
    return fact;
}
Also used : KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 9 with KeyManagerFactory

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

Example 10 with KeyManagerFactory

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

the class MockHttpServer method before.

public void before(final Description description) throws Exception {
    this.service = Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TestHttpServer-%d").build());
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
    if (hasSsl) {
        byte[] sampleTruststore1 = Base64.decode(TEST_TS1);
        byte[] sampleKeystore1 = Base64.decode(TEST_KS1);
        keystore = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
        truststore = File.createTempFile("SecureAcceptAllGetTest", ".truststore");
        FileOutputStream keystoreFileOut = new FileOutputStream(keystore);
        try {
            keystoreFileOut.write(sampleKeystore1);
        } finally {
            keystoreFileOut.close();
        }
        FileOutputStream truststoreFileOut = new FileOutputStream(truststore);
        try {
            truststoreFileOut.write(sampleTruststore1);
        } finally {
            truststoreFileOut.close();
        }
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystore), PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, PASSWORD.toCharArray());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(new FileInputStream(truststore), PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        HttpsServer secureServer = HttpsServer.create(inetSocketAddress, 0);
        secureServer.setHttpsConfigurator(new HttpsConfigurator(sc) {

            public void configure(HttpsParameters params) {
                SSLContext c = getSSLContext();
                SSLParameters sslparams = c.getDefaultSSLParameters();
                params.setSSLParameters(sslparams);
            }
        });
        server = secureServer;
    } else {
        server = HttpServer.create(inetSocketAddress, 0);
    }
    server.setExecutor(service);
    for (Entry<String, HttpHandler> handler : handlers.entrySet()) {
        server.createContext(handler.getKey(), handler.getValue());
    }
    server.start();
    localHttpServerPort = server.getAddress().getPort();
    System.out.println(description.getClassName() + " TestServer is started: " + getServerUrl());
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) SSLParameters(javax.net.ssl.SSLParameters) FileOutputStream(java.io.FileOutputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpsServer(com.sun.net.httpserver.HttpsServer)

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