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.apache.felix.karaf.jaas.config.impl.ResourceKeystoreInstance.java

public TrustManager[] getTrustManager(String algorithm)
        throws KeyStoreException, NoSuchAlgorithmException, KeystoreIsLocked {
    if (isKeystoreLocked()) {
        throw new KeystoreIsLocked("Keystore '" + name + "' is locked.");
    }/*from   w  ww.j  a va  2 s . c  o  m*/
    if (!loadKeystoreData()) {
        return null;
    }
    TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(algorithm);
    trustFactory.init(keystore);
    return trustFactory.getTrustManagers();
}

From source file:com.microsoft.tfs.core.config.httpclient.internal.DefaultX509TrustManager.java

public DefaultX509TrustManager(final KeyStore keyStore)
        throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
    final TrustManagerFactory factory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keyStore);//from w  w w .  j a va  2s .co  m
    final TrustManager[] trustManagers = factory.getTrustManagers();

    if (trustManagers.length == 0) {
        throw new NoSuchAlgorithmException("No trust manager found"); //$NON-NLS-1$
    }

    if (!(trustManagers[0] instanceof X509TrustManager)) {
        throw new NoSuchAlgorithmException("No X509 trust manager found"); //$NON-NLS-1$
    }

    standardTrustManager = (X509TrustManager) trustManagers[0];
}

From source file:org.apache.hadoop.io.crypto.bee.RestClient.java

private InputStream httpsWithCertificate(final URL url) throws KeyStoreException, NoSuchAlgorithmException,
        CertificateException, IOException, KeyManagementException {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);// Make an empty store

    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    FileInputStream fis = new FileInputStream(BeeConstants.BEE_HTTPS_CERTIFICATE_DEFAULT_PATH);
    BufferedInputStream bis = new BufferedInputStream(fis);
    while (bis.available() > 0) {
        Certificate cert = cf.generateCertificate(bis);
        // System.out.println(cert.getPublicKey().toString());
        trustStore.setCertificateEntry("jetty" + bis.available(), cert);
    }// w w w  . j  ava 2  s  .  co  m

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, tmf.getTrustManagers(), null);
    SSLSocketFactory sslFactory = ctx.getSocketFactory();

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            if (0 == hostname.compareToIgnoreCase(url.getHost())) {
                return true;
            }
            return false;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    urlConnection.setSSLSocketFactory(sslFactory);

    return urlConnection.getInputStream();
}

From source file:com.salesmanager.core.service.common.impl.EasySSLProtocolSocketFactory.java

public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {

    super();/*from ww w. j a v  a2 s  .c  om*/

    TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509");
    factory.init(keystore);

    TrustManager[] trustmanagers = factory.getTrustManagers();

    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("SunX509 trust manager not supported");
    }

    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:com.netflix.spinnaker.orca.webhook.config.WebhookConfiguration.java

