当前位置:   article > 正文

Android5.0以下手机通过https请求服务器报SSLException异常的原因及解决方案

sslexception

问题描述:

    最近突然收到了测试组提的bug,说是部分手机打开APP的时候会报服务器连接异常。然后我就用我的手机试了下,发现没有问题。然后用他们在缺陷描述中写的测试机型和系统版本进行了测试。发现确实会报服务区连接异常。然后通过本地调试,发现网络请求的时候报了如下异常:


该异常为SSL握手失败,但是为什么有的手机可以成功有的手机又失败了呢。然后又试了下其他版本的手机,发现Android5.0以下系统的手机都不通。

问题原因:

首先想到的是后台服务器那边前两天更新了https协议的版本。推测应该是和这个有关。经确认,后台服务器是禁止TLS1.0,TLS1.1和TLS1.2放行。然后查看了一下Google关于SSLEngine的官方文档说明,不同Android版本针对于TLS协议的默认配置图如下:


从上图可以可知:

  • TLSv1.0从API 1+就被默认打开
  • TLSv1.1和TLSv1.2只有在API 20+ 才会被默认打开
  • 也就是说低于API 20+的版本是默认关闭对TLSv1.1和TLSv1.2的支持,若要支持则必须自己打开

这就能解释为什么Android5.0以下系统在进行HTTPS访问时会产生上述异常。

解决方案:

首先我们可以让服务器配置支持TLS1.0、1.1、1.2,这样客户端不需要做任何处理,就可以兼容Android5.0以下的手机了。

但后台那边也是出于安全,才会只放行安全性更高的TLS1.1和TLS1.2协议。所以我们只能在客户端添加新协议的支持。

