Skip to content

Commit 099eaf5

Browse files
authored
Merge pull request #8487 from julek-wolfssl/zd/19391
TLS EMS: Set haveEMS when we negotiate TLS 1.3
2 parents 6761dbb + ab64597 commit 099eaf5

6 files changed

Lines changed: 172 additions & 26 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,7 @@ if(WOLFSSL_EXAMPLES)
25912591
tests/api/test_dtls.c
25922592
tests/api/test_ocsp.c
25932593
tests/api/test_evp.c
2594+
tests/api/test_tls_ext.c
25942595
tests/srp.c
25952596
tests/suites.c
25962597
tests/w64wrapper.c

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: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
#include <tests/api/test_dtls.h>
324324
#include <tests/api/test_ocsp.h>
325325
#include <tests/api/test_evp.h>
326+
#include <tests/api/test_tls_ext.h>
326327

327328
#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_TLS) && \
328329
!defined(NO_RSA) && !defined(SINGLE_THREADED) && \
@@ -12864,31 +12865,6 @@ static int test_wolfSSL_set_alpn_protos(void)
1286412865

1286512866
#endif /* HAVE_ALPN_PROTOS_SUPPORT */
1286612867

12867-
static int test_wolfSSL_DisableExtendedMasterSecret(void)
12868-
{
12869-
EXPECT_DECLS;
12870-
#if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT) && \
12871-
!defined(NO_TLS)
12872-
WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
12873-
WOLFSSL *ssl = wolfSSL_new(ctx);
12874-
12875-
ExpectNotNull(ctx);
12876-
ExpectNotNull(ssl);
12877-
12878-
/* error cases */
12879-
ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
12880-
ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
12881-
12882-
/* success cases */
12883-
ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
12884-
ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
12885-
12886-
wolfSSL_free(ssl);
12887-
wolfSSL_CTX_free(ctx);
12888-
#endif
12889-
return EXPECT_RESULT();
12890-
}
12891-
1289212868
static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
1289312869
{
1289412870
EXPECT_DECLS;
@@ -67648,6 +67624,7 @@ TEST_CASE testCases[] = {
6764867624
/* Uses Assert in handshake callback. */
6764967625
TEST_DECL(test_wolfSSL_set_alpn_protos),
6765067626
#endif
67627+
TEST_DECL(test_tls_ems_downgrade),
6765167628
TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
6765267629
TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
6765367630
TEST_DECL(test_wolfSSL_SCR_Reconnect),

tests/api/include.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ tests_unit_test_SOURCES += tests/api/test_dtls.c
5252
# TLS Feature
5353
tests_unit_test_SOURCES += tests/api/test_ocsp.c
5454
tests_unit_test_SOURCES += tests/api/test_evp.c
55+
tests_unit_test_SOURCES += tests/api/test_tls_ext.c
5556
endif
5657

5758
EXTRA_DIST += tests/api/api.h
@@ -101,4 +102,5 @@ EXTRA_DIST += tests/api/test_ocsp.h
101102
EXTRA_DIST += tests/api/test_ocsp_test_blobs.h
102103
EXTRA_DIST += tests/api/create_ocsp_test_blobs.py
103104
EXTRA_DIST += tests/api/test_evp.h
105+
EXTRA_DIST += tests/api/test_tls_ext.h
104106

tests/api/test_tls_ext.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* test_tls_ext.c
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#include <tests/unit.h>
23+
24+
#ifdef NO_INLINE
25+
#include <wolfssl/wolfcrypt/misc.h>
26+
#else
27+
#define WOLFSSL_MISC_INCLUDED
28+
#include <wolfcrypt/src/misc.c>
29+
#endif
30+
31+
#include <wolfssl/internal.h>
32+
#include <tests/utils.h>
33+
#include <tests/api/test_tls_ext.h>
34+
35+
int test_tls_ems_downgrade(void)
36+
{
37+
EXPECT_DECLS;
38+
#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
39+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
40+
defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
41+
struct test_memio_ctx test_ctx;
42+
WOLFSSL_CTX *ctx_c = NULL;
43+
WOLFSSL_CTX *ctx_s = NULL;
44+
WOLFSSL *ssl_c = NULL;
45+
WOLFSSL *ssl_s = NULL;
46+
WOLFSSL_SESSION* session = NULL;
47+
/* TLS EMS extension in binary form */
48+
const char ems_ext[] = { 0x00, 0x17, 0x00, 0x00 };
49+
char data = 0;
50+
51+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
52+
53+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
54+
wolfTLS_client_method, wolfTLS_server_method), 0);
55+
56+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
57+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
58+
59+
/* Verify that the EMS extension is present in Client's message */
60+
ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len,
61+
ems_ext, sizeof(ems_ext)));
62+
63+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
64+
ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION);
65+
66+
/* Do a round of reads to exchange the ticket message */
67+
ExpectIntEQ(wolfSSL_read(ssl_s, &data, sizeof(data)), -1);
68+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
69+
ExpectIntEQ(wolfSSL_read(ssl_c, &data, sizeof(data)), -1);
70+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
71+
72+
ExpectNotNull(session = wolfSSL_get1_session(ssl_c));
73+
ExpectTrue(session->haveEMS);
74+
75+
wolfSSL_free(ssl_c);
76+
ssl_c = NULL;
77+
wolfSSL_free(ssl_s);
78+
ssl_s = NULL;
79+
80+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
81+
wolfTLS_client_method, wolfTLS_server_method), 0);
82+
83+
/* Resuming the connection */
84+
ExpectIntEQ(wolfSSL_set_session(ssl_c, session), WOLFSSL_SUCCESS);
85+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
86+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
87+
88+
/* Verify that the EMS extension is still present in the resumption CH
89+
* even though we used TLS 1.3 */
90+
ExpectNotNull(mymemmem(test_ctx.s_buff, test_ctx.s_len,
91+
ems_ext, sizeof(ems_ext)));
92+
93+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
94+
ExpectIntEQ(wolfSSL_version(ssl_c), TLS1_3_VERSION);
95+
96+
wolfSSL_SESSION_free(session);
97+
wolfSSL_free(ssl_c);
98+
wolfSSL_free(ssl_s);
99+
wolfSSL_CTX_free(ctx_c);
100+
wolfSSL_CTX_free(ctx_s);
101+
#endif
102+
return EXPECT_RESULT();
103+
}
104+
105+
106+
int test_wolfSSL_DisableExtendedMasterSecret(void)
107+
{
108+
EXPECT_DECLS;
109+
#if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT) && \
110+
!defined(NO_TLS)
111+
WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
112+
WOLFSSL *ssl = wolfSSL_new(ctx);
113+
114+
ExpectNotNull(ctx);
115+
ExpectNotNull(ssl);
116+
117+
/* error cases */
118+
ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
119+
ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
120+
121+
/* success cases */
122+
ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
123+
ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
124+
125+
wolfSSL_free(ssl);
126+
wolfSSL_CTX_free(ctx);
127+
#endif
128+
return EXPECT_RESULT();
129+
}

tests/api/test_tls_ext.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* test_tls_ext.h
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#ifndef TESTS_API_TEST_TLS_EMS_H
23+
#define TESTS_API_TEST_TLS_EMS_H
24+
25+
int test_tls_ems_downgrade(void);
26+
int test_wolfSSL_DisableExtendedMasterSecret(void);
27+
28+
#endif /* TESTS_API_TEST_TLS_EMS_H */

0 commit comments

Comments
 (0)