Example usage for javax.net.ssl TrustManagerFactory getInstance

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

Introduction

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

Prototype

public static final TrustManagerFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a TrustManagerFactory object that acts as a factory for trust managers.

Usage

From source file:android.net.http.CertificateChainValidator.java

/**
 * Creates a new certificate chain validator. This is a pivate constructor.
 * If you need a Certificate chain validator, call getInstance().
 *///from   w  w w  .ja  v  a 2s . com
private CertificateChainValidator() {
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers != null && trustManagers.length > 0) {
            for (TrustManager trustManager : trustManagers) {
                if (trustManager instanceof X509TrustManager) {
                    mDefaultTrustManager = (X509TrustManager) (trustManager);
                    break;
                }
            }
        }
    } catch (Exception exc) {
        if (HttpLog.LOGV) {
            HttpLog.v("CertificateChainValidator():" + " failed to initialize the trust manager");
        }
    }
}

From source file:au.edu.monash.merc.capture.util.httpclient.ssl.EasyX509TrustManager.java

/**
 * Constructor for EasyX509TrustManager.
 *//* w w  w. j a  v a  2 s .  co m*/
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("no trust manager found");
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:info.fetter.logstashforwarder.protocol.LumberjackClient.java

public LumberjackClient(String keyStorePath, String server, int port, int timeout) throws IOException {
    this.server = server;
    this.port = port;

    try {/*w ww .ja  v a2 s.  c  om*/
        if (keyStorePath == null) {
            throw new IOException("Key store not configured");
        }
        if (server == null) {
            throw new IOException("Server address not configured");
        }

        keyStore = KeyStore.getInstance("JKS");
        keyStore.load(new FileInputStream(keyStorePath), null);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
        tmf.init(keyStore);

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, tmf.getTrustManagers(), null);

        SSLSocketFactory socketFactory = context.getSocketFactory();
        socket = new Socket();
        socket.connect(new InetSocketAddress(InetAddress.getByName(server), port), timeout);
        socket.setSoTimeout(timeout);
        sslSocket = (SSLSocket) socketFactory.createSocket(socket, server, port, true);
        sslSocket.setUseClientMode(true);
        sslSocket.startHandshake();

        output = new DataOutputStream(new BufferedOutputStream(sslSocket.getOutputStream()));
        input = new DataInputStream(sslSocket.getInputStream());

        logger.info("Connected to " + server + ":" + port);
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.imogene.client.ssl.EasyX509TrustManager.java

/**
 * Constructor for EasyX509TrustManager.
 *//*from   w  ww . ja va2 s .  c  om*/
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("no trust manager found"); //$NON-NLS-1$
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}

From source file:org.candlepin.client.CustomSSLProtocolSocketFactory.java

private SSLContext createCustomSSLContext() {
    try {/*from w w  w  .ja  v a  2 s  . c  o m*/
        KeyManager[] keyManagers = null;
        // Generate key managers off of the identity certificates if
        // doing client auth.
        if (clientAuth) {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            String[] keyCert = FileUtil.readKeyAndCert(configuration.getConsumerIdentityFilePath());
            kmf.init(PemUtil.pemToKeyStore(keyCert[1], keyCert[0], "password"), "password".toCharArray());
            keyManagers = kmf.getKeyManagers();
        }
        /* and provide them for the SSLContext */
        SSLContext ctx = SSLContext.getInstance("TLS");
        if (configuration.isIgnoreTrustManagers()) {
            ctx.init(keyManagers, Utils.DUMMY_TRUST_MGRS, new SecureRandom());
        } else {
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            KeyStore ks2 = KeyStore.getInstance(KeyStore.getDefaultType());
            ks2.load(null, null);

            ks2.setCertificateEntry("candlepin", PemUtil.readCert("/etc/candlepin/certs/candlepin-ca.crt"));
            // ks2.load(
            // new FileInputStream(configuration.getKeyStoreFileLocation()),
            // passwd);
            tmf.init(ks2);
            ctx.init(keyManagers, tmf.getTrustManagers(), new SecureRandom());
        }

        return ctx;
    } catch (Exception e) {
        e.printStackTrace();
        throw new HttpClientError(e.getMessage());
    }
}

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,/* w ww  . j a  v  a  2s  .  c om*/
            new TrustManager[] {
                    new TrustManagerDelegate((X509TrustManager) customCaTrustManager.getTrustManagers()[0],
                            (X509TrustManager) javaDefaultTrustManager.getTrustManagers()[0]) },
            secureRandom);
    return sslContext;
}

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

public MyTrustManager() {
    try {/*w ww . j a  v a2  s  .  c o  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:org.openhab.binding.neato.internal.VendorVorwerk.java

/**
 * Trust the self signed certificate.// w  w  w.  j  ava  2 s .  co m
 *
 * @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:edu.internet2.middleware.subject.provider.LdapPEMSocketFactory.java

protected void initManagers() {

    // trust managers
    try {//  w ww.j a  va  2 s.  c  o  m
        X509Certificate cert = null;
        if (caFilename != null)
            cert = readCertificate(caFilename);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        ks.setCertificateEntry("CACERT", cert);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        trustManagers = tmf.getTrustManagers();
    } catch (Exception e) {
        log.error("ldap source cacert error: " + e);
    }

    // key managers
    if (certFilename != null && keyFilename != null) {
        char[] pw = new char[] { 0 };

        try {
            X509Certificate cert = readCertificate(certFilename);
            PKCS1 pkcs = new PKCS1();
            PrivateKey key = pkcs.readKey(keyFilename);
            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(null, null);
            X509Certificate[] chain = new X509Certificate[1];
            chain[0] = cert;
            ks.setKeyEntry("CERT", (Key) key, pw, chain);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, pw);
            keyManagers = kmf.getKeyManagers();
        } catch (Exception e) {
            log.error("ldap source cert/key error: " + e);
        }
    }

}

From source file:com.sun.socialsite.util.LeniantX509TrustManager.java

/**
 * Constructor for LeniantX509TrustManager.
 *///  w  w w .  j  a v a  2s  .  co  m
public LeniantX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    factory.init(keystore);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException("no trust manager found");
    }
    this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}