Question

Looks like SSL/TLS support has recently been added to Dart via the SecureSocket class, which is great.

So, for example this

SecureSocket.connect(_host, _port).then(
        (Socket socket) {
          ...

opens a socket with TLS enabled right away. However, what I'd like to do is open a regular (not secure) socket, send and receive some unencrypted data first, and enable TLS on it later.

Here's how it can be done in PHP:

$socket = fsockopen($server, $port, $errno, $errstr);
// ... do some unencrypted stuff...
stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);

Any way to do this in Dart?

EDIT: I guess what I'm looking for is a Dart implementation of STARTTLS.

Was it helpful?

Solution

I haven't tried it myself, but it looks like the secure method in the SecureSocket documentation is exactly what you're looking for (assuming you want to use a client-side handshake, otherwise see secureServer):

Future<SecureSocket> secure(Socket socket, {host, 
       bool sendClientCertificate: false, String certificateName, bool
       onBadCertificate(X509Certificate certificate)})

Takes an already connected socket and starts client side TLS handshake to make the communication secure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top