我是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
你爲什麼將clientMode設置爲false?服務器是否設置爲clientMode = true?這不是默認設置,很難更改它。 – EJP
SSL服務器握手絕對不應該花那麼長時間。 Android測試用例包括運行SSL服務器,這不需要很長時間。你在哪裏獲得證書?這可能是需要10秒鐘;證書生成往往需要很長時間。 –