Example usage for javax.net.ssl SSLContext getSocketFactory

List of usage examples for javax.net.ssl SSLContext getSocketFactory

Introduction

In this page you can find the example usage for javax.net.ssl SSLContext getSocketFactory.

Prototype

public final SSLSocketFactory getSocketFactory() 

Source Link

Document

Returns a SocketFactory object for this context.

Usage

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;
            }/*from   w  ww  . j  a  v  a2s  .  com*/
        });
    }

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

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

From source file:org.jumpmind.symmetric.transport.TransportManagerFactory.java

/**
 * Create an SSL Socket Factory that accepts self signed certificates.
 * /* ww w . j av a 2  s.  co  m*/
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 * @throws KeyStoreException
 */
private static SSLSocketFactory createSelfSignedSocketFactory()
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    SSLSocketFactory factory = null;

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, new TrustManager[] { new SelfSignedX509TrustManager(null) }, new SecureRandom());
    factory = context.getSocketFactory();

    return factory;
}

From source file:com.ethercamp.harmony.util.TrustSSL.java

public static void applyAnother() {
    try {//from   w w w .j a v  a2 s .co  m
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                System.out.println("hostname = [" + hostname + "], session = [" + session + "]");
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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.
 * //from   ww w.  j  a  va 2 s  .  c om
 * @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.grantedbyme.example.ServletUtils.java

public static GrantedByMe getSDK(HttpServlet context) throws IOException {
    // read private key
    String privateKey = null;//from www.j  a  va2  s. c  o m
    InputStream privateKeyInputStream = context.getClass().getResourceAsStream("/private_key.pem");
    try {
        privateKey = IOUtils.toString(privateKeyInputStream);
    } finally {
        privateKeyInputStream.close();
    }
    // read server key
    String serverKey = null;
    InputStream serverKeyInputStream = context.getClass().getResourceAsStream("/server_key.pem");
    try {
        serverKey = IOUtils.toString(serverKeyInputStream);
    } finally {
        serverKeyInputStream.close();
    }
    // _log(serverKey);
    // initialize BouncyCastle security provider
    Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 0);
    // create sdk
    GrantedByMe sdk = new GrantedByMe(privateKey, serverKey);
    // 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(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // set SDK parameters
    sdk.apiURL = "https://api-dev.grantedby.me/v1/service/";
    //sdk.isDebug = true;
    return sdk;
}

From source file:sit.web.client.HTTPTrustHelper.java

public static void init(String securityAlgorithm, KeyManager[] kms, TrustManager[] tms) {
    // Install the all-trusting trust manager
    try {/*  ww w. j a  v a2s. c o  m*/
        SSLContext sc = SSLContext.getInstance(securityAlgorithm);
        sc.init(kms, tms, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ex) {
        Logger.getLogger(HttpHelper.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
    }

    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    });
    HttpsURLConnection.setFollowRedirects(true);
}

From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java

/**
 * Open an URL connection. If HTTPS, accepts any certificate even if not
 * valid, and connects to any host name.
 * //from   w ww .  j av  a  2  s. c  om
 * @param url
 *            The destination URL, HTTP or HTTPS.
 * @return The URLConnection.
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public 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.github.opengarageapp.GarageService.java

/**
 * Trust every server - dont check for any certificate
 *///from www  .j  a v a  2 s  .  c  om
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    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 {
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.rhq.plugins.www.util.WWWUtils.java

private static void disableCertificateVerification(HttpsURLConnection connection) {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }/*from   ww  w  . j  a  v  a  2 s .  com*/

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            return;
        }
    } };
    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        connection.setSSLSocketFactory(sslContext.getSocketFactory());
        connection.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession sslSession) {
                return true;
            }
        });
    } catch (Exception e) {
        Log log = LogFactory.getLog(WWWUtils.class);
        log.warn("Failed to disable certificate validation.", e);
    }
}

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;
        //            }
        ///*w w  w  .ja  v a  2 s.  c  om*/
        //            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) {
        ;
    }

}