Files
micropython/tests/multi_net/tls_dtls_server_client.py
Damien George 8987b39e0b
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
tests/multi_net: Add test for DTLS server and client.
This adds a multi-test for DTLS server and client behaviour.  It works on
all ports that enable this feature (eg unix, esp32, rp2, stm32), but
bare-metal ports that use lwIP are not reliable as the DTLS server because
the lwIP bindings only support queuing one UDP packet at a time (that needs
to be fixed).

Also, to properly implement a DTLS server sockets need to support
`socket.recvfrom(n, MSG_PEEK)`.  That can be implemented in the future.

Signed-off-by: Damien George <damien@micropython.org>
2025-02-14 12:56:47 +11:00

90 lines
2.3 KiB
Python

# Test DTLS server and client, sending a small amount of data between them.
try:
import socket
import tls
except ImportError:
print("SKIP")
raise SystemExit
PORT = 8000
# These are test certificates. See tests/README.md for details.
certfile = "ec_cert.der"
keyfile = "ec_key.der"
try:
with open(certfile, "rb") as cf:
cert = cadata = cf.read()
with open(keyfile, "rb") as kf:
key = kf.read()
except OSError:
print("SKIP")
raise SystemExit
# DTLS server.
def instance0():
multitest.globals(IP=multitest.get_network_ip())
# Create a UDP socket and bind it to accept incoming connections.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
multitest.next()
# Wait for the client to connect.
data, client_addr = s.recvfrom(1)
print("incoming connection", data)
# Connect back to the client, so the UDP socket can be used like a stream.
s.connect(client_addr)
# Create the DTLS context and load the certificate.
ctx = tls.SSLContext(tls.PROTOCOL_DTLS_SERVER)
ctx.load_cert_chain(cert, key)
# Wrap the UDP socket in server mode.
print("wrap socket")
s = ctx.wrap_socket(s, server_side=1)
# Transfer some data.
for _ in range(4):
print(s.recv(16))
s.send(b"server to client")
# Close the DTLS and UDP connection.
s.close()
# DTLS client.
def instance1():
multitest.next()
# Create a UDP socket and connect to the server.
addr = socket.getaddrinfo(IP, PORT)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("connect")
s.connect(addr)
# Send one byte to indicate a connection, and so the server can obtain our address.
s.write("X")
# Create a DTLS context and load the certificate.
ctx = tls.SSLContext(tls.PROTOCOL_DTLS_CLIENT)
ctx.verify_mode = tls.CERT_REQUIRED
ctx.load_verify_locations(cadata)
# Wrap the UDP socket.
print("wrap socket")
s = ctx.wrap_socket(s, server_hostname="micropython.local")
# Transfer some data.
for _ in range(4):
s.send(b"client to server")
print(s.recv(16))
# Close the DTLS and UDP connection.
s.close()