Skip to content

add modussl_mbedtls.c methods and exceptions. esp32/unix #5436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

tuxlinuxien
Copy link

@tuxlinuxien tuxlinuxien commented Dec 18, 2019

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 6ccb4cf) 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:]

@tuxlinuxien tuxlinuxien changed the title add modussl_mbedtls.c methods and exceptions. add modussl_mbedtls.c methods and exceptions. esp32/unix Dec 18, 2019
@tve
Copy link
Contributor

tve commented Feb 5, 2020

Interesting addition / fix!
Has anyone other than the PR author looked into this or tested it?
I'm a bit puzzled in that I use mqtt_as (https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as/mqtt_as.py) on esp32 with SSL and that uses a non-blocking socket, so I'm wondering why this PR is needed...? (Asking out of ignorance more than anything else.)

@tuxlinuxien
Copy link
Author

tuxlinuxien commented Feb 5, 2020

@tve I can understand that my PR is not really important for micropython unix port. I had done these changes in order to build my own http library that works well in china (TCP and SSL are definitely not stable here if I try to ping a server outside of the GFW). I also don't like the idea of using sleep_ms in my code in order to wait for an event.

Since mbedTLS makes the distinction between read/write blocking socket, I thought it was better to use them.

Sorry that I couldn't do that for axTLS as well.

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Jul 27, 2021
@dpgeorge
Copy link
Member

Related #5840

@tuxlinuxien
Copy link
Author

@dpgeorge should I remove this merge request?

@dpgeorge
Copy link
Member

should I remove this merge request?

Not at this stage, I think it's a valid alternative to consider for non-blocking SSL.

tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Oct 13, 2021
_stage: Fix handling of scaled display in the stage library
@dpgeorge
Copy link
Member

dpgeorge commented Sep 5, 2023

Non-blocking SSL sockets have been implemented in a different way in ed58d6e

@dpgeorge dpgeorge closed this Sep 5, 2023
@dpgeorge
Copy link
Member

dpgeorge commented Sep 5, 2023

See also related commit ef71028

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants