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.test.controller.ResourceController.java

private static void trustAllHttpsCertificates() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        //            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        //                return null;
        //            }
        ///*from ww w .  jav a2  s  . c o m*/
        //            public void checkClientTrusted(X509Certificate[] certs, String authType) {
        //            }
        //
        //            public void checkServerTrusted(X509Certificate[] certs, String authType) {
        //            }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            //                return new java.security.cert.X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
            return null;
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        ;
    }

}

From source file:eu.alefzero.owncloud.authenticator.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from  w ww .  j av a 2s .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:de.thingweb.client.security.Security4NicePlugfest.java

public Security4NicePlugfest() {
    // Install the all-trusting trust manager
    // TODO setup trust-manager properly (not meant for production)
    try {//from www  . j ava  2  s  .co m
        SSLContext sc = SSLContext.getInstance("SSL");
        TrustManager[] trustAllCerts = new TrustManager[] { new AllTrustingX509TrustManager() };
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (GeneralSecurityException e) {
        log.error(e.getMessage());
    }
}

From source file:com.owera.xaps.web.app.page.monitor.EasySSLProtocolSocketFactory.java

/**
 * Creates a new EasySSLProtocolSocket object.
 *
 * @return the SSL context/*from w w w.  j  a v a2  s.  c  om*/
 */
private static SSLContext createEasySSLContext() {
    try {
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        throw new HttpClientError(e.toString());
    }
}

From source file:com.app.mvc.http.ext.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from w  ww  . j  a  v a  2 s . c o m*/
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } }, null);
        return context;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.qpark.eip.core.spring.security.https.EipHttpsClientHttpRequestFactory.java

/**
 * @see org.springframework.http.client.SimpleClientHttpRequestFactory#prepareConnection(java.net.HttpURLConnection,
 *      java.lang.String)//from   w w  w  . ja v  a2  s .  c om
 */
@Override
protected void prepareConnection(final HttpURLConnection connection, final String httpMethod) {
    try {
        /* Setup HttpsURLConnection. */
        if (HttpsURLConnection.class.isInstance(connection)) {
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
            httpsConnection.setHostnameVerifier(this.x509TrustManager);
            TrustManager[] trustManagers = new TrustManager[] { this.x509TrustManager };
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustManagers, new java.security.SecureRandom());
            ((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
        }
        super.prepareConnection(connection, httpMethod);
        /* Setup the basic Authentication. */
        if (HttpURLConnection.class.isInstance(connection) && this.userName != null) {
            HttpURLConnection httpsConnection = connection;
            httpsConnection.setRequestProperty("Authorization",
                    new StringBuffer(128).append("Basic ").append(this.base64UserNamePassword).toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.qingstor.sdk.request.QSOkHttpRequestClient.java

private static OkHttpClient getUnsafeOkHttpClient() {
    try {/*  ww w  .  java 2s . c o  m*/
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(QSConstant.HTTPCLIENT_CONNECTION_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(QSConstant.HTTPCLIENT_READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(QSConstant.HTTPCLIENT_WRITE_TIME_OUT, TimeUnit.SECONDS);
        builder.sslSocketFactory(sslSocketFactory);
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:com.sun.socialsite.util.LeniantSSLProtocolSocketFactory.java

private static SSLContext createLeniantSSLContext() {
    try {/*from  w w  w .  j av a2  s  .  c  o  m*/
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new LeniantX509TrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:securitytools.common.http.TrustingSSLConnectionSocketFactory.java

private SSLContext getSSLContext() throws IOException {
    if (sslContext == null) {
        try {//  w  ww .  j  a v a2  s . c o m
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { new TrustingX509TrustManager() }, null);
        } catch (NoSuchAlgorithmException nsae) {
            throw new IOException(nsae.getMessage(), nsae);
        } catch (KeyManagementException kme) {
            throw new IOException(kme.getMessage(), kme);
        }
    }
    return sslContext;
}

From source file:org.thoughtcrime.ssl.pinning.util.PinningHelper.java

/**
 * Constructs an HttpsURLConnection that will validate HTTPS connections against a set of
 * specified pins.//from   w  w  w . j a v a2  s  .  c o m
 *
 * @param pins An array of encoded pins to match a seen certificate
 *             chain against. A pin is a hex-encoded hash of a X.509 certificate's
 *             SubjectPublicKeyInfo. A pin can be generated using the provided pin.py
 *             script: python ./tools/pin.py certificate_file.pem
 *
 */

public static HttpsURLConnection getPinnedHttpsURLConnection(Context context, String[] pins, URL url)
        throws IOException {
    try {
        if (!url.getProtocol().equals("https")) {
            throw new IllegalArgumentException("Attempt to construct pinned non-https connection!");
        }

        TrustManager[] trustManagers = new TrustManager[1];
        trustManagers[0] = new PinningTrustManager(SystemKeyStore.getInstance(context), pins, 0);

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

        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());

        return urlConnection;
    } catch (NoSuchAlgorithmException nsae) {
        throw new AssertionError(nsae);
    } catch (KeyManagementException e) {
        throw new AssertionError(e);
    }
}