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.sittinglittleduck.DirBuster.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*w  ww  . j  a  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:io.github.retz.web.Client.java

protected Client(URI uri, Authenticator authenticator, boolean checkCert) {
    this.uri = Objects.requireNonNull(uri);
    this.authenticator = Objects.requireNonNull(authenticator);
    this.checkCert = checkCert;
    if (uri.getScheme().equals("https") && !checkCert) {
        LOG.warn(/*  ww  w .j a va  2 s. co  m*/
                "DANGER ZONE: TLS certificate check is disabled. Set 'retz.tls.insecure = false' at config file to supress this message.");
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new WrongTrustManager() }, new java.security.SecureRandom());
            socketFactory = sc.getSocketFactory();
            hostnameVerifier = new NoOpHostnameVerifier();
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e.toString());
        } catch (KeyManagementException e) {
            throw new AssertionError(e.toString());
        }
    } else {
        socketFactory = null;
        hostnameVerifier = null;
    }
    this.retz = Retz.connect(uri, authenticator, socketFactory, hostnameVerifier);
    System.setProperty("http.agent", Client.VERSION_STRING);
}

From source file:io.personium.test.jersey.HttpClientFactory.java

/**
 * SSLSocket?.//from   w w w  .jav a2s . c  o 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 = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    // socketFactory.setHostnameVerifier((X509HostnameVerifier)
    // hostnameVerifier);

    return socketFactory;
}

From source file:com.fujitsu.dc.test.jersey.HttpClientFactory.java

/**
 * SSLSocket?.//from  w w w .  j  ava  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 final void checkClientTrusted(final X509Certificate[] certs, final String authType) {
                // System.out.println("checkClientTrusted =============");
            }

            public final 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 = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    // socketFactory.setHostnameVerifier((X509HostnameVerifier)
    // hostnameVerifier);

    return socketFactory;
}

From source file:com.wso2.identity.oauth.sample.OAuth2ServiceClient.java

private void enableSelfSignedCertificates(ServiceClient client) {
    String protocol = "SSL";
    try {/*from   w  ww .  j a v a 2s  .c  o  m*/
        SSLContext sslCtx = SSLContext.getInstance(protocol);
        sslCtx.init(null, new TrustManager[] { new TrustAllTrustManager() }, null);
        client.getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                new Protocol("https", (ProtocolSocketFactory) new SSLProtocolSocketFactory(sslCtx), 443));
    } catch (KeyManagementException e) {
        throw new IllegalStateException("Key management exception for '" + protocol + "' SSL context", e);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Unable to get SSL context for '" + protocol + "' protocol", e);
    }

}

From source file:be.solidx.hot.nio.http.SSLContextBuilder.java

protected SSLContext buildSSLContext(Map<String, Object> options) throws SSLContextInitializationException {

    Map<String, Object> sslOptions = loadSSLOptions(options);
    KeyManager[] keyManagers = null;
    TrustManager[] trustManagers = null;

    try {//  w  ww  . j a v  a  2 s  .  c  om
        String secureProtocol = sslOptions.get(SECUREPROTOCOL).toString();
        SSLContext sslContext = SSLContext.getInstance(secureProtocol);

        if (sslOptions.get(JKS) != null) {
            keyManagers = handleClientKeystoreURLProvided(sslOptions).getKeyManagers();
        } else if (sslOptions.get(P12) != null) {
            keyManagers = handleClientPFXURLProvided(sslOptions).getKeyManagers();
        } else if (sslOptions.get(KEY) != null) {
            keyManagers = handleClientKeyCertURLProvided(sslOptions).getKeyManagers();
        }
        trustManagers = handleTrustManagers(sslOptions);
        sslContext.init(keyManagers, trustManagers, null);
        return sslContext;
    } catch (KeyManagementException | NoSuchAlgorithmException | SSLContextInitializationException
            | CertificateException | IOException | URISyntaxException e) {
        throw new SSLContextInitializationException(e);
    }
}

From source file:org.jasig.cas.util.http.SimpleHttpClientTests.java

private SSLConnectionSocketFactory getFriendlyToAllSSLSocketFactory() throws Exception {
    final TrustManager trm = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from   ww w.java  2 s.c o m*/

        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, new NoopHostnameVerifier());
}

From source file:HttpClient.EasySSLProtocolSocketFactory.java

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

From source file:com.wso2telco.identity.application.authentication.endpoint.util.MutualSSLClient.java

/**
 * create basic SSL connection factory//from   ww w.  j  a  va 2 s.c  om
 *
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyStoreException
 * @throws java.security.KeyManagementException
 * @throws java.io.IOException
 * @throws java.security.UnrecoverableKeyException
 */
public static void initMutualSSLConnection() throws NoSuchAlgorithmException, KeyStoreException,
        KeyManagementException, IOException, UnrecoverableKeyException {

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KEY_MANAGER_TYPE);
    keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TRUST_MANAGER_TYPE);
    trustManagerFactory.init(trustStore);
    SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    sslSocketFactory = sslContext.getSocketFactory();
}