OkHttp SSLHandshakeException SSL握手中止SSL库中的失败,协议错误

Ank*_*kur 7 android retrofit okhttp retrofit2 okhttp3

04-23 17:17:38.434 21599-21956/ D/NativeCrypto: ssl=0x0 NativeCrypto_SSL_interrupt
04-23 17:17:38.435 21599-21956/ D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x635d8808: Failure in SSL library, usually a protocol error
    error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x5e6c46fd:0x00000000)
Run Code Online (Sandbox Code Playgroud)

Android较低版本的设备(4.1 - 4.4)给出了SSL错误.以前版本适用于以下版本:

implementation 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
Run Code Online (Sandbox Code Playgroud)

但升级这些库后,事情就会发生变化 每个服务调用都会提供SSL握手异常.

implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'
Run Code Online (Sandbox Code Playgroud)

此外,如果我将这些库降级到以前的版本,它仍然无法正常工作.但git checkout到上一次提交工作正常.懵懵懂懂.

Ank*_*kur 14

所以我通过将以下内容添加到我的http客户端对象来解决它

 ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
            .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
            .cipherSuites(
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA)
            .build();

httpClient.connectionSpecs(Collections.singletonList(spec))
Run Code Online (Sandbox Code Playgroud)

参考:https://github.com/square/okhttp/issues/3894

  • 要知道服务器支持哪些TLS版本和密码套件,请先通过任何SSL分析器(即https://sslanalyzer.comodoca.com)进行分析,然后相应地更新ConnectionSpec代码。 (2认同)

Ste*_*ley 7

我在升级到 OkHttp 4.x 时遇到了这个问题。不必像Anker 推荐的那样跟踪所有已知的 TLS 版本和所有已知的密码,而是使用 OkHttp 的allEnabledTlsVersionsallEnabledCipherSuites方法:

val builder = OkHttpClient.Builder()
…
// The default OkHttp configuration does not support older versions of TLS,
// or all cipher suites.  Make our support as reasonably broad as possible.
builder.connectionSpecs(listOf(ConnectionSpec.CLEARTEXT,
    ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
        .allEnabledTlsVersions()
        .allEnabledCipherSuites()
        .build()))
…
val okHttpClient = builder.build()
Run Code Online (Sandbox Code Playgroud)

只要您定期升级 OkHttp,这些列表就会保持最新。来自ConnectionSpec API 文档

使用 Builder.allEnabledTlsVersions 和 Builder.allEnabledCipherSuites 将所有功能选择推迟到底层 SSL 套接字。

每个规范的配置随着每个 OkHttp 版本而变化。这很烦人:升级 OkHttp 库会破坏与某些 Web 服务器的连接!但这是一个必要的烦恼,因为 TLS 生态系统是动态的,保持最新状态对于保持安全是必要的。请参阅 OkHttp 的 TLS 配置历史记录以跟踪这些更改。