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:com.adobe.api.platform.msc.client.config.ClientConfig.java

/**
 * Used for creating http connections to 3rd party API. It's a heavy-weight object and it's useful to reuse it.
 *
 * @return A JaxRS Client instance/*  ww w.j a va 2  s . com*/
 */
@Bean
public Client getJaxrsClient() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

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

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

        // Ignore differences between given hostname and certificate hostname
        SSLContext ctx = SSLContext.getInstance("SSL");
        ctx.init(null, trustAllCerts, null);

        ResteasyClientBuilder builder = new ResteasyClientBuilder().sslContext(ctx)
                .register(JacksonConfig.class)
                .hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY);

        //if not set, builder sets a pool with 10 threads
        if (workerThreadPooSize != null) {
            // if necessary expose the thread pool as bean (reuse the same thread pool for other scenarios).
            builder.asyncExecutor(Executors.newFixedThreadPool(workerThreadPooSize));
        }

        if (connectionPoolSize != null) {
            builder.connectionPoolSize(connectionPoolSize);
        }

        if (connectionTTL != null) {
            builder.connectionTTL(connectionTTL, TimeUnit.SECONDS);
        }

        if (socketTimeout != null) {
            builder.socketTimeout(socketTimeout, TimeUnit.SECONDS);
        }

        return builder.build();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

public ThriftAuthClient(String serverIP, String remoteServerPort, String webContextRoot)
        throws AuthenticationException {

    try {//from  w w  w  . j a  va  2s . c o m
        TrustManager easyTrustManager = new X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) {
            }

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

            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);

        //REGISTERS SCHEMES FOR BOTH HTTP AND HTTPS
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", sf, Integer.parseInt(remoteServerPort)));

        PoolingClientConnectionManager manager = new PoolingClientConnectionManager(registry);
        HttpClient httpClient = new DefaultHttpClient(manager);

        //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..", e);
    } catch (Exception e) {
        throw new AuthenticationException("Error in creating thrift authentication client..", e);
    }
}

From source file:at.gv.egiz.bku.spring.SSLSocketFactoryBean.java

@Override
public Object getObject() throws Exception {
    PKITrustManager pkiTrustManager = new PKITrustManager();
    pkiTrustManager.setConfiguration(configurationFacade.configuration);
    pkiTrustManager.setPkiProfile(pkiProfile);

    SSLContext sslContext = SSLContext.getInstance(configurationFacade.getSslProtocol());
    sslContext.init(null, new TrustManager[] { pkiTrustManager }, null);

    return sslContext.getSocketFactory();
}

From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java

private SSLContext createContext(KeyStore keystore, KeyManagerFactory kmf) throws Exception {
    TrustManagerFactory trustFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustFactory.init(keystore);//from  w  w  w . ja v  a  2  s . c  om

    SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
    sslContext.init(kmf == null ? null : kmf.getKeyManagers(), trustFactory.getTrustManagers(), null);

    return sslContext;
}

From source file:org.elasticsearch.xpack.ssl.SSLClientAuthTests.java

private SSLContext getSSLContext() {
    try (InputStream in = Files.newInputStream(
            getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks"))) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(in, "testclient".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);// w  ww .j  a  v  a 2 s. c  o  m
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, "testclient".toCharArray());
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
    }
}

From source file:at.diamonddogs.net.ssl.CustomSSLSocketFactory.java

private SSLContext createCustomSSLContext(KeyStore store) {
    try {//from ww  w.  j a  v a 2  s  .c  o  m
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(store);

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, CustomX509TrustManager.getWrappedTrustmanager(tmf.getTrustManagers()), null);
        return context;
    } catch (Exception e) {
        LOGGER.error("unable to create ssl context", e);
        return null;
    }
}

From source file:com.gsf.dowload.nfe.HSProtocolSocketFactory.java

private SSLContext createSSLContext() {
    try {//from   w w w . ja v  a 2 s. c o m
        KeyManager[] keyManagers = createKeyManagers();
        TrustManager[] trustManagers = createTrustManagers();
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);

        return sslContext;
    } catch (KeyManagementException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (KeyStoreException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (NoSuchAlgorithmException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (CertificateException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (IOException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    }
    return null;
}

From source file:org.skfiy.typhon.rnsd.service.handler.AppleRechargingHandler.java

public AppleRechargingHandler() throws Exception {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
    HC_BUILDER.setSslcontext(sc);/* ww  w  . ja  va2 s .c  o m*/
}

From source file:riddimon.android.asianetautologin.HttpManager.java

private HttpManager(Boolean debug, String version) {
    // Set basic data
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpProtocolParams.setUserAgent(params, HttpUtils.userAgent);

    // Make pool/*from w w w  .  j  ava2 s .com*/
    ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    ConnManagerParams.setMaxTotalConnections(params, 20);

    // Set timeout
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // Some client params
    HttpClientParams.setRedirecting(params, false);

    // Register http/s schemas!
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    if (debug) {
        // Install the all-trusting trust manager
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustManagers = new X509TrustManager[1];
        trustManagers[0] = new TrustAllManager();

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustManagers, null);
            schReg.register(new Scheme("https", (SocketFactory) sc.getSocketFactory(), 443));
        } catch (Exception e) {
            ;
        }
    } else {
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    }
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    client = new DefaultHttpClient(conMgr, params);

}