Example usage for javax.net.ssl TrustManagerFactory getTrustManagers

List of usage examples for javax.net.ssl TrustManagerFactory getTrustManagers

Introduction

In this page you can find the example usage for javax.net.ssl TrustManagerFactory getTrustManagers.

Prototype

public final TrustManager[] getTrustManagers() 

Source Link

Document

Returns one trust manager for each type of trust material.

Usage

From source file:org.globus.gsi.jsse.SSLConfigurator.java

private void configureContext() throws GlobusSSLConfigurationException {

    ManagerFactoryParameters parameters = getCertPathParameters();
    TrustManager[] trustManagers;
    try {/*from  ww  w  .j  ava  2  s.  c o m*/
        TrustManagerFactory fact = TrustManagerFactory.getInstance("GSI");
        fact.init(parameters);
        trustManagers = fact.getTrustManagers();
    } catch (NoSuchAlgorithmException e1) {
        throw new GlobusSSLConfigurationException(e1);
    } catch (InvalidAlgorithmParameterException e) {
        throw new GlobusSSLConfigurationException(e);
    }

    KeyManager[] keyManagers = loadKeyManagers();

    SecureRandom secureRandom = loadSecureRandom();

    sslContext = loadSSLContext();

    try {
        sslContext.init(keyManagers, trustManagers, secureRandom);
    } catch (KeyManagementException e) {
        throw new GlobusSSLConfigurationException(e);
    }

}

From source file:org.appenders.log4j2.elasticsearch.jest.JKSCertInfo.java

@Override
public void applyTo(HttpClientConfig.Builder clientConfigBuilder) {

    try (FileInputStream keystoreFile = new FileInputStream(new File(keystorePath));
            FileInputStream truststoreFile = new FileInputStream(new File(truststorePath))) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(keystoreFile, keystorePassword.toCharArray());
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, keystorePassword.toCharArray());

        KeyStore trustStore = KeyStore.getInstance("jks");
        trustStore.load(truststoreFile, truststorePassword.toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        // TODO: add support for hostname verification modes
        clientConfigBuilder.sslSocketFactory(new SSLConnectionSocketFactory(sslContext));
        clientConfigBuilder//  ww w . j  a va2 s . c o m
                .httpsIOSessionStrategy(new SSLIOSessionStrategy(sslContext, new NoopHostnameVerifier()));

    } catch (IOException | GeneralSecurityException e) {
        throw new ConfigurationException(configExceptionMessage, e);
    }
}

From source file:org.appenders.log4j2.elasticsearch.jest.PEMCertInfo.java

@Override
public void applyTo(HttpClientConfig.Builder builder) {

    if (java.security.Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }/*from  w  w w  .  j a  va2  s .  c o  m*/

    try (FileInputStream clientCert = new FileInputStream(new File(clientCertPath));
            FileInputStream key = new FileInputStream(new File(keyPath));
            FileInputStream certificateAuthoritiies = new FileInputStream(new File(caPath))) {
        KeyStore keyStore = PemReader.loadKeyStore(clientCert, key, Optional.ofNullable(keyPassphrase));
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, keyPassphrase.toCharArray());

        KeyStore trustStore = PemReader.loadTrustStore(certificateAuthoritiies);

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        // TODO: add support for hostname verification modes
        builder.sslSocketFactory(new SSLConnectionSocketFactory(sslContext));
        builder.httpsIOSessionStrategy(new SSLIOSessionStrategy(sslContext, new NoopHostnameVerifier()));

    } catch (IOException | GeneralSecurityException e) {
        throw new ConfigurationException(configExceptionMessage, e);
    }

}

From source file:org.jboss.as.test.integration.logging.syslogserver.TLSSyslogServer.java

/**
 * Creates custom sslContext from keystore and truststore configured in
 *
 * @see org.productivity.java.syslog4j.server.impl.net.tcp.TCPNetSyslogServer#initialize()
 *//*w w  w .  j a  va 2  s .  co m*/
