diff --git a/okhttp-tests/src/test/java/okhttp3/DelegatingSSLSocketFactory.java b/okhttp-tests/src/test/java/okhttp3/DelegatingSSLSocketFactory.java index 00a968e7b..5a14d0fbd 100644 --- a/okhttp-tests/src/test/java/okhttp3/DelegatingSSLSocketFactory.java +++ b/okhttp-tests/src/test/java/okhttp3/DelegatingSSLSocketFactory.java @@ -34,51 +34,43 @@ public class DelegatingSSLSocketFactory extends SSLSocketFactory { this.delegate = delegate; } - @Override - public SSLSocket createSocket() throws IOException { + @Override public SSLSocket createSocket() throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(); return configureSocket(sslSocket); } - @Override - public SSLSocket createSocket(String host, int port) throws IOException, UnknownHostException { + @Override public SSLSocket createSocket(String host, int port) throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port); return configureSocket(sslSocket); } - @Override - public SSLSocket createSocket(String host, int port, InetAddress localAddress, int localPort) - throws IOException, UnknownHostException { + @Override public SSLSocket createSocket( + String host, int port, InetAddress localAddress, int localPort) throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port, localAddress, localPort); return configureSocket(sslSocket); } - @Override - public SSLSocket createSocket(InetAddress host, int port) throws IOException { + @Override public SSLSocket createSocket(InetAddress host, int port) throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port); return configureSocket(sslSocket); } - @Override - public SSLSocket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort) - throws IOException { + @Override public SSLSocket createSocket( + InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port, localAddress, localPort); return configureSocket(sslSocket); } - @Override - public String[] getDefaultCipherSuites() { + @Override public String[] getDefaultCipherSuites() { return delegate.getDefaultCipherSuites(); } - @Override - public String[] getSupportedCipherSuites() { + @Override public String[] getSupportedCipherSuites() { return delegate.getSupportedCipherSuites(); } - @Override - public SSLSocket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException { + @Override public SSLSocket createSocket( + Socket socket, String host, int port, boolean autoClose) throws IOException { SSLSocket sslSocket = (SSLSocket) delegate.createSocket(socket, host, port, autoClose); return configureSocket(sslSocket); } diff --git a/okhttp/src/main/java/okhttp3/OkHttpClient.java b/okhttp/src/main/java/okhttp3/OkHttpClient.java index 43a2a7906..bcbf14d25 100644 --- a/okhttp/src/main/java/okhttp3/OkHttpClient.java +++ b/okhttp/src/main/java/okhttp3/OkHttpClient.java @@ -28,8 +28,10 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.X509TrustManager; import okhttp3.internal.Internal; import okhttp3.internal.InternalCache; +import okhttp3.internal.Platform; import okhttp3.internal.RouteDatabase; import okhttp3.internal.Util; import okhttp3.internal.http.StreamAllocation; @@ -130,6 +132,7 @@ public final class OkHttpClient implements Cloneable, Call.Factory { final InternalCache internalCache; final SocketFactory socketFactory; final SSLSocketFactory sslSocketFactory; + final X509TrustManager trustManager; final HostnameVerifier hostnameVerifier; final CertificatePinner certificatePinner; final Authenticator proxyAuthenticator; @@ -160,7 +163,7 @@ public final class OkHttpClient implements Cloneable, Call.Factory { this.internalCache = builder.internalCache; this.socketFactory = builder.socketFactory; - boolean isTLS = true; + boolean isTLS = false; for (ConnectionSpec spec : connectionSpecs) { isTLS = isTLS || spec.isTls(); } @@ -176,6 +179,16 @@ public final class OkHttpClient implements Cloneable, Call.Factory { throw new AssertionError(); // The system has no TLS. Just give up. } } + if (this.sslSocketFactory != null) { + this.trustManager = Platform.get().trustManager(sslSocketFactory); + if (trustManager == null) { + throw new IllegalStateException("Unable to extract the trust manager on " + Platform.get() + + ", sslSocketFactory is " + sslSocketFactory.getClass()); + } + } else { + this.trustManager = null; + } + this.hostnameVerifier = builder.hostnameVerifier; this.certificatePinner = builder.certificatePinner; this.proxyAuthenticator = builder.proxyAuthenticator; diff --git a/okhttp/src/main/java/okhttp3/internal/Platform.java b/okhttp/src/main/java/okhttp3/internal/Platform.java index 9e5e5545d..bf5144155 100644 --- a/okhttp/src/main/java/okhttp3/internal/Platform.java +++ b/okhttp/src/main/java/okhttp3/internal/Platform.java @@ -18,6 +18,7 @@ package okhttp3.internal; import android.util.Log; import java.io.IOException; +import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -29,6 +30,8 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.X509TrustManager; import okhttp3.Protocol; import okio.Buffer; @@ -55,6 +58,11 @@ import static okhttp3.internal.Internal.logger; * unstable. * * Supported on OpenJDK 7 and 8 (via the JettyALPN-boot library). + * + *
Supported on Android 2.3+ and OpenJDK 7+. There are no public APIs to recover the trust
+ * manager that was used to create an {@link SSLSocketFactory}.
*/
public class Platform {
private static final Platform PLATFORM = findPlatform();
@@ -78,6 +86,10 @@ public class Platform {
public void untagSocket(Socket socket) throws SocketException {
}
+ public X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) {
+ return null;
+ }
+
/**
* Configure TLS extensions on {@code sslSocket} for {@code route}.
*
@@ -112,11 +124,13 @@ public class Platform {
private static Platform findPlatform() {
// Attempt to find Android 2.3+ APIs.
try {
+ Class> sslParametersClass;
try {
- Class.forName("com.android.org.conscrypt.OpenSSLSocketImpl");
+ sslParametersClass = Class.forName("com.android.org.conscrypt.SSLParametersImpl");
} catch (ClassNotFoundException e) {
// Older platform before being unbundled.
- Class.forName("org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl");
+ sslParametersClass = Class.forName(
+ "org.apache.harmony.xnet.provider.jsse.SSLParametersImpl");
}
OptionalMethod