Search in sources :

Example 11 with KeyManagerFactory

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

the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.

protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
    Properties supplementalProperties = new Properties();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    SecureRandom sr = null;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    // Ignore
    }
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(null, null, null);
    SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
    supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
    supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
    supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
    supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
    if (sr != null) {
        supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
        supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
    }
    supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
    supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
    // Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
    String ssp = "";
    for (String protocol : socket.getSupportedProtocols()) {
        if (!"SSLv2Hello".equals(protocol)) {
            ssp = protocol;
            break;
        }
    }
    supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
    return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 12 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 13 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 14 with KeyManagerFactory

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

the class X509Util method createKeyManager.

public static X509KeyManager createKeyManager(String keyStoreLocation, String keyStorePassword) throws KeyManagerException {
    FileInputStream inputStream = null;
    try {
        char[] keyStorePasswordChars = keyStorePassword.toCharArray();
        File keyStoreFile = new File(keyStoreLocation);
        KeyStore ks = KeyStore.getInstance("JKS");
        inputStream = new FileInputStream(keyStoreFile);
        ks.load(inputStream, keyStorePasswordChars);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, keyStorePasswordChars);
        for (KeyManager km : kmf.getKeyManagers()) {
            if (km instanceof X509KeyManager) {
                return (X509KeyManager) km;
            }
        }
        throw new KeyManagerException("Couldn't find X509KeyManager");
    } catch (Exception e) {
        throw new KeyManagerException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : KeyManagerException(org.apache.zookeeper.common.X509Exception.KeyManagerException) X509KeyManager(javax.net.ssl.X509KeyManager) IOException(java.io.IOException) File(java.io.File) KeyStore(java.security.KeyStore) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) TrustManagerException(org.apache.zookeeper.common.X509Exception.TrustManagerException) IOException(java.io.IOException) KeyManagerException(org.apache.zookeeper.common.X509Exception.KeyManagerException) SSLContextException(org.apache.zookeeper.common.X509Exception.SSLContextException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 15 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project cas by apereo.

the class FileTrustStoreSslSocketFactory method getKeyManager.

/**
     * Gets key manager.
     *
     * @param algorithm the algorithm
     * @param keystore  the keystore
     * @param password  the password
     * @return the key manager
     * @throws Exception the exception
     */
private static X509KeyManager getKeyManager(final String algorithm, final KeyStore keystore, final char[] password) throws Exception {
    final KeyManagerFactory factory = KeyManagerFactory.getInstance(algorithm);
    factory.init(keystore, password);
    return (X509KeyManager) factory.getKeyManagers()[0];
}
Also used : X509KeyManager(javax.net.ssl.X509KeyManager) 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