-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
extmod/modussl_mbedtls: Updated Wire in support for DTLS. #15764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Wrapped DTLS Server | ||
Wrapped DTLS Client | ||
OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- instance0 --- | ||
incoming connection b'X' | ||
wrap socket | ||
b'client to server' | ||
b'client to server' | ||
b'client to server' | ||
b'client to server' | ||
--- instance1 --- | ||
connect | ||
wrap socket | ||
b'server to client' | ||
b'server to client' | ||
b'server to client' | ||
b'server to client' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
write_buffer
is never actually used. So this function could just doreturn len(data)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed!