2012-05-08 29 views
3

我是Android新手。对于应用程序,我需要在Android上将普通TCP套接字升级到SSL套接字(服务器模式)。以下代码正常工作,但在Android 2.3.3(API 10)上完成Samsung Galaxy S上的SSL握手大约需要12秒。我也使用android 4.0.3(API 15)在模拟器上测试了相同的代码,结果是一样的。客户端是Chrome浏览器。从Wireshark跟踪中,所有SSL握手消息都非常快速地流动,然后在Change Cipher Spec/Encrypted Handshake消息(服务器端SSL握手的最后两条消息)和第一个应用数据(它是来自chrome的HTTP GET)。服务器模式Android上的SSL startHandshake需要12秒完成

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握手?

非常感谢帮助,

/Kaiduan

+0

你为什么将clientMode设置为false?服务器是否设置为clientMode = true?这不是默认设置,很难更改它。 – EJP

+0

SSL服务器握手绝对不应该花那么长时间。 Android测试用例包括运行SSL服务器,这不需要很长时间。你在哪里获得证书?这可能是需要10秒钟;证书生成往往需要很长时间。 –

回答

0

你可以监控设备的负载,看看如果CPU是在那些10秒忙。也可以尝试在不同的设备和/或模拟器上查看是否获得相同的结果。另一种可能性是它在等待DNS,但那不应该那么晚。检查您的数据包捕获的DNS查询。

+0

可能与此http://code.google.com/p/android/issues/detail?id=13117有关 –

相关问题