Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -38257,7 +38257,9 @@ static int IsTls13CipherSuite(byte first, byte second)
/* RFC 9146 Sect. 3: the CID is only in use when the peer answered with
* its own connection_id. Drop the state otherwise, or the record layer
* keeps framing and authenticating receives as if a CID were there.
* The DTLS 1.3 client does this in DoTls13ServerHello(). */
* The DTLS 1.3 client does this in DoTls13ServerHello(), but a 1.3
* client downgraded to 1.2 gets here, since its extensions are only
* parsed in this function. */
if (ssl->options.dtls && ssl->options.useDtlsCID)
DtlsCIDOnExtensionsParsed(ssl);
#endif /* WOLFSSL_DTLS_CID */
Expand Down
11 changes: 6 additions & 5 deletions src/tls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -6223,17 +6223,18 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
ssl->msgsReceived.got_hello_retry_request = 1;
ssl->msgsReceived.got_server_hello = 0;
}
#ifdef WOLFSSL_DTLS_CID
/* Extensions are parsed, drop the CID if the server did not answer
* it. A downgraded client does this in DoServerHello() instead. */
else if (ssl->options.useDtlsCID)
DtlsCIDOnExtensionsParsed(ssl);
#endif /* WOLFSSL_DTLS_CID */
}

if (args->totalExtSz > 0) {
args->idx += args->totalExtSz;
}

#ifdef WOLFSSL_DTLS_CID
if (ssl->options.useDtlsCID && *extMsgType == server_hello)
DtlsCIDOnExtensionsParsed(ssl);
#endif /* WOLFSSL_DTLS_CID */

if (IsAtLeastTLSv1_3(ssl->version)) {
*inOutIdx = args->idx;
}
Expand Down
94 changes: 94 additions & 0 deletions tests/api/test_dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -17029,3 +17029,97 @@ int test_dtls_set_rfd_wfd(void)
#endif
return EXPECT_RESULT();
}

/* RFC 9146 Sect. 3: a DTLS 1.2 server answers the client's connection_id with
* its own. A client that offered DTLS 1.3 and is downgraded to DTLS 1.2 must
* accept that answer and use the CID. */
int test_dtls13_cid_downgrade_to_dtls12(void)
{
EXPECT_DECLS;
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && \
!defined(WOLFSSL_NO_TLS12)
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
struct test_memio_ctx test_ctx;
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2 };
unsigned char server_cid[] = { 0, 1, 2, 3, 4, 5 };
const char msg[] = "hello";
char readBuf[32];

XMEMSET(&test_ctx, 0, sizeof(test_ctx));

ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_c), 1);
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, client_cid, sizeof(client_cid)), 1);
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_s), 1);
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_s, server_cid, sizeof(server_cid)), 1);

ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
ExpectStrEQ(wolfSSL_get_version(ssl_c), "DTLSv1.2");
ExpectIntEQ(wolfSSL_dtls_cid_is_enabled(ssl_c), 1);
ExpectIntEQ(wolfSSL_dtls_cid_is_enabled(ssl_s), 1);

ExpectIntEQ(wolfSSL_write(ssl_c, msg, (int)sizeof(msg)), (int)sizeof(msg));
XMEMSET(readBuf, 0, sizeof(readBuf));
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
(int)sizeof(msg));
ExpectStrEQ(readBuf, msg);

wolfSSL_free(ssl_c);
wolfSSL_CTX_free(ctx_c);
wolfSSL_free(ssl_s);
wolfSSL_CTX_free(ctx_s);
#endif
return EXPECT_RESULT();
}

/* A client that offered DTLS 1.3 with a CID and is downgraded by a DTLS 1.2
* server that ignores the CID must drop it and keep exchanging ordinary
* records in both directions. */
int test_dtls13_cid_downgrade_not_negotiated(void)
{
EXPECT_DECLS;
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) && \
!defined(WOLFSSL_NO_TLS12)
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
struct test_memio_ctx test_ctx;
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2 };
const char msg[] = "hello";
char readBuf[32];

XMEMSET(&test_ctx, 0, sizeof(test_ctx));

ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
/* Only the client asks for a CID. */
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_c), 1);
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, client_cid, sizeof(client_cid)), 1);

ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
ExpectStrEQ(wolfSSL_get_version(ssl_c), "DTLSv1.2");
ExpectIntEQ(wolfSSL_dtls_cid_is_enabled(ssl_c), 0);
ExpectIntEQ(wolfSSL_dtls_cid_is_enabled(ssl_s), 0);

ExpectIntEQ(wolfSSL_write(ssl_s, msg, (int)sizeof(msg)), (int)sizeof(msg));
XMEMSET(readBuf, 0, sizeof(readBuf));
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)),
(int)sizeof(msg));
ExpectStrEQ(readBuf, msg);

ExpectIntEQ(wolfSSL_write(ssl_c, msg, (int)sizeof(msg)), (int)sizeof(msg));
XMEMSET(readBuf, 0, sizeof(readBuf));
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
(int)sizeof(msg));
ExpectStrEQ(readBuf, msg);

wolfSSL_free(ssl_c);
wolfSSL_CTX_free(ctx_c);
wolfSSL_free(ssl_s);
wolfSSL_CTX_free(ctx_s);
#endif
return EXPECT_RESULT();
}
6 changes: 5 additions & 1 deletion tests/api/test_dtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ int test_wolfSSL_dtls_stateless_maxfrag(void);
int test_wolfSSL_dtls_stateless_resume(void);
int test_wolfSSL_dtls_stateless_downgrade(void);
int test_WOLFSSL_dtls_version_alert(void);
int test_dtls13_cid_downgrade_to_dtls12(void);
int test_dtls13_cid_downgrade_not_negotiated(void);

/* Shared with tests/api/test_dtls13.c. */
#define TEST_DTLS13_FOUR_FRAG_CH_SZ 832
Expand Down Expand Up @@ -424,5 +426,7 @@ int test_dtls_no_cookie_ch_pause(WOLFSSL* ssl, void* ctx);
TEST_DECL_GROUP("dtls", test_dtls12_cookie_secret_generate_fail), \
TEST_DECL_GROUP("dtls", test_dtls13_hrr_cookie_secret_generate_fail), \
TEST_DECL_GROUP("dtls", test_dtls_max_fragment_write), \
TEST_DECL_GROUP("dtls", test_dtls_set_rfd_wfd)
TEST_DECL_GROUP("dtls", test_dtls_set_rfd_wfd), \
TEST_DECL_GROUP("dtls", test_dtls13_cid_downgrade_to_dtls12), \
TEST_DECL_GROUP("dtls", test_dtls13_cid_downgrade_not_negotiated)
#endif /* TESTS_API_DTLS_H */
Loading