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:org.orcid.examples.jopmts.impl.SSLConfig.java

public static void trustSelfSignedSSL() {
    try {/*from  w  w  w .  j  a va 2  s . co 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);
        SSLContext.setDefault(ctx);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.github.droidfu.http.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   w  ww.j ava2s .co  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, null, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.mcxiaoke.commons.http.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//  w w w.j  ava  2s.com
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new TrustAllManager() }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:me.willowcheng.makerthings.util.MyAsyncHttpClient.java

public MyAsyncHttpClient(Context ctx) {
    super();//from   www . j a  va  2 s.  co m
    //      super(ctx);
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, MemorizingTrustManager.getInstanceList(ctx), new java.security.SecureRandom());
        sslSocketFactory = new MySSLSocketFactory(sslContext);
        if (PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean(Constants.PREFERENCE_SSLHOST, false))
            sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        this.setSSLSocketFactory(sslSocketFactory);
    } catch (Exception ex) {
    }
}

From source file:gov.nist.appvet.tool.synchtest.util.SSLWrapper.java

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base) {
    SSLContext ctx = null;/* w w w. j  a  va  2 s  . c  o  m*/
    X509TrustManager tm = null;
    SSLSocketFactory ssf = null;
    SchemeRegistry sr = null;
    try {
        ctx = SSLContext.getInstance("TLSv1.2");
        tm = new X509TrustManager() {

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

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

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

        };
        ctx.init(null, new TrustManager[] { tm }, null);
        ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        final ClientConnectionManager ccm = base.getConnectionManager();
        sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception e) {
        return null;
    } finally {
        sr = null;
        ssf = null;
        tm = null;
        ctx = null;
    }
}

From source file:com.datasingularity.http.asyncget.ssl.EasySSLSocketFactory.java

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

From source file:la.niub.network.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from  w ww  .  ja  va2  s. c  o  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }
        } }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:ro.teodorbaciu.commons.client.ws.util.WebClientDevWrapper.java

/**
 * Provides a new instance of http client that wraps the 
 * instance specified as parameter./*from  w w w  . j  a v a 2  s. c o m*/
 */
@SuppressWarnings("deprecation")
public static DefaultHttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

            }

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

        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:at.co.blogspot.javaskeleton.WebClientDevWrapper.java

public static HttpClient wrapClient(final HttpClient base, final int port) {
    try {/*w w w.  j av  a2  s . com*/
        final SSLContext ctx = SSLContext.getInstance("TLS");
        final X509TrustManager tm = new X509TrustManager() {

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

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        final X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(final String string, final SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(final String string, final X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(final String string, final String[] strings, final String[] strings1)
                    throws SSLException {
            }

            @Override
            public boolean verify(final String string, final SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        final SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        final ClientConnectionManager ccm = base.getConnectionManager();
        final SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, port));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception ex) {
        LOG.error("Error enabling https-connections", ex);
        return null;
    }
}

From source file:com.dvdprime.android.app.http.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*  w w w.  ja  va  2s .c  o m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
                //               return null;
            }

            public void checkClientTrusted(X509Certificate[] certificates, String authType) {

            }

            public void checkServerTrusted(X509Certificate[] certificates, String authType) {

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