This commit adds the SSLContext class to the ssl module, and retains the
existing ssl.wrap_socket() function to maintain backwards compatibility.
CPython deprecated the ssl.wrap_socket() function since CPython 3.7 and
instead one should use ssl.SSLContext().wrap_socket(). This commit makes
that possible.
For the axtls implementation:
- ssl.SSLContext is added, although it doesn't hold much state because
axtls requires calling ssl_ctx_new() for each new socket
- ssl.SSLContext.wrap_socket() is added
- ssl.PROTOCOL_TLS_CLIENT and ssl.PROTOCOL_TLS_SERVER are added
For the mbedtls implementation:
- ssl.SSLContext is added, and holds most of the mbedtls state
- ssl.verify_mode is added (getter and setter)
- ssl.SSLContext.wrap_socket() is added
- ssl.PROTOCOL_TLS_CLIENT and ssl.PROTOCOL_TLS_SERVER are added
The signatures match CPython:
- SSLContext(protocol)
- SSLContext.wrap_socket(sock, *, server_side=False,
do_handshake_on_connect=True, server_hostname=None)
The existing ssl.wrap_socket() functions retain their existing signature.
Signed-off-by: Damien George <damien@micropython.org>
70 lines
1.2 KiB
Python
70 lines
1.2 KiB
Python
# very basic test of ssl module, just to test the methods exist
|
|
|
|
try:
|
|
import io
|
|
import ssl
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
|
|
class TestSocket(io.IOBase):
|
|
def write(self, buf):
|
|
return len(buf)
|
|
|
|
def readinto(self, buf):
|
|
return 0
|
|
|
|
def ioctl(self, cmd, arg):
|
|
print("TestSocket.ioctl", cmd, arg)
|
|
return 0
|
|
|
|
def setblocking(self, value):
|
|
print("TestSocket.setblocking({})".format(value))
|
|
|
|
|
|
# create in client mode
|
|
try:
|
|
ss = ssl.wrap_socket(TestSocket(), server_hostname="test.example.com")
|
|
except OSError as er:
|
|
print("OSError: client")
|
|
|
|
# create in server mode (can use this object for further tests)
|
|
ss = ssl.wrap_socket(TestSocket(), server_side=1, do_handshake=0)
|
|
|
|
# print
|
|
print(ss)
|
|
|
|
# setblocking() propagates call to the underlying stream object
|
|
ss.setblocking(False)
|
|
ss.setblocking(True)
|
|
|
|
# write
|
|
try:
|
|
ss.write(b"aaaa")
|
|
except OSError:
|
|
pass
|
|
|
|
# read (underlying socket has no data)
|
|
try:
|
|
ss.read(8)
|
|
except OSError:
|
|
pass
|
|
|
|
# close
|
|
ss.close()
|
|
# close 2nd time
|
|
ss.close()
|
|
|
|
# read on closed socket
|
|
try:
|
|
ss.read(10)
|
|
except OSError as er:
|
|
print("OSError: read")
|
|
|
|
# write on closed socket
|
|
try:
|
|
ss.write(b"aaaa")
|
|
except OSError as er:
|
|
print("OSError: write")
|