Example usage for javax.net.ssl TrustManagerFactory getDefaultAlgorithm

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

Introduction

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

Prototype

public static final String getDefaultAlgorithm() 

Source Link

Document

Obtains the default TrustManagerFactory algorithm name.

Usage

From source file:Main.java

private static TrustManager[] prepareTrustManager(InputStream... certificates) {
    if (certificates != null && certificates.length > 0) {
        try {/* ww  w . j  a v  a 2  s .  c  om*/
            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

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;// www.j  a v  a 2s  .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();
}

From source file:android.apn.androidpn.server.xmpp.ssl.SSLTrustManagerFactory.java

public static TrustManager[] getTrustManagers(String storeType, String truststore, String trustpass)
        throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
    TrustManager[] trustManagers;
    if (truststore == null) {
        trustManagers = null;//w  w  w  .ja va2 s.  c o  m
    } else {
        TrustManagerFactory trustFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        if (trustpass == null) {
            trustpass = "";
        }
        KeyStore keyStore = KeyStore.getInstance(storeType);
        keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
        trustFactory.init(keyStore);
        trustManagers = trustFactory.getTrustManagers();
    }
    return trustManagers;
}

From source file:com.swisscom.safeconnect.backend.SwisscomSslSocketFactory.java

public SwisscomSslSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);

    String alg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
    tmFact.init(truststore);/*from  w  w  w  .  j av a 2  s .c  o m*/

    sslContext.init(null, tmFact.getTrustManagers(), null);
}

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

/**
 * Creates a trust manager capable of accepting self-signed certificates.
 *
 * @param keyStore/*from   w w w .java  2s . c  om*/
 *        The {@link KeyStore} to use for user-specified keys (or
 *        <code>null</code>)
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 */
public SelfSignedX509TrustManager(final KeyStore keyStore) throws NoSuchAlgorithmException, KeyStoreException {
    final TrustManagerFactory factory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keyStore);
    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:info.guardianproject.cacert.CustomTrust.java

public CustomTrust(Context context, int rawResource, String password) throws IOException, KeyStoreException,
        KeyManagementException, NoSuchAlgorithmException, CertificateException {

    // Setup the SSL context to use the truststore
    ssl_ctx = SSLContext.getInstance("TLS");

    // Setup truststore
    KeyStore ksCACert = KeyStore.getInstance("BKS");
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    InputStream trustStoreStream = context.getResources().openRawResource(rawResource);
    ksCACert.load(trustStoreStream, password.toCharArray());

    //init factory with custom cacert
    trustManagerFactory.init(ksCACert);//from  w  ww.j  ava  2  s . co m
    Log.d("SSL", "CACerts " + ksCACert.size());
    Log.d("SSL", "trustManagerFactory " + trustManagerFactory.getTrustManagers().length);

    // Setup client keystore
    /*
    KeyStore keyStore = KeyStore.getInstance("BKS");
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    InputStream keyStoreStream = context.getResources().openRawResource(R.raw.clientkeystore);
    keyStore.load(keyStoreStream, "testtest".toCharArray());
    keyManagerFactory.init(keyStore, "testtest".toCharArray());
    Log.d("SSL", "Key " + keyStore.size());
            
    Log.d("SSL", "keyManagerFactory " + keyManagerFactory.getKeyManagers().length);
    */

    //nothing implemented yet
    SecureRandom secRand = SecureRandom.getInstance(RANDOM_ALGORITHM);

    ssl_ctx.init(null, trustManagerFactory.getTrustManagers(), secRand);

    socketFactory = (SSLSocketFactory) ssl_ctx.getSocketFactory();

}

From source file:android.apn.androidpn.server.xmpp.ssl.SSLTrustManagerFactory.java

public static TrustManager[] getTrustManagers(KeyStore truststore, String trustpass) {
    TrustManager[] trustManagers;
    try {//from www.  jav  a  2 s. c  o m
        if (truststore == null) {
            trustManagers = null;
        } else {
            TrustManagerFactory trustFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            if (trustpass == null) {
                trustpass = SSLConfig.getc2sTrustPassword();
            }

            trustFactory.init(truststore);

            trustManagers = trustFactory.getTrustManagers();
        }
    } catch (KeyStoreException e) {
        trustManagers = null;
        log.error("SSLTrustManagerFactory startup problem.", e);
    } catch (NoSuchAlgorithmException e) {
        trustManagers = null;
        log.error("SSLTrustManagerFactory startup problem.", e);
    }
    return trustManagers;
}

From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLSocketFactory.java

/**
 *
 * @param sslCerts these certs will be used to validate the ssl connection
 * @return/*from  w  w w.  jav a2  s.com*/
 * @throws IOException
 * @throws GeneralSecurityException
 */
static public SSLSocketFactory getSSLSocketFactory(List<X509Certificate> sslCerts)
        throws IOException, GeneralSecurityException {
    SSLContext sc = null;
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null); //Important, this intializes the keystore
    int counter = 0;
    for (X509Certificate cert : sslCerts) {
        ks.setCertificateEntry("rhn-key-" + counter, cert);
        counter++;
    }
    sc = SSLContext.getInstance("SSL");
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    sc.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
    return sc.getSocketFactory();
}

From source file:com.iflytek.spider.protocol.httpclient.DummyX509TrustManager.java

/**
 * Constructor for DummyX509TrustManager.
 *//*w  w  w.j  a  v a  2  s . c o  m*/
public DummyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    String algo = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory factory = TrustManagerFactory.getInstance(algo);
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException(algo + " trust manager not supported");
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:test.unit.be.fedict.eid.idp.protocol.openid.OpenIDTrustManager.java

public OpenIDTrustManager(X509Certificate serverCertificate)
        throws NoSuchAlgorithmException, KeyStoreException {
    this.serverCertificate = serverCertificate;
    String algorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(algorithm);
    trustManagerFactory.init((KeyStore) null);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    for (TrustManager trustManager : trustManagers) {
        if (trustManager instanceof X509TrustManager) {
            this.defaultTrustManager = (X509TrustManager) trustManager;
            break;
        }/*www . ja v a2s.  c  o m*/
    }
    if (null == this.defaultTrustManager) {
        throw new IllegalStateException("no default X509 trust manager found");
    }
}