Files
micropython/tests/extmod/tls_dtls.py
Keenan Johnson 321b30ca56 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>
2025-02-14 12:55:25 +11:00

52 lines
1.5 KiB
Python

# 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")