Example usage for javax.net.ssl X509TrustManager X509TrustManager

List of usage examples for javax.net.ssl X509TrustManager X509TrustManager

Introduction

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

Prototype

X509TrustManager

Source Link

Usage

From source file:com.ibm.sbt.services.util.SSLUtil.java

public static DefaultHttpClient wrapHttpClient(DefaultHttpClient base) {
    try {/*  w  w  w. j  a v a  2  s .co m*/
        // Create and assign a dummy TrustManager
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            @Override
            public void checkServerTrusted(X509Certificate[] cert, String s) throws CertificateException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);

        // When Apache Client AllowAllHostnameVerifier is strict, this should be used
        // Stays here for reference
        X509HostnameVerifier verifier = new X509HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }

            @Override
            public void verify(String s, SSLSocket sslSession) throws IOException {
            }

            @Override
            public void verify(String s, String[] ss1, String[] ss2) throws SSLException {
            }

            @Override
            public void verify(String s, X509Certificate cerst) throws SSLException {
            }

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

public static void trustSelfSignedSSL() {
    try {/*  ww  w  . ja v a2  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);
        SSLContext.setDefault(ctx);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base) {
    SSLContext ctx = null;//from  w w  w .  j av  a 2s  .  co 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:org.codeartisans.proxilet.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*w w  w.j  ava 2s . co m*/

        TrustManager[] trustAllManager = new TrustManager[] { new X509TrustManager() {

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

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

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

        } };

        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, trustAllManager, null);

        return context;
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

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.// w ww  . ja  v a  2s .c  om
 */
@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:la.niub.network.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*  ww w.j av a  2 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:at.co.blogspot.javaskeleton.WebClientDevWrapper.java

public static HttpClient wrapClient(final HttpClient base, final int port) {
    try {//from w  w w. j  av a 2s  .  co m
        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.ethercamp.harmony.util.TrustSSL.java

public static void applyAnother() {
    try {/*w  w w  . ja v a 2  s .  com*/
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

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

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                System.out.println("hostname = [" + hostname + "], session = [" + session + "]");
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:estacionamento.util.HTTPUtil.java

/**
 *
 * @return//from w w w  .  j  av  a 2 s. co  m
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

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

        }

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

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, trustAllCerts, new SecureRandom());

    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

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

private static SSLContext createEasySSLContext() throws IOException {
    try {/*ww  w .  j  a va  2  s . 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());
    }
}