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:org.nuclos.common.tls.CustomSecureProtocolSocketFactory.java

private static SSLContext createCustomSSLContext() {
    try {//from   w w  w .j a  v a  2  s. com
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new CustomX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new IllegalStateException(e);
    }
}

From source file:pt.hive.cameo.ssl.SSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/* w ww. j a  v  a  2 s  . co  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new TrivialTrustManager() }, null);
        return context;
    } catch (Exception exception) {
        throw new IOException(exception.getMessage());
    }
}

From source file:com.farru.android.network.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from  ww  w .  j ava2  s.com
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

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

public static void applyAnother() {
    try {/* w w  w .  j  a  v  a 2 s  . co 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.linkedin.d2.discovery.stores.glu.TrustingSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from  ww w.  ja v a  2 s  .  co m*/
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } }, null);
        return context;
    } catch (Exception e) {
        throw new HttpClientError(e.toString());
    }
}

From source file:com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpClientProvider.java

private static SSLContext buildSslContext() {
    try {// ww w  .j  a  va 2s  .c o m
        TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() };
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustAllCerts, null);
        return context;
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        log.warn("Error building SSLContext object", e);
        return null;
    }
}

From source file:org.thoughtcrime.ssl.pinning.util.PinningHelper.java

/**
 * Constructs an HttpsURLConnection that will validate HTTPS connections against a set of
 * specified pins./*w  w  w .j av a 2 s. co  m*/
 *
 * @param pins An array of encoded pins to match a seen certificate
 *             chain against. A pin is a hex-encoded hash of a X.509 certificate's
 *             SubjectPublicKeyInfo. A pin can be generated using the provided pin.py
 *             script: python ./tools/pin.py certificate_file.pem
 *
 */

public static HttpsURLConnection getPinnedHttpsURLConnection(Context context, String[] pins, URL url)
        throws IOException {
    try {
        if (!url.getProtocol().equals("https")) {
            throw new IllegalArgumentException("Attempt to construct pinned non-https connection!");
        }

        TrustManager[] trustManagers = new TrustManager[1];
        trustManagers[0] = new PinningTrustManager(SystemKeyStore.getInstance(context), pins, 0);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagers, null);

        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());

        return urlConnection;
    } catch (NoSuchAlgorithmException nsae) {
        throw new AssertionError(nsae);
    } catch (KeyManagementException e) {
        throw new AssertionError(e);
    }
}

From source file:com.grantedbyme.example.ServletUtils.java

public static GrantedByMe getSDK(HttpServlet context) throws IOException {
    // read private key
    String privateKey = null;//w ww  . jav  a2 s  . c  o  m
    InputStream privateKeyInputStream = context.getClass().getResourceAsStream("/private_key.pem");
    try {
        privateKey = IOUtils.toString(privateKeyInputStream);
    } finally {
        privateKeyInputStream.close();
    }
    // read server key
    String serverKey = null;
    InputStream serverKeyInputStream = context.getClass().getResourceAsStream("/server_key.pem");
    try {
        serverKey = IOUtils.toString(serverKeyInputStream);
    } finally {
        serverKeyInputStream.close();
    }
    // _log(serverKey);
    // initialize BouncyCastle security provider
    Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 0);
    // create sdk
    GrantedByMe sdk = new GrantedByMe(privateKey, serverKey);
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // set SDK parameters
    sdk.apiURL = "https://api-dev.grantedby.me/v1/service/";
    //sdk.isDebug = true;
    return sdk;
}

From source file:be.emich.labs.villohelper.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from ww  w  .j a va2s .  c o m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:io.specto.hoverfly.junit.HoverflyRuleUtils.java

static void setHoverflyTrustStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException,
        IOException, KeyManagementException, URISyntaxException {
    // load your key store as a stream and initialize a KeyStore
    InputStream trustStream = findResourceOnClasspath("hoverfly.jks").toURL().openStream();

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

    // load the stream to your store
    trustStore.load(trustStream, "hoverfly".toCharArray());

    // initialize a trust manager factory with the trusted store
    TrustManagerFactory trustFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustFactory.init(trustStore);/*from  ww  w . j  ava2  s.co  m*/

    // get the trust managers from the factory
    TrustManager[] trustManagers = trustFactory.getTrustManagers();

    // initialize an ssl context to use these managers and set as default
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagers, null);
    SSLContext.setDefault(sslContext);
}