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.elasticsearch.client.RestClientBuilderIntegTests.java

private static SSLContext getSslContext() throws Exception {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    try (InputStream in = RestClientBuilderIntegTests.class.getResourceAsStream("/testks.jks")) {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(in, "password".toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keyStore, "password".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(keyStore);//from w  ww  .j a v a2 s  .c  om
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    }
    return sslContext;
}

From source file:org.gege.caldavsyncadapter.caldav.EasySSLSocketFactory.java

private EasySSLSocketFactory() {
    super();/*www . j  av a  2s .  c  om*/
    TrustManager[] tm = new TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // do nothing
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // do nothing
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

    } };
    try {
        this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
        this.sslcontext.init(null, tm, new SecureRandom());
        this.socketfactory = this.sslcontext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Faild to instantiate TrustAllSSLSocketFactory!", e);
    } catch (KeyManagementException e) {
        Log.e(TAG, "Failed to instantiate TrustAllSSLSocketFactory!", e);
    }
}

From source file:helpers.Methods.java

public static void trustAllCertificates() {
    //Certification check
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//from  w  w  w .  j  ava2  s.c o  m
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

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

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    // 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 (GeneralSecurityException ex) {
        Variables.logger.Log(Methods.class, Variables.LogType.Error,
                "Error in trusting all certificates. Details:\r\n" + ex.getMessage());
    }
}

From source file:co.cask.cdap.gateway.router.NettyRouterHttpsTest.java

@Override
protected DefaultHttpClient getHTTPClient() throws Exception {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // set up a TrustManager that trusts everything
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override/*from  w  w w  . j ava  2  s  . c  o  m*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //
        }

    } }, new SecureRandom());

    SSLSocketFactory sf = new SSLSocketFactory(sslContext);
    Scheme httpsScheme = new Scheme("https", 10101, sf);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);

    // apache HttpClient version >4.2 should use BasicClientConnectionManager
    ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
    return new DefaultHttpClient(cm);
}

From source file:org.apache.reef.runtime.hdinsight.client.sslhacks.UnsafeClientConstructor.java

private SSLContext getSSLContext() throws KeyManagementException, NoSuchAlgorithmException {
    final SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(new KeyManager[0], new TrustManager[] { new UnsafeTrustManager() }, new SecureRandom());
    return sc;/*w ww .j  ava2  s. co m*/
}

From source file:be.fedict.eid.idp.sp.protocol.openid.OpenIDSSLSocketFactory.java

/**
 * Trust only the given server certificate, and the default trusted server
 * certificates.//from  www .j a  v  a  2 s.com
 * 
 * @param serverCertificate
 *            SSL certificate to trust
 * @throws NoSuchAlgorithmException
 *             could not get an SSLContext instance
 * @throws KeyManagementException
 *             failed to initialize the SSLContext
 * @throws KeyStoreException
 *             failed to intialize the {@link OpenIDTrustManager}
 */
public OpenIDSSLSocketFactory(X509Certificate serverCertificate)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    this.sslContext = SSLContext.getInstance("SSL");
    TrustManager trustManager = new OpenIDTrustManager(serverCertificate);
    TrustManager[] trustManagers = { trustManager };
    this.sslContext.init(null, trustManagers, null);
}

From source file:org.jsnap.request.SSLSocketFactory.java

private SSLSocketFactory(boolean trustAll) {
    sf = null;//from w  w w  . j a v a2 s  .com
    if (trustAll) {
        // Create a trust manager that does not validate certificate chains.
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            sf = sc.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        } catch (KeyManagementException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        }
    } else {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, null, null);
            sf = sc.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        } catch (KeyManagementException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        }
    }
}

From source file:slash.navigation.rest.ssl.SSLConnectionManagerFactory.java

private SSLContext createSSLContext() throws KeyStoreException, CertificateException, NoSuchAlgorithmException,
        KeyManagementException, IOException {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    TrustManagerFactory javaDefaultTrustManager = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    javaDefaultTrustManager.init((KeyStore) null);
    TrustManagerFactory customCaTrustManager = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    customCaTrustManager.init(getKeyStore());

    sslContext.init(null,/*w w w  . j  av  a  2  s.c  o  m*/
            new TrustManager[] {
                    new TrustManagerDelegate((X509TrustManager) customCaTrustManager.getTrustManagers()[0],
                            (X509TrustManager) javaDefaultTrustManager.getTrustManagers()[0]) },
            secureRandom);
    return sslContext;
}

From source file:org.sakuli.services.forwarder.icinga2.Icinga2RestCient.java

private SSLContext getTrustEverythingSSLContext() {
    try {/*from w  ww. ja v  a  2 s  . com*/
        final SSLContext sslContext = SSLContext.getInstance("SSL");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } }, new SecureRandom());
        return sslContext;
    } catch (Exception e) {
        throw new SakuliRuntimeException("Unable to create SSL-Context", e);
    }
}

From source file:com.zaubersoftware.mule.module.jenkins.api.impl.HttpJenkinsService.java

protected void addSslConfiguration() throws NoSuchAlgorithmException, KeyManagementException {
    final SSLContext ctx = SSLContext.getInstance("SSL");

    ctx.init(null, null, null);/*  w  w  w  .j  av  a 2s .c  om*/
}