From 62163142a0c815cb1aa7c134fe86b97684044622 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 1 Oct 2024 12:44:33 +0200 Subject: [PATCH 1/2] dtls_server: allow unexpected messages during handshake If MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE happens during the handshake, don't show it as an "error". It might be an error, but it might also be a fact of life if it happens during the second or more handshake: it can be a duplicated packet or a close_notify alert from the previous connection, which is hard to avoid and harmless. Fixes #9652. Signed-off-by: Gilles Peskine --- programs/ssl/dtls_server.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/programs/ssl/dtls_server.c b/programs/ssl/dtls_server.c index 0a02694eb7..d1063cbe47 100644 --- a/programs/ssl/dtls_server.c +++ b/programs/ssl/dtls_server.c @@ -291,7 +291,14 @@ reset: ret = 0; goto reset; } else if (ret != 0) { - printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret); + printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n", (unsigned int) -ret); + if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) { + printf(" An unexpected message was received from our peer. If this happened at\n"); + printf(" the beginning of the handshake, this is likely a duplicated packet or\n"); + printf(" a close_notify alert from the previous connection, which is harmless.\n"); + ret = 0; + } + printf("\n"); goto reset; } From 6f8ff55884278feca293a12b1105dab73cc43eee Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 1 Oct 2024 12:48:42 +0200 Subject: [PATCH 2/2] Remove "error" allowance in dtls_server Now that dtls_server doesn't print "error" when it receives stray messages while it's waiting for a second handshake, have the tests fail if "error" is printed for some other reason. Signed-off-by: Gilles Peskine --- tests/opt-testcases/sample.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/opt-testcases/sample.sh b/tests/opt-testcases/sample.sh index 8b2bc995a3..ff847cc645 100644 --- a/tests/opt-testcases/sample.sh +++ b/tests/opt-testcases/sample.sh @@ -325,11 +325,6 @@ run_test "Sample: ssl_pthread_server, gnutls client, TLS 1.3" \ -S "error" \ -C "ERROR" -# The server complains of extra data after it closes the connection -# because the client keeps sending data, so the server receives -# more application data when it expects a new handshake. We consider -# the test a success if both sides have sent and received application -# data, no matter what happens afterwards. run_test "Sample: dtls_client with dtls_server" \ -P 4433 \ "$PROGRAMS_DIR/dtls_server" \ @@ -339,13 +334,9 @@ run_test "Sample: dtls_client with dtls_server" \ -s "[1-9][0-9]* bytes written" \ -c "[1-9][0-9]* bytes read" \ -c "[1-9][0-9]* bytes written" \ + -S "error" \ -C "error" -# The server complains of extra data after it closes the connection -# because the client keeps sending data, so the server receives -# more application data when it expects a new handshake. We consider -# the test a success if both sides have sent and received application -# data, no matter what happens afterwards. run_test "Sample: ssl_client2, dtls_server" \ -P 4433 \ "$PROGRAMS_DIR/dtls_server" \ @@ -355,6 +346,7 @@ run_test "Sample: ssl_client2, dtls_server" \ -s "[1-9][0-9]* bytes written" \ -c "[1-9][0-9]* bytes read" \ -c "[1-9][0-9]* bytes written" \ + -S "error" \ -C "error" requires_protocol_version dtls12