diff --git a/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.java b/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.java index a6f7242ab..417e94d2b 100644 --- a/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.java +++ b/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.java @@ -23,12 +23,14 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.net.Socket; +import java.security.NoSuchAlgorithmException; import java.security.Security; import java.security.cert.Certificate; import java.security.cert.TrustAnchor; import java.security.cert.X509Certificate; import java.util.List; import javax.annotation.Nullable; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; @@ -427,4 +429,20 @@ class AndroidPlatform extends Platform { return trustManager.hashCode() + 31 * findByIssuerAndSignatureMethod.hashCode(); } } + + @Override public SSLContext getSSLContext() { + if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) { + try { + return SSLContext.getInstance("TLSv1.2"); + } catch (NoSuchAlgorithmException e) { + // fallback to TLS + } + } + + try { + return SSLContext.getInstance("TLS"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("No TLS provider", e); + } + } } diff --git a/okhttp/src/main/java/okhttp3/internal/platform/Platform.java b/okhttp/src/main/java/okhttp3/internal/platform/Platform.java index 1a5311f63..480335913 100644 --- a/okhttp/src/main/java/okhttp3/internal/platform/Platform.java +++ b/okhttp/src/main/java/okhttp3/internal/platform/Platform.java @@ -124,8 +124,8 @@ public class Platform { return null; } - public void connectSocket(Socket socket, InetSocketAddress address, - int connectTimeout) throws IOException { + public void connectSocket(Socket socket, InetSocketAddress address, int connectTimeout) + throws IOException { socket.connect(address, connectTimeout); } @@ -176,8 +176,10 @@ public class Platform { X509TrustManager trustManager = trustManager(sslSocketFactory); if (trustManager == null) { - throw new IllegalStateException("Unable to extract the trust manager on " + Platform.get() - + ", sslSocketFactory is " + sslSocketFactory.getClass()); + throw new IllegalStateException("Unable to extract the trust manager on " + + Platform.get() + + ", sslSocketFactory is " + + sslSocketFactory.getClass()); } return buildCertificateChainCleaner(trustManager); @@ -265,6 +267,16 @@ public class Platform { } public SSLContext getSSLContext() { + String jvmVersion = System.getProperty("java.specification.version"); + if ("1.7".equals(jvmVersion)) { + try { + // JDK 1.7 (public version) only support > TLSv1 with named protocols + return SSLContext.getInstance("TLSv1.2"); + } catch (NoSuchAlgorithmException e) { + // fallback to TLS + } + } + try { return SSLContext.getInstance("TLS"); } catch (NoSuchAlgorithmException e) {