@Override
public void initialize() throws SyslogRuntimeException {
    super.initialize();

    if (isBouncyCastleInstalled()) {
        removeBouncyCastle();
        addBouncyCastleOnShutdown = true;
    }

    final SSLTCPNetSyslogServerConfigIF config = (SSLTCPNetSyslogServerConfigIF) this.tcpNetSyslogServerConfig;

    try {
        final char[] keystorePwd = config.getKeyStorePassword().toCharArray();
        final KeyStore keystore = loadKeyStore(config.getKeyStore(), keystorePwd);
        final char[] truststorePassword = config.getTrustStorePassword().toCharArray();
        final KeyStore truststore = loadKeyStore(config.getTrustStore(), truststorePassword);

        final KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, keystorePwd);

        final TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(truststore);

        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    } catch (Exception e) {
        LOGGER.error("Exception occurred during SSLContext for TLS syslog server initialization", e);
        throw new SyslogRuntimeException(e);
    }
}

From source file:com.vmware.photon.controller.common.auth.AuthOIDCClient.java

private IdmClient createIdmClient(String domainControllerFQDN, int domainControllerPort, String user,
        String password) throws AuthException {
    try {/*  w w  w  .  j  a  v  a 2  s  .c  o  m*/
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(certificateStore.getKeyStore());
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        IdmClient idmClient = new IdmClient(domainControllerFQDN, domainControllerPort,
                new DefaultHostnameVerifier(), sslContext);

        com.vmware.identity.openidconnect.client.AccessToken accessToken = getTokenHandler()
                .getAdminServerAccessToken(user, password).getAccessToken();

        com.vmware.identity.rest.core.client.AccessToken restAccessToken = new com.vmware.identity.rest.core.client.AccessToken(
                accessToken.getValue(), com.vmware.identity.rest.core.client.AccessToken.Type.JWT);
        idmClient.setToken(restAccessToken);
        return idmClient;
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new AuthException("Failed to createIdmClient", e);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.http.SunJsseListener.java

protected SSLServerSocketFactory createFactory() throws Exception {
    _keystore = System.getProperty(KEYSTORE_PROPERTY, _keystore);

    log.info(KEYSTORE_PROPERTY + "=" + _keystore);

    if (_password == null)
        _password = Password.getPassword(PASSWORD_PROPERTY, null, null);
    log.info(PASSWORD_PROPERTY + "=" + _password.toStarString());

    if (_keypassword == null)
        _keypassword = Password.getPassword(KEYPASSWORD_PROPERTY, null, _password.toString());
    log.info(KEYPASSWORD_PROPERTY + "=" + _keypassword.toStarString());

    KeyStore ks = null;//w w  w  .  j  a v a 2 s.co  m

    log.info(KEYSTORE_TYPE_PROPERTY + "=" + _keystore_type);

    if (_keystore_provider_class != null) {
        // find provider.
        // avoid creating another instance if already installed in Security.
        java.security.Provider[] installed_providers = Security.getProviders();
        java.security.Provider myprovider = null;
        for (int i = 0; i < installed_providers.length; i++) {
            if (installed_providers[i].getClass().getName().equals(_keystore_provider_class)) {
                myprovider = installed_providers[i];
                break;
            }
        }
        if (myprovider == null) {
            // not installed yet, create instance and add it
            myprovider = (java.security.Provider) Class.forName(_keystore_provider_class).newInstance();
            Security.addProvider(myprovider);
        }
        log.info(KEYSTORE_PROVIDER_CLASS_PROPERTY + "=" + _keystore_provider_class);
        ks = KeyStore.getInstance(_keystore_type, myprovider.getName());
    } else if (_keystore_provider_name != null) {
        log.info(KEYSTORE_PROVIDER_NAME_PROPERTY + "=" + _keystore_provider_name);
        ks = KeyStore.getInstance(_keystore_type, _keystore_provider_name);
    } else {
        ks = KeyStore.getInstance(_keystore_type);
        log.info(KEYSTORE_PROVIDER_NAME_PROPERTY + "=[DEFAULT]");
    }

    ks.load(new FileInputStream(new File(_keystore)), _password.toString().toCharArray());

    KeyManagerFactory km = KeyManagerFactory.getInstance("SunX509", "SunJSSE");
    km.init(ks, _keypassword.toString().toCharArray());
    KeyManager[] kma = km.getKeyManagers();

    TrustManagerFactory tm = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
    if (_useDefaultTrustStore) {
        tm.init((KeyStore) null);
    } else {
        tm.init(ks);
    }

    TrustManager[] tma = tm.getTrustManagers();

    SSLContext sslc = SSLContext.getInstance("SSL");
    sslc.init(kma, tma, SecureRandom.getInstance("SHA1PRNG"));

    SSLServerSocketFactory ssfc = sslc.getServerSocketFactory();
    log.info("SSLServerSocketFactory=" + ssfc);
    return ssfc;
}

From source file:org.openmrs.module.rheashradapter.util.GenerateORU_R01Alert.java

public void sendRequest(String msg, Encounter e)
        throws IOException, TransformerFactoryConfigurationError, TransformerException, KeyStoreException,
        NoSuchAlgorithmException, CertificateException, KeyManagementException {
    // Get the key store that includes self-signed cert as a "trusted"
    // entry.//from   w ww  .j a  v  a2 s .c om
    InputStream keyStoreStream = GenerateORU_R01Alert.class.getResourceAsStream("/truststore-prod.jks");

    // Load the keyStore

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(keyStoreStream, keystorePassword.toCharArray());
    log.info("KeyStoreStream = " + IOUtils.toString(keyStoreStream));
    keyStoreStream.close();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(keyStore);

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, tmf.getTrustManagers(), null);

    // set SSL Factory to be used for all HTTPS connections
    sslFactory = ctx.getSocketFactory();

    callQueryFacility(msg, e);

}

From source file:com.openshift.internal.restclient.authorization.AuthorizationClient.java

private X509TrustManager getCurrentTrustManager() throws NoSuchAlgorithmException, KeyStoreException {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init((KeyStore) null);

    X509TrustManager x509TrustManager = null;
    for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
        if (trustManager instanceof X509TrustManager) {
            x509TrustManager = (X509TrustManager) trustManager;
            break;
        }//from  w  w  w .  j  av a  2  s.c om
    }
    return x509TrustManager;
}

