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.qi4j.library.http.AbstractSecureJettyTest.java

@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
    defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String string, SSLSession ssls) {
            return true;
        }//from   ww  w . ja  va2s .  co  m

    });
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
    caTrustManagerFactory.init(truststore);
    sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}

From source file:com.amalto.workbench.utils.SSLContextProvider.java

private static TrustManager[] buildTrustManagers(String path, String storePass, String trusttype)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException {
    InputStream stream = null;/* ww  w  . j  a va 2  s.  c o  m*/
    try {
        if (StringUtils.isEmpty(path)) {
            return new TrustManager[] { TRUST_ALL };
        }
        if (!new File(path).exists()) {
            throw new KeyStoreException(Messages.bind(Messages.noKeystoreFile_error, path));
        }
        stream = new FileInputStream(path);

        KeyStore tks = KeyStore.getInstance(trusttype);
        tks.load(stream, storePass.toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
        tmf.init(tks);

        return tmf.getTrustManagers();
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:com.nesscomputing.tinyhttp.ssl.HttpsTrustManagerFactory.java

@Nonnull
private static X509TrustManager trustManagerFromKeystore(final KeyStore keystore)
        throws GeneralSecurityException {
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    trustManagerFactory.init(keystore);//  w w  w.j  a  va 2 s. c  o m

    final TrustManager[] tms = trustManagerFactory.getTrustManagers();

    for (final TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            final X509TrustManager manager = X509TrustManager.class.cast(tm);
            final X509Certificate[] acceptedIssuers = manager.getAcceptedIssuers();
            LOG.debug("Found TrustManager with %d authorities.", acceptedIssuers.length);
            for (int i = 0; i < acceptedIssuers.length; i++) {
                X509Certificate issuer = acceptedIssuers[i];
                LOG.trace("Issuer #%d, subject DN=<%s>, serial=<%s>", i, issuer.getSubjectDN(),
                        issuer.getSerialNumber());
            }

            return manager;
        }
    }
    throw new IllegalStateException("Could not locate X509TrustManager!");
}

From source file:Main.java

private static TrustManager[] prepareTrustManager(InputStream... certificates) {
    if (certificates != null && certificates.length > 0) {
        try {/*from w w  w .  java2  s  .c  o  m*/
            CertificateFactory e = CertificateFactory.getInstance("X.509");
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load((KeyStore.LoadStoreParameter) null);
            int index = 0;
            InputStream[] trustManagerFactory = certificates;
            int trustManagers = certificates.length;

            for (int i$ = 0; i$ < trustManagers; ++i$) {
                InputStream certificate = trustManagerFactory[i$];
                String certificateAlias = Integer.toString(index++);
                keyStore.setCertificateEntry(certificateAlias, e.generateCertificate(certificate));

                try {
                    if (certificate != null) {
                        certificate.close();
                    }
                } catch (IOException var10) {
                    ;
                }
            }

            trustManagerFactory = null;
            TrustManagerFactory var15 = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            var15.init(keyStore);
            TrustManager[] var16 = var15.getTrustManagers();
            return var16;
        } catch (NoSuchAlgorithmException var11) {
            var11.printStackTrace();
        } catch (CertificateException var12) {
            var12.printStackTrace();
        } catch (KeyStoreException var13) {
            var13.printStackTrace();
        } catch (Exception var14) {
            var14.printStackTrace();
        }

        return null;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
 */// w  w w .j  av  a 2  s.  c om
public static SSLSocketFactory newSslSocketFactoryForCa(InputStream certChain) throws Exception {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(new BufferedInputStream(certChain));
    X500Principal principal = cert.getSubjectX500Principal();
    ks.setCertificateEntry(principal.getName("RFC2253"), cert);
    //    ks.setCertificateEntry("ca", cert);

    // Set up trust manager factory to use our key store.
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(ks);
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, trustManagerFactory.getTrustManagers(), null);
    return context.getSocketFactory();
}

From source file:org.apache.hadoop.gateway.jetty.JettyHttpsTest.java

private static TrustManager[] createTrustManagers(String trustStoreType, String trustStorePath,
        String trustStorePassword) throws Exception {
    KeyStore trustStore = loadKeyStore(trustStoreType, trustStorePath, trustStorePassword);
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);/* www.  j  a  v  a 2  s  .c o  m*/
    return tmf.getTrustManagers();
}

From source file:org.openo.nfvo.vnfmadapter.service.csm.connect.AbstractSslContext.java

protected static TrustManager[] createTrustManager(JSONObject sslConf) {
    TrustManager[] tms = null;/*from  www .  j a  v a 2  s .  c  om*/
    try {

        String TRUST_STORE = "etc/conf/trust.jks";
        String TRUST_STORE_PASSWORD = "Changeme_123";
        String TRUST_STORE_TYPE = "jks";
        if (sslConf != null) {
            TRUST_STORE = sslConf.getString("trustStore");
            TRUST_STORE_PASSWORD = sslConf.getString("trustStorePass");
            TRUST_STORE_TYPE = sslConf.getString("trustStoreType");
        }
        FileInputStream f_trustStore = new FileInputStream(TRUST_STORE);
        KeyStore ks = KeyStore.getInstance(TRUST_STORE_TYPE);
        ks.load(f_trustStore, TRUST_STORE_PASSWORD.toCharArray());
        f_trustStore.close();

        String alg = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
        tmFact.init(ks);
        tms = tmFact.getTrustManagers();

    } catch (Exception e) {
        LOG.error("create TrustManager fail!", e);
    }
    return tms;
}

From source file:com.nesscomputing.httpclient.internal.HttpClientTrustManagerFactory.java

@Nonnull
private static X509TrustManager trustManagerFromKeystore(final KeyStore keystore)
        throws GeneralSecurityException {
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    trustManagerFactory.init(keystore);/* ww w  .jav  a  2s.  c  o  m*/

    final TrustManager[] tms = trustManagerFactory.getTrustManagers();

    for (TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            final X509TrustManager manager = (X509TrustManager) tm;
            X509Certificate[] acceptedIssuers = manager.getAcceptedIssuers();
            LOG.debug("Found TrustManager with %d authorities.", acceptedIssuers.length);
            for (int i = 0; i < acceptedIssuers.length; i++) {
                X509Certificate issuer = acceptedIssuers[i];

                LOG.trace("Issuer #%d, subject DN=<%s>, serial=<%s>", i, issuer.getSubjectDN(),
                        issuer.getSerialNumber());
            }

            return manager;
        }
    }
    throw new IllegalStateException("Could not find an X509TrustManager");
}

From source file:com.openmeap.util.SSLUtils.java

/**
 * @param keyStore is passed into TrustManagerFactory.init(), and may be null for default behavior.
 * @return an array of the default trust managers
 * @throws NoSuchAlgorithmException// w w w . ja v  a 2s .c o  m
 * @throws KeyStoreException
 */
public static TrustManager[] getDefaultTrustManagers(KeyStore keyStore)
        throws NoSuchAlgorithmException, KeyStoreException {

    String defaultFactoryManagerAlg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory managerFactory = TrustManagerFactory.getInstance(defaultFactoryManagerAlg);
    managerFactory.init((KeyStore) keyStore);
    return managerFactory.getTrustManagers();
}

From source file:Main.java

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;// w  w w .  ja v  a  2  s . co  m
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}