According to the documentation on SSLSocket:
You may register to receive event notification of handshake
completion.
Why would you not register for this? Does the fact that SSLSocket.startHandshake() succeeds without an SSLException occurring ensure that certificates are trusted? Or do you get some extra level of assurance by waiting for the handshake to complete?
There are 3 conditions to start the handshake on an
SSLSocket:Any handshake failure will generate an exception (including when the certificate isn’t trusted):
Often, calling
startHandshake()explicitly when establishing the connection is unnecessary, since the handshake is initiated when you start reading from the inputstream (or writing to the outputstream). Any failure there would cause an exception and stop the normal control flow. You don’t need to register explicitly to capture the completion of the handshake in those cases: if you can read/write from the streams, it’s done.The notification of handshake completion is mostly useful if you’re a server asking for re-negotiation (by calling
startHandshake()after some application data has been exchanged). In this case, you may want to wait for that handshake to have completed before proceeding. For example, if the server requests a client-certificate after receiving an HTTP request for a particular path, it may want to wait for the handshake to complete successfully to be able to authorise the client-certificate, before sending the response. This is becausestartHandshake()doesn’t stop the flow of application data: