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:Main.java

public static void trustAllHosts(boolean trustAnyCert, boolean trustAnyHost) {
    try {/*from w  w  w  .j a va2 s  .  c  o  m*/
        if (trustAnyCert) {
            X509TrustManager easyTrustManager = new X509TrustManager() {

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

                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

            SSLContext sc = SSLContext.getInstance("TLS");

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

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        }

        if (trustAnyHost) {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        }

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

From source file:com.cloudbees.tftwoway.Client.java

private static SSLContext createSSLContext() throws Exception {
    KeyManager[] serverKeyManagers = getKeyManager();
    TrustManager[] serverTrustManagers = getTrustManager();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(serverKeyManagers, serverTrustManagers, new SecureRandom());

    return sslContext;
}

From source file:info.ajaxplorer.client.http.AjxpSSLSocketFactory.java

private static SSLContext createEasySSLContext(String certKey) throws IOException {
    try {/*from w w w  .j  a  v  a 2s  .co m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new AjxpSSLTrustManager(certKey) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:cloudfoundry.norouter.f5.client.NaiveTrustManager.java

public static LayeredConnectionSocketFactory getSocketFactory() {
    try {/*from w ww.  j  a  va  2s.  c om*/
        final SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, TRUST_MANAGERS, null);
        return new SSLConnectionSocketFactory(context, new X509HostnameVerifier() {
            @Override
            public void verify(String host, SSLSocket ssl) throws IOException {
            }

            @Override
            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new Error(e);
    }
}

From source file:org.transdroid.daemon.util.FakeSocketFactory.java

private static SSLContext createEasySSLContext(String certKey) throws IOException {
    try {/* w  w w  . ja  v a 2 s.c  o m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new FakeTrustManager(certKey) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.manning.androidhacks.hack023.net.SimpleSSLSocketFactory.java

private static SSLContext createSimpleSSLContext() throws IOException {
    try {/*  w ww .ja  v  a 2  s  .  c  o  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new AcceptInvalidX509TrustManager() }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static SSLContext getDefaultSLLContext() {
    SSLContext sslContext = null;
    try {//from w w w  .j  a  va  2  s.  c o  m
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trustManagers }, new SecureRandom());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sslContext;
}

From source file:com.odoo.core.rpc.http.OdooSafeClient.java

private static SSLSocketFactory getSecureConnectionSetting() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override/*from   ww  w . j  ava  2  s .  c o m*/
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {

        }

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

        }

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

    } };
    SSLSocketFactory ssf = null;
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, null);
        ssf = new OSSLSocketFactory(sc);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (Exception ea) {
        ea.printStackTrace();
    }
    return ssf;
}

From source file:com.android.volley.toolbox.SslSocketFactory.java

private static SSLContext createSSLContext(InputStream keyStore, String keyStorePassword)
        throws GeneralSecurityException {
    SSLContext sslcontext = null;
    try {/*www.j  ava  2  s.c  o  m*/
        sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { new SsX509TrustManager(keyStore, keyStorePassword) }, null);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    } catch (KeyManagementException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    }

    return sslcontext;
}

From source file:com.vmware.loginsightapi.util.NonValidatingSSLSocketFactory.java

/**
 * Initializes the SSLContext with dummy values and return
 * // w  w w .  j  a va  2s .  c  o  m
 * @return SSLContext
 */
public static SSLContext getSSLContext() {
    SSLContext context;
    try {
        context = SSLContext.getInstance(TLS);
        context.init(null, new TrustManager[] { new DummyX509TrustManager() }, null);
        return context;
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }
}