-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ssl: restructure micropython interface in a tls module #793
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
metadata(version="0.6.0") | ||
metadata(version="0.7.0") | ||
|
||
# Originally written by Paul Sokolovsky. | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
metadata(version="0.8.1", pypi="requests") | ||
metadata(version="0.9.0", pypi="requests") | ||
|
||
package("requests") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
metadata(version="0.1.0") | ||
metadata(version="0.2.0") | ||
|
||
module("ssl.py") | ||
module("ssl.py", opt=3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
from ussl import * | ||
import ussl as _ussl | ||
import tls | ||
from tls import ( | ||
CERT_NONE, | ||
CERT_OPTIONAL, | ||
CERT_REQUIRED, | ||
MBEDTLS_VERSION, | ||
PROTOCOL_TLS_CLIENT, | ||
PROTOCOL_TLS_SERVER, | ||
) | ||
|
||
# Constants | ||
for sym in "CERT_NONE", "CERT_OPTIONAL", "CERT_REQUIRED": | ||
if sym not in globals(): | ||
globals()[sym] = object() | ||
|
||
class SSLContext: | ||
def __init__(self, *args): | ||
self._context = tls.SSLContext(*args) | ||
self._context.verify_mode = CERT_NONE | ||
|
||
@property | ||
def verify_mode(self): | ||
return self._context.verify_mode | ||
|
||
@verify_mode.setter | ||
def verify_mode(self, val): | ||
self._context.verify_mode = val | ||
|
||
def load_cert_chain(self, certfile, keyfile): | ||
if isinstance(certfile, str): | ||
with open(certfile, "rb") as f: | ||
certfile = f.read() | ||
if isinstance(keyfile, str): | ||
with open(keyfile, "rb") as f: | ||
keyfile = f.read() | ||
self._context.load_cert_chain(certfile, keyfile) | ||
|
||
def load_verify_locations(self, cafile=None, cadata=None): | ||
if cafile: | ||
with open(cafile, "rb") as f: | ||
cadata = f.read() | ||
self._context.load_verify_locations(cadata) | ||
|
||
def wrap_socket( | ||
self, sock, server_side=False, do_handshake_on_connect=True, server_hostname=None | ||
): | ||
return self._context.wrap_socket( | ||
sock, | ||
server_side=server_side, | ||
do_handshake_on_connect=do_handshake_on_connect, | ||
server_hostname=server_hostname, | ||
) | ||
|
||
|
||
def wrap_socket( | ||
sock, | ||
keyfile=None, | ||
certfile=None, | ||
server_side=False, | ||
key=None, | ||
cert=None, | ||
cert_reqs=CERT_NONE, | ||
*, | ||
ca_certs=None, | ||
server_hostname=None | ||
cadata=None, | ||
server_hostname=None, | ||
do_handshake=True, | ||
): | ||
# TODO: More arguments accepted by CPython could also be handled here. | ||
# That would allow us to accept ca_certs as a positional argument, which | ||
# we should. | ||
kw = {} | ||
if keyfile is not None: | ||
kw["keyfile"] = keyfile | ||
if certfile is not None: | ||
kw["certfile"] = certfile | ||
if server_side is not False: | ||
kw["server_side"] = server_side | ||
if cert_reqs is not CERT_NONE: | ||
kw["cert_reqs"] = cert_reqs | ||
if ca_certs is not None: | ||
kw["ca_certs"] = ca_certs | ||
if server_hostname is not None: | ||
kw["server_hostname"] = server_hostname | ||
return _ussl.wrap_socket(sock, **kw) | ||
con = SSLContext(PROTOCOL_TLS_SERVER if server_side else PROTOCOL_TLS_CLIENT) | ||
if cert or key: | ||
con.load_cert_chain(cert, key) | ||
if cadata: | ||
con.load_verify_locations(cadata=cadata) | ||
con.verify_mode = cert_reqs | ||
return con.wrap_socket( | ||
sock, | ||
server_side=server_side, | ||
do_handshake_on_connect=do_handshake, | ||
server_hostname=server_hostname, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems now like a "breaking" change, one needs to create and pass a
SSLContext
, see e.g. https://github.com/orgs/micropython/discussions/13624, I think this should be something likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was not aware that breaking changes here are not possible. I would argue that this breaking change is good here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this breaking change is a fair change to make. We need to improve and make progress on things and sometimes it's not practical to retain backwards compatibility on everything.
In this case using the new library with old user code will raise an exception if the
ssl
argument is used. So it's easy for the user to know that things need to be changed.And the new scheme matches better how SSL works in
asyncio
, passing in anSSLContext
.In the end a user can just keep using an old version of
umqtt
if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then document this somewhere in the
umqtt
's README or next release info should be fine 👍🏼