Example usage for javax.net.ssl KeyManagerFactory getDefaultAlgorithm

List of usage examples for javax.net.ssl KeyManagerFactory getDefaultAlgorithm

Introduction

In this page you can find the example usage for javax.net.ssl KeyManagerFactory getDefaultAlgorithm.

Prototype

public static final String getDefaultAlgorithm() 

Source Link

Document

Obtains the default KeyManagerFactory algorithm name.

Usage

From source file:edu.washington.shibboleth.attribute.resolver.provider.dataConnector.RwsDataConnector.java

/**
 * This sets the key managers that will be used for all TLS and SSL connections to the ldap. 
 * /*from  w  w  w. jav  a 2  s .  c om*/
 * @see #clearCache()
 * @see #initializeHttpPool()
 * @see #setSslSocketFactory(SSLSocketFactory)
 * 
 * @param kc <code>X509Credential</code> to create KeyManagers with
 */
public void setSslKeyManagers(X509Credential kc) {
    if (kc != null) {
        try {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(null, null);
            keystore.setKeyEntry("ldap_tls_client_auth", kc.getPrivateKey(), "changeit".toCharArray(),
                    kc.getEntityCertificateChain().toArray(new X509Certificate[0]));
            kmf.init(keystore, "changeit".toCharArray());
            sslKeyManagers = kmf.getKeyManagers();
        } catch (GeneralSecurityException e) {
            log.error("Error initializing key managers", e);
        } catch (IOException e) {
            log.error("Error initializing key managers", e);
        }
    }
}

From source file:com.sat.vcse.automation.utils.http.HttpClient.java

private SSLContext getSSLContext() {

    final String METHOD_NAME = "getSSLContext(): ";
    SSLContext sslContext = null;

    try {/*ww  w  .  ja  va 2 s  .co m*/
        //Get the TrustManager based on client truststore file presence or no
        final TrustManager[] trustManager = getTrustManagers();
        // Configure the SSLContext object with the defined cryptoProtocol 
        sslContext = SSLContext.getInstance(this.cryptoProtocol);

        if (this.isClientAuthEnabled) {
            // Load the Client Keystore
            final KeyManagerFactory kmf = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            final KeyStore clientKeystore = KeyStore.getInstance(KeyStore.getDefaultType());

            InputStream keystoreis = null;
            //see if the file is present otherwise read from class path
            File keStoreFile = new File(this.keystore);
            if (keStoreFile.exists()) {
                keystoreis = new FileInputStream(keStoreFile);
            } else {
                LogHandler.warn("File not found, so trying to read it from class path now");
                keystoreis = HttpClient.class.getResourceAsStream(this.keystore);
            }

            clientKeystore.load(keystoreis, this.keystorePasswd.toCharArray());
            kmf.init(clientKeystore, this.keystorePasswd.toCharArray());
            // Configure the SSLContext object with the Keystore, Truststore and random data 
            sslContext.init(kmf.getKeyManagers(), trustManager, new SecureRandom());

        } else {
            // Configure the SSLContext object with the only a Truststore and random data 
            sslContext.init(null, trustManager, new SecureRandom());
        }

    } catch (Exception exp) {
        LogHandler.error(CLASS_NAME + METHOD_NAME + exp.getMessage());
        throw new CoreRuntimeException(exp, CLASS_NAME + METHOD_NAME + exp.getMessage());
    }

    return sslContext;
}

From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

private static SocketFactory extractSocketFactory(String trustStore, String trustStorePassword, String keyStore,
        String keyStorePassword) {
    TrustManager[] trustManagers;
    KeyManager[] keyManagers;//from  w  w  w .  j  a v a  2s.  com

    try (InputStream trustStream = new FileInputStream(trustStore)) {
        char[] trustStorePass = trustStorePassword.toCharArray();
        KeyStore trustStoreJKS = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStoreJKS.load(trustStream, trustStorePass);
        TrustManagerFactory trustFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustFactory.init(trustStoreJKS);
        trustManagers = trustFactory.getTrustManagers();
    } catch (FileNotFoundException e) {
        throw new MongoTableException("Trust store file not found for secure connections to mongodb. "
                + "Trust Store file path : '" + trustStore + "'.", e);
    } catch (IOException e) {
        throw new MongoTableException(
                "I/O Exception in creating trust store for secure connections to mongodb. "
                        + "Trust Store file path : '" + trustStore + "'.",
                e);
    } catch (CertificateException e) {
        throw new MongoTableException("Certificates in the trust store could not be loaded for secure "
                + "connections to mongodb. Trust Store file path : '" + trustStore + "'.", e);
    } catch (NoSuchAlgorithmException e) {
        throw new MongoTableException("The algorithm used to check the integrity of the trust store cannot be "
                + "found. Trust Store file path : '" + trustStore + "'.", e);
    } catch (KeyStoreException e) {
        throw new MongoTableException("Exception in creating trust store, no Provider supports aKeyStoreSpi "
                + "implementation for the specified type. Trust Store file path : '" + trustStore + "'.", e);
    }

    try (InputStream keyStream = new FileInputStream(keyStore)) {
        char[] keyStorePass = keyStorePassword.toCharArray();
        KeyStore keyStoreJKS = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStoreJKS.load(keyStream, keyStorePass);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStoreJKS, keyStorePass);
        keyManagers = keyManagerFactory.getKeyManagers();
    } catch (FileNotFoundException e) {
        throw new MongoTableException("Key store file not found for secure connections to mongodb. "
                + "Key Store file path : '" + keyStore + "'.", e);
    } catch (IOException e) {
        throw new MongoTableException(
                "I/O Exception in creating trust store for secure connections to mongodb. "
                        + "Key Store file path : '" + keyStore + "'.",
                e);
    } catch (CertificateException e) {
        throw new MongoTableException("Certificates in the trust store could not be loaded for secure "
                + "connections to mongodb. Key Store file path : '" + keyStore + "'.", e);
    } catch (NoSuchAlgorithmException e) {
        throw new MongoTableException("The algorithm used to check the integrity of the trust store cannot be "
                + "found. Key Store file path : '" + keyStore + "'.", e);
    } catch (KeyStoreException e) {
        throw new MongoTableException(
                "Exception in creating trust store, no Provider supports aKeyStoreSpi "
                        + "implementation for the specified type. Key Store file path : '" + keyStore + "'.",
                e);
    } catch (UnrecoverableKeyException e) {
        throw new MongoTableException(
                "Key in the keystore cannot be recovered. " + "Key Store file path : '" + keyStore + "'.", e);
    }

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(keyManagers, trustManagers, null);
        SSLContext.setDefault(sslContext);
        return sslContext.getSocketFactory();
    } catch (KeyManagementException e) {
        throw new MongoTableException(
                "Error in validating the key in the key store/ trust store. " + "Trust Store file path : '"
                        + trustStore + "'. " + "Key Store file path : '" + keyStore + "'.",
                e);
    } catch (NoSuchAlgorithmException e) {
        throw new MongoTableException(
                " SSL Algorithm used to create SSL Socket Factory for mongodb connections " + "is not found.",
                e);
    }

}

From source file:org.wildfly.security.sasl.entity.EntityTest.java

private X509KeyManager getX509KeyManager(final File keyStore, final char[] keyStorePassword)
        throws GeneralSecurityException, IOException {
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(loadKeyStore(keyStore), keyStorePassword);
    for (KeyManager keyManager : keyManagerFactory.getKeyManagers()) {
        if (keyManager instanceof X509KeyManager) {
            return (X509KeyManager) keyManager;
        }/*www  .ja  v  a 2  s . c  o  m*/
    }
    return null;
}

From source file:org.deviceconnect.android.message.DevicePluginContext.java

/**
 * SSLContext ?????./*from   w w w . j  a  v  a  2s  .c  om*/
 * <p>
 * ? Web ?????Manager???????????SSLContext ???
 * </p>
 * @param keyStore 
 * @return SSLContext?
 * @throws GeneralSecurityException SSLContext???????
 */
protected SSLContext createSSLContext(final KeyStore keyStore) throws GeneralSecurityException {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, "0000".toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
            new SecureRandom());
    return sslContext;
}

From source file:android.core.SSLSocketTest.java

/**
 * Loads a keystore from a base64-encoded String. Returns the KeyManager[]
 * for the result./*from  w w w .  j  a  v  a  2  s  .  c  o  m*/
 */
private KeyManager[] getKeyManagers(String keys) throws Exception {
    byte[] bytes = new 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();
}

From source file:org.apache.juddi.samples.JuddiAdminService.java

void printStatusSingleNode(Transport transport, String authtoken) throws Exception {
    String replicationUrl = clerkManager.getClientConfig().getUDDINode(curentnode).getReplicationUrl();

    SSLContext sc = SSLContext.getInstance("SSLv3");

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(new FileInputStream(System.getProperty("javax.net.ssl.keyStore")),
            System.getProperty("javax.net.ssl.keyStorePassword").toCharArray());

    kmf.init(ks, System.getProperty("javax.net.ssl.keyStorePassword").toCharArray());

    sc.init(kmf.getKeyManagers(), null, null);

    UDDIReplicationPortType uddiReplicationPort = new UDDIService().getUDDIReplicationPort();
    ((BindingProvider) uddiReplicationPort).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            replicationUrl);/*from  ww  w.  ja v a2s  . c  om*/
    ((BindingProvider) uddiReplicationPort).getRequestContext()
            .put("com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory", sc.getSocketFactory());
    /*((BindingProvider) uddiReplicationPort).getRequestContext()
     .put(
     JAXWSProperties.SSL_SOCKET_FACTORY,
     sc.getSocketFactory());*/

    String doPing = uddiReplicationPort.doPing(new DoPing());
    System.out.println(doPing + ".., success");

}

From source file:lucee.runtime.tag.Http.java

private void ssl(HttpClientBuilder builder) throws PageException {
    try {// w w  w  .  ja  v a2 s.  c om
        // SSLContext sslcontext = SSLContexts.createSystemDefault();
        SSLContext sslcontext = SSLContext.getInstance("TLSv1.2");
        if (!StringUtil.isEmpty(this.clientCert)) {
            if (this.clientCertPassword == null)
                this.clientCertPassword = "";
            File ksFile = new File(this.clientCert);
            KeyStore clientStore = KeyStore.getInstance("PKCS12");
            clientStore.load(new FileInputStream(ksFile), this.clientCertPassword.toCharArray());

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(clientStore, this.clientCertPassword.toCharArray());

            sslcontext.init(kmf.getKeyManagers(), null, new java.security.SecureRandom());
        } else {
            sslcontext.init(null, null, new java.security.SecureRandom());
        }
        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactoryImpl(sslcontext,
                new DefaultHostnameVerifierImpl());
        builder.setSSLSocketFactory(sslsf);
        Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf)
                .build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
                new DefaultHttpClientConnectionOperatorImpl(reg), null, -1, TimeUnit.MILLISECONDS); // TODO review -1 setting
        builder.setConnectionManager(cm);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
}