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:org.socialbiz.cog.util.SSLPatch.java

/**
* a call to disableSSLCertValidation will disable certificate validation
* for SSL connection made after this call.   This is installed as the
* default in the JVM for future calls.//  w  w  w.ja  v a  2 s  .  c o  m
*
* Returns the properly initialized SSLContext in case it is needed for
* something else (like Apache HttpClient libraries) but if you don't need
* it you can ignore it.
*/
public static SSLContext disableSSLCertValidation() throws Exception {

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { getDummyTrustManager() };

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

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(getAllHostVerifier());

    return sc;
}

From source file:com.frostwire.http.HttpClient.java

private static SSLSocketFactory buildSSLSocketFactory() {
    try {//  ww  w. ja va 2  s .co m
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[] { new AllX509TrustManager() }, new SecureRandom());
        SSLSocketFactory d = sc.getSocketFactory();
        return new WrapSSLSocketFactory(d);
    } catch (Throwable e) {
        LOG.error("Unable to create custom SSL socket factory", e);
    }

    return null;
}

From source file:com.groupon.odo.tests.HttpUtils.java

public static String doProxyHttpsGet(String url, BasicNameValuePair[] data) throws Exception {
    String fullUrl = url;/*from w w w .ja  v  a2 s .co  m*/

    if (data != null) {
        if (data.length > 0) {
            fullUrl += "?";
        }

        for (BasicNameValuePair bnvp : data) {
            fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&";
        }
    }

    TrustManager[] trustAllCerts = 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) {
        }
    } };

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

    URL uri = new URL(fullUrl);
    int port = Utils.getSystemPort(Constants.SYS_FWD_PORT);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));
    URLConnection connection = uri.openConnection(proxy);

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String accumulator = "";
    String line = "";
    Boolean firstLine = true;
    while ((line = rd.readLine()) != null) {
        accumulator += line;
        if (!firstLine) {
            accumulator += "\n";
        } else {
            firstLine = false;
        }
    }

    return accumulator;
}

From source file:org.appverse.web.framework.backend.ws.helpers.StubHelper.java

public static void configureEndpoint(String endpointPropertiesFile, String timeoutPropertyName,
        ServiceClient _serviceClient) {//ww w .  j av  a 2  s  . co  m
    Properties endpointsProperties = new Properties();
    InputStream endPointsInputStream = StubHelper.class.getResourceAsStream(endpointPropertiesFile);
    try {
        endpointsProperties.load(endPointsInputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String accountTimeoutString = (String) endpointsProperties.get(timeoutPropertyName);
    try {
        long accountTimeout = new Long(accountTimeoutString) * 1000;
        _serviceClient.getOptions().setTimeOutInMilliSeconds(accountTimeout);
    } catch (NumberFormatException e) {
        logger.equals("Error login axis account service timeout");
    }
    String endpointProxyEnabled = (String) endpointsProperties.get("endpoint.proxy.enabled");
    if (endpointProxyEnabled != null && endpointProxyEnabled.equals("true")) {
        HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
        String endpointProxyHost = endpointsProperties.getProperty("endpoint.proxy.host");
        proxyProperties.setProxyName(endpointProxyHost);
        int endpointProxyPort = new Integer(endpointsProperties.getProperty("endpoint.proxy.port"));
        proxyProperties.setProxyPort(endpointProxyPort);
        _serviceClient.getOptions().setProperty(HTTPConstants.PROXY, proxyProperties);
    }
    if (endpointsProperties.getProperty("endpoint.ignore_SSL_errors") != null
            && endpointsProperties.getProperty("endpoint.ignore_SSL_errors").equals("true")) {
        // Create a trust manager that does not validate certificate
        // chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

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

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

        // 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) {
        }
    }

    ConfigurationContext configurationContext = _serviceClient.getServiceContext().getConfigurationContext();
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(50);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
    configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

}

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 ww . 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);
    }
}

From source file:com.bytelightning.opensource.pokerface.HelloWorldScriptTest.java

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    PrevSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    PrevHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();

    proxy = new PokerFace();
    XMLConfiguration conf = new XMLConfiguration();
    conf.load(ProxySpecificTest.class.getResource("/HelloWorldTestConfig.xml"));
    proxy.config(conf);/*from www.  ja v a2s.c  o m*/
    boolean started = proxy.start();
    Assert.assertTrue("Successful proxy start", started);

    SSLContext sc = SSLContext.getInstance("TLS");
    TrustManager[] trustAllCertificates = { new X509TrustAllManager() };
    sc.init(null, trustAllCertificates, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true; // Just allow them all.
        }
    });

}

From source file:org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.client.OAuthRequestInterceptor.java

private static SSLSocketFactory getSimpleTrustedSSLSocketFactory() {
    try {//from ww  w .  jav  a  2s . com
        TrustManager[] trustAllCerts = 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) {
            }
        } };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        return sc.getSocketFactory();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        return null;
    }
}

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;
            }//from www.j  a  v  a2s  .  co 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.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;
            }// ww w .ja  va  2  s. com

            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: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 a  va2 s.  co  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);
}