For testing purposes, I am trying to add a factory socket to my okHttp client, which trusts everyone while the proxy server is installed. This has been done many times, but my implementation of a trusted factory socket seems to be missing something:
class TrustEveryoneManager implements 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 null; } } OkHttpClient client = new OkHttpClient(); final InetAddress ipAddress = InetAddress.getByName("XX.XXX.XXX.XXX");
No requests are sent from my application, and no exceptions are logged, so it seems that it does not work in okHttp mode. Upon further investigation, it seems that an exception occurs in okHttp Connection.upgradeToTls() when a handshake is forced. An exception I get: javax.net.ssl.SSLException: SSL handshake terminated: ssl=0x74b522b0: SSL_ERROR_ZERO_RETURN occurred. You should never see this. javax.net.ssl.SSLException: SSL handshake terminated: ssl=0x74b522b0: SSL_ERROR_ZERO_RETURN occurred. You should never see this.
The following code creates an SSLContext that works like a charm when creating an SSLSocketFactory that does not throw any exceptions:
protected SSLContext getTrustingSslContext() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { final SSLContextBuilder trustingSSLContextBuilder = SSLContexts.custom() .loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true;
The problem is that I am trying to completely remove all the Apache HttpClient dependencies from my application. The main Apache HttpClient code for creating an SSLContext seems simple enough, but I obviously missed something because I cannot configure my SSLContext to match this.
Can anyone create an SSLContext implementation that does what I want without using Apache HttpClient?
seato Aug 26 '14 at 15:10 2014-08-26 15:10
source share