gpt4 book ai didi

java - TLS v1.2 上的 Android 客户端/服务器

转载 作者:太空狗 更新时间:2023-10-29 15:42:11 26 4
gpt4 key购买 nike

我正在尝试在服务器和 Android 客户端之间创建 TLS v1.2 通信。我建立了一个有任何问题的 TLS v1.0 连接,但我无法获得 v1.2。这是服务器代码:

char[] passphrase = "myComplexPass1".toCharArray();
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream("cacerts"), passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keystore, passphrase);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
SSLContext sslContext.init(keyManagers, null, null);
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port);
sslServerSocket.setEnabledProtocols(new String [] { "TLSv1", "TLSv1.1", "TLSv1.2" });
sslServerSocket.setUseClientMode(false);
sslServerSocket.setWantClientAuth(false);
sslServerSocket.setNeedClientAuth(false);
sslSocket = (SSLSocket)sslServerSocket.accept();

虽然这是客户端代码:

char[] passphrase = "myComplexPass1".toCharArray();
KeyStore keystore = KeyStore.getInstance("BKS");
keystore.load(this.getApplicationContext().getResources().openRawResource(R.raw.jb), passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, passphrase);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
Log.d("Context Protocol",sslContext.getProtocol());//this prints correctly TLS v1.2!
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
TrustManager[] trustManagers = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{

}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{

}
}
};
sslContext.init(keyManagers, trustManagers, new SecureRandom());
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) sslContext.getSocketFactory();
SSLSocket skt = (SSLSocket) sslSocketFactory.createSocket(HOST, PORT);
skt.setKeepAlive(true);

客户端代码,在我的电脑上运行在 JRE7 上的 java 客户端中编写,完美运行,我看到 getProtocol(服务器端)TLSv1.2 具有正确的密码,由 tlsv1.2 支持。android 上的相同代码建立 tlsv1.0 连接!我真的不明白。在 Java 客户端 JRE7 上工作,在 android 上仅 tlsv1.0有什么建议吗?

这是我的第一个问题,我搜索了很多。可能我的格式不正确:(

最佳答案

回答这个问题有点晚了,但也许其他人需要答案。

我遇到了同样的问题。无论您是否为 SSLContext.init() 方法提供 TLSv1.2,我尝试过的某些 Android 版本都不会启用 TLS 1.2。您必须使用 setEnabledProtocols() 在您的客户端套接字上启用它,就像您对服务器套接字所做的那样。对我来说,我是在我创建的自定义 SSLSocketFactory 中执行此操作的:

public class MySSLSocketFactory extends SSLSocketFactory
throws NoSuchAlgorithmException {

private SSLContext mSSLContext;

public MySSLSocketFactory(KeyManager km) {
...
mSSLContext = SSLContext.getInstance("TLSv1.2");
...
mSSLContext.init(new KeyManager[] {km}, null, null);
...
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException {
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(socket, host, port, autoClose);
s.setEnabledProtocols(new String[] {"TLSv1.2"} );
return s;
}

...
}

关于java - TLS v1.2 上的 Android 客户端/服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531807/

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