因此我们需要给低版本的协议添加Cipher suites,我们需要自定义SSLSocketFactory,自定义的SSLSocketFactory如下:

  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.Socket;
  4. import java.net.UnknownHostException;
  5. import java.security.GeneralSecurityException;
  6. import java.util.Arrays;
  7. import java.util.HashSet;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import javax.net.ssl.SSLContext;
  11. import javax.net.ssl.SSLSocket;
  12. import javax.net.ssl.SSLSocketFactory;
  13. import javax.net.ssl.X509TrustManager;
  14. public class SSLSocketFactoryCompat extends SSLSocketFactory {
  15. private SSLSocketFactory defaultFactory;
  16. // Android 5.0+ (API level21) provides reasonable default settings
  17. // but it still allows SSLv3
  18. // https://developer.android.com/about/versions/android-5.0-changes.html#ssl
  19. static String protocols[] = null, cipherSuites[] = null;
  20. static {
  21. try {
  22. SSLSocket socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket();
  23. if (socket != null) {
  24. /* set reasonable protocol versions */
  25. // - enable all supported protocols (enables TLSv1.1 and TLSv1.2 on Android <5.0)
  26. // - remove all SSL versions (especially SSLv3) because they're insecure now
  27. List<String> protocols = new LinkedList<>();
  28. for (String protocol : socket.getSupportedProtocols())
  29. if (!protocol.toUpperCase().contains("SSL"))
  30. protocols.add(protocol);
  31. SSLSocketFactoryCompat.protocols = protocols.toArray(new String[protocols.size()]);
  32. /* set up reasonable cipher suites */
  33. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  34. // choose known secure cipher suites
  35. List<String> allowedCiphers = Arrays.asList(
  36. // TLS 1.2
  37. "TLS_RSA_WITH_AES_256_GCM_SHA384",
  38. "TLS_RSA_WITH_AES_128_GCM_SHA256",
  39. "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
  40. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  41. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  42. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  43. "TLS_ECHDE_RSA_WITH_AES_128_GCM_SHA256",
  44. // maximum interoperability
  45. "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
  46. "TLS_RSA_WITH_AES_128_CBC_SHA",
  47. // additionally
  48. "TLS_RSA_WITH_AES_256_CBC_SHA",
  49. "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
  50. "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  51. "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
  52. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
  53. List<String> availableCiphers = Arrays.asList(socket.getSupportedCipherSuites());
  54. // take all allowed ciphers that are available and put them into preferredCiphers
  55. HashSet<String> preferredCiphers = new HashSet<>(allowedCiphers);
  56. preferredCiphers.retainAll(availableCiphers);
  57. /* For maximum security, preferredCiphers should *replace* enabled ciphers (thus disabling
  58. * ciphers which are enabled by default, but have become unsecure), but I guess for
  59. * the security level of DAVdroid and maximum compatibility, disabling of insecure
  60. * ciphers should be a server-side task */
  61. // add preferred ciphers to enabled ciphers
  62. HashSet<String> enabledCiphers = preferredCiphers;
  63. enabledCiphers.addAll(new HashSet<>(Arrays.asList(socket.getEnabledCipherSuites())));
  64. SSLSocketFactoryCompat.cipherSuites = enabledCiphers.toArray(new String[enabledCiphers.size()]);
  65. }
  66. }
  67. } catch (IOException e) {
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. public SSLSocketFactoryCompat(X509TrustManager tm) {
  72. try {
  73. SSLContext sslContext = SSLContext.getInstance("TLS");
  74. sslContext.init(null, (tm != null) ? new X509TrustManager[] { tm } : null, null);
  75. defaultFactory = sslContext.getSocketFactory();
  76. } catch (GeneralSecurityException e) {
  77. throw new AssertionError(); // The system has no TLS. Just give up.
  78. }
  79. }
  80. private void upgradeTLS(SSLSocket ssl) {
  81. // Android 5.0+ (API level21) provides reasonable default settings
  82. // but it still allows SSLv3
  83. // https://developer.android.com/about/versions/android-5.0-changes.html#ssl
  84. if (protocols != null) {
  85. ssl.setEnabledProtocols(protocols);
  86. }
  87. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && cipherSuites != null) {
  88. ssl.setEnabledCipherSuites(cipherSuites);
  89. }
  90. }
  91. @Override
  92. public String[] getDefaultCipherSuites() {
  93. return cipherSuites;
  94. }
  95. @Override
  96. public String[] getSupportedCipherSuites() {
  97. return cipherSuites;
  98. }
  99. @Override
  100. public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
  101. Socket ssl = defaultFactory.createSocket(s, host, port, autoClose);
  102. if (ssl instanceof SSLSocket)
  103. upgradeTLS((SSLSocket)ssl);
  104. return ssl;
  105. }
  106. @Override
  107. public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
  108. Socket ssl = defaultFactory.createSocket(host, port);
  109. if (ssl instanceof SSLSocket)
  110. upgradeTLS((SSLSocket)ssl);
  111. return ssl;
  112. }
  113. @Override
  114. public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
  115. Socket ssl = defaultFactory.createSocket(host, port, localHost, localPort);
  116. if (ssl instanceof SSLSocket)
  117. upgradeTLS((SSLSocket)ssl);
  118. return ssl;
  119. }
  120. @Override
  121. public Socket createSocket(InetAddress host, int port) throws IOException {
  122. Socket ssl = defaultFactory.createSocket(host, port);
  123. if (ssl instanceof SSLSocket)
  124. upgradeTLS((SSLSocket)ssl);
  125. return ssl;
  126. }
  127. @Override
  128. public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
  129. Socket ssl = defaultFactory.createSocket(address, port, localAddress, localPort);
  130. if (ssl instanceof SSLSocket)
  131. upgradeTLS((SSLSocket)ssl);
  132. return ssl;
  133. }
  134. }

然后只需要给我们的请求设置这个SSLSocketFactory就可以了,我们以okhttp为例:

  1. //定义一个信任所有证书的TrustManager
  2. final X509TrustManager trustAllCert = new X509TrustManager() {
  3. @Override
  4. public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
  5. }
  6. @Override
  7. public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
  8. }
  9. @Override
  10. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  11. return new java.security.cert.X509Certificate[]{};
  12. }
  13. };
  14. //设置OkHttpClient
  15. OkHttpClient client = new OkHttpClient.Builder().
  16. sslSocketFactory(new SSLSocketFactoryCompat(trustAllCert), trustAllCert).build();

设置之后,用低版本手机测试https,就可以测试成功了。

准备最近这几天再写一篇,详细介绍一下HTTPS 和 SSL/TLS 协议。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/650863
推荐阅读
相关标签
  

闽ICP备14008679号