我是 Android 新手。对于应用程序,我需要在 Android 上将普通 TCP 套接字升级为 SSL 套接字(服务器模式)。以下代码有效,但在使用 Android 2.3.3 (API 10) 的三星 Galaxy S 上完成 SSL 握手大约需要 12 秒。我还在模拟器上用 android 4.0.3 (API 15) 测试了相同的代码,结果是一样的。客户端是 Chrome 浏览器。从 Wireshark 跟踪来看,所有 SSL 握手消息都流动得非常快,然后 Change Cipher Spec/Encrypted Handshake 消息(服务器端 SSL 握手的最后两条消息)和第一个应用程序数据(它是来自 Chrome 的 HTTP GET)。
private boolean handleSSLHandshake() {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(this.kmf.getKeyManagers(), null, null);
SSLSocketFactory factory = sc.getSocketFactory();
this.sslSocket = (SSLSocket) factory.createSocket(this.socket,
this.socket.getInetAddress().getHostAddress(),
this.socket.getPort(),
true);
this.sslSocket.setUseClientMode(false);
this.sslSocket.addHandshakeCompletedListener(this);
this.sslSocket.startHandshake();
Log.d(TAG, "SSL upgrade succeeds!");
this.input = this.sslSocket.getInputStream();
this.output = this.sslSocket.getOutputStream();
} catch (NoSuchAlgorithmException e) {
Log.d(TAG, "Got NoSuchAlgorithmException while upgrading to SSL" + e);
this.sslSocket = null;
return false;
} catch (KeyManagementException e) {
Log.d(TAG, "Got KeyManagementException while upgrading to SSL" + e);
} catch (UnknownHostException e) {
Log.d(TAG, "Got UnknownHostException while upgrading to SSL" + e);
this.sslSocket = null;
return false;
} catch (IOException e) {
Log.d(TAG, "Got IOException while upgrading to SSL" + e);
this.sslSocket = null;
return false;
}
return true;
}
public void handshakeCompleted(HandshakeCompletedEvent event) {
Log.d(TAG, "SSL handshake completed");
}
有没有人知道哪个代码花了 10 秒?如何减少这个?以及如何在 Android 上调试 SSL 握手?
非常感谢您的帮助,
/开端
最佳答案
您可以监控设备上的负载以查看 CPU 在这 10 秒内是否忙碌。也可以尝试不同的设备和/或模拟器,看看是否得到相同的结果。另一种可能性是它正在等待 DNS,但这不应该发生那么晚。检查您的数据包捕获是否有 DNS 查询。
关于android - Android 上的服务器模式 SSL startHandshake 需要 12 秒才能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10505036/