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:edu.vt.middleware.ldap.ssl.AbstractSSLContextInitializer.java

/** {@inheritDoc} */
public SSLContext initSSLContext(final String protocol) throws GeneralSecurityException {
    final SSLContext ctx = SSLContext.getInstance(protocol);
    ctx.init(this.getKeyManagers(), this.getTrustManagers(), null);
    return ctx;/*from w w  w .ja  va 2 s  . co  m*/
}

From source file:Main.java

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;//from  ww w .  jav  a2  s . co  m
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}

From source file:com.codesharp.cooper.plugins.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   www .j a  v a  2  s  .co m*/
        SSLContext context = SSLContext.getInstance("TLS");

        // 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(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }

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

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.framework.library.connection.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from  w w w . ja v  a2 s .  c o  m
        SSLContext context = SSLContext.getInstance("TLS");

        // 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(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // do nothing
            }

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

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:de.gfred.lbbms.mobile.services.IgnoreSelfCertificatesSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   ww  w .j a  va 2  s.  c om*/
        SSLContext context = SSLContext.getInstance("TLS");

        // 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(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                // do nothing
            }

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

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.isec.helperapp.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    Log.i(TAG, "createEasySSLContext");
    try {//from www .jav  a  2  s . co m
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, TrustAllTrustManager.getTrustManagers(), null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.fusioninventory.utils.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    FusionInventory.log(EasySSLSocketFactory.class, "Create Easy SSL Context", Log.ERROR);
    try {//from  w w w  . j av a 2  s  .c  om
        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:com.ntsync.android.sync.client.MySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   www .  j  a v  a 2s  .  c om*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new MyX509TrustManager() }, null);
        return context;
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "Init SSLContext failed.", e);
        throw new IOException(e.getMessage());
    }
}

From source file:com.foundstone.certinstaller.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext(OnCertsRecievedListener listener) throws IOException {
    try {//from   ww  w. ja  v a 2 s. com
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null, listener) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:gov.nist.appvet.servlet.shared.SSLWrapper.java

@SuppressWarnings("deprecation")
public synchronized static HttpClient wrapClient(HttpClient base) {
    SSLContext ctx = null;//from  w ww .  j a v a  2 s.com
    X509TrustManager tm = null;
    SSLSocketFactory ssf = null;
    SchemeRegistry sr = null;
    try {
        ctx = SSLContext.getInstance("TLSv1.2");
        tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

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

        };
        ctx.init(null, new TrustManager[] { tm }, null);
        ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        final ClientConnectionManager ccm = base.getConnectionManager();
        sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception e) {
        log.error(e.getMessage().toString());
        return null;
    } finally {
        sr = null;
        ssf = null;
        tm = null;
        ctx = null;
    }
}