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.utest.domain.service.util.TrustedSSLUtil.java

private static SSLContext createSSLContext() {
    try {/*from   w  w w  .ja  va  2  s.  com*/
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { trustAllCerts }, null);
        return context;
    } catch (final Exception e) {
        throw new HttpClientError(e.toString());
    }
}

From source file:com.bytelightning.opensource.pokerface.HelloWorldScriptTest.java

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    PrevSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    PrevHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();

    proxy = new PokerFace();
    XMLConfiguration conf = new XMLConfiguration();
    conf.load(ProxySpecificTest.class.getResource("/HelloWorldTestConfig.xml"));
    proxy.config(conf);/*  w  w  w.j  av a  2 s.  c  o m*/
    boolean started = proxy.start();
    Assert.assertTrue("Successful proxy start", started);

    SSLContext sc = SSLContext.getInstance("TLS");
    TrustManager[] trustAllCertificates = { new X509TrustAllManager() };
    sc.init(null, trustAllCertificates, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true; // Just allow them all.
        }
    });

}

From source file:org.zywx.wbpalmstar.platform.certificates.HSSLSocketFactory.java

public HSSLSocketFactory(KeyStore ksP12, String keyPass) throws Exception {
    super(ksP12);
    mSSLContext = SSLContext.getInstance(SSLSocketFactory.TLS);
    KeyManagerFactory kMgrFact = null;
    TrustManager[] tMgrs = null;/*from   w  ww.java2 s.  com*/
    KeyManager[] kMgrs = null;
    TrustManager tMgr = null;
    tMgr = new HX509TrustManager(ksP12);
    kMgrFact = KeyManagerFactory.getInstance(Http.algorithm);
    if (null != keyPass) {
        kMgrFact.init(ksP12, keyPass.toCharArray());
    } else {
        kMgrFact.init(ksP12, null);
    }
    kMgrs = kMgrFact.getKeyManagers();
    tMgrs = new TrustManager[] { tMgr };
    SecureRandom secureRandom = new java.security.SecureRandom();
    mSSLContext.init(kMgrs, tMgrs, secureRandom);
    if (!Http.isCheckTrustCert()) {
        setHostnameVerifier(new HX509HostnameVerifier());
    } else {
        setHostnameVerifier(STRICT_HOSTNAME_VERIFIER);
    }
}

From source file:com.cloudera.nav.sdk.client.SSLUtils.java

/**
 * Return the TLS SSLContext with the TrustManager specified by the config
 * @param config//from   ww  w .  j  av a 2s  .com
 */
public static SSLContext getSSLContext(ClientConfig config) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { getTrustManager(config) }, null);
        return ctx;
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.cttapp.bby.mytlc.layer8apps.SimpleSSLSocketFactory.java

public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(null);/* w w w .j  a v a 2 s  .c  o m*/

    try {
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains and simply
        // accept all type of certificates
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }
        } };

        // Initialize the socket factory
        context.init(null, trustAllCerts, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:comsat.sample.tomcat.SampleTomcatTwoConnectorsApplicationTests.java

@BeforeClass
public static void setUp() {
    try {/*ww w .j  a  va  2 s.  c  o  m*/
        // setup ssl context to ignore certificate errors
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

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

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLContext.setDefault(ctx);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.thoughtworks.go.server.web.PermissiveSSLSocketFactory.java

private SSLContext getSSLContext() {
    if (this.sslcontext == null) {
        SSLContext context;//from  ww  w.  ja  v a 2 s  .c  om
        try {
            context = SSLContext.getInstance("SSL");
            context.init(null, new TrustManager[] { new PermissiveX509TrustManager(null) }, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        this.sslcontext = context;
    }
    return this.sslcontext;
}

From source file:Main.java

/**
 * Generate a SSLSocketFactory wich checks the certificate given
 * @param context Context to use//from   w  w w.  jav a2s  . c  o  m
 * @param rResource int with url of the resource to read the certificate
 * @parma password String to use with certificate
 * @return SSLSocketFactory generated to validate this certificate
 */
public static SSLSocketFactory newSslSocketFactory(Context context, int rResource, String password)
        throws CertificateException, NoSuchProviderException, KeyStoreException, NoSuchAlgorithmException,
        IOException, UnrecoverableKeyException, KeyManagementException {

    // Get an instance of the Bouncy Castle KeyStore format
    KeyStore trusted = KeyStore.getInstance("BKS");
    // Get the raw resource, which contains the keystore with
    // your trusted certificates (root and any intermediate certs)
    InputStream is = context.getApplicationContext().getResources().openRawResource(rResource);

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
    X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is);
    String alias = "alias";//cert.getSubjectX500Principal().getName();

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);
    trustStore.setCertificateEntry(alias, cert);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    kmf.init(trustStore, null);
    KeyManager[] keyManagers = kmf.getKeyManagers();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

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

}

From source file:com.lolay.android.security.OpenSSLSocketFactory.java

private SSLContext getContext() {
    if (context == null) {
        try {/*from w w  w.  j av a2 s.  c o m*/
            SSLContext context = SSLContext.getInstance("SSL");
            context.init(null, new TrustManager[] { new OpenX509TrustManager() }, null);
            this.context = context;
        } catch (NoSuchAlgorithmException e) {
            //            throw new IOException(e);
        } catch (KeyManagementException e) {
            //            throw new IOException(e);
        }
    }

    return this.context;
}

From source file:com.mingsoft.weixin.http.WeixinSSLSocketFactory.java

private static SSLContext createSContext() {
    SSLContext sslcontext = null;
    try {/*from  w  w w  .ja v a2  s .  c  o  m*/
        sslcontext = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    try {
        sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() }, null);
    } catch (KeyManagementException e) {
        e.printStackTrace();
        return null;
    }
    return sslcontext;
}