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.cloudhopper.httpclient.util.SchemeFactory.java

static public Scheme createDoNotVerifyHttpsScheme() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager sslTrustManager = new DoNotVerifySSLCertificateTrustManager();
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { sslTrustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return new Scheme("https", sf, 443);
}

From source file:com.datasingularity.http.asyncget.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from ww w  .  ja va  2  s  . c  o  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new VeryTrustingTrustManager() }, new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.bobarctor.Rm3Wifi.utils.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from  www .  j a  v a2 s .com*/
        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:net.sf.jsignpdf.ssl.SSLInitializer.java

/**
 * @param options/*w w  w . j  a v a  2  s. c o m*/
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws CertificateException
 * @throws KeyStoreException
 * @throws KeyManagementException
 * @throws UnrecoverableKeyException
 */
public static void init(BasicSignerOptions options) throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException, CertificateException, IOException, UnrecoverableKeyException {
    KeyManager[] km = null;
    if (options != null && options.getTsaServerAuthn() == ServerAuthentication.CERTIFICATE) {
        char[] pwd = null;
        if (StringUtils.isNotEmpty(options.getTsaCertFilePwd())) {
            pwd = options.getTsaCertFilePwd().toCharArray();
        }
        LOGGER.info(Constants.RES.get("ssl.keymanager.init", options.getTsaCertFile()));
        final String ksType = StringUtils.defaultIfBlank(options.getTsaCertFileType(), "PKCS12");
        KeyStore keyStore = KeyStoreUtils.loadKeyStore(ksType, options.getTsaCertFile(), pwd);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, pwd);
        km = keyManagerFactory.getKeyManagers();
    }
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(km, TRUST_MANAGERS, null);

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}

From source file:org.eclipse.lyo.client.oauth.sample.OAuthClient.java

private static void disableCertificateValidatation(HttpClient client) {
    try {// w ww .  j  a  v  a 2  s.  co m
        final SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

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

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } }, new java.security.SecureRandom());
        final SSLSocketFactory socketFactory = new SSLSocketFactory(sc, new X509HostnameVerifier() {
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        });
        final Scheme https = new Scheme("https", 443, socketFactory);
        client.getConnectionManager().getSchemeRegistry().register(https);
    } catch (GeneralSecurityException e) {
    }
}

From source file:com.dvdprime.android.app.http.CustomSSLSocketFactory.java

/**
 * Creates a new CustomSSLSocket object.
 * //from  ww w. j  a  v  a  2  s  . co  m
 * @return the SSL context
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws KeyManagementException
 *             the key management exception
 * @throws KeyStoreException
 *             the key store exception
 */
private static SSLContext createSSLContext()
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    SSLContext context = SSLContext.getInstance(SSL_PROTOCOL_NAME);
    context.init(null, new TrustManager[] { new CustomX509TrustManager(null) }, null);
    return context;
}

From source file:com.jiubang.core.util.HttpUtils.java

/**
 * Open an URL connection. If HTTPS, accepts any certificate even if not
 * valid, and connects to any host name.
 * /*w w w.  j a  va  2 s  .  com*/
 * @param url
 *            The destination URL, HTTP or HTTPS.
 * @return The URLConnection.
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
private static URLConnection getConnection(URL url)
        throws IOException, NoSuchAlgorithmException, KeyManagementException {
    URLConnection conn = url.openConnection();
    if (conn instanceof HttpsURLConnection) {
        // Trust all certificates
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(new KeyManager[0], TRUST_MANAGER, new SecureRandom());
        SSLSocketFactory socketFactory = context.getSocketFactory();
        ((HttpsURLConnection) conn).setSSLSocketFactory(socketFactory);

        // Allow all hostnames
        ((HttpsURLConnection) conn).setHostnameVerifier(HOSTNAME_VERIFIER);

    }
    conn.setConnectTimeout(SOCKET_TIMEOUT);
    conn.setReadTimeout(SOCKET_TIMEOUT);
    return conn;
}

From source file:com.aliyun.oss.common.comm.HttpClientFactory.java

private static SSLSocketFactory getSSLSocketFactory() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from  w ww . j ava 2  s .c om*/

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

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

    try {
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, trustAllCerts, null);
        SSLSocketFactory ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return ssf;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.jsignpdf.ssl.SSLInitializer.java

public static final void init() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
        CertificateException, IOException {
    if (Constants.RELAX_SSL_SECURITY) {
        LOGGER.debug("Relaxing SSL security.");

        //Details for the properties - http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html
        //Workaround for http://sourceforge.net/tracker/?func=detail&atid=1037906&aid=3491269&group_id=216921
        System.setProperty("jsse.enableSNIExtension", "false");

        //just in case...
        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
        System.setProperty("sun.security.ssl.allowLegacyHelloMessages", "true");

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }/*  ww  w .j a v a 2s. c o m*/
        });
    }

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

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}

From source file:com.github.droidfu.http.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from  ww w.java2s  .  co  m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, null, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}