private X509TrustManager getTrustManager(KeyStore keyStore) {
    try {//www  .  j  a v  a2 s  . co m
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        return (X509TrustManager) trustManagers[0];
    } catch (KeyStoreException | NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.ftpserver.ssl.SSLTestTemplate.java

protected FTPSClient createFTPClient() throws Exception {
    FTPSClient ftpsClient = new FTPSClient(useImplicit());

    FileInputStream fin = new FileInputStream(FTPCLIENT_KEYSTORE);
    KeyStore store = KeyStore.getInstance("jks");
    store.load(fin, KEYSTORE_PASSWORD.toCharArray());
    fin.close();/* w  w  w  .j  a v a  2 s  .c  o m*/

    // initialize key manager factory
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(store, KEYSTORE_PASSWORD.toCharArray());

    // initialize trust manager factory
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());

    trustManagerFactory.init(store);

    clientKeyManager = keyManagerFactory.getKeyManagers()[0];
    clientTrustManager = trustManagerFactory.getTrustManagers()[0];

    ftpsClient.setKeyManager(clientKeyManager);
    ftpsClient.setTrustManager(clientTrustManager);

    String auth = getAuthValue();
    if (auth != null) {
        ftpsClient.setAuthValue(auth);

        if (auth.equals("SSL")) {
            ftpsClient.setEnabledProtocols(new String[] { "SSLv3" });
        }
    }
    return ftpsClient;
}

From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java

private SSLContext createContext(KeyStore keystore, KeyManagerFactory kmf) throws Exception {
    TrustManagerFactory trustFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustFactory.init(keystore);/* ww  w . ja v a  2 s  . co  m*/

    SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
    sslContext.init(kmf == null ? null : kmf.getKeyManagers(), trustFactory.getTrustManagers(), null);

    return sslContext;
}

From source file:com.gamesalutes.utils.EncryptUtils.java

/**
 * Creates an <code>SSLContext</code> that uses the specified trusted certificates.
 * //from   w w w.j av a2 s  .  c  o  m
 * @param protocol the {@link TransportSecurityProtocol} to use for the context
 * @param trustedCerts certificates to import into the <code>SSLContext</code> or <code>null</code>
 *         to accept all issuers
 * @param privateKey the client key to authenticate the client with the server
 * @return the created <code>SSLContext</code>
 * @throws Exception if error occurs during the process of creating the context
 */
public static SSLContext createSSLContext(TransportSecurityProtocol protocol, PrivateKey privateKey,
        java.security.cert.X509Certificate... trustedCerts) throws Exception {
    if (trustedCerts != null && trustedCerts.length == 0)
        throw new IllegalArgumentException("trustedCerts is empty");

    X509TrustManager defaultManager = null;
    KeyManager[] keyManagers = null;
    KeyStore keyStore = null;

    if (privateKey != null || trustedCerts != null) {
        // create a new key store instance that will install the certificates
        // and/or the private keys
        keyStore = KeyStore.getInstance(JKS_TYPE);
        keyStore.load(null, null);
    }

    // import the certs
    if (trustedCerts != null) {
        // set up the key manager for the certificates
        javax.net.ssl.TrustManagerFactory trustFact = javax.net.ssl.TrustManagerFactory
                .getInstance(KEY_MANAGEMENT_ALG_SUN_X509);

        // install the certificates in the key store and give them a unique alias
        int imported = 0;
        for (java.security.cert.X509Certificate cert : trustedCerts) {
            if (cert != null)
                keyStore.setCertificateEntry("cert" + ++imported, cert);
        }
        if (imported == 0)
            throw new IllegalArgumentException("no non-null certs in trustedCerts");
        // add the certs to the trust factory
        trustFact.init(keyStore);

        // get a default trust manager
        TrustManager[] tms = trustFact.getTrustManagers();
        if (tms != null && tms.length >= 1)
            defaultManager = (X509TrustManager) tms[0];
    }

    // import the private key
    if (privateKey != null) {
        keyStore.setKeyEntry("client", privateKey, null, null);
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(privateKey.getAlgorithm());

        kmfactory.init(keyStore, null);
        keyManagers = kmfactory.getKeyManagers();
    }
    //create the SSL context based on these parameters
    SSLContext sslContext = SSLContext.getInstance(protocol.toString());

    // use a CertX509TrustManager since default one will still fail validation for 
    // self-signed certs
    sslContext.init(keyManagers,
            new TrustManager[] { trustedCerts != null ? new CertX509TrustManager(defaultManager, trustedCerts)
                    : new CertX509TrustManager() },
            null);

    return sslContext;

}

From source file:com.mgmtp.perfload.core.client.web.ssl.LtSSLSocketFactory.java

private TrustManager[] createTrustManagers(final KeyStore keyStore)
        throws KeyStoreException, NoSuchAlgorithmException {
    log.debug("Initializing trust managers");

    TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmfactory.init(keyStore);//w  w w  .j  a v  a  2s  .  co  m

    TrustManager[] trustmanagers = tmfactory.getTrustManagers();
    for (int i = 0; i < trustmanagers.length; ++i) {
        if (trustmanagers[i] instanceof X509TrustManager) {
            trustmanagers[i] = new LtX509TrustManager((X509TrustManager) trustmanagers[i]);
        }
    }
    return trustmanagers;
}

From source file:com.mgmtp.jfunk.web.ssl.JFunkSSLSocketFactory.java

private TrustManager[] createTrustManagers(final KeyStore keyStore)
        throws KeyStoreException, NoSuchAlgorithmException {
    log.debug("Initializing trust managers");

    TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmfactory.init(keyStore);//from   ww  w.ja v  a  2s .c  om

    TrustManager[] trustmanagers = tmfactory.getTrustManagers();
    for (int i = 0; i < trustmanagers.length; ++i) {
        if (trustmanagers[i] instanceof X509TrustManager) {
            trustmanagers[i] = new JFunkX509TrustManager((X509TrustManager) trustmanagers[i]);
        }
    }
    return trustmanagers;
}