ussl (TLS/SSL) is not implemented yet on Pico 2 W
Description
Unfortunately:
ussl is missing from the MicroPython build for RP2350/Pico 2 W
So when I try:
import ussl
I get:
ImportError: no module named 'ussl'
Code Size
No response
Implementation
I hope the MicroPython maintainers or community will implement this feature
Code of Conduct
Yes, I agree
add modussl_mbedtls.c methods and exceptions. esp32/unix
Changes
This pull requests adds:
- send
- recv
- do_handshake
methods to ussl (mbedtls version only) plus few exceptions.
The goal is to fully support non-blocking ssl sockets and reduce the number of call to poll by throwing the exact I/O error like SSL_WANT_READ or SSL_WANT_WRITE. the user has now the possibility to call do_handshake() later if ussl.wrap_socket was set with do_handshake = False
Tests
successfully tested on micropython/ports/esp-32 (esp-ifd rev 6ccb4cf5b7d1fdddb8c2492f9cbc926abaf230df) and micropython/ports/unix
Example
# this snippet SHOULD be seen as an example
# on how to use these new methods and is not
# optimized.
import usocket
import ussl
import uselect
# [...]
# create socket and ussl_wrap(sock)
# [...]
def do_handshake(sock, is_ssl):
if not is_ssl:
return
poller = uselect.poll()
poller.register(sock)
while True:
try:
sock.do_handshake()
break
except ussl.SSLWantReadError:
poller.modify(sock, uselect.POLLIN)
poller.poll(5000)
continue
except ussl.SSLWantWriteError:
poller.modify(sock, uselect.POLLOUT)
poller.poll(5000)
continue
except ussl.SSLInProgress:
continue
def reader(sock):
poller = uselect.poll()
poller.register(sock)
while True:
try:
buff = sock.recv(4096)
except ussl.SSLWantReadError:
poller.modify(sock, uselect.POLLIN)
poller.poll(5000)
continue
except ussl.SSLWantWriteError:
poller.modify(sock, uselect.POLLOUT)
poller.poll(5000)
continue
except Exception:
raise
if not buff:
break
yield buff
return []
def sender(sock, buffer):
poller = uselect.poll()
poller.register(sock)
while buffer:
try:
sent = sock.send(buffer)
except ussl.SSLWantReadError:
poller.modify(sock, uselect.POLLIN)
poller.poll(5000)
continue
except ussl.SSLWantWriteError:
poller.modify(sock, uselect.POLLOUT)
poller.poll(5000)
continue
except Exception:
raise
buffer = buffer[sent:]