Skip to content

Commit f15ff68

Browse files
committed
TLS EMS: Set haveEMS when we negotiate TLS 1.3
1 parent 2c585d7 commit f15ff68

4 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/ssl_sess.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,16 @@ void SetupSession(WOLFSSL* ssl)
35613561
session->side = (byte)ssl->options.side;
35623562
if (!IsAtLeastTLSv1_3(ssl->version) && ssl->arrays != NULL)
35633563
XMEMCPY(session->masterSecret, ssl->arrays->masterSecret, SECRET_LEN);
3564-
session->haveEMS = ssl->options.haveEMS;
3564+
/* RFC8446 Appendix D.
3565+
* implementations which support both TLS 1.3 and earlier versions SHOULD
3566+
* indicate the use of the Extended Master Secret extension in their APIs
3567+
* whenever TLS 1.3 is used.
3568+
* Set haveEMS so that we send the extension in subsequent connections that
3569+
* offer downgrades. */
3570+
if (IsAtLeastTLSv1_3(ssl->version))
3571+
session->haveEMS = 1;
3572+
else
3573+
session->haveEMS = ssl->options.haveEMS;
35653574
#ifdef WOLFSSL_SESSION_ID_CTX
35663575
/* If using compatibility layer then check for and copy over session context
35673576
* id. */

tests/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67624,6 +67624,7 @@ TEST_CASE testCases[] = {
6762467624
/* Uses Assert in handshake callback. */
6762567625
TEST_DECL(test_wolfSSL_set_alpn_protos),
6762667626
#endif
67627+
TEST_DECL(test_tls_ems_downgrade),
6762767628
TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
6762867629
TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
6762967630
TEST_DECL(test_wolfSSL_SCR_Reconnect),

tests/api/test_tls_ext.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,82 @@
3535
#include <wolfcrypt/src/misc.c>
3636
#endif
3737

38+
#include <wolfssl/internal.h>
3839
#include <tests/unit.h>
40+
#include <tests/utils.h>
3941
#include <tests/api/test_tls_ext.h>
4042

43+
int test_tls_ems_downgrade(void)
44+
{
45+
EXPECT_DECLS;
46+
#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
47+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
48+
defined(HAVE_SESSION_TICKET)
49+
struct test_memio_ctx test_ctx;
50+
WOLFSSL_CTX *ctx_c = NULL;
51+
WOLFSSL_CTX *ctx_s = NULL;
52+
WOLFSSL *ssl_c = NULL;
53+
WOLFSSL *ssl_s = NULL;
54+
WOLFSSL_SESSION* session = NULL;
55+
/* TLS EMS extension in binary form */
56+
const char ems_ext[] = { 0x00, 0x17, 0x00, 0x00 };
57+
char data = 0;
58+
59+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
60+
61+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
62+
wolfTLS_client_method, wolfTLS_server_method), 0);
63+
64+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
65+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
66+
67+
/* Verify that the EMS extension is present in Client's message */
68+
ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len,
69+
ems_ext, sizeof(ems_ext)));
70+
71+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
72+
ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION);
73+
74+
/* Do a round of reads to exchange the ticket message */
75+
ExpectIntEQ(wolfSSL_read(ssl_s, &data, sizeof(data)), -1);
76+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
77+
ExpectIntEQ(wolfSSL_read(ssl_c, &data, sizeof(data)), -1);
78+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
79+
80+
ExpectNotNull(session = wolfSSL_get1_session(ssl_c));
81+
ExpectTrue(session->haveEMS);
82+
83+
wolfSSL_free(ssl_c);
84+
ssl_c = NULL;
85+
wolfSSL_free(ssl_s);
86+
ssl_s = NULL;
87+
88+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
89+
wolfTLS_client_method, wolfTLS_server_method), 0);
90+
91+
/* Resuming the connection */
92+
ExpectIntEQ(wolfSSL_set_session(ssl_c, session), WOLFSSL_SUCCESS);
93+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
94+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
95+
96+
/* Verify that the EMS extension is still present in the resumption CH
97+
* even though we used TLS 1.3 */
98+
ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len,
99+
ems_ext, sizeof(ems_ext)));
100+
101+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
102+
ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION);
103+
104+
wolfSSL_SESSION_free(session);
105+
wolfSSL_free(ssl_c);
106+
wolfSSL_free(ssl_s);
107+
wolfSSL_CTX_free(ctx_c);
108+
wolfSSL_CTX_free(ctx_s);
109+
#endif
110+
return EXPECT_RESULT();
111+
}
112+
113+
41114
int test_wolfSSL_DisableExtendedMasterSecret(void)
42115
{
43116
EXPECT_DECLS;

tests/api/test_tls_ext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#ifndef TESTS_API_TEST_TLS_EMS_H
2323
#define TESTS_API_TEST_TLS_EMS_H
2424

25+
int test_tls_ems_downgrade(void);
2526
int test_wolfSSL_DisableExtendedMasterSecret(void);
2627

2728
#endif /* TESTS_API_TEST_TLS_EMS_H */

0 commit comments

Comments
 (0)