From source file:org.reficio.ws.it.util.SslTunnel.java

public void start() {
    try {/*w w  w  .  j a  v a2 s. com*/
        sslContext = SSLContext.getInstance("SSLv3");
        KeyManager[] keyManagers = null;
        TrustManager[] trustManagers = null;

        if (keyStore != null) {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
            X509KeyManager defaultKeyManager = (X509KeyManager) keyManagerFactory.getKeyManagers()[0];
            keyManagers = new KeyManager[] { defaultKeyManager };

        }
        if (trustStore != null) {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trustStore);
            X509TrustManager defaultTrustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
            trustManagers = new TrustManager[] { defaultTrustManager };
        }

        sslContext.init(keyManagers, trustManagers, new SecureRandom());

        SSLServerSocketFactory socketFactory = sslContext.getServerSocketFactory();
        socket = socketFactory.createServerSocket();
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(sourcePort));
        new ServerThread(socket, run).start();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:com.screenslicer.common.LenientHttpsConfig.java

private LenientHttpsConfig() {
    AsyncHttpClientConfig configTmp = null;
    SSLContext sslContextTmp = null;
    try {/*w w w  . j  ava  2  s  .c om*/
        AsyncHttpClient client = new AsyncHttpClient();
        configTmp = client.getConfig();
        IOUtils.closeQuietly(client);
        client = null;

        X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509")
                .generateCertificate(CommonUtil.class.getResourceAsStream("screenslicer.internal.cert"));
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);
        keyStore.setCertificateEntry(cert.getSubjectX500Principal().getName(), cert);
        KeyManagerFactory keyManager = KeyManagerFactory.getInstance("SunX509");
        keyManager.init(keyStore, null);
        TrustManagerFactory trustManager = TrustManagerFactory.getInstance("X509");
        trustManager.init(keyStore);
        sslContextTmp = SSLContext.getInstance("TLS");
        sslContextTmp.init(keyManager.getKeyManagers(), trustManager.getTrustManagers(), null);
    } catch (Throwable t) {
    }
    config = configTmp;
    sslContext = sslContextTmp;
}