Example usage for javax.net.ssl SSLContext init

List of usage examples for javax.net.ssl SSLContext init

Introduction

In this page you can find the example usage for javax.net.ssl SSLContext init.

Prototype

public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException 

Source Link

Document

Initializes this context.

Usage

From source file:com.globo.aclapi.client.ClientAclAPI.java

private static ApacheHttpTransport getTransport(int timeout, boolean verifySSL) throws RuntimeException {
    if (verifySSL) {
        return new ApacheHttpTransport(newDefaultHttpClient(SSLSocketFactory.getSocketFactory(),
                getHttpParams(timeout), ProxySelector.getDefault()));

    } else {/* w w w .  j av a  2s. c  o m*/
        try {
            SSLContext ctx = SSLContext.getInstance("SSL");
            X509TrustManager tm = new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            };

            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            return new ApacheHttpTransport(
                    newDefaultHttpClient(ssf, getHttpParams(timeout), ProxySelector.getDefault()));

        } catch (Exception e) {
            throw new RuntimeException("ERRO ssl schema", e);
        }
    }
}

From source file:com.framework.library.connection.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from w ww  .j  a  v  a2  s  .  c  o m*/
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }
        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.codesharp.cooper.plugins.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from   w w w  . j a  v a 2 s  . c o m
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.orange.oidc.secproxy_service.HttpOpenidConnect.java

/**
 * WARNING : only use in development environment,
 * DO NOT USE in production or commercial environments !!!
 * Trust every server - do not check for any certificate
 *///from  ww  w .j  a  v a2  s.  c om
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {

        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {

        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }

    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.careerly.utils.HttpClientUtils.java

/**
 * ??https//from  w w w .j a v  a  2s .c o m
 *
 * @param base
 * @return
 */
private static HttpClient wrapHttpsClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
        return new DefaultHttpClient(mgr, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.example.android.networkconnect.MainActivity.java

private static void trustAllHosts() {

    X509TrustManager easyTrustManager = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // Oh, I am easy!
        }//from w ww . j a  v  a2s .  c o  m

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // Oh, I am easy!
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

    };

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { easyTrustManager };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.gfred.lbbms.mobile.services.IgnoreSelfCertificatesSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   ww  w . j av  a  2  s.  com*/
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                // do nothing
            }

            public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                // do nothing
            }

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

private static void sslClient(HttpClient httpClient) {
    try {//from  w ww .  j  a  va 2 s. c  o m
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {

            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", ssf, 443));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.aliyun.api.gateway.demo.util.HttpUtil.java

private static void sslClient(HttpClient httpClient) {
    try {/*from  w  w w. j  av a 2 s .co  m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {

            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.micromux.cassandra.jdbc.CassandraConnection.java

private static SSLContext getSSLContext(String trustPath, String trustPass)
        throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
        UnrecoverableKeyException, KeyManagementException {

    FileInputStream tsf = null;/*  w w w .  ja  va 2s.c  o m*/
    SSLContext ctx = null;

    try {

        tsf = new FileInputStream(trustPath);
        ctx = SSLContext.getInstance("SSL");

        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(tsf, trustPass.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);

        ctx.init(null, tmf.getTrustManagers(), new SecureRandom());

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (tsf != null) {
            try {
                tsf.close();
            } catch (IOException ix) {
                logger.warn("Error Closing Trust Store: " + trustPath, ix);
            }
        }

    }

    return ctx;

}