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.vmware.identity.openidconnect.client.AuthenticationFrameworkHelper.java

public void populateSSLCertificates(KeyStore keyStore)
        throws OIDCClientException, SSLConnectionException, AdminServerException {

    HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET,
            this.authenticationFrameworkSSLCertificateURL);
    HTTPResponse httpResponse = null;/*from   w ww  . j a  v  a 2 s . c o  m*/

    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

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

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        sslContext.init(null, new TrustManager[] { tm }, null);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new OIDCClientException("Failed to build SSL context: " + e.getMessage(), e);
    }

    httpResponse = OIDCClientUtils.sendSecureRequest(httpRequest, sslContext);
    try {
        if (httpResponse.getStatusCode() != 200 && httpResponse.getStatusCode() != 204) {
            throw AdminServerHelper.convertToAdminServerException(httpResponse.getStatusCode(),
                    httpResponse.getContentAsJSONObject());
        }
    } catch (ParseException e) {
        throw new OIDCClientException("Exception caught during exception conversion: " + e.getMessage(), e);
    }

    JSONArray jsonArray = (JSONArray) JSONValue.parse(httpResponse.getContent());
    int index = 1;
    for (Object object : jsonArray) {
        JSONObject jsonObject = (JSONObject) object;
        String cert = (String) jsonObject.get("encoded");
        cert = cert.replaceAll(X509Factory.BEGIN_CERT, "").replaceAll(X509Factory.END_CERT, "");
        try {
            keyStore.setCertificateEntry(String.format("VecsSSLCert%d", index), convertToX509Certificate(cert));
        } catch (KeyStoreException e) {
            throw new OIDCClientException("Failed to set X509 certificate in key store: " + e.getMessage(), e);
        }
        index++;
    }
}

From source file:org.wisdom.framework.vertx.VertxDispatcherTest.java

public void prepareHttps() throws KeyManagementException, NoSuchAlgorithmException {
    // 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   ww w  . ja  v  a2s  .  c  o  m*/

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };

    // 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 = (hostname, session) -> true;

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}

From source file:com.snaker.ssl.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from w w w . j a v a  2  s .c  om*/
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } }, null);
        return context;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.splunk.shuttl.archiver.http.InsecureHttpClientFactory.java

private static TrustManager getTrustManager() {
    return new X509TrustManager() {
        @Override/*from   ww  w  .  ja  v a 2  s  . c  om*/
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

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

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

From source file:org.jasig.cas.util.http.SimpleHttpClientTests.java

private SSLConnectionSocketFactory getFriendlyToAllSSLSocketFactory() throws Exception {
    final TrustManager trm = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from  w  ww .  ja v a2s .  com*/

        public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
        }

        public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
        }
    };
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[] { trm }, null);
    return new SSLConnectionSocketFactory(sc, new NoopHostnameVerifier());
}

From source file:com.force.api.systest.EasyX509TrustManager.java

/**
 * Constructor for EasyX509TrustManager.
 *//*from  w  w w.  ja  v  a2s .c om*/
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
    super();
    // Create a trust manager that does not validate certificate chains
    standardTrustManager = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

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

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

}

From source file:bear.plugins.java.JenkinsCache.java

public static File download2(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri,
        String user, String pass) {
    try {//from w  ww . j a  v  a  2s  . c o  m
        Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion);

        if (!optional.isPresent()) {
            throw new RuntimeException("could not find: " + jdkVersion);
        }

        String uri = optional.get().filepath;

        //                agent.get()

        //                agent.get()

        SSLContext sslContext = SSLContext.getInstance("TLSv1");

        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);

        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);

        DefaultHttpClient httpClient = new DefaultHttpClient(
                new PoolingClientConnectionManager(schemeRegistry));

        MechanizeAgent agent = new MechanizeAgent();
        Cookie cookie2 = agent.cookies().addNewCookie("gpw_e24", ".", "oracle.com");
        cookie2.getHttpCookie().setPath("/");
        cookie2.getHttpCookie().setSecure(false);

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("gpw_e24", ".");
        cookie.setDomain("oracle.com");
        cookie.setPath("/");
        cookie.setSecure(true);

        cookieStore.addCookie(cookie);

        httpClient.setCookieStore(cookieStore);

        HttpPost httppost = new HttpPost("https://login.oracle.com");

        httppost.setHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8"));

        HttpResponse response = httpClient.execute(httppost);

        int code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("unable to auth: " + code);
        }

        //                EntityUtils.consumeQuietly(response.getEntity());

        httppost = new HttpPost(uri);

        response = httpClient.execute(httppost);

        code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("to download: " + uri);
        }

        File file = new File(tempDestDir, optional.get().name);
        HttpEntity entity = response.getEntity();

        final long length = entity.getContentLength();

        final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file));

        System.out.printf("Downloading %s to %s...%n", uri, file);

        Thread progressThread = new Thread(new Runnable() {
            double lastProgress;

            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    long copied = os.getCount();

                    double progress = copied * 100D / length;

                    if (progress != lastProgress) {
                        System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1));
                    }

                    lastProgress = progress;

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }, "progressThread");

        progressThread.start();

        ByteStreams.copy(entity.getContent(), os);

        progressThread.interrupt();

        System.out.println("Download complete.");

        return file;
    } catch (Exception e) {
        throw Exceptions.runtime(e);
    }
}

From source file:org.wso2.carbon.appmgt.gateway.handlers.security.thrift.ThriftAuthClient.java

public ThriftAuthClient(String serverIP, String remoteServerPort, String webContextRoot)
        throws AuthenticationException {
    try {/*from w  w  w  .jav a  2  s . c  o  m*/
        TrustManager easyTrustManager = new X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }

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

        //skip host name verification
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { easyTrustManager }, null);
        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme httpsScheme = new Scheme("https", sf, Integer.parseInt(remoteServerPort));

        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getConnectionManager().getSchemeRegistry().register(httpsScheme);

        //If the webContextRoot is null or /
        if (webContextRoot == null || "/".equals(webContextRoot)) {
            //Assign it an empty value since it is part of the thriftServiceURL.
            webContextRoot = "";
        }
        String thriftServiceURL = "https://" + serverIP + ":" + remoteServerPort + webContextRoot + "/"
                + "thriftAuthenticator";
        client = new THttpClient(thriftServiceURL, httpClient);

    } catch (TTransportException e) {
        throw new AuthenticationException("Error in creating thrift authentication client..");
    } catch (Exception e) {
        throw new AuthenticationException("Error in creating thrift authentication client..");
    }
}

From source file:biz.mosil.webtools.MosilSSLSocketFactory.java

public MosilSSLSocketFactory(KeyStore _truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(_truststore);

    TrustManager trustManager = new X509TrustManager() {

        @Override//w ww .ja v  a 2s.co m
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] _chain, String _authType) throws CertificateException {

        }

        @Override
        public void checkClientTrusted(X509Certificate[] _chain, String _authType) throws CertificateException {

        }
    };

    mSSLContext.init(null, new TrustManager[] { trustManager }, null);
}

From source file:br.gov.frameworkdemoiselle.behave.integration.alm.httpsclient.MySSLSocketFactory.java

public MySSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);

    TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }/*ww w.  j  a va  2s.co m*/

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

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    sslContext.init(null, new TrustManager[] { tm }, null);
}