Example usage for javax.net.ssl TrustManagerFactory init

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

Introduction

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

Prototype

public final void init(ManagerFactoryParameters spec) throws InvalidAlgorithmParameterException 

Source Link

Document

Initializes this factory with a source of provider-specific trust material.

Usage

From source file:org.everit.authentication.cas.ecm.tests.SecureHttpClient.java

/**
 * Constructor.// w w w  .  ja  v  a  2  s.com
 */
public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception {
    this.principal = principal;

    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(new BasicCookieStore());

    KeyStore trustStore = KeyStore.getInstance("jks");
    trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(),
            "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, new SecureRandom());

    httpClient = HttpClientBuilder.create().setSslcontext(sslContext)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}

From source file:be.fgov.kszbcss.rhq.websphere.connector.security.TrustStoreManager.java

private void reloadTrustManager() throws GeneralSecurityException, IOException {
    if (trustManager.get() == null) {
        log.info("Initializing trust manager using " + truststoreFile);
    } else {/* ww  w. j a v a2  s  .  com*/
        log.info("Reinitializing trust manager");
    }
    TrustManagerFactory factory = TrustManagerFactory.getInstance("IbmPKIX", "IBMJSSE2");
    factory.init(loadTrustStore());
    TrustManager[] trustManagers = factory.getTrustManagers();
    if (log.isDebugEnabled()) {
        log.debug("Returned trust managers: " + Arrays.asList(trustManagers));
    }
    trustManager.set((X509TrustManager) trustManagers[0]);
}

From source file:org.apache.jmeter.protocol.smtp.sampler.protocol.LocalTrustStoreSSLSocketFactory.java

public LocalTrustStoreSSLSocketFactory(File truststore) {
    SSLContext sslcontext = null;
    try {//from   w  w w  .j  a v  a2  s  .co  m
        KeyStore ks = KeyStore.getInstance("JKS"); // $NON-NLS-1$
        InputStream stream = null;
        try {
            stream = new BufferedInputStream(new FileInputStream(truststore));
            ks.load(stream, null);
        } finally {
            IOUtils.closeQuietly(stream);
        }

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        TrustManager[] trustmanagers = tmf.getTrustManagers();
        sslcontext = SSLContext.getInstance("TLS"); // $NON-NLS-1$
        sslcontext.init(null, trustmanagers, new SecureRandom());
    } catch (Exception e) {
        throw new RuntimeException("Could not create the SSL context", e);
    }
    factory = sslcontext.getSocketFactory();
}

From source file:org.elasticsearch.xpack.ssl.SSLClientAuthTests.java

private SSLContext getSSLContext() {
    try (InputStream in = Files.newInputStream(
            getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks"))) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(in, "testclient".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, "testclient".toCharArray());
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
    }//from w w  w . ja  v  a2s. co m
}

From source file:org.xdi.net.SslDefaultHttpClient.java

private TrustManager[] getTrustManagers() throws Exception {
    KeyStore keyStore = getKeyStore(this.trustStoreType, this.trustStorePath, this.trustStorePassword);

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

    return tmFactory.getTrustManagers();
}

From source file:at.diamonddogs.net.ssl.CustomSSLSocketFactory.java

private SSLContext createCustomSSLContext(KeyStore store) {
    try {/* ww w  . jav  a2s.c o m*/
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(store);

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, CustomX509TrustManager.getWrappedTrustmanager(tmf.getTrustManagers()), null);
        return context;
    } catch (Exception e) {
        LOGGER.error("unable to create ssl context", e);
        return null;
    }
}

From source file:software.betamax.util.DynamicSelfSignedSslEngineSource.java

private void initializeSSLContext() {
    String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
        algorithm = "SunX509";
    }/*from  ww w.  j  av  a2s . c o m*/

    try {
        final KeyStore ks = KeyStore.getInstance("JKS");
        // ks.load(new FileInputStream("keystore.jks"),
        // "changeit".toCharArray());
        ks.load(new FileInputStream(keyStoreFile), PASSWORD.toCharArray());

        // Set up key manager factory to use our key store
        final KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(ks, PASSWORD.toCharArray());

        // Set up a trust manager factory to use our key store
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
        tmf.init(ks);

        TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
            // TrustManager that trusts all servers
            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        KeyManager[] keyManagers = kmf.getKeyManagers();

        // Initialize the SSLContext to work with our key managers.
        sslContext = SSLContext.getInstance(PROTOCOL);
        sslContext.init(keyManagers, trustManagers, null);
    } catch (final Exception e) {
        throw new Error("Failed to initialize the server-side SSLContext", e);
    }
}

From source file:com.linkedin.pinot.common.utils.ClientSSLContextGenerator.java

private TrustManager[] setupTrustManagers()
        throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException {
    // This is the cert authority that validates server's cert, so we need to put it in our
    // trustStore.
    if (_serverCACertFile != null) {
        LOGGER.info("Initializing trust store from {}", _serverCACertFile);
        FileInputStream is = new FileInputStream(new File(_serverCACertFile));
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null);//from w ww. jav  a  2s  .  c  o m
        CertificateFactory certificateFactory = CertificateFactory.getInstance(CERTIFICATE_TYPE);
        int i = 0;
        while (is.available() > 0) {
            X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is);
            LOGGER.info("Read certificate serial number {} by issuer {} ", cert.getSerialNumber().toString(16),
                    cert.getIssuerDN().toString());

            String serverKey = "https-server-" + i;
            trustStore.setCertificateEntry(serverKey, cert);
            i++;
        }

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(CERTIFICATE_TYPE);
        tmf.init(trustStore);
        LOGGER.info("Successfully initialized trust store");
        return tmf.getTrustManagers();
    }
    // Server verification disabled. Trust all servers
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    } };
    return trustAllCerts;
}

From source file:com.gsf.dowload.nfe.HSProtocolSocketFactory.java

public TrustManager[] createTrustManagers()
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    KeyStore trustStore = KeyStore.getInstance("JKS");

    trustStore.load(new FileInputStream(TRUSTSTORE), "sistec".toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    return trustManagerFactory.getTrustManagers();
}