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:com.groupon.odo.tests.HttpUtils.java

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

    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:pt.hive.cameo.ssl.SSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/* w  w w . ja va2s  .c  om*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new TrivialTrustManager() }, null);
        return context;
    } catch (Exception exception) {
        throw new IOException(exception.getMessage());
    }
}

From source file:com.fatwire.dta.sscrawler.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {// www .j a  v a2  s .  c o  m
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (final Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.alibaba.antx.config.resource.http.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from   w  w  w  .  ja v a2  s  .  c  o m*/
        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.cloud.network.bigswitch.TrustingProtocolSocketFactory.java

public TrustingProtocolSocketFactory() throws IOException {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//w  w  w.  j  av  a2  s  .  c  o  m
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            // Trust always
        }

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

    try {
        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        ssf = sc.getSocketFactory();
    } catch (KeyManagementException e) {
        throw new IOException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    }
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClient(byte[] sslCertificateBytes) {
    DefaultHttpClient httpClient;//from  ww  w.  j a  va  2s  .  c o  m
    Certificate[] sslCertificate;

    httpClient = new DefaultHttpClient();
    try {
        sslCertificate = convertByteArrayToCertificate(sslCertificateBytes);

        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:ch.truesolutions.payit.https.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {// ww w.j  a va 2s  . c  o  m
        // DS create a KeyStore todo
        KeyStore keyStore = KeyStore.getInstance("JKS");

        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(keyStore) }, null);
        return context;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e.toString());
    }
}

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;// www . j  ava  2s. co  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:eu.nullbyte.android.urllib.CertPinningSSLSocketFactory.java

private SSLContext createSSLContext() throws IOException {
    //Log.v(TAG, "createSSLContext()");
    try {/*from  ww  w  .  java 2s . c  o m*/
        SSLContext context = SSLContext.getInstance("TLS");
        mTrustManager = new CertPinningTrustManager(certificates, lastHost);
        KeyManager[] keyManagers = null;
        if (mClientCertificate != null) {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(mClientCertificate.getKeyStore(), mClientCertificate.getPassword().toCharArray());
            keyManagers = kmf.getKeyManagers();
        }
        context.init(keyManagers, new TrustManager[] { mTrustManager }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

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

private static SSLContext createEasySSLContext() {
    try {//from  w ww. j a  v  a 2s.  co  m
        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());
    }
}