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.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  .  j a  va2s .c o m

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

    return sslContext;
}

From source file:be.solidx.hot.nio.HttpsClient.java

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

    KeyManager[] keyManagers = null;
    TrustManager[] trustManagers = null;

    try {/*from   w  w w  .  ja  v  a 2  s .com*/
        String secureProtocol = options.get(SECUREPROTOCOL).toString();
        SSLContext sslContext = SSLContext.getInstance(secureProtocol);

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

From source file:com.zotoh.maedr.device.HttpIOTrait.java

/**
 * @param createContext// w w w.  j a va 2  s.c om
 * @param sslType
 * @param key
 * @param pwd
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnrecoverableEntryException
 * @throws KeyStoreException
 * @throws CertificateException
 * @throws IOException
 * @throws KeyManagementException
 */
protected static Tuple cfgSSL(boolean createContext, String sslType, URL key, String pwd)
        throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException, CertificateException,
        IOException, KeyManagementException {

    boolean jks = key.getFile().endsWith(".jks");
    InputStream inp = key.openStream();
    CryptoStore s;

    try {
        s = jks ? new JKSStore() : new PKCSStore();
        s.init(pwd);
        s.addKeyEntity(inp, pwd);
    } finally {
        StreamUte.close(inp);
    }

    SSLContext c = null;
    if (createContext) {
        c = SSLContext.getInstance(sslType);
        c.init(s.getKeyManagerFactory().getKeyManagers(), s.getTrustManagerFactory().getTrustManagers(),
                Crypto.getInstance().getSecureRandom());
    }

    return new Tuple(s, c);
}

From source file:wsattacker.plugin.intelligentdos.requestSender.Http4RequestSenderImpl.java

private SSLSocketFactory get() {

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new TrustAllManager() };

    // Install the all-trusting trust manager
    try {/*from ww  w.ja  va2  s .co  m*/
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        return new SSLSocketFactory(sc, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (RuntimeException e) {
        ;
    } catch (Exception e) {
        ;
    }

    return null;

}

From source file:com.fujitsu.dc.client.http.HttpClientFactory.java

/**
 * This method is used to generate SSLSocket.
 * @return SSLSocket that is generated/*w w w.ja v a 2  s  .  co m*/
 */
private static SSLSocketFactory createInsecureSSLSocketFactory() {
    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);
    }

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

    return socketFactory;
}

From source file:io.personium.client.http.HttpClientFactory.java

/**
 * This method is used to generate SSLSocket.
 * @return SSLSocket that is generated/*from  w  w  w.ja  va2 s.co m*/
 */
private static SSLSocketFactory createInsecureSSLSocketFactory() {
    SSLContext sslContext = null;
    try {
        sslContext = SSLContext.getInstance("TLSv1.2");
    } 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);
    }

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

    return socketFactory;
}

From source file:com.amazon.alexa.avs.auth.companionservice.CompanionServiceClient.java

/**
 * Loads the CA certificate into an in-memory keystore and creates an {@link SSLSocketFactory}.
 *
 * @return SSLSocketFactory//w w w.j av a 2  s .co m
 */
public SSLSocketFactory getPinnedSSLSocketFactory() {
    InputStream caCertInputStream = null;
    InputStream clientKeyPair = null;
    try {
        // Load the CA certificate into memory
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        caCertInputStream = new FileInputStream(deviceConfig.getCompanionServiceInfo().getSslCaCert());
        Certificate caCert = cf.generateCertificate(caCertInputStream);

        // Load the CA certificate into the trusted KeyStore
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        trustStore.setCertificateEntry("myca", caCert);

        // Create a TrustManagerFactory with the trusted KeyStore
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        // Load the client certificate and private key into another KeyStore
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        clientKeyPair = new FileInputStream(deviceConfig.getCompanionServiceInfo().getSslClientKeyStore());
        keyStore.load(clientKeyPair,
                deviceConfig.getCompanionServiceInfo().getSslClientKeyStorePassphrase().toCharArray());

        // Create a TrustManagerFactory with the client key pair KeyStore
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore,
                deviceConfig.getCompanionServiceInfo().getSslClientKeyStorePassphrase().toCharArray());

        // Initialize the SSLContext and return an SSLSocketFactory;
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        return sc.getSocketFactory();
    } catch (CertificateException | KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException
            | IOException | KeyManagementException e) {
        throw new RuntimeException("The KeyStore for contacting the Companion Service could not be loaded.", e);
    } finally {
        IOUtils.closeQuietly(caCertInputStream);
        IOUtils.closeQuietly(clientKeyPair);
    }
}

From source file:org.apache.commons.httpclient.contrib.ssl.StrictSSLProtocolSocketFactory.java

/**
 * Constructor for StrictSSLProtocolSocketFactory.
 * @param verifyHostname  The host name verification flag. If set to 
 * <code>true</code> the SSL sessions server host name will be compared
 * to the host name returned in the server certificates "Common Name" 
 * field of the "SubjectDN" entry.  If these names do not match a
 * Exception is thrown to indicate this.  Enabling host name verification 
 * will help to prevent from man-in-the-middle attacks.  If set to 
 * <code>false</code> host name verification is turned off.
 * //from  w w w .  ja va  2 s .c om
 * Code sample:
 *  
 *     <blockquote>
 *     Protocol stricthttps = new Protocol( 
 *         "https", new StrictSSLProtocolSocketFactory(true), 443);
 *
 *     HttpClient client = new HttpClient();
 *     client.getHostConfiguration().setHost("localhost", 443, stricthttps);
 *     </blockquote>
 * @throws NoSuchAlgorithmException 
 *
 */
public StrictSSLProtocolSocketFactory(boolean verifyHostname) throws NoSuchAlgorithmException {
    super(SSLContext.getInstance("SSL"));
    this.verifyHostname = verifyHostname;
}

From source file:com.ds.kaixin.Kaixin.java

private Kaixin() {
    System.setProperty("http.keepAlive", "false");

    SSLContext sslContext = null;

    try {//from   w  ww . j a  va  2s  .  com
        sslContext = SSLContext.getInstance("TLS");
        X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
        sslContext.init(null, xtmArray, new java.security.SecureRandom());
    } catch (GeneralSecurityException gse) {
    }
    if (sslContext != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    }

    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}