Skip to content

Commit dada525

Browse files
committed
ssl: Restructure micropython interface in a tls module.
Signed-off-by: Felix Dörre <felix@dogcraft.de>
1 parent 7cdf708 commit dada525

File tree

4 files changed

+72
-40
lines changed

4 files changed

+72
-40
lines changed

micropython/umqtt.simple/umqtt/simple.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ def __init__(
1616
user=None,
1717
password=None,
1818
keepalive=0,
19-
ssl=False,
20-
ssl_params={},
19+
ssl=None,
2120
):
2221
if port == 0:
23-
port = 8883 if ssl else 1883
22+
port = 8883 if ssl is not None else 1883
2423
self.client_id = client_id
2524
self.sock = None
2625
self.server = server
2726
self.port = port
2827
self.ssl = ssl
29-
self.ssl_params = ssl_params
3028
self.pid = 0
3129
self.cb = None
3230
self.user = user
@@ -66,10 +64,8 @@ def connect(self, clean_session=True):
6664
self.sock = socket.socket()
6765
addr = socket.getaddrinfo(self.server, self.port)[0][-1]
6866
self.sock.connect(addr)
69-
if self.ssl:
70-
import ussl
71-
72-
self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
67+
if self.ssl is not None:
68+
self.sock = self.ssl.wrap_socket(self.sock, server_hostname=self.server)
7369
premsg = bytearray(b"\x10\0\0\0\0\0")
7470
msg = bytearray(b"\x04MQTT\x04\x02\0\0")
7571

micropython/urllib.urequest/urllib/urequest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def urlopen(url, data=None, method="GET"):
1212
if proto == "http:":
1313
port = 80
1414
elif proto == "https:":
15-
import ussl
15+
import ssl
1616

1717
port = 443
1818
else:
@@ -29,7 +29,7 @@ def urlopen(url, data=None, method="GET"):
2929
try:
3030
s.connect(ai[-1])
3131
if proto == "https:":
32-
s = ussl.wrap_socket(s, server_hostname=host)
32+
s = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT).wrap_socket(s, server_hostname=host)
3333

3434
s.write(method)
3535
s.write(b" /")

python-ecosys/requests/requests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def request(
6363
if proto == "http:":
6464
port = 80
6565
elif proto == "https:":
66-
import ussl
66+
import ssl
6767

6868
port = 443
6969
else:
@@ -90,7 +90,7 @@ def request(
9090
try:
9191
s.connect(ai[-1])
9292
if proto == "https:":
93-
s = ussl.wrap_socket(s, server_hostname=host)
93+
s = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT).wrap_socket(s, server_hostname=host)
9494
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
9595
if "Host" not in headers:
9696
s.write(b"Host: %s\r\n" % host)

python-stdlib/ssl/ssl.py

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,72 @@
1-
from ussl import *
2-
import ussl as _ussl
1+
import tls
2+
from tls import (
3+
CERT_NONE,
4+
CERT_OPTIONAL,
5+
CERT_REQUIRED,
6+
MBEDTLS_VERSION,
7+
PROTOCOL_TLS_CLIENT,
8+
PROTOCOL_TLS_SERVER,
9+
)
310

4-
# Constants
5-
for sym in "CERT_NONE", "CERT_OPTIONAL", "CERT_REQUIRED":
6-
if sym not in globals():
7-
globals()[sym] = object()
11+
12+
class SSLContext:
13+
def __init__(self, *args):
14+
self._context = tls.SSLContext(*args)
15+
self._context.verify_mode = CERT_NONE
16+
17+
@property
18+
def verify_mode(self):
19+
return self._context.verify_mode
20+
21+
@verify_mode.setter
22+
def verify_mode(self, val):
23+
self._context.verify_mode = val
24+
25+
def load_cert_chain(self, certfile, keyfile):
26+
if isinstance(certfile, str):
27+
with open(certfile, "rb") as f:
28+
certfile = f.read()
29+
if isinstance(keyfile, str):
30+
with open(keyfile, "rb") as f:
31+
keyfile = f.read()
32+
self._context.load_cert_chain(certfile, keyfile)
33+
34+
def load_verify_locations(self, cafile=None, cadata=None):
35+
if cafile is not None:
36+
with open(cafile, "rb") as f:
37+
cadata = f.read()
38+
self._context.load_verify_locations(cadata=cadata)
39+
40+
def wrap_socket(
41+
self, sock, server_side=False, do_handshake_on_connect=True, server_hostname=None
42+
):
43+
return self._context.wrap_socket(
44+
sock,
45+
server_side=server_side,
46+
do_handshake_on_connect=do_handshake_on_connect,
47+
server_hostname=server_hostname,
48+
)
849

950

1051
def wrap_socket(
1152
sock,
12-
keyfile=None,
13-
certfile=None,
1453
server_side=False,
54+
key=None,
55+
cert=None,
1556
cert_reqs=CERT_NONE,
16-
*,
17-
ca_certs=None,
18-
server_hostname=None
57+
cadata=None,
58+
server_hostname=None,
59+
do_handshake=True,
1960
):
20-
# TODO: More arguments accepted by CPython could also be handled here.
21-
# That would allow us to accept ca_certs as a positional argument, which
22-
# we should.
23-
kw = {}
24-
if keyfile is not None:
25-
kw["keyfile"] = keyfile
26-
if certfile is not None:
27-
kw["certfile"] = certfile
28-
if server_side is not False:
29-
kw["server_side"] = server_side
30-
if cert_reqs is not CERT_NONE:
31-
kw["cert_reqs"] = cert_reqs
32-
if ca_certs is not None:
33-
kw["ca_certs"] = ca_certs
34-
if server_hostname is not None:
35-
kw["server_hostname"] = server_hostname
36-
return _ussl.wrap_socket(sock, **kw)
61+
con = SSLContext(PROTOCOL_TLS_SERVER if server_side else PROTOCOL_TLS_CLIENT)
62+
if cert is not None or key is not None:
63+
con.load_cert_chain(cert, key)
64+
if cadata is not None:
65+
con.load_verify_locations(cadata=cadata)
66+
con.verify_mode = cert_reqs
67+
return con.wrap_socket(
68+
sock,
69+
server_side=server_side,
70+
do_handshake_on_connect=do_handshake,
71+
server_hostname=server_hostname,
72+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy