gpt4 book ai didi

android - 如何在 Android 应用程序中启用 TLS 1.2 支持(在 Android 4.1 JB 上运行)

转载 作者:IT老高 更新时间:2023-10-28 13:21:03 25 4
gpt4 key购买 nike

根据 Android 中的 SSLSocketSSLContext 文档,API 级别 16+ 支持 TLS v1.1 和 v1.2 协议(protocol),但未启用默认。 http://developer.android.com/reference/javax/net/ssl/SSLSocket.html http://developer.android.com/reference/javax/net/ssl/SSLContext.html

如何在运行 Android 4.1 或更高版本(但低于 5.0)的设备上启用它?

我尝试创建一个自定义 SSLSocketFactory,它在创建 Socket 时启用所有支持的协议(protocol),然后将我的自定义实现用作:

HttpsURLConnection.setDefaultSSLSocketFactory(new MySSLSocketFactory());

public class MySSLSocketFactory extends SSLSocketFactory {

private SSLContext sc;
private SSLSocketFactory ssf;

public MySSLSocketFactory() {
try {
sc = SSLContext.getInstance("TLS");
sc.init(null, null, null);
ssf = sc.getSocketFactory();

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose)
throws IOException {
SSLSocket ss = (SSLSocket) ssf.createSocket(s, host, port, autoClose);
ss.setEnabledProtocols(ss.getSupportedProtocols());
ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
return ss;
}

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

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

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
SSLSocket ss = (SSLSocket) ssf.createSocket(host, port);
ss.setEnabledProtocols(ss.getSupportedProtocols());
ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
return ss;
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
SSLSocket ss = (SSLSocket) ssf.createSocket(host, port);
ss.setEnabledProtocols(ss.getSupportedProtocols());
ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
return ss;
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException, UnknownHostException {
SSLSocket ss = (SSLSocket) ssf.createSocket(host, port, localHost, localPort);
ss.setEnabledProtocols(ss.getSupportedProtocols());
ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
return ss;
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
int localPort) throws IOException {
SSLSocket ss = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
ss.setEnabledProtocols(ss.getSupportedProtocols());
ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
return ss;
}
}

但在尝试与启用了仅 TLS 1.2 的服务器建立连接时,它仍然会出现异常。

这是我得到的异常(exception):

03-09 09:21:38.427: W/System.err(2496):javax.net.ssl.SSLHandshakeException:javax.net.ssl.SSLProtocolException: SSL handshake aborted:ssl=0xb7fa0620: Failure in SSL library, usually a protocol error

03-09 09:21:38.427: W/System.err(2496): error:14077410:SSLroutines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure(external/openssl/ssl/s23_clnt.c:741 0xa90e6990:0x00000000)

最佳答案

启用 TLSv1.1 和 TLSv1.2 的两种方法:

  1. 使用此准则: http://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/
  2. 使用这个类 https://github.com/erickok/transdroid/blob/master/app/src/main/java/org/transdroid/daemon/util/TlsSniSocketFactory.java
    schemeRegistry.register(new Scheme("https", new TlsSniSocketFactory(), port));

关于android - 如何在 Android 应用程序中启用 TLS 1.2 支持(在 Android 4.1 JB 上运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28943660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com