peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

Fixes to SSL/TLS on 8266 example #73

Closed SooOverpowered closed 2 years ago

SooOverpowered commented 2 years ago

I tried to add do_handshake ssl parameter to False, client then can establish connection with SSL to broker

peterhinch commented 2 years ago

Interesting. I had gathered from this issue that TLS on ESP8266 was broken. Unfortunately I don't have a good grasp of the theory of TLS. Can you please explain how your fix works?

SooOverpowered commented 2 years ago

According to Micropython SSL module:

do_handshake determines whether the handshake is done as part of the wrap_socket or whether it is deferred to be done as part of the initial reads or writes (there is no do_handshake method as in CPython). For blocking sockets doing the handshake immediately is standard. For non-blocking sockets (i.e. when the sock passed into wrap_socket is in non-blocking mode) the handshake should generally be deferred because otherwise wrap_socket blocks until it completes. Note that in AXTLS the handshake can be deferred until the first read or write but it then blocks until completion.

Technically, since we are using non-blocking sockets, when setting do_handshake to False, it will defer the handshake and will do the handshake when we start writing data to the socket, allowing SSL/TLS to work. I checked using Wireshark to see if it actually worked and yes it did actually work.

peterhinch commented 2 years ago

Thank you for this, I'll update the docs.