Feature request: read the server certificate from a TLS rawSocket

Request: expose the certificate the server presented on a TLS interfaces.rawSocket connection (signature for illustration):

String getPeerCertificate()    // PEM of the server's leaf certificate, null before the handshake completes

Use case: bridge-free Google TV / Android TV control. The TV's remote protocol runs over TLS with a client certificate, and pairing hashes the TV's RSA public key with the code shown on screen, so the driver needs the TV's certificate.

The rest of the protocol works in a driver today (2.5.2.138, C-7, against a real Android TV box): client-certificate TLS, RSA key generation, a self-signed certificate built in the driver, pairing, and the remote session. The TV's certificate is currently read from a hand-built TLS 1.2 ClientHello sent over a plain socket, where the Certificate message comes back in the clear. Under TLS 1.3 it is encrypted, so this stops working once a TV refuses TLS 1.2.

Related rawSocket behaviour:

  1. certificate and privateKey without caCertificate make connect() throw NullPointerException. With ignoreSSLIssues: true any PEM certificate works there, so it's a placeholder. Could it be optional?
  2. When the server rejects the handshake, socketStatus() is called about every 6 ms with the same "Connection has closed" error until close().
  3. With no tlsVersion, the socket negotiates TLS 1.2 with a server that offers 1.3. httpGet and asynchttpGet negotiate 1.3 with a server offering both.
  4. When a TLS server closes the connection cleanly, socketStatus() is not called and parse() stops; a driver needs its own idle timer.
  5. close() called from inside parse() logs java.lang.RuntimeException: java.lang.InterruptedException (method socketStatus) as an error, with no line number, even when the driver's socketStatus catches all exceptions. A close scheduled with runInMillis avoids it on an established session, but not after the server has rejected a pairing code.

@gopher.ny for your consideration.

Correction to item 5: the InterruptedException isn't tied to close() inside parse(). Closing any open TLS rawSocket logs it:

  • after a successful pairing, from a runInMillis close
  • when close() ends an established session
  • when connect() opens a new socket while one is still open

close() itself returns normally. The hub then calls socketStatus("receive error: Socket closed") and logs java.lang.RuntimeException: java.lang.InterruptedException (method socketStatus) as an error on the device, with no line number:

  • In a singleThreaded driver the method body never runs.
  • Without singleThreaded the body runs and returns, and the error is logged a few ms later. Once a java.sql.SQLException was logged in its place.

So a driver can't catch or avoid it, and every TLS close shows an error in the device's log. Closing a plain (non-TLS) socket logs nothing. The next connect() works, so it looks harmless, but users could read it as a driver fault.

Fixed in 2.5.3.104 :turkey: (please don't forget the docs for getPeerCertificate())