3

我是安卓新手。对于应用程序,我需要在 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 握手?

非常感谢帮助,

/开端

4

1 回答 1

0

You could monitor load on the device to see if the CPU is busy during those 10 secs. Also try on different devices and/or the emulator to see if you get the same results. Another possibility is that it's waiting on DNS, but that shouldn't happen that late. Check your packet captures for DNS queries.

于 2012-05-09T02:59:17.493 回答