Search in sources :

Example 31 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project graylog2-server by Graylog2.

the class KeyUtil method initKeyStore.

public static KeyManager[] initKeyStore(File tlsKeyFile, File tlsCertFile, String tlsKeyPassword) throws IOException, GeneralSecurityException {
    final KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, null);
    final CertificateFactory cf = CertificateFactory.getInstance("X.509");
    final Collection<? extends Certificate> certChain = cf.generateCertificates(new FileInputStream(tlsCertFile));
    final PrivateKey privateKey = loadPrivateKey(tlsKeyFile, tlsKeyPassword);
    final char[] password = Strings.nullToEmpty(tlsKeyPassword).toCharArray();
    ks.setKeyEntry("key", privateKey, password, certChain.toArray(new Certificate[certChain.size()]));
    if (LOG.isDebugEnabled()) {
        LOG.debug("Private key file: {}", tlsKeyFile);
        LOG.debug("Certificate file: {}", tlsCertFile);
        LOG.debug("Aliases: {}", join(ks.aliases()));
    }
    final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, password);
    return kmf.getKeyManagers();
}
Also used : PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) FileInputStream(java.io.FileInputStream) Certificate(java.security.cert.Certificate) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 32 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 33 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project GNS by MobilityFirst.

the class AuthTestClient method getSSLFactory.

private static SSLSocketFactory getSSLFactory(String jksFile) throws Exception {
    // Create key store
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManager[] kmfs = null;
    if (jksFile.length() > 0) {
        keyStore.load(new FileInputStream(jksFile), CLIENT_PWD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, CLIENT_PWD.toCharArray());
        kmfs = kmf.getKeyManagers();
    }
    // create trust store (validates the self-signed server!)
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(new FileInputStream(TRUSTSTORE), CLIENT_PWD.toCharArray());
    TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustFactory.init(trustStore);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmfs, trustFactory.getTrustManagers(), null);
    return sslContext.getSocketFactory();
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 34 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project GNS by MobilityFirst.

the class GNSHttpsServer method createSSLContext.

private SSLContext createSSLContext() throws CertificateException, IOException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    char[] keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
    FileInputStream ksInputStream = new FileInputStream(System.getProperty("javax.net.ssl.keyStore"));
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(ksInputStream, keyStorePassword);
    // setup the key manager factory
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    keyManagerFactory.init(keyStore, keyStorePassword);
    char[] trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray();
    FileInputStream tsInputStream = new FileInputStream(System.getProperty("javax.net.ssl.trustStore"));
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(tsInputStream, trustStorePassword);
    // setup the trust manager factory
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
    trustManagerFactory.init(trustStore);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    // setup the HTTPS context and parameters
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    return sslContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 35 with KeyManagerFactory

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

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)

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