Example usage for javax.net.ssl SSLContext init

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

Introduction

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

Prototype

public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException 

Source Link

Document

Initializes this context.

Usage

From source file:org.socialbiz.cog.util.SSLPatch.java

/**
* a call to disableSSLCertValidation will disable certificate validation
* for SSL connection made after this call.   This is installed as the
* default in the JVM for future calls.//from  w w  w.j  a  v  a 2 s  .  c om
*
* Returns the properly initialized SSLContext in case it is needed for
* something else (like Apache HttpClient libraries) but if you don't need
* it you can ignore it.
*/
public static SSLContext disableSSLCertValidation() throws Exception {

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { getDummyTrustManager() };

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

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

    return sc;
}

From source file:io.dropwizard.revolver.http.RevolverHttpClientFactory.java

private static SSLContext getSSLContext(final String keyStorePath, final String keyStorePassword)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        KeyManagementException, UnrecoverableKeyException {
    final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream instream = RevolverHttpClientFactory.class.getClassLoader()
            .getResourceAsStream(keyStorePath)) {
        keyStore.load(instream, keyStorePassword.toCharArray());
    }/*from w  ww  .  jav  a 2s  .c om*/
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
    final SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
            new SecureRandom());
    return sslContext;
}

From source file:com.cloudera.nav.sdk.client.SSLUtils.java

/**
 * Return the TLS SSLContext with the TrustManager specified by the config
 * @param config/*w w  w . ja  v  a 2  s. c o  m*/
 */
public static SSLContext getSSLContext(ClientConfig config) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { getTrustManager(config) }, null);
        return ctx;
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.frostwire.http.HttpClient.java

private static SSLSocketFactory buildSSLSocketFactory() {
    try {/*from   www . j av  a 2 s  . c o m*/
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[] { new AllX509TrustManager() }, new SecureRandom());
        SSLSocketFactory d = sc.getSocketFactory();
        return new WrapSSLSocketFactory(d);
    } catch (Throwable e) {
        LOG.error("Unable to create custom SSL socket factory", e);
    }

    return null;
}

From source file:com.test.controller.ResourceController.java

private static void trustAllHttpsCertificates() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        //            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        //                return null;
        //            }
        ////from ww  w  . java 2s  .c o m
        //            public void checkClientTrusted(X509Certificate[] certs, String authType) {
        //            }
        //
        //            public void checkServerTrusted(X509Certificate[] certs, String authType) {
        //            }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            //                return new java.security.cert.X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
            return null;
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        ;
    }

}

From source file:ee.ria.xroad.proxy.serverproxy.HttpClientCreator.java

private static SSLConnectionSocketFactory createSSLSocketFactory() throws Exception {
    SSLContext ctx = SSLContext.getInstance(CryptoUtils.SSL_PROTOCOL);
    ctx.init(createServiceKeyManager(), new TrustManager[] { new ServiceTrustManager() }, new SecureRandom());

    log.info("SSL context successfully created");

    return new CustomSSLSocketFactory(ctx, SystemProperties.getProxyClientTLSProtocols(),
            SystemProperties.getProxyClientTLSCipherSuites(), NoopHostnameVerifier.INSTANCE);
}

From source file:com.sittinglittleduck.DirBuster.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {//w ww  .ja  v a 2 s .c om
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        // LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.groupon.odo.tests.HttpUtils.java

public static String doProxyHttpsGet(String url, BasicNameValuePair[] data) throws Exception {
    String fullUrl = url;/*w w w .  j  av a  2 s .  c o  m*/

    if (data != null) {
        if (data.length > 0) {
            fullUrl += "?";
        }

        for (BasicNameValuePair bnvp : data) {
            fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&";
        }
    }

    TrustManager[] trustAllCerts = 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) {
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }

    URL uri = new URL(fullUrl);
    int port = Utils.getSystemPort(Constants.SYS_FWD_PORT);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));
    URLConnection connection = uri.openConnection(proxy);

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String accumulator = "";
    String line = "";
    Boolean firstLine = true;
    while ((line = rd.readLine()) != null) {
        accumulator += line;
        if (!firstLine) {
            accumulator += "\n";
        } else {
            firstLine = false;
        }
    }

    return accumulator;
}

From source file:cn.hi321.browser.weave.client.WeaveSSLSocketFactory.java

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

From source file:com.ct855.util.HttpsClientUtil.java

public static String getUrl(String url)
        throws IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException {
    //SSLContext??
    TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

    //SSLContextSSLSocketFactory
    SSLSocketFactory ssf = sslContext.getSocketFactory();

    URL aURL = new java.net.URL(url);
    HttpsURLConnection aConnection = (HttpsURLConnection) aURL.openConnection();
    aConnection.setRequestProperty("Ocp-Apim-Subscription-Key", "d8400b4cdf104015bb23d7fe847352c8");
    aConnection.setSSLSocketFactory(ssf);
    aConnection.setDoOutput(true);//from  www .  ja  v a 2 s .com
    aConnection.setDoInput(true);
    aConnection.setRequestMethod("GET");

    InputStream resultStream = aConnection.getInputStream();
    BufferedReader aReader = new java.io.BufferedReader(new java.io.InputStreamReader(resultStream));
    StringBuffer aResponse = new StringBuffer();
    String aLine = aReader.readLine();
    while (aLine != null) {
        aResponse.append(aLine + "\n");
        aLine = aReader.readLine();
    }
    resultStream.close();
    return aResponse.toString();
}