Gemäß den Dokumenten in Android für SSLSocket
und SSLContext
werden TLS v1.1 und v1.2 Protokolle in API-Ebene 16+ unterstützt, sind aber nicht standardmäßig aktiviert. http://developer.android.com/reference/javax/net/ssl/SSLSocket.html http://developer.android.com/reference/javax/net/ssl/SSLContext.htmlSo aktivieren Sie TLS 1.2-Unterstützung in einer Android-Anwendung (unter Android 4.1 JB)
Wie aktiviere ich es auf einem Gerät mit Android 4.1 oder höher (aber unter 5,0)?
Ich habe versucht, eine benutzerdefinierte SSLSocketFactory zu schaffen, die die unterstützten alle Protokolle ermöglicht, wenn Socket
‚s erstellt und später meine benutzerdefinierte Implementierung verwenden wie:
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;
}
}
Aber es immer noch eine Ausnahme gibt, während Sie versuchen, Stellen Sie eine Verbindung mit einem Server her, auf dem Nur TLS 1.2 aktiviert ist. Hier
ist die Ausnahme, die ich erhalten:
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:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0xa90e6990:0x00000000)
In meinen Tests fand ich, dass während TLSv1. 2 ist verfügbar und kann auf API 16-18 aktiviert werden. Es gibt immer noch Probleme mit bestimmten Anwendungen, z. B. das Bereitstellen von Clientzertifikaten, auch nach dem Einbeziehen von Play Services. Keine Lösung noch. – hooby3dfx
Was das noch verwirrender macht ist, dass SSLEngine die Unterstützung für TLS1.1/1.2 nur in API20 + angibt. http://developer.android.com/reference/javax/net/ssl/SSLEngine.html –