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.keycloak.truststore.JSSETruststoreConfigurator.java

public TrustManager[] getTrustManagers() {
    if (provider == null) {
        return null;
    }/*  w w  w.  ja  va2s  . co m*/

    if (tm == null) {
        synchronized (this) {
            if (tm == null) {
                TrustManagerFactory tmf = null;
                try {
                    tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                    tmf.init(provider.getTruststore());
                    tm = tmf.getTrustManagers();
                } catch (Exception e) {
                    throw new RuntimeException("Failed to initialize TrustManager: ", e);
                }
            }
        }
    }
    return tm;
}

From source file:org.projectforge.business.ldap.MyTrustManager.java

public MyTrustManager() {
    try {/* ww  w. j  a v  a2 s.  co m*/
        final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        // create a TrustManager using our KeyStore
        final TrustManagerFactory factory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        factory.init(keyStore);
        this.trustManager = getX509TrustManager(factory.getTrustManagers());
    } catch (final KeyStoreException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final NoSuchAlgorithmException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final CertificateException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final IOException ex) {
        log.error("Exception encountered " + ex, ex);
    }
}

From source file:com.adito.server.jetty.CustomJsseListener.java

protected SSLServerSocketFactory createFactory() throws Exception {
    if (KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).isKeyStoreEmpty()) {
        throw new Exception(
                "The keystore does not contain any certificates. Please run the installation wizard (--install).");
    }/* w  ww .  j  av a 2  s. c  o m*/
    KeyStore ks = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getKeyStore();
    String pw = ContextHolder.getContext().getConfig()
            .retrieveProperty(new ContextKey("webServer.keystore.sslCertificate.password"));
    KeyManager[] kma = new KeyManager[] { new CustomKeyManager(pw) };
    TrustManager[] tma = null;
    if (trustManager == null) {
        TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tm.init(ks);
        tma = tm.getTrustManagers();
    } else {

        // LDP - Add the existing trust managers so that outgoing certificates are still trusted.
        TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tm.init(ks);

        tma = new TrustManager[tm.getTrustManagers().length + 1];
        for (int i = 0; i < tm.getTrustManagers().length; i++) {
            tma[i] = tm.getTrustManagers()[i];
        }
        tma[tma.length - 1] = trustManager;
    }
    SSLContext sslc = SSLContext.getInstance("SSL");
    sslc.init(kma, tma, SecureRandom.getInstance("SHA1PRNG"));
    SSLServerSocketFactory ssfc = sslc.getServerSocketFactory();
    if (log.isInfoEnabled())
        log.info("SSLServerSocketFactory=" + ssfc);
    initialised = true;
    return ssfc;
}

From source file:com.sslexplorer.server.jetty.CustomJsseListener.java

protected SSLServerSocketFactory createFactory() throws Exception {
    if (KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).isKeyStoreEmpty()) {
        throw new Exception(
                "The keystore does not contain any certificates. Please run the installation wizard (--install).");
    }/*from   ww w .j  a  v  a2 s .com*/
    KeyStore ks = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getKeyStore();
    String pw = ContextHolder.getContext().getConfig()
            .retrieveProperty(new ContextKey("webServer.keystore.sslCertificate.password"));
    KeyManager[] kma = new KeyManager[] { new CustomKeyManager(pw) };
    TrustManager[] tma = null;
    if (trustManager == null) {
        TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tm.init(ks);
        tma = tm.getTrustManagers();
    } else {

        // LDP - Add the existing trust managers so that outgoing certificates are still trusted.
        TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tm.init(ks);

        tma = new TrustManager[tm.getTrustManagers().length + 1];
        for (int i = 0; i < tm.getTrustManagers().length - 1; i++) {
            tma[i] = tm.getTrustManagers()[i];
        }
        tma[tma.length - 1] = trustManager;
    }
    SSLContext sslc = SSLContext.getInstance("SSL");
    sslc.init(kma, tma, SecureRandom.getInstance("SHA1PRNG"));
    SSLServerSocketFactory ssfc = sslc.getServerSocketFactory();
    if (log.isInfoEnabled())
        log.info("SSLServerSocketFactory=" + ssfc);
    initialised = true;
    return ssfc;
}

From source file:learn.encryption.ssl.SSLContext_Https.java

