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:com.codedx.burp.security.SSLConnectionSocketFactoryFactory.java

/**
 * Creates a new SSLConnectionSocketFactory with the behavior described in
 * {@link #getFactory(String)}. Instead of returning, this method registers
 * the factory instance to the <code>factoriesByHost<code> map, as well as
 * registering its <code>ExtraCertManager</code> to the
 * <code>certManagersByHost</code> map. The cert manager registration is
 * important in order to detect and purge trusted certificates on a per-host
 * basis.//from   w  w w.ja  v  a  2 s  . c om
 * 
 * @param host
 * @param burpExtender
 * @throws IOException
 * @throws GeneralSecurityException
 */
private static void initializeFactory(String host, BurpExtender burpExtender)
        throws IOException, GeneralSecurityException {
    // set up the certificate management
    File managedKeyStoreFile = getTrustStoreForHost(host);
    ExtraCertManager certManager = new SingleExtraCertManager(managedKeyStoreFile, "u9lwIfUpaN");

    // get the default hostname verifier that gets used by the modified one
    // and the invalid cert dialog
    HostnameVerifier defaultHostnameVerifier = new DefaultHostnameVerifier();

    InvalidCertificateStrategy invalidCertStrat = new InvalidCertificateDialogStrategy(defaultHostnameVerifier,
            host, burpExtender);

    /*
     * Set up a composite trust manager that uses the default trust manager
     * before delegating to the "reloadable" trust manager that allows users
     * to accept invalid certificates.
     */
    List<X509TrustManager> trustManagersForComposite = new LinkedList<>();
    X509TrustManager systemTrustManager = getDefaultTrustManager();
    ReloadableX509TrustManager customTrustManager = new ReloadableX509TrustManager(certManager,
            invalidCertStrat);
    trustManagersForComposite.add(systemTrustManager);
    trustManagersForComposite.add(customTrustManager);
    X509TrustManager trustManager = new CompositeX509TrustManager(trustManagersForComposite);

    // setup the SSLContext using the custom trust manager
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { trustManager }, null);
    // the actual hostname verifier that will be used with the socket
    // factory
    Set<String> allowedHosts = new HashSet<>();
    allowedHosts.add(host);
    HostnameVerifier modifiedHostnameVerifier = new HostnameVerifierWithExceptions(defaultHostnameVerifier,
            allowedHosts);

    SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext, modifiedHostnameVerifier);
    // Register the `factory` and the `customTrustManager` under the given
    // `host`
    factoriesByHost.put(host, factory);
    customTrustByHost.put(host, customTrustManager);
}

From source file:com.mani.fileupload.http.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from  w  w  w .j a v  a 2 s  .  c om*/

        // Client should send the valid key to Server 
        InputStream clientStream = null;
        char[] password = null;

        clientStream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.client);
        password = "fileupload".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // CA key obtained from server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.ca);

        try {
            trustStore.load(instream, "casecret".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

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

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

From source file:com.ycj.android.common.utils.OtherUtils.java

public static void trustAllSSLForHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (trustAllCerts == null) {
        trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }//w w w. j a va2  s.c o  m

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }};
    }
    // Install the all-trusting trust manager
    final SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
    }
    HttpsURLConnection.setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}

From source file:com.ephesoft.dcma.batch.service.EphesoftSSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {//from w  w  w. j av a 2  s. c om
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        return context;
    } catch (final Exception exception) {
        LOGGER.error(exception.getMessage(), exception);
        throw new HttpClientError(exception.toString());
    }
}

From source file:com.snaker.ssl.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from   w  w  w  . j  a  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) {
        LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.dongfang.utils.OtherUtils.java

public static void trustAllSSLForHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (trustAllCerts == null) {
        trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }/*  w  ww  . j av a  2s.c o  m*/

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
    }
    // Install the all-trusting trust manager
    final SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (Throwable e) {
        ULog.e(e.getMessage(), e);
    }
    HttpsURLConnection
            .setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}

From source file:com.owncloud.android.authenticator.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() {
    Log.d(TAG, "Creating Easy SSL Context");
    try {/* www . jav a2s  . c o m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (Exception er) {
        Log.e(TAG, er.getMessage() + "");
        throw new HttpClientError(er.toString());
    }
}

From source file:com.cellobject.oikos.util.NetworkHelper.java

/**
 * Trust every server - don't check for any certificate.
 *///from  w w w . j  a  va  2  s .c o  m
public static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }

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

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                throws CertificateException {
        }
    } };
    // Install the all-trusting trust manager
    try {
        final SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

From source file:com.predic8.membrane.test.AssertUtils.java

public static void trustAnyHTTPSServer(int port) throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext context = SSLContext.getInstance("SSL");
    context.init(null, new TrustManager[] { new X509TrustManager() {
        @Override/*  w  w w  .j  ava  2 s .c  om*/
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }
    } }, new SecureRandom());

    SSLSocketFactory sslsf = new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme scheme = new Scheme("https", port, sslsf);
    if (hc == null)
        hc = new DefaultHttpClient();
    hc.getConnectionManager().getSchemeRegistry().register(scheme);
}

From source file:cn.com.infohold.p2papp.common.gate.OtherUtils.java

public static void trustAllSSLForHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (trustAllCerts == null) {
        trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }/* w w w  . j av a 2s. c om*/

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
    }
    // Install the all-trusting trust manager
    final SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
    }
    HttpsURLConnection
            .setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}