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:be.fedict.eid.idp.sp.protocol.openid.OpenIDSSLSocketFactory.java

/**
 * Trusts all server certificates.//from  w  w w. ja va 2 s  . c  om
 * 
 * @throws NoSuchAlgorithmException
 *             could not get an SSLContext instance
 * @throws KeyManagementException
 *             failed to initialize the SSLContext
 */
public OpenIDSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    this.sslContext = SSLContext.getInstance("SSL");
    TrustManager trustManager = new OpenIDTrustManager();
    TrustManager[] trustManagers = { trustManager };
    this.sslContext.init(null, trustManagers, null);
}

From source file:com.fanmei.pay4j.http.WeixinSSLRequestExecutor.java

public WeixinSSLRequestExecutor(WeixinConfig weixinConfig) throws WeixinException {
    InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream(weixinConfig.getCertificateFile());
    try {/*from ww w .j a  v  a 2 s  .  com*/
        String password = weixinConfig.getAccount().getCertificateKey();
        KeyStore keyStore = KeyStore.getInstance(Constants.PKCS12);
        keyStore.load(inputStream, password.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(Constants.SunX509);
        kmf.init(keyStore, password.toCharArray());
        SSLContext sslContext = SSLContext.getInstance(Constants.TLS);
        sslContext.init(kmf.getKeyManagers(), null, new java.security.SecureRandom());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } catch (Exception e) {
        throw WeixinException.of("Key load error", e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {

            }
        }
    }
}

From source file:com.cloupia.feature.nimble.http.MySSLSocketFactory.java

public Socket createSocket(String host, int port) throws IOException, UnknownHostException {

    TrustManager[] trustAllCerts = getTrustManager();

    try {/*from  w w  w  .  java2 s.  c  o m*/

        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        SocketFactory socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

        return socketFactory.createSocket(host, port);

    }

    catch (Exception ex) {

        throw new UnknownHostException("Problems to connect " + host + ex.toString());

    }

}

From source file:org.fineract.module.stellar.fineractadapter.RestAdapterProvider.java

OkHttpClient createClient() {

    final OkHttpClient client = new OkHttpClient();

    final TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override//from  ww  w  .jav  a2 s .  c om
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (final java.security.GeneralSecurityException ignored) {
    }

    try {
        client.setHostnameVerifier((hostname, session) -> true);
        if (ctx != null) {
            client.setSslSocketFactory(ctx.getSocketFactory());
        }
    } catch (final Exception ignored) {
    }

    return client;
}

From source file:ch.cyberduck.core.ssl.CustomTrustSSLProtocolSocketFactory.java

private SSLContext createEasySSLContext() {
    try {/*from   www  .  j  ava 2  s.c  o  m*/
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { trustManager }, null);
        return context;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return null;
    }
}

From source file:com.ethercamp.harmony.util.TrustSSL.java

public static void applyAnother() {
    try {/*from  ww  w .ja  v  a2 s .  c  o m*/
        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:com.trsst.client.AnonymSSLSocketFactory.java

/**
 * Create the SSL Context./*ww  w.j  av  a 2 s  . c o m*/
 * 
 * @return The SSLContext
 */
private static SSLContext createEasySSLContext() {
    try {
        SSLContext context = SSLContext.getInstance("SSL"); //$NON-NLS-1$
        context.init(null, new TrustManager[] { new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }
        } }, null);
        return context;
    } catch (Exception e) {
        throw new HttpClientError(e.toString());
    }
}

From source file:com.cazoodle.crawl.DummySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {//from   w  ww .jav  a 2s  .c  o m
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
        throw new HttpClientError(e.toString());
    }
}

From source file:com.lugia.timetable.SSLHttpClient.java

public static SSLHttpClient getHttpClient()
        throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException

{
    HttpClient client = new DefaultHttpClient();

    X509TrustManager tm = createX509TrustManager();

    SSLContext ctx = SSLContext.getInstance("TLS");

    ctx.init(null, new TrustManager[] { tm }, null);

    SSLSocketFactory ssf = new MySSLSocketFactory(ctx);

    ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    ClientConnectionManager ccm = client.getConnectionManager();

    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", ssf, 443));

    return new SSLHttpClient(new ThreadSafeClientConnManager(client.getParams(), sr), client.getParams());
}

From source file:org.reficio.ws.client.ssl.SSLUtils.java

public static SSLSocketFactory getMergedSocketFactory(org.reficio.ws.client.core.Security securityOne,
        Security securityTwo) throws GeneralSecurityException {
    X509KeyManager keyManagerOne = getKeyManager(securityOne.getKeyStore(), securityOne.getKeyStorePassword());
    X509KeyManager keyManagerTwo = getKeyManager(securityTwo.getKeyStore(), securityTwo.getKeyStorePassword());

    X509TrustManager trustManager = getMultiTrustManager(getTrustManager(securityOne.getTrustStore()),
            getTrustManager(securityTwo.getTrustStore()));

    SSLContext context = SSLContext.getInstance(securityOne.getSslContextProtocol());
    boolean strictHostVerification = securityOne.isStrictHostVerification()
            && securityTwo.isStrictHostVerification();

    context.init(new KeyManager[] { keyManagerOne, keyManagerTwo }, new TrustManager[] { trustManager },
            new SecureRandom());
    X509HostnameVerifier verifier = strictHostVerification ? SSLSocketFactory.STRICT_HOSTNAME_VERIFIER
            : SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    return new SSLSocketFactory(context, verifier);
}