public static SSLContext getSSLContext2(String servercerfile, String clientkeyStore, String clientPass) {
    if (sslContext != null) {
        return sslContext;
    }//from   w  w  w  .j av a 2s .  c  o  m
    try {
        // ??, ??assets
        //InputStream inputStream = App.getInstance().getAssets().open("serverkey.cer");
        InputStream inputStream = new FileInputStream(new File(servercerfile));
        // ??
        CertificateFactory cerFactory = CertificateFactory.getInstance("X.509");
        Certificate cer = cerFactory.generateCertificate(inputStream);
        // ?KeyStore
        KeyStore keyStore = KeyStore.getInstance("PKCS12");//eclipse?jksandroidPKCS12??
        keyStore.load(null, null);
        keyStore.setCertificateEntry("trust", cer);

        // KeyStoreTrustManagerFactory
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);

        sslContext = SSLContext.getInstance("TLS");

        //?clientKeyStore(android??bks)
        //KeyStore clientKeyStore = KeyStore.getInstance("BKS");
        KeyStore clientKeyStore = KeyStore.getInstance("jks");
        //clientKeyStore.load(App.getInstance().getAssets().open("clientkey.bks"), "123456".toCharArray());
        clientKeyStore.load(new FileInputStream(new File(clientkeyStore)), clientPass.toCharArray());

        // ?clientKeyStorekeyManagerFactory
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(clientKeyStore, clientPass.toCharArray());

        // ?SSLContext  trustManagerFactory.getTrustManagers()
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
                new SecureRandom());//new TrustManager[]{trustManagers}??
    } catch (Exception e) {
        e.printStackTrace();
    }

    return sslContext;
}

From source file:dk.netarkivet.common.distribute.HTTPSRemoteFileRegistry.java

private HTTPSRemoteFileRegistry() {
    FileInputStream keyStoreInputStream = null;
    try {/*from   w w w .j  a  v  a2  s  .  c  o  m*/
        keyStoreInputStream = new FileInputStream(KEYSTORE_PATH);
        KeyStore store = KeyStore.getInstance(SUN_JCEKS_KEYSTORE_TYPE);
        store.load(keyStoreInputStream, KEYSTORE_PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(SUN_X509_CERTIFICATE_ALGORITHM);
        kmf.init(store, KEY_PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(SUN_X509_CERTIFICATE_ALGORITHM);
        tmf.init(store);
        sslContext = SSLContext.getInstance(SSL_PROTOCOL);
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
                SecureRandom.getInstance(SHA1_PRNG_RANDOM_ALGORITHM));
    } catch (GeneralSecurityException | IOException e) {
        throw new IOFailure("Unable to create secure environment for keystore '" + KEYSTORE_PATH + "'", e);
    } finally {
        IOUtils.closeQuietly(keyStoreInputStream);
    }
}

From source file:org.openhab.binding.neato.internal.VendorVorwerk.java

/**
 * Trust the self signed certificate.//from  ww  w.j  av  a  2s .  c om
 *
 * @param connection
 */
public void applyNucleoSslConfiguration(HttpsURLConnection connection) {
    KeyStore keyStore;
    try {
        keyStore = KeyStore.getInstance("JKS");
        keyStore.load(this.getClass().getClassLoader().getResourceAsStream("keystore.jks"),
                "geheim".toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        SSLContext sslctx = SSLContext.getInstance("SSL");
        sslctx.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
        connection.setSSLSocketFactory(sslctx.getSocketFactory());
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:slash.navigation.rest.ssl.SSLConnectionManagerFactory.java

private SSLContext createSSLContext() throws KeyStoreException, CertificateException, NoSuchAlgorithmException,
        KeyManagementException, IOException {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    TrustManagerFactory javaDefaultTrustManager = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    javaDefaultTrustManager.init((KeyStore) null);
    TrustManagerFactory customCaTrustManager = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    customCaTrustManager.init(getKeyStore());

    sslContext.init(null,//from  w ww  . j  a v  a  2s.  c  o  m
            new TrustManager[] {
                    new TrustManagerDelegate((X509TrustManager) customCaTrustManager.getTrustManagers()[0],
                            (X509TrustManager) javaDefaultTrustManager.getTrustManagers()[0]) },
            secureRandom);
    return sslContext;
}

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

private SSLContext createCustomSSLContext(KeyStore store) {
    try {//from   w ww  .  ja va 2 s . co  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:org.elasticsearch.hadoop.rest.commonshttp.SSLSocketFactory.java

private TrustManager[] loadTrustManagers() throws GeneralSecurityException, IOException {
    if (!StringUtils.hasText(trustStoreLocation)) {
        return null;
    }/*  w w  w .  j  ava 2  s.  c o  m*/

    char[] pass = (StringUtils.hasText(trustStorePass) ? trustStorePass.trim().toCharArray() : null);
    KeyStore keyStore = loadKeyStore(trustStoreLocation, pass);
    TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmFactory.init(keyStore);
    TrustManager[] tms = tmFactory.getTrustManagers();

    if (tms != null && trust != null) {
        // be defensive since the underlying impl might not give us a copy
        TrustManager[] clone = new TrustManager[tms.length];

        for (int i = 0; i < tms.length; i++) {
            TrustManager tm = tms[i];
            if (tm instanceof X509TrustManager) {
                tm = new TrustManagerDelegate((X509TrustManager) tm, trust);
            }
            clone[i] = tm;
        }
        tms = clone;
    }

    return tms;
}