如何使用OkHttp添加对特定TLSv1.2 CipherSuits的支持--Android 4.4 Kitkat(api 19)

时间:2017-09-10 03:57:58

标签: android tls1.2 okhttp3

我的api仅支持关注CipherSuits(在ssllab的帮助下找到此信息)

TLSv1.2
    TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 - OkHttp: yes
    TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 - OkHttp: yes
    TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 - OkHttp: yes
    TLS_DHE_RAS_WITH_AES_128_GCM_SHA256 - OkHttp: no

所有这些都支持Android api 20+,如SSLSocket

所示

我向OkHttp尝试了adding support for TLSv1.2但是,我仍然得到正常的错误

HTTP FAILED: javax.net.ssl.SSLHandshakeException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: null

然后我将CipherSuits添加到ConnectionSpec并失败了

ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .cipherSuites(
                            TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
                            TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                            TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
                    )
                    .build();

HTTP FAILED: java.net.UnknownServiceException: Unable to find acceptable protocols. isFallback=false, modes=[ConnectionSpec(cipherSuites=[TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_256_GCM_SHA384], tlsVersions=[TLS_1_2], supportsTlsExtensions=true)], supported protocols=[TLSv1.2]

Android api 21及以上版本的连接正常。

那么可以添加对这些CipherSuits的支持吗?

1 个答案:

答案 0 :(得分:0)

将此添加到您的okhttp客户端

public class TLSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory delegate;

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, null, null);
    delegate = context.getSocketFactory();
}

@Override
public String[] getDefaultCipherSuites() {
    return delegate.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
    return delegate.getSupportedCipherSuites();
}

@Override
public Socket createSocket() throws IOException {
    return enableTLSOnSocket(delegate.createSocket());
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
    return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    return enableTLSOnSocket(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
    return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
    return enableTLSOnSocket(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
    return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
}

private Socket enableTLSOnSocket(Socket socket) {
    if(socket != null && (socket instanceof SSLSocket)) {
        ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
    }
    return socket;
}

}
像这样

OkHttpClient client=new OkHttpClient();
try {
    client = new OkHttpClient.Builder()
            .sslSocketFactory(new TLSSocketFactory())
            .build();
} catch (KeyManagementException e) {
    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}