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

private static void initSsl() throws Exception {
    TrustManager[] trustall = new TrustManager[] { new X509TrustManager() {

        @Override// w w  w. ja  va  2 s.c o  m
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            System.out.println("Trust no one");
        }

        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            System.out.println("Trust no one");
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            System.out.println("Trust no one");
            return null;
        }
    } };
    SSLContext sc = SSLContext.getInstance("SSL");

    sc.init(null, trustall, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

From source file:com.marklogic.client.functionaltest.TestSSLConnection.java

@Test
public void testSSLConnectionNonSSLServer()
        throws IOException, NoSuchAlgorithmException, KeyManagementException {
    System.out.println("Running testSSLConnectionNonSSLServer");

    String filename = "facebook-10443244874876159931";

    // create a trust manager
    // (note: a real application should verify certificates)
    TrustManager naiveTrustMgr = new X509TrustManager() {
        @Override//from w w w.  j av  a 2s .c om
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

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

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

    // create an SSL context
    SSLContext sslContext = SSLContext.getInstance("SSLv3");
    sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null);

    // create the client
    // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier)
    DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x",
            Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY);

    String expectedException = "com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated";
    String exception = "";

    // write doc
    try {
        writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text");
    } catch (Exception e) {
        exception = e.toString();
    }

    assertEquals("Exception is not thrown", expectedException, exception);

    // release client
    client.release();
}

From source file:gov.nrel.bacnet.consumer.DatabusSender.java

public DefaultHttpClient createSecureOne(PoolingClientConnectionManager mgr) {
    try {/*www  .j  a  va  2  s. c  o m*/
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        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;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @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 hostname, SSLSession session) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void verify(String arg0, SSLSocket arg1) throws IOException {
                // TODO Auto-generated method stub

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        SchemeRegistry sr = mgr.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, port));
        return new DefaultHttpClient(mgr);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.wso2.developerstudio.appcloud.utils.client.HttpsJaggeryClient.java

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base, String urlStr) {
    try {// w  ww.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);

        ClientConnectionManager ccm = new ThreadSafeClientConnManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        URL url = new URL(urlStr);
        int port = url.getPort();
        if (port == -1) {
            port = 443;
        }
        String protocol = url.getProtocol();
        if ("https".equals(protocol)) {
            if (port == -1) {
                port = 443;
            }
        } else if ("http".equals(protocol)) {
            if (port == -1) {
                port = 80;
            }
        }
        sr.register(new Scheme(protocol, ssf, port));

        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Throwable ex) {
        ex.printStackTrace();
        log.error("Trust Manager Error", ex);
        return null;
    }
}

From source file:org.gluu.oxtrust.ldap.service.StatusCheckerTimer.java

private String getHttpdPage(String idpUrl, String httpdTestPageName) {
    String[] urlParts = idpUrl.split("://");
    if ("https".equals(urlParts[0])) {
        // 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  a v a  2 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());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }
    }

    StringBuilder sb = new StringBuilder();
    // Now you can access an https URL without having the certificate in the
    // truststore
    try {
        String[] hostAndPort = urlParts[1].split(":");
        URL url = null;
        if (hostAndPort.length < 2) {
            url = new URL(urlParts[0], hostAndPort[0], httpdTestPageName);
        } else {
            url = new URL(urlParts[0], hostAndPort[0], Integer.parseInt(hostAndPort[1]), httpdTestPageName);
        }
        InputStream in = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        in.close();

    } catch (Exception e) {
        // log.error("Failed to get test page: ", e);
    }
    return sb.toString();
}

From source file:mp.platform.cyclone.webservices.CyclosWebServicesClientFactory.java

private TrustManager[] getTrustManagers() {
    final TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(final java.security.cert.X509Certificate[] certs,
                final String authType) {
        }// ww w  .  j  av a  2s  . c o m

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

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

    return trustManagers;
}

From source file:nl.strohalm.cyclos.webservices.CyclosWebServicesClientFactory.java

private TrustManager[] getTrustManagers() {
    final TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
        @Override/*  w ww.ja v  a  2  s  .  c  o  m*/
        public void checkClientTrusted(final java.security.cert.X509Certificate[] certs,
                final String authType) {
        }

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

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

    return trustManagers;
}

From source file:com.curso.listadapter.net.RESTClient.java

/**
 * this method utoacepts all certificates in httpsurlconections
 * *///w  w  w.  ja v a 2  s .co  m
@SuppressLint("TrulyRandom")
private static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }
    } };
    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.sitewhere.wso2.identity.scim.Wso2ScimAssetModule.java

protected SSLContext createContext() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from w  w w. j ava 2 s . c om

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

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

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        SSLContext.setDefault(sc);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return sc;

    } catch (Exception e) {
    }
    return null;
}

From source file:org.onosproject.protocol.http.ctl.HttpSBControllerImpl.java

private Client ignoreSslClient() {
    SSLContext sslcontext = null;

    try {//w w w  .ja  v a  2 s .  c  om
        sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new java.security.SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        e.printStackTrace();
    }

    return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build();
}