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:org.jasig.cas.util.SimpleHttpClientTests.java

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

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

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/*from w  w  w . j  a v a  2 s .  c om*/
 */
@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:com.kappaware.logtrawler.output.httpclient.HttpClient.java

public HttpClient(SslHandling sslHandling, On404 on404) {
    this.on404 = on404;
    if (sslHandling == SslHandling.NONE) {
        httpclient = HttpClients.custom().build();
    } else {/* w ww . j  a  va  2  s .c  o m*/
        try {
            SSLConnectionSocketFactory sslConnectionSocketFactory;
            SSLContext sslContext = SSLContext.getInstance("TLS");
            if (sslHandling == SslHandling.STRICT_SSL) {
                sslContext.init(null, null, null);
                sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                        new BrowserCompatHostnameVerifier());
            } else {
                sslContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, null);
                sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                        new AllowAllHostnameVerifier());
            }
            httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
        } catch (Exception e) {
            throw new RuntimeException("Exception in SSL configuration", e);
        }
    }
}

From source file:com.cloudbees.tftwoway.Client.java

private static SSLContext createSSLContext() throws Exception {
    KeyManager[] serverKeyManagers = getKeyManager();
    TrustManager[] serverTrustManagers = getTrustManager();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(serverKeyManagers, serverTrustManagers, new SecureRandom());

    return sslContext;
}

From source file:com.springsource.hq.plugin.tcserver.serverconfig.web.support.UntrustedSSLProtocolSocketFactory.java

public UntrustedSSLProtocolSocketFactory() {
    super();//from w w w .  j a  v  a  2s .  c om

    try {
        BogusTrustManager trustMan;
        SSLContext tlsContext;

        trustMan = new BogusTrustManager();
        tlsContext = SSLContext.getInstance("TLS");
        tlsContext.init(null, new X509TrustManager[] { trustMan }, null);
        this.factory = tlsContext.getSocketFactory();
    } catch (NoSuchAlgorithmException exc) {
        throw new IllegalStateException("Unable to get SSL context: " + exc.getMessage());
    } catch (KeyManagementException exc) {
        throw new IllegalStateException(
                "Unable to initialize ctx " + "with BogusTrustManager: " + exc.getMessage());
    }
}

From source file:edu.washington.iam.tools.IamConnectionManager.java

public IamConnectionManager(String caFile, String certFile, String keyFile) {
    log.debug("create connection manager");
    caFilename = caFile;// ww  w. jav a 2s  . c o m
    certFilename = certFile;
    keyFilename = keyFile;
    String protocol = "https";
    int port = 443;

    initManagers();

    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(keyManagers, trustManagers, null);
        socketFactory = new SSLSocketFactory(ctx);
        Scheme scheme = new Scheme(protocol, socketFactory, port);
        schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(scheme);

        log.debug("create conn mgr");
        connectionManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);

    } catch (Exception e) {
        log.error("sf error: " + e);
    }
}

From source file:inet.encode.SecureMonitor.java

private static void createHttpsServer() {
    try {//from w  w w. ja va2 s  . c o  m
        server = HttpsServer.create(new InetSocketAddress(MONITOR_SERVER_PORT), 0);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        // initialise the keystore
        char[] password = Encoder.KEY_STORE_PASS_PHRASE.toCharArray();
        KeyStore ks = KeyStore.getInstance("JKS");
        FileInputStream fis = new FileInputStream(Encoder.KEY_STORE_PATH);
        ks.load(fis, password);

        // setup the key manager factory
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, password);

        // setup the trust manager factory
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(ks);

        // setup the HTTPS context and parameters
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        server.setHttpsConfigurator(new HttpsConfigurator(sslContext));
        server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
        server.start();
    } catch (Exception ex) {
        Logger.log(ex);
    }
}

From source file:com.orange.cloud.servicebroker.filter.core.config.OkHttpClientConfig.java

@Bean
public OkHttpClient squareHttpClient() {
    HostnameVerifier hostnameVerifier = new HostnameVerifier() {
        @Override//from   w  w  w.j  av  a  2  s .  c  om
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    TrustManager[] trustAllCerts = new TrustManager[] { new TrustAllCerts() };

    SSLSocketFactory sslSocketFactory = null;
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        sslSocketFactory = (SSLSocketFactory) sc.getSocketFactory();
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        new IllegalArgumentException(e);
    }

    log.info("===> configuring OkHttp");
    OkHttpClient.Builder ohc = new OkHttpClient.Builder().protocols(Arrays.asList(Protocol.HTTP_1_1))
            .followRedirects(true).followSslRedirects(true).hostnameVerifier(hostnameVerifier)
            .sslSocketFactory(sslSocketFactory).addInterceptor(LOGGING_INTERCEPTOR);

    if ((this.proxyHost != null) && (this.proxyHost.length() > 0)) {
        log.info("Activating proxy on host {} port {}", this.proxyHost, this.proxyPort);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, this.proxyPort));
        ohc.proxy(proxy);
        ohc.proxySelector(new ProxySelector() {
            @Override
            public List<Proxy> select(URI uri) {
                return Arrays.asList(proxy);
            }

            @Override
            public void connectFailed(URI uri, SocketAddress socket, IOException e) {
                throw new IllegalArgumentException("connection to proxy failed", e);
            }
        });
    }

    return ohc.build();
}

From source file:io.personium.core.utils.HttpClientFactory.java

/**
 * SSLSocket?.//www  . j  a v a 2  s  .co  m
 * @return ???SSLSocket
 */
private static SSLSocketFactory createInsecureSSLSocketFactory() {
    // CHECKSTYLE:OFF
    SSLContext sslContext = null;
    try {
        sslContext = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException e1) {
        throw new RuntimeException(e1);
    }

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

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

            public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
                // System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());
    } catch (KeyManagementException e1) {
        throw new RuntimeException(e1);
    }
    // CHECKSTYLE:ON

    HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    // socketFactory.setHostnameVerifier((X509HostnameVerifier)
    // hostnameVerifier);

    return socketFactory;
}

From source file:net.sf.ufsc.ftp.FTPSClient.java

public FTPSClient() {
    super();//w  ww.  ja  v a2 s . c om

    try {
        KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
        keyStore.load(null, PASSWORD.toCharArray());

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());

        keyManagerFactory.init(keyStore, PASSWORD.toCharArray());

        SSLContext context = SSLContext.getInstance(PROTOCOL);
        context.init(keyManagerFactory.getKeyManagers(), new TrustManager[] { new SimpleTrustManager() }, null);

        this.socketFactory = new SecureSocketFactory(context);
    } catch (Exception e) {
        e.printStackTrace();
    }
}