-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Adds TLS-PSK support to Python SSL context #14396
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: moprg <moprg@yannix.com>
Signed-off-by: moprg <moprg@yannix.com>
Signed-off-by: moprg <moprg@yannix.com>
Signed-off-by: moprg <moprg@yannix.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #14396 +/- ##
=======================================
Coverage 98.42% 98.43%
=======================================
Files 161 161
Lines 21253 21278 +25
=======================================
+ Hits 20919 20944 +25
Misses 334 334 ☔ View full report in Codecov by Sentry. |
Code size report:
|
@aminop1us since this would be a new feature, could you add a test (see |
thank for your suggestion, I will add a new test case. |
Signed-off-by: moprg <moprg@yannix.com>
I added the test and updated the pull request |
please let me know if there is anything else I can do |
Thank you for the contribution, this is a good feature that's definitely very useful in embedded contexts. Please see a prior attempt at this in #5544. I did not realise that CPython had very recently added support for PSK. That's great... although we did move to a custom So, since we have our own @aminop1us for your use case, do you find it beneficial for your MicroPython code to use PSK in a CPython compatible way, ie with this callback function? Or would you be just as happy using a custom MicroPython |
Signed-off-by: Sumeta Boonchamoi <sumeta.prg@gmail.com>
For reference, this is the PR that added PSK support in CPython: python/cpython#103181 |
As long as it is possible for us to write a wrapper to be CPython compatible, we are fine with that. |
Hey @aminop1us and @dpgeorge I was just inquiring what the status here was and if you wanted anyone to jump in to finish this task out? Having a PSK would micropython to fully support DTLS which we merged the main work for earlier this year in #15764. Our project previously implemented a similiar version of this in the way that @dpgeorge described above I think spread through these commits: If it is useful for someone help complete this, I'm happy to jump in or submit our work in a new PR or whatever is most useful to see this closed out. Thanks all! |
I am assuming the original author does not intend to finish this, so I'll try to pick it up. |
This pull request adds support to the ssl module for TLS-PSK (pre-shared key) authentication. This implementation is code compatible with CPython 3.13 for both client and server side, for example as described here:
https://docs.python.org/3.13/library/ssl.html#ssl.SSLContext.set_psk_server_callback
https://docs.python.org/3.13/library/ssl.html#ssl.SSLContext.set_psk_client_callback
The strong motivation for adding this functionality to MicroPython is that typical TSL using PKI on microcontrollers is heavy and resource intensive. For the kinds of platforms that MicroPython is targeting, many developers will prefer the lighter and simpler PSK alternative, particularly for development and testing. In fact this is exactly the target that TLS-PSK was designed for, so MicroPython would benefit greatly from this support being built-in to the ssl module. Since CPython 3.13 added this as a standard, we tried to be as compliant as possible. We are currently using these TLS-PSK capabilities in several in-house projects, and wanted to share it back in the hope that others might benefit from this, too.
Note: due to the underlying MBEDTLS library not supporting client side callbacks for TLS-PSK, we kept the client-side callback API compatible with CPython, but call the callback immediately and pass the returned identity and key to MBEDTLS when it creates the TLS connection. We tried to keep to CPython compatibility as much as possible for interoperability. We have tested with simple server and client code that runs on both our MicroPython branch as well as CPython 3.13.0a3.
We have tried to follow the MicroPython standard coding conventions, but if there are any requests for changes before it can be merged, please let us know and we will do our best to assist in any changes required.
In summary, this implementation allows the application to set a TLS PSK callback function on SSLContext for server-side connections, so the callback function is called for each handshake. Here is the documentation from the set_psk_server_callback link above:
"The parameter callback is a callable object with the signature: def callback(identity: str | None) -> bytes. The identity parameter is an optional identity sent by the client which can be used to select a corresponding PSK. The return value is a bytes-like object representing the pre-shared key. Return a zero length PSK to reject the connection."