Example usage for javax.net.ssl SSLContext getInstance

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

Introduction

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

Prototype

public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SSLContext object that implements the specified secure socket protocol.

Usage

From source file:Main.java

private synchronized static SSLSocketFactory getDefaultSSLSocketFactory() {
    if (defaultSslSocketFactory == null) {
        try {/*from   ww w .j  a  va2 s.c  om*/
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            defaultSslSocketFactory = sslContext.getSocketFactory();
        } catch (GeneralSecurityException e) {
            throw new AssertionError(); // The system has no TLS. Just give
            // up.
        }
    }
    return defaultSslSocketFactory;
}

From source file:Main.java

private static HttpsURLConnection setSSLSocketFactory(HttpsURLConnection connection)
        throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext sc;/*from  w w w.  j a  va2  s . c  o m*/
    sc = SSLContext.getInstance("TLS");
    sc.init(null, null, new java.security.SecureRandom());
    connection.setSSLSocketFactory(sc.getSocketFactory());
    return connection;
}

From source file:Main.java

static public void DisableSecurity() throws GeneralSecurityException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }/*from w ww.j av a 2 s. c om*/

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

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

    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

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

From source file:Main.java

public static javax.net.ssl.SSLSocketFactory getSSLSocketFactoryIgnoreSSLCertificate() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from w  ww.j  av  a2  s.co  m

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        return sc.getSocketFactory();
    } catch (Exception ignored) {
        return null;
    }
}

From source file:com.aincc.ber.utils.FakeSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   w  ww .  j  a  va2 s  .c o  m*/
        final SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new NaiveTrustManager() }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }//  w w w  .ja v a  2 s  .com

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }
    } };

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

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

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:com.diaw.lib.tool.FakeSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from  w  w  w.j a v  a2 s .  c o  m
        final SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new NaiveTrustManager() }, null);
        return context;
    } catch (GeneralSecurityException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.ksc.internal.SdkSSLContext.java

/**
 * @see SSLContexts#createDefault()//from ww w. j  a  v  a  2  s . com
 */
public static final SSLContext getPreferredSSLContext(final SecureRandom secureRandom) {
    try {
        final SSLContext sslcontext = SSLContext.getInstance("TLS");
        // http://download.java.net/jdk9/docs/technotes/guides/security/jsse/JSSERefGuide.html
        sslcontext.init(null, null, secureRandom);
        return sslcontext;
    } catch (final NoSuchAlgorithmException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    } catch (final KeyManagementException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    }
}

From source file:Main.java

/**
 * Trust every server - don't check for any certificate only for testing!
 *///from   ww  w . jav  a 2  s .c o m
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }

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

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

    // 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.micromata.genome.gwiki.fssvn.SslUtils.java

public static SSLSocketFactory createEasySSLSocketFactory() {
    try {//from  ww w.  j  a v  a 2 s. c  o m
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return ssf;
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}