-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ssl: Restructure micropython SSL interface to a new tls module.
MicroPython now supplies SSL/TLS functionality in a new built-in `tls` module. The `ssl` module is now implemented purely in Python, in this repository. Other libraries are updated to work with this scheme. Signed-off-by: Felix Dörre <[email protected]>
- Loading branch information
1 parent
803452a
commit 35d41db
Showing
9 changed files
with
81 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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, | ||
) |