summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKeenan Johnson <keenan.johnson@gmail.com>2025-02-13 13:11:38 -0800
committerDamien George <damien@micropython.org>2025-02-14 12:55:25 +1100
commit321b30ca564bb33c625292247d00f7dd29dc9559 (patch)
tree34f04febf16e5825f069b002494dd512b0c30331 /tests
parentaef6705a321fbefb06288b5be1f5931bf8c42fe3 (diff)
extmod/modtls_mbedtls: Wire in support for DTLS.
This commit enables support for DTLS, i.e. TLS over datagram transport protocols like UDP. While support for DTLS is absent in CPython, it is worth supporting it in MicroPython because it is the basis of the ubiquitous CoAP protocol, used in many IoT projects. To select DTLS, a new set of "protocols" are added to SSLContext: - ssl.PROTOCOL_DTLS_CLIENT - ssl.PROTOCOL_DTLS_SERVER If one of these is set, the library assumes that the underlying socket is a datagram-like socket (i.e. UDP or similar). Our own timer callbacks are implemented because the out of the box implementation relies on `gettimeofday()`. This new DTLS feature is enabled on all ports that use mbedTLS. This commit is an update to a previous PR #10062. Addresses issue #5270 which requested DTLS support. Signed-off-by: Keenan Johnson <keenan.johnson@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/extmod/tls_dtls.py51
-rw-r--r--tests/extmod/tls_dtls.py.exp3
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/extmod/tls_dtls.py b/tests/extmod/tls_dtls.py
new file mode 100644
index 000000000..b2d716769
--- /dev/null
+++ b/tests/extmod/tls_dtls.py
@@ -0,0 +1,51 @@
+# Test DTLS functionality including timeout handling
+
+try:
+ from tls import PROTOCOL_DTLS_CLIENT, PROTOCOL_DTLS_SERVER, SSLContext, CERT_NONE
+ import io
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+class DummySocket(io.IOBase):
+ def __init__(self):
+ self.write_buffer = bytearray()
+ self.read_buffer = bytearray()
+
+ def write(self, data):
+ return len(data)
+
+ def readinto(self, buf):
+ # This is a placeholder socket that doesn't actually read anything
+ # so the read buffer is always empty.
+ return None
+
+ def ioctl(self, req, arg):
+ if req == 4: # MP_STREAM_CLOSE
+ return 0
+ return -1
+
+
+# Create dummy sockets for testing
+server_socket = DummySocket()
+client_socket = DummySocket()
+
+# Wrap the DTLS Server
+dtls_server_ctx = SSLContext(PROTOCOL_DTLS_SERVER)
+dtls_server_ctx.verify_mode = CERT_NONE
+dtls_server = dtls_server_ctx.wrap_socket(server_socket, do_handshake_on_connect=False)
+print("Wrapped DTLS Server")
+
+# Wrap the DTLS Client
+dtls_client_ctx = SSLContext(PROTOCOL_DTLS_CLIENT)
+dtls_client_ctx.verify_mode = CERT_NONE
+dtls_client = dtls_client_ctx.wrap_socket(client_socket, do_handshake_on_connect=False)
+print("Wrapped DTLS Client")
+
+# Trigger the timing check multiple times with different elapsed times
+for i in range(10): # Try multiple iterations to hit the timing window
+ dtls_client.write(b"test")
+ data = dtls_server.read(1024) # This should eventually hit the timing condition
+
+print("OK")
diff --git a/tests/extmod/tls_dtls.py.exp b/tests/extmod/tls_dtls.py.exp
new file mode 100644
index 000000000..78d72bff1
--- /dev/null
+++ b/tests/extmod/tls_dtls.py.exp
@@ -0,0 +1,3 @@
+Wrapped DTLS Server
+Wrapped DTLS Client
+OK