android - Android 上的服务器模式 SSL startHandshake 需要 12 秒才能完成

标签 android ssl

我是 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/

相关文章:

android - 读取系统跟踪日志

android - 连接到特定的 wifi 有时会在 android 上失败

java - 我如何验证我的字符串(括号)- Android?

android - 如何在 Android Studio 中启用 Java 8 语言特性

python - 无法在 Ubuntu 14.04 上升级 pip 1.5.4 - InsecurePlatformWarning : A true SSLContext object is not available

android - 在带有标题的图像列表中显示图形而不是字符串

java - RestEasy 的客户端实现导致 javax.net.ssl.SSLException : hostname in certificate didn't match

ssl - 结合 Wireshark 使用 Burp Suite 代理嗅探 https/SSL 流量

ssl - 读取 X.509 key 或证书文件时出错 : Error in parsing

ASP.NET 启用点符号 web.config