4

Is there a way make the SSLSocket wait until the handshake is completed without using the addHandshakeListener() method? I would like it to block until it has completed the handshake.

Edit: A little bit more clarification: a regular (Non-SSL) Socket is initially used (this is required, because a package must be sent to the server before the actual handshake takes place). This socket is "converted" into a SSLSocket using SSlSocketFactory.createSocket. My question is that whether there is a way to now synchronously do the handshake.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • Maybe this will help you: http://stackoverflow.com/questions/3246792/how-to-enable-logging-for-apache-commons-httpclient-on-android – pawelzieba Oct 18 '12 at 14:36

1 Answers1

7

SSLSocket.startHandshake() is a synchronous call when you start handshake between the peers very first time, it will block the calling thread until handshake has completed:

... ...

// initial handshake between peers, don't need Listener:
sslSocket.startHandshake(); // <- this will block the calling thread.
// handshake is completed at this point

... ...

This is not fully documented in Android API, see original Java API for the complete javadoc:

Starts an SSL handshake on this connection. Common reasons include a need to use new encryption keys, to change cipher suites, or to initiate a new session. To force complete reauthentication, the current session could be invalidated before starting this handshake.

If data has already been sent on the connection, it continues to flow during this handshake. When the handshake completes, this will be signaled with an event. This method is synchronous for the initial handshake on a connection and returns when the negotiated handshake is complete. Some protocols may not support multiple handshakes on an existing socket and may throw an IOException.

The situation becomes pretty complicated if you manipulate handshake multiple times between the peers, it could be an asynchronous call in some cases (when you need Listener), check out this online document Fundamental Networking in Java for more details.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • @UkuLoskit, I have never used startHandshake() API in Android project, however, according to the javadoc, as long as it is a first time handshake, startHandshake() is involved synchronously. Note that Android start restricting network operation on UI thread since API Level 11, so be careful when using this network API in your Android app and make sure your code works across different API level. – yorkw Oct 30 '12 at 22:15
  • @UkuLoskit, you don't even need to use `startHandshake()`, just start reading/writing with the I/O streams from your SSLSocket, this will initiate the handshake, and it won't happen before it has completed successfully. – Bruno Oct 31 '12 at 01:36
  • Ok, my bad. I read the doc, but the way my application functioned lead me to erroneously believe that it did not block. I will award you the bounty in two hours (cannot do this now due to limitations). – Uku Loskit Oct 31 '12 at 11:15