Search in sources :

Example 81 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project cdap by caskdata.

the class ExternalMTLSAuthenticationServerTest method getInvalidKeyManagers.

/**
 * Sets up the client's keystore Using a certificate that is not part of the server's trustore
 *
 * @return
 * @throws Exception
 */
@Override
protected KeyManager[] getInvalidKeyManagers() throws Exception {
    URL clientKeystoreURL = ExternalMTLSAuthenticationServerTest.class.getClassLoader().getResource("invalid-client" + ".jks");
    Assert.assertNotNull(clientKeystoreURL);
    final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    KeyStore ks = KeyStore.getInstance("JKS");
    char[] ksPass = "secret".toCharArray();
    try (FileInputStream fis = new FileInputStream(clientKeystoreURL.getPath())) {
        ks.load(fis, ksPass);
        kmf.init(ks, configuration.get("security.auth.server.ssl.keystore.password", "secret").toCharArray());
    }
    return kmf.getKeyManagers();
}
Also used : KeyStore(java.security.KeyStore) URL(java.net.URL) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 82 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project fluency by komamitsu.

the class SSLTestServerSocketFactory method create.

public SSLServerSocket create() throws IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException {
    String trustStorePath = SSLSocketBuilder.class.getClassLoader().getResource("truststore.jks").getFile();
    System.getProperties().setProperty("javax.net.ssl.trustStore", trustStorePath);
    String keyStorePath = SSLSocketBuilder.class.getClassLoader().getResource("keystore.jks").getFile();
    InputStream keystoreStream = null;
    try {
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystoreStream = new FileInputStream(new File(keyStorePath));
        keystore.load(keystoreStream, KEYSTORE_PASSWORD.toCharArray());
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, KEY_PASSWORD.toCharArray());
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
        SSLServerSocket serverSocket = (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket();
        serverSocket.setEnabledCipherSuites(serverSocket.getSupportedCipherSuites());
        serverSocket.bind(new InetSocketAddress(0));
        return serverSocket;
    } finally {
        if (keystoreStream != null) {
            keystoreStream.close();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InetSocketAddress(java.net.InetSocketAddress) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket) KeyStore(java.security.KeyStore) File(java.io.File) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 83 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project runwar by cfmlprojects.

the class SSLUtil method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, final char[] passphrase, final String[] addCertificatePaths) throws IOException {
    KeyManager[] keyManagers;
    try {
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, passphrase);
        keyManagers = keyManagerFactory.getKeyManagers();
    } catch (NoSuchAlgorithmException ex) {
        throw new IOException("Unable to initialise KeyManager[]", ex);
    } catch (UnrecoverableKeyException ex2) {
        throw new IOException("Unable to initialise KeyManager[]", ex2);
    } catch (KeyStoreException ex3) {
        throw new IOException("Unable to initialise KeyManager[]", ex3);
    }
    if (addCertificatePaths != null && addCertificatePaths.length > 0) {
        for (int length = addCertificatePaths.length, i = 0; i < length; ++i) {
            addCertificate(keyStore, new File(addCertificatePaths[i]), "addedKey" + i);
        }
    }
    TrustManager[] trustManagers;
    try {
        final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        trustManagers = trustManagerFactory.getTrustManagers();
    } catch (NoSuchAlgorithmException ex4) {
        throw new IOException("Unable to initialise TrustManager[]", ex4);
    } catch (KeyStoreException ex5) {
        throw new IOException("Unable to initialise TrustManager[]", ex5);
    }
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);
    } catch (NoSuchAlgorithmException ex6) {
        throw new IOException("Unable to create and initialise the SSLContext", ex6);
    } catch (KeyManagementException ex7) {
        throw new IOException("Unable to create and initialise the SSLContext", ex7);
    }
    Arrays.fill(passphrase, '*');
    return sslContext;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) 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) File(java.io.File)

Example 84 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project incubator-pulsar by apache.

the class SecurityUtility method setupKeyManager.

private static KeyManager[] setupKeyManager(KeyStoreHolder ksh, PrivateKey privateKey, Certificate[] certificates) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    KeyManager[] keyManagers = null;
    if (certificates != null && privateKey != null) {
        ksh.setPrivateKey("private", privateKey, certificates);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ksh.getKeyStore(), "".toCharArray());
        keyManagers = kmf.getKeyManagers();
    }
    return keyManagers;
}
Also used : KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 85 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project webcert by sklintyg.

the class KeystoreBasedSocketFactory method createSSLContext.

private static SSLContext createSSLContext(final KeyStore truststore) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(null, null);
    KeyManager[] keymanagers = kmfactory.getKeyManagers();
    TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmfactory.init(truststore);
    TrustManager[] trustmanagers = tmfactory.getTrustManagers();
    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) JKSKeyManager(org.springframework.security.saml.key.JKSKeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager)

Aggregations

KeyManagerFactory (javax.net.ssl.KeyManagerFactory)439 KeyStore (java.security.KeyStore)322 SSLContext (javax.net.ssl.SSLContext)218 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)203 FileInputStream (java.io.FileInputStream)135 IOException (java.io.IOException)122 InputStream (java.io.InputStream)106 KeyManager (javax.net.ssl.KeyManager)104 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)79 TrustManager (javax.net.ssl.TrustManager)76 KeyStoreException (java.security.KeyStoreException)62 SecureRandom (java.security.SecureRandom)58 CertificateException (java.security.cert.CertificateException)57 UnrecoverableKeyException (java.security.UnrecoverableKeyException)54 KeyManagementException (java.security.KeyManagementException)51 File (java.io.File)37 X509Certificate (java.security.cert.X509Certificate)33 GeneralSecurityException (java.security.GeneralSecurityException)31 X509TrustManager (javax.net.ssl.X509TrustManager)29 Certificate (java.security.cert.Certificate)28