blob: f556c0f26b55814e7ce53ce0b514160b6902c22e [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
26#if defined(MBEDTLS_SSL_CLI_C)
27
Jerry Yubc20bdd2021-08-24 15:59:48 +080028#include <string.h>
29
Jerry Yu56fc07f2021-09-01 17:48:49 +080030#include "mbedtls/debug.h"
31#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080032#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yubdc71882021-09-14 19:30:36 +080034#include "ssl_misc.h"
35#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080036#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010037#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080038
Jerry Yubc20bdd2021-08-24 15:59:48 +080039/* Write extensions */
40
Jerry Yu92c6b402021-08-27 16:59:09 +080041/*
42 * ssl_tls13_write_supported_versions_ext():
43 *
44 * struct {
45 * ProtocolVersion versions<2..254>;
46 * } SupportedVersions;
47 */
Jerry Yuf4436812021-08-26 22:59:56 +080048static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080049 unsigned char *buf,
50 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000051 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080052{
53 unsigned char *p = buf;
54
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
63 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Jerry Yu92c6b402021-08-27 16:59:09 +080065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
66
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080067 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080071 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080072 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080074 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080075 *p++ = 0x2;
76
Jerry Yu0c63af62021-09-02 12:59:12 +080077 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 *
Jerry Yu0c63af62021-09-02 12:59:12 +080081 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080082 */
Jerry Yueecfbf02021-08-30 18:32:07 +080083 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
84 ssl->conf->max_minor_ver,
85 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080088 ssl->conf->max_major_ver,
89 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080090
Xiaofei Baid25fab62021-12-02 06:36:27 +000091 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080092
93 return( 0 );
94}
Jerry Yubc20bdd2021-08-24 15:59:48 +080095
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Jerry Yub85277e2021-10-13 13:36:05 +0800102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
103 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
113 return( 0 );
114}
115
Jerry Yubc20bdd2021-08-24 15:59:48 +0800116#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
117
XiaokangQian16acd4b2022-01-14 07:35:47 +0000118static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000119{
120 uint16_t group_id = ssl->handshake->offered_group_id;
121 if( group_id == 0 )
122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
123
XiaokangQian355e09a2022-01-20 11:14:50 +0000124#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000125 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000126 {
127 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
128 return( 0 );
129 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000130 else
131#endif /* MBEDTLS_ECDH_C */
132 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000133 {
134 /* Do something */
135 }
136
137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
138}
139
140/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800141 * Functions for writing key_share extension.
142 */
143#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800144static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800145 mbedtls_ssl_context *ssl,
146 uint16_t named_group,
147 unsigned char *buf,
148 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000149 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800150{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800152 const mbedtls_ecp_curve_info *curve_info =
153 mbedtls_ecp_curve_info_from_tls_id( named_group );
154
155 if( curve_info == NULL )
156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
157
158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
159
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800160 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
161 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800162 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800164 return( ret );
165 }
166
Xiaofei Baid25fab62021-12-02 06:36:27 +0000167 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
168 buf, end - buf,
169 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800170 if( ret != 0 )
171 {
172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
173 return( ret );
174 }
175
176 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
177 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800178 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800179}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800180#endif /* MBEDTLS_ECDH_C */
181
Jerry Yub60e3cf2021-09-08 16:41:02 +0800182static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
183 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800184{
185 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
186
Jerry Yu56fc07f2021-09-01 17:48:49 +0800187
Jerry Yu56fc07f2021-09-01 17:48:49 +0800188#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100189 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800190 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100191 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800192 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
193
Brett Warren14efd332021-10-06 09:32:11 +0100194 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800195 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000196 const mbedtls_ecp_curve_info *curve_info;
197 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
198 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100199 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200 {
Brett Warren14efd332021-10-06 09:32:11 +0100201 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800202 return( 0 );
203 }
204 }
205#else
206 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208#endif /* MBEDTLS_ECDH_C */
209
210 /*
211 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800212 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213 */
214
215 return( ret );
216}
217
218/*
219 * ssl_tls13_write_key_share_ext
220 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800221 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222 *
223 * struct {
224 * NamedGroup group;
225 * opaque key_exchange<1..2^16-1>;
226 * } KeyShareEntry;
227 * struct {
228 * KeyShareEntry client_shares<0..2^16-1>;
229 * } KeyShareClientHello;
230 */
231static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
232 unsigned char *buf,
233 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000234 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800235{
236 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000237 unsigned char *client_shares; /* Start of client_shares */
238 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
241
Xiaofei Baid25fab62021-12-02 06:36:27 +0000242 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800243
Jerry Yub60e3cf2021-09-08 16:41:02 +0800244 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 * - extension_type (2 bytes)
246 * - extension_data_length (2 bytes)
247 * - client_shares_length (2 bytes)
248 */
249 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
250 p += 6;
251
252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
253
254 /* HRR could already have requested something else. */
255 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800256 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
257 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800258 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800259 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260 &group_id ) );
261 }
262
263 /*
264 * Dispatch to type-specific key generation function.
265 *
266 * So far, we're only supporting ECDHE. With the introduction
267 * of PQC KEMs, we'll want to have multiple branches, one per
268 * type of KEM, and dispatch to the corresponding crypto. And
269 * only one key share entry is allowed.
270 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000271 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800273 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800275 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000276 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800277 /* Length of key_exchange */
278 size_t key_exchange_len;
279
280 /* Check there is space for header of KeyShareEntry
281 * - group (2 bytes)
282 * - key_exchange_length (2 bytes)
283 */
284 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
285 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800286 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
287 p, end,
288 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 p += key_exchange_len;
290 if( ret != 0 )
291 return( ret );
292
293 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000294 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 }
298 else
299#endif /* MBEDTLS_ECDH_C */
300 if( 0 /* other KEMs? */ )
301 {
302 /* Do something */
303 }
304 else
305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
306
Jerry Yub60e3cf2021-09-08 16:41:02 +0800307 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000308 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800309 if( client_shares_len == 0)
310 {
311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
312 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800313 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314 /* Write extension_type */
315 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
316 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800317 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800318 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800319 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320
321 /* Update offered_group_id field */
322 ssl->handshake->offered_group_id = group_id;
323
324 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000325 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800326
Xiaofei Baid25fab62021-12-02 06:36:27 +0000327 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328
329 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
330
331cleanup:
332
333 return( ret );
334}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800335
Jerry Yue1b9c292021-09-10 10:08:31 +0800336#if defined(MBEDTLS_ECDH_C)
337
Jerry Yuc068b662021-10-11 22:30:19 +0800338static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800339{
340 const mbedtls_ecp_curve_info *curve_info;
341 mbedtls_ecp_group_id grp_id;
342#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
343 grp_id = ssl->handshake->ecdh_ctx.grp.id;
344#else
345 grp_id = ssl->handshake->ecdh_ctx.grp_id;
346#endif
347
348 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
349 if( curve_info == NULL )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
353 }
354
355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
356
Jerry Yue1b9c292021-09-10 10:08:31 +0800357 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800358 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800359
360 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
361 MBEDTLS_DEBUG_ECDH_QP );
362
363 return( 0 );
364}
365
Jerry Yuc068b662021-10-11 22:30:19 +0800366static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
367 const unsigned char *buf,
368 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800369{
370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
371
Jerry Yuc068b662021-10-11 22:30:19 +0800372 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800373 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800374 if( ret != 0 )
375 {
376 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800377
378 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
379 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
380 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800381 }
382
Jerry Yuc068b662021-10-11 22:30:19 +0800383 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800384 {
Jerry Yuc068b662021-10-11 22:30:19 +0800385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800386
Jerry Yu4a173382021-10-11 21:45:31 +0800387 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
388 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
389 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800390 }
391
392 return( 0 );
393}
Jerry Yu4a173382021-10-11 21:45:31 +0800394#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800395
XiaokangQiand59be772022-01-24 10:12:51 +0000396/*
397 * ssl_tls13_parse_hrr_key_share_ext()
398 * Parse key_share extension in Hello Retry Request
399 *
400 * struct {
401 * NamedGroup selected_group;
402 * } KeyShareHelloRetryRequest;
403 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000404static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000405 const unsigned char *buf,
406 const unsigned char *end )
407{
XiaokangQianb851da82022-01-14 04:03:11 +0000408 const mbedtls_ecp_curve_info *curve_info = NULL;
409 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000410 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000411 int found = 0;
412
413 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
414 if( group_list == NULL )
415 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
416
417 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
418
419 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000420 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000421 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000423
424 /* Upon receipt of this extension in a HelloRetryRequest, the client
425 * MUST first verify that the selected_group field corresponds to a
426 * group which was provided in the "supported_groups" extension in the
427 * original ClientHello.
428 * The supported_group was based on the info in ssl->conf->group_list.
429 *
430 * If the server provided a key share that was not sent in the ClientHello
431 * then the client MUST abort the handshake with an "illegal_parameter" alert.
432 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000433 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000434 {
435 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000436 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000437 continue;
438
439 /* We found a match */
440 found = 1;
441 break;
442 }
443
444 /* Client MUST verify that the selected_group field does not
445 * correspond to a group which was provided in the "key_share"
446 * extension in the original ClientHello. If the server sent an
447 * HRR message with a key share already provided in the
448 * ClientHello then the client MUST abort the handshake with
449 * an "illegal_parameter" alert.
450 */
XiaokangQiand59be772022-01-24 10:12:51 +0000451 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000452 {
453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
454 MBEDTLS_SSL_PEND_FATAL_ALERT(
455 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
456 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
457 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
458 }
459
460 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000461 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000462
463 return( 0 );
464}
465
Jerry Yue1b9c292021-09-10 10:08:31 +0800466/*
Jerry Yub85277e2021-10-13 13:36:05 +0800467 * ssl_tls13_parse_key_share_ext()
468 * Parse key_share extension in Server Hello
469 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800470 * struct {
471 * KeyShareEntry server_share;
472 * } KeyShareServerHello;
473 * struct {
474 * NamedGroup group;
475 * opaque key_exchange<1..2^16-1>;
476 * } KeyShareEntry;
477 */
Jerry Yu4a173382021-10-11 21:45:31 +0800478static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800479 const unsigned char *buf,
480 const unsigned char *end )
481{
Jerry Yub85277e2021-10-13 13:36:05 +0800482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800483 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800484 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800485
Jerry Yu4a173382021-10-11 21:45:31 +0800486 /* ...
487 * NamedGroup group; (2 bytes)
488 * ...
489 */
490 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
491 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800492 p += 2;
493
Jerry Yu4a173382021-10-11 21:45:31 +0800494 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800496 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800497 {
498 MBEDTLS_SSL_DEBUG_MSG( 1,
499 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800500 (unsigned) offered_group, (unsigned) group ) );
501 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
502 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
503 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800504 }
505
506#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800507 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800508 {
509 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800510 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800511 if( ret != 0 )
512 return( ret );
513 }
Jerry Yub85277e2021-10-13 13:36:05 +0800514 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800515#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800516 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800517 {
518 /* Do something */
519 }
520 else
521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
522
523 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
524 return( ret );
525}
526
Jerry Yubc20bdd2021-08-24 15:59:48 +0800527#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
528
XiaokangQiand59be772022-01-24 10:12:51 +0000529/*
530 * ssl_tls13_parse_cookie_ext()
531 * Parse cookie extension in Hello Retry Request
532 *
533 * struct {
534 * opaque cookie<1..2^16-1>;
535 * } Cookie;
536 *
537 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
538 * extension to the client (this is an exception to the usual rule that
539 * the only extensions that may be sent are those that appear in the
540 * ClientHello). When sending the new ClientHello, the client MUST copy
541 * the contents of the extension received in the HelloRetryRequest into
542 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
543 * cookies in their initial ClientHello in subsequent connections.
544 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000545static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
546 const unsigned char *buf,
547 const unsigned char *end )
548{
549 size_t cookie_len;
550 const unsigned char *p = buf;
551 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
552
553 /* Retrieve length field of cookie */
554 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
555 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
556 p += 2;
557
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
559 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
560
561 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000562 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000563 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
564 if( handshake->verify_cookie == NULL )
565 {
566 MBEDTLS_SSL_DEBUG_MSG( 1,
567 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
568 cookie_len ) );
569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
570 }
571
572 memcpy( handshake->verify_cookie, p, cookie_len );
573 handshake->verify_cookie_len = (unsigned char) cookie_len;
574
575 return( 0 );
576}
XiaokangQian43550bd2022-01-21 04:32:58 +0000577
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800578/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800579 * CipherSuite cipher_suites<2..2^16-2>;
580 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800581static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800582 mbedtls_ssl_context *ssl,
583 unsigned char *buf,
584 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000585 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800586{
Jerry Yufec982e2021-09-07 17:26:06 +0800587 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800588 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000589 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800590 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800591
Xiaofei Baid25fab62021-12-02 06:36:27 +0000592 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800593
594 /*
595 * Ciphersuite list
596 *
597 * This is a list of the symmetric cipher options supported by
598 * the client, specifically the record protection algorithm
599 * ( including secret key length ) and a hash to be used with
600 * HKDF, in descending order of client preference.
601 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800602 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800603
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800604 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800605 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
606 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800607
Jerry Yu0c63af62021-09-02 12:59:12 +0800608 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000609 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800610 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800611 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800612 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800613 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800614
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800615 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800616 if( ciphersuite_info == NULL )
617 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800618 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
619 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800620 continue;
621
622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800623 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800624 ciphersuite_info->name ) );
625
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800626 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800627 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
628 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
629 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800630 }
631
Jerry Yu0c63af62021-09-02 12:59:12 +0800632 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000633 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800634 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800635 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800636 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
637 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800638
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800639 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000640 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800641
Jerry Yu6a643102021-08-31 14:40:36 +0800642 return( 0 );
643}
644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645/*
646 * Structure of ClientHello message:
647 *
648 * struct {
649 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
650 * Random random;
651 * opaque legacy_session_id<0..32>;
652 * CipherSuite cipher_suites<2..2^16-2>;
653 * opaque legacy_compression_methods<1..2^8-1>;
654 * Extension extensions<8..2^16-1>;
655 * } ClientHello;
656 */
Jerry Yu08906d02021-08-31 11:05:27 +0800657static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800658 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800659 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000660 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800661{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800662
Jerry Yubc20bdd2021-08-24 15:59:48 +0800663 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000664 unsigned char *p_extensions_len; /* Pointer to extensions length */
665 size_t output_len; /* Length of buffer used by function */
666 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800667
Jerry Yubc20bdd2021-08-24 15:59:48 +0800668 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800669 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800670
Xiaofei Baid25fab62021-12-02 06:36:27 +0000671 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800672
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800673 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800674 ssl->major_ver = ssl->conf->min_major_ver;
675 ssl->minor_ver = ssl->conf->min_minor_ver;
676
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800677 /*
678 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800679 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800680 *
681 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800682 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800683 */
Jerry Yufec982e2021-09-07 17:26:06 +0800684 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800685 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800686 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800687
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800688 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800689 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
690 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800691 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800692 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
693 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800694
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800695 /*
696 * Write legacy_session_id
697 *
698 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
699 * which has been merged with pre-shared keys in this version. A client
700 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
701 * this field to that value. In compatibility mode, this field MUST be
702 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
703 * a new 32-byte value. This value need not be random but SHOULD be
704 * unpredictable to avoid implementations fixating on a specific value
705 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
706 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800707 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100708#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
709 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
710 *p++ = (unsigned char)ssl->session_negotiate->id_len;
711 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
712 p += ssl->session_negotiate->id_len;
713
714 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
715 ssl->session_negotiate->id_len );
716#else
Jerry Yubbe09522021-09-06 21:17:54 +0800717 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
718 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100719#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800720
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800721 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800722 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800723 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800724 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800725 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800726
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800727 /* Write legacy_compression_methods
728 *
729 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800730 * one byte set to zero, which corresponds to the 'null' compression
731 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800732 */
Jerry Yubbe09522021-09-06 21:17:54 +0800733 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
734 *p++ = 1;
735 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800736
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800737 /* Write extensions */
738
739 /* Keeping track of the included extensions */
740 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800741
742 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800743 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000744 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800745 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800746
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800747 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800748 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800749 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750 */
Jerry Yubbe09522021-09-06 21:17:54 +0800751 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800752 if( ret != 0 )
753 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800754 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
756#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800757
Jerry Yub925f212022-01-12 11:17:02 +0800758 /*
759 * Add the extensions related to (EC)DHE ephemeral key establishment only if
760 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800761 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800762 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
763 {
764 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
765 if( ret != 0 )
766 return( ret );
767 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800768
Jerry Yuf46b0162022-01-11 16:28:00 +0800769 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
770 if( ret != 0 )
771 return( ret );
772 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800773
Jerry Yuf017ee42022-01-12 15:49:48 +0800774 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800775 if( ret != 0 )
776 return( ret );
777 p += output_len;
778 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800779#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
780
Xiaofei Bai15a56812021-11-05 10:52:12 +0000781#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000782 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000783 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
784 if( ret != 0 )
785 return( ret );
786 p += output_len;
787#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
788
Jerry Yubc20bdd2021-08-24 15:59:48 +0800789 /* Add more extensions here */
790
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800791 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000792 extensions_len = p - p_extensions_len - 2;
793 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800795 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000796 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797
Xiaofei Baid25fab62021-12-02 06:36:27 +0000798 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800799 return( 0 );
800}
801
Jerry Yu335aca92021-09-12 20:18:56 +0800802static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800803{
Jerry Yu92c6b402021-08-27 16:59:09 +0800804 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
805 return( 0 );
806}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800807
Jerry Yu92c6b402021-08-27 16:59:09 +0800808static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
809{
810 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800811
Jerry Yu92c6b402021-08-27 16:59:09 +0800812 if( ssl->conf->f_rng == NULL )
813 {
814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
815 return( MBEDTLS_ERR_SSL_NO_RNG );
816 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800817
Jerry Yu92c6b402021-08-27 16:59:09 +0800818 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
819 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800820 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800821 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800822 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800823 return( ret );
824 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800825
Ronald Cron49ad6192021-11-24 16:25:31 +0100826#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
827 /*
828 * Create a session identifier for the purpose of middlebox compatibility
829 * only if one has not been created already.
830 */
831 if( ssl->session_negotiate->id_len == 0 )
832 {
833 /* Creating a session id with 32 byte length */
834 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
835 ssl->session_negotiate->id, 32 ) ) != 0 )
836 {
837 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
838 return( ret );
839 }
840 ssl->session_negotiate->id_len = 32;
841 }
842#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
843
Jerry Yu6f13f642021-08-26 17:18:15 +0800844 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800845}
846
Jerry Yu92c6b402021-08-27 16:59:09 +0800847/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800848 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800849 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800850 */
851static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800852{
Jerry Yu92c6b402021-08-27 16:59:09 +0800853 int ret = 0;
854 unsigned char *buf;
855 size_t buf_len, msg_len;
856
857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
858
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800859 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800860
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800861 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
862 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
863 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800864
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800865 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800866 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800867 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800868
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800869 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
870 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800871 msg_len );
872 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800873
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800874 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
875 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
876 buf_len,
877 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800878
879cleanup:
880
881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
882 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800883}
884
Jerry Yu687101b2021-09-14 16:03:56 +0800885/*
Jerry Yu4a173382021-10-11 21:45:31 +0800886 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800887 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800888/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800889 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
890 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000891 * to indicate which message is expected and to be parsed next.
892 */
Jerry Yub85277e2021-10-13 13:36:05 +0800893#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
894#define SSL_SERVER_HELLO_COORDINATE_HRR 1
895static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
896 const unsigned char *buf,
897 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800898{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800899 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800900 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
901 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
902 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
903 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
904
905 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
906 *
Jerry Yu4a173382021-10-11 21:45:31 +0800907 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800908 * special value of the SHA-256 of "HelloRetryRequest".
909 *
910 * struct {
911 * ProtocolVersion legacy_version = 0x0303;
912 * Random random;
913 * opaque legacy_session_id_echo<0..32>;
914 * CipherSuite cipher_suite;
915 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800916 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 * } ServerHello;
918 *
919 */
Jerry Yub85277e2021-10-13 13:36:05 +0800920 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800921
922 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800923 {
Jerry Yub85277e2021-10-13 13:36:05 +0800924 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 }
926
Jerry Yub85277e2021-10-13 13:36:05 +0800927 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800928}
929
Jerry Yu745bb612021-10-13 22:01:04 +0800930/* Fetch and preprocess
931 * Returns a negative value on failure, and otherwise
932 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
933 * - SSL_SERVER_HELLO_COORDINATE_HRR
934 */
Jerry Yub85277e2021-10-13 13:36:05 +0800935static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
936 unsigned char **buf,
937 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800938{
Jerry Yu4a173382021-10-11 21:45:31 +0800939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800940
XiaokangQian355e09a2022-01-20 11:14:50 +0000941 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
942 MBEDTLS_SSL_HS_SERVER_HELLO,
943 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800944
Jerry Yub85277e2021-10-13 13:36:05 +0800945 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
946 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 {
Jerry Yu745bb612021-10-13 22:01:04 +0800948 case SSL_SERVER_HELLO_COORDINATE_HELLO:
949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
950 break;
951 case SSL_SERVER_HELLO_COORDINATE_HRR:
952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000953 /* If a client receives a second
954 * HelloRetryRequest in the same connection (i.e., where the ClientHello
955 * was itself in response to a HelloRetryRequest), it MUST abort the
956 * handshake with an "unexpected_message" alert.
957 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000958 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000959 {
960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
961 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
962 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
963 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
964 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000965 /*
966 * Clients must abort the handshake with an "illegal_parameter"
967 * alert if the HelloRetryRequest would not result in any change
968 * in the ClientHello.
969 * In a PSK only key exchange that what we expect.
970 */
971 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
972 {
973 MBEDTLS_SSL_DEBUG_MSG( 1,
974 ( "Unexpected HRR in pure PSK key exchange." ) );
975 MBEDTLS_SSL_PEND_FATAL_ALERT(
976 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
977 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
978 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
979 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000980
XiaokangQiand9e068e2022-01-18 06:23:32 +0000981 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000982
Jerry Yu745bb612021-10-13 22:01:04 +0800983 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800984 }
985
986cleanup:
987
988 return( ret );
989}
990
Jerry Yu4a173382021-10-11 21:45:31 +0800991static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
992 const unsigned char **buf,
993 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800994{
995 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800996 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997
Jerry Yude4fb2c2021-09-19 18:05:08 +0800998 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800999 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001000
Jerry Yu4a173382021-10-11 21:45:31 +08001001 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001002
1003 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001004 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1005 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001006 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1008 ssl->session_negotiate->id,
1009 ssl->session_negotiate->id_len );
1010 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001011 legacy_session_id_echo_len );
1012
1013 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1014 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1015
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1017 }
1018
Jerry Yu4a173382021-10-11 21:45:31 +08001019 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 *buf = p;
1021
1022 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001023 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001024 return( 0 );
1025}
1026
Jerry Yu4a173382021-10-11 21:45:31 +08001027static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1028 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001029{
Jerry Yu4a173382021-10-11 21:45:31 +08001030 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1031
Jerry Yue1b9c292021-09-10 10:08:31 +08001032 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001033 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 {
Jerry Yu4a173382021-10-11 21:45:31 +08001035 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001036 {
1037 return( 1 );
1038 }
1039 }
1040 return( 0 );
1041}
1042
1043/* Parse ServerHello message and configure context
1044 *
1045 * struct {
1046 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1047 * Random random;
1048 * opaque legacy_session_id_echo<0..32>;
1049 * CipherSuite cipher_suite;
1050 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001051 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001052 * } ServerHello;
1053 */
Jerry Yuc068b662021-10-11 22:30:19 +08001054static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1055 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001056 const unsigned char *end,
1057 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001058{
Jerry Yub85277e2021-10-13 13:36:05 +08001059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001061 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001062 size_t extensions_len;
1063 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001064 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001066 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001067 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001068
1069 /*
1070 * Check there is space for minimal fields
1071 *
1072 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001073 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001074 * - legacy_session_id_echo ( 1 byte ), minimum size
1075 * - cipher_suite ( 2 bytes)
1076 * - legacy_compression_method ( 1 byte )
1077 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001078 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001079
1080 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1082
Jerry Yu4a173382021-10-11 21:45:31 +08001083 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001084 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001085 * ...
1086 * with ProtocolVersion defined as:
1087 * uint16 ProtocolVersion;
1088 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1090 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1091 {
1092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1093 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1094 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001095 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1096 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001097 }
1098 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001100 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001101 * Random random;
1102 * ...
1103 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001104 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001105 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001106 if( !is_hrr )
1107 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001108 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001109 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1110 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1111 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1112 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001113 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001114
Jerry Yu4a173382021-10-11 21:45:31 +08001115 /* ...
1116 * opaque legacy_session_id_echo<0..32>;
1117 * ...
1118 */
1119 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 {
XiaokangQian52da5582022-01-26 09:49:29 +00001121 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001122 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001123 }
1124
Jerry Yu4a173382021-10-11 21:45:31 +08001125 /* ...
1126 * CipherSuite cipher_suite;
1127 * ...
1128 * with CipherSuite defined as:
1129 * uint8 CipherSuite[2];
1130 */
1131 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001132 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1133 p += 2;
1134
Jerry Yu4a173382021-10-11 21:45:31 +08001135
XiaokangQian355e09a2022-01-20 11:14:50 +00001136 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001137 /*
1138 * Check whether this ciphersuite is supported and offered.
1139 * Via the force_ciphersuite version we may have instructed the client
1140 * to use a different ciphersuite.
1141 */
Jerry Yu4a173382021-10-11 21:45:31 +08001142 if( ciphersuite_info == NULL ||
1143 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 {
XiaokangQian52da5582022-01-26 09:49:29 +00001145 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001147 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001148 * If we received an HRR before and that the proposed selected
1149 * ciphersuite in this server hello is not the same as the one
1150 * proposed in the HRR, we abort the handshake and send an
1151 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001152 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001153 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1154 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001155 {
XiaokangQian52da5582022-01-26 09:49:29 +00001156 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001157 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001158
XiaokangQian52da5582022-01-26 09:49:29 +00001159 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001160 {
1161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1162 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001163 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001164 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001165
Jerry Yu4a173382021-10-11 21:45:31 +08001166 /* Configure ciphersuites */
1167 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1168
XiaokangQian355e09a2022-01-20 11:14:50 +00001169 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001170 ssl->session_negotiate->ciphersuite = cipher_suite;
1171
Jerry Yue1b9c292021-09-10 10:08:31 +08001172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1173 cipher_suite, ciphersuite_info->name ) );
1174
1175#if defined(MBEDTLS_HAVE_TIME)
1176 ssl->session_negotiate->start = time( NULL );
1177#endif /* MBEDTLS_HAVE_TIME */
1178
Jerry Yu4a173382021-10-11 21:45:31 +08001179 /* ...
1180 * uint8 legacy_compression_method = 0;
1181 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001182 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001183 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 if( p[0] != 0 )
1185 {
Jerry Yub85277e2021-10-13 13:36:05 +08001186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001187 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001188 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 }
1190 p++;
1191
Jerry Yub85277e2021-10-13 13:36:05 +08001192 /* ...
1193 * Extension extensions<6..2^16-1>;
1194 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001195 * struct {
1196 * ExtensionType extension_type; (2 bytes)
1197 * opaque extension_data<0..2^16-1>;
1198 * } Extension;
1199 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001200 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001201 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001202 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001203
Jerry Yu4a173382021-10-11 21:45:31 +08001204 /* Check extensions do not go beyond the buffer of data. */
1205 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1206 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001207
Jerry Yu4a173382021-10-11 21:45:31 +08001208 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001209
Jerry Yu4a173382021-10-11 21:45:31 +08001210 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001211 {
1212 unsigned int extension_type;
1213 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001214 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001215
Jerry Yu4a173382021-10-11 21:45:31 +08001216 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1218 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1219 p += 4;
1220
Jerry Yu4a173382021-10-11 21:45:31 +08001221 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001222 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001223
1224 switch( extension_type )
1225 {
XiaokangQianb851da82022-01-14 04:03:11 +00001226 case MBEDTLS_TLS_EXT_COOKIE:
1227
XiaokangQiand9e068e2022-01-18 06:23:32 +00001228 if( !is_hrr )
1229 {
XiaokangQian52da5582022-01-26 09:49:29 +00001230 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001231 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001232 }
1233
XiaokangQian43550bd2022-01-21 04:32:58 +00001234 ret = ssl_tls13_parse_cookie_ext( ssl,
1235 p, extension_data_end );
1236 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001237 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001238 MBEDTLS_SSL_DEBUG_RET( 1,
1239 "ssl_tls13_parse_cookie_ext",
1240 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001241 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001242 }
XiaokangQianb851da82022-01-14 04:03:11 +00001243 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001244
Jerry Yue1b9c292021-09-10 10:08:31 +08001245 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001246 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 MBEDTLS_SSL_DEBUG_MSG( 3,
1248 ( "found supported_versions extension" ) );
1249
Jerry Yuc068b662021-10-11 22:30:19 +08001250 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001251 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001252 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001254 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001255 break;
1256
1257 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001260
XiaokangQian52da5582022-01-26 09:49:29 +00001261 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001262 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001263
1264#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1265 case MBEDTLS_TLS_EXT_KEY_SHARE:
1266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001267 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1268 {
XiaokangQian52da5582022-01-26 09:49:29 +00001269 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001270 goto cleanup;
1271 }
1272
XiaokangQian53f20b72022-01-18 10:47:33 +00001273 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001274 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001275 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001276 else
XiaokangQianb851da82022-01-14 04:03:11 +00001277 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001278 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001279 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 {
1281 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001282 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001283 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001284 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001285 }
1286 break;
1287#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1288
1289 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001290 MBEDTLS_SSL_DEBUG_MSG(
1291 3,
1292 ( "unknown extension found: %u ( ignoring )",
1293 extension_type ) );
1294
XiaokangQian52da5582022-01-26 09:49:29 +00001295 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001296 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001297 }
1298
1299 p += extension_data_len;
1300 }
1301
XiaokangQian78b1fa72022-01-19 06:56:30 +00001302 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001303 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001305 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001306 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001307 }
1308
XiaokangQiand59be772022-01-24 10:12:51 +00001309cleanup:
1310
XiaokangQian52da5582022-01-26 09:49:29 +00001311 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001312 {
1313 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1314 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1315 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1316 }
XiaokangQian52da5582022-01-26 09:49:29 +00001317 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001318 {
1319 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1320 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1321 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1322 }
1323 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001324}
1325
XiaokangQian355e09a2022-01-20 11:14:50 +00001326static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001327{
Jerry Yub85277e2021-10-13 13:36:05 +08001328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001329 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001330 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001331 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001332
Jerry Yub85277e2021-10-13 13:36:05 +08001333 /* Determine the key exchange mode:
1334 * 1) If both the pre_shared_key and key_share extensions were received
1335 * then the key exchange mode is PSK with EPHEMERAL.
1336 * 2) If only the pre_shared_key extension was received then the key
1337 * exchange mode is PSK-only.
1338 * 3) If only the key_share extension was received then the key
1339 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001340 */
Jerry Yub85277e2021-10-13 13:36:05 +08001341 switch( handshake->extensions_present &
1342 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001343 {
Jerry Yu745bb612021-10-13 22:01:04 +08001344 /* Only the pre_shared_key extension was received */
1345 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001346 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001347 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001348
Jerry Yu745bb612021-10-13 22:01:04 +08001349 /* Only the key_share extension was received */
1350 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001351 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001352 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001353
Jerry Yu745bb612021-10-13 22:01:04 +08001354 /* Both the pre_shared_key and key_share extensions were received */
1355 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001356 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001357 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001358
Jerry Yu745bb612021-10-13 22:01:04 +08001359 /* Neither pre_shared_key nor key_share extension was received */
1360 default:
1361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1362 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1363 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001364 }
1365
1366 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1367 *
1368 * TODO: We don't have to do this in case we offered 0-RTT and the
1369 * server accepted it. In this case, we could skip generating
1370 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001371 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001372 if( ret != 0 )
1373 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001375 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001376 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001377 }
1378
1379 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001380 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001381 if( ret != 0 )
1382 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001384 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001385 }
1386
1387 /* Next evolution in key schedule: Establish handshake secret and
1388 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001389 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001390 if( ret != 0 )
1391 {
Jerry Yuc068b662021-10-11 22:30:19 +08001392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1393 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001394 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001395 }
1396
Jerry Yub85277e2021-10-13 13:36:05 +08001397 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001398 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001399 {
1400 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1401 goto cleanup;
1402 }
Jerry Yu0b177842021-09-05 19:41:30 +08001403
1404 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1405 ssl->conf->endpoint,
1406 ssl->session_negotiate->ciphersuite,
1407 &traffic_keys,
1408 ssl );
1409 if( ret != 0 )
1410 {
1411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001412 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001413 }
1414
Jerry Yub85277e2021-10-13 13:36:05 +08001415 handshake->transform_handshake = transform_handshake;
1416 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001417
1418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1419 ssl->session_in = ssl->session_negotiate;
1420
1421 /*
1422 * State machine update
1423 */
1424 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1425
Jerry Yu4a173382021-10-11 21:45:31 +08001426cleanup:
1427
Jerry Yu0b177842021-09-05 19:41:30 +08001428 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001429 if( ret != 0 )
1430 {
Jerry Yub85277e2021-10-13 13:36:05 +08001431 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001432
Jerry Yu4a173382021-10-11 21:45:31 +08001433 MBEDTLS_SSL_PEND_FATAL_ALERT(
1434 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1435 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1436 }
1437 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001438}
1439
XiaokangQian355e09a2022-01-20 11:14:50 +00001440static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001441{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001442#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1444
XiaokangQian647719a2021-12-07 09:16:29 +00001445#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1446 /* If not offering early data, the client sends a dummy CCS record
1447 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001448 * its second ClientHello or before its encrypted handshake flight.
1449 */
XiaokangQian647719a2021-12-07 09:16:29 +00001450 mbedtls_ssl_handshake_set_state( ssl,
1451 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1452#else
1453 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1454#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1455
XiaokangQian78b1fa72022-01-19 06:56:30 +00001456 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001457
XiaokangQian78b1fa72022-01-19 06:56:30 +00001458 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001459 * We are going to re-generate a shared secret corresponding to the group
1460 * selected by the server, which is different from the group for which we
1461 * generated a shared secret in the first client hello.
1462 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001463 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001464 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001465 if( ret != 0 )
1466 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001467#else
1468 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001469#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001470
1471 return( 0 );
1472}
1473
Jerry Yue1b9c292021-09-10 10:08:31 +08001474/*
Jerry Yu4a173382021-10-11 21:45:31 +08001475 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001476 * Handler for MBEDTLS_SSL_SERVER_HELLO
1477 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001478static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001479{
Jerry Yu4a173382021-10-11 21:45:31 +08001480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001481 unsigned char *buf = NULL;
1482 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001483 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001484
1485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1486
1487 /* Coordination step
1488 * - Fetch record
1489 * - Make sure it's either a ServerHello or a HRR.
1490 * - Switch processing routine in case of HRR
1491 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001492 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1493 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1494
XiaokangQian16acd4b2022-01-14 07:35:47 +00001495 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001496 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001497 goto cleanup;
1498 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001499 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001500
XiaokangQianb851da82022-01-14 04:03:11 +00001501 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001502 buf + buf_len,
1503 is_hrr ) );
1504 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001505 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1506
XiaokangQianb851da82022-01-14 04:03:11 +00001507 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1508 MBEDTLS_SSL_HS_SERVER_HELLO,
1509 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001510
XiaokangQiand9e068e2022-01-18 06:23:32 +00001511 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001512 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001513 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001514 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001515
1516cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1518 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001519 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001520}
1521
1522/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523 *
1524 * EncryptedExtensions message
1525 *
1526 * The EncryptedExtensions message contains any extensions which
1527 * should be protected, i.e., any which are not needed to establish
1528 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001529 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001530
1531/*
1532 * Overview
1533 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001534
1535/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001536static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001537
XiaokangQian97799ac2021-10-11 10:05:54 +00001538static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1539 const unsigned char *buf,
1540 const unsigned char *end );
1541static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542
1543/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001544 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001545 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001546static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001547{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001548 int ret;
1549 unsigned char *buf;
1550 size_t buf_len;
1551
1552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1553
Xiaofei Bai746f9482021-11-12 08:53:56 +00001554 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001555 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001556 &buf, &buf_len ) );
1557
1558 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001559 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001560 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001561
Xiaofei Bai746f9482021-11-12 08:53:56 +00001562 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001563 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001564
XiaokangQian97799ac2021-10-11 10:05:54 +00001565 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001566
1567cleanup:
1568
1569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1570 return( ret );
1571
1572}
1573
XiaokangQian08da26c2021-10-09 10:12:11 +00001574/* Parse EncryptedExtensions message
1575 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001576 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001577 * } EncryptedExtensions;
1578 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001579static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1580 const unsigned char *buf,
1581 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001582{
1583 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001584 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001585 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001586 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001587
XiaokangQian08da26c2021-10-09 10:12:11 +00001588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001589 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001590 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591
XiaokangQian97799ac2021-10-11 10:05:54 +00001592 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001593 extensions_end = p + extensions_len;
1594 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001595
XiaokangQian8db25ff2021-10-13 05:56:18 +00001596 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001597 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001598 unsigned int extension_type;
1599 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001600
XiaokangQian08da26c2021-10-09 10:12:11 +00001601 /*
1602 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001603 * ExtensionType extension_type; (2 bytes)
1604 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001605 * } Extension;
1606 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001607 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001608 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1609 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1610 p += 4;
1611
XiaokangQian8db25ff2021-10-13 05:56:18 +00001612 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001613
XiaokangQian97799ac2021-10-11 10:05:54 +00001614 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001615 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001616 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001617 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001618 switch( extension_type )
1619 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001620
XiaokangQian08da26c2021-10-09 10:12:11 +00001621 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1623 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001624
XiaokangQian08da26c2021-10-09 10:12:11 +00001625 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001626 MBEDTLS_SSL_DEBUG_MSG(
1627 3, ( "unsupported extension found: %u ", extension_type) );
1628 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001629 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001630 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1631 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001632 }
1633
XiaokangQian08da26c2021-10-09 10:12:11 +00001634 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001635 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001636
XiaokangQian97799ac2021-10-11 10:05:54 +00001637 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001638 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001639 {
1640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001641 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001642 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001643 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001644 }
1645
1646 return( ret );
1647}
1648
XiaokangQian97799ac2021-10-11 10:05:54 +00001649static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001650{
Jerry Yua93ac112021-10-27 16:31:48 +08001651#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001652 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001653 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1654 else
Jerry Yud2674312021-10-29 10:08:19 +08001655 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001656#else
1657 ((void) ssl);
1658 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1659#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001660 return( 0 );
1661}
1662
Jerry Yua93ac112021-10-27 16:31:48 +08001663#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001664/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665 *
1666 * STATE HANDLING: CertificateRequest
1667 *
Jerry Yud2674312021-10-29 10:08:19 +08001668 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001669#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1670#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001671/* Coordination:
1672 * Deals with the ambiguity of not knowing if a CertificateRequest
1673 * will be sent. Returns a negative code on failure, or
1674 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1675 * - SSL_CERTIFICATE_REQUEST_SKIP
1676 * indicating if a Certificate Request is expected or not.
1677 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001679{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001681
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001682 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001683 {
1684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1685 return( SSL_CERTIFICATE_REQUEST_SKIP );
1686 }
1687
1688 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001689 {
1690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1691 return( ret );
1692 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001693 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001694
1695 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1696 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1697 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001699 }
1700
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001701 return( SSL_CERTIFICATE_REQUEST_SKIP );
1702}
1703
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001704/*
1705 * ssl_tls13_parse_certificate_request()
1706 * Parse certificate request
1707 * struct {
1708 * opaque certificate_request_context<0..2^8-1>;
1709 * Extension extensions<2..2^16-1>;
1710 * } CertificateRequest;
1711 */
1712static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1713 const unsigned char *buf,
1714 const unsigned char *end )
1715{
1716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1717 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001718 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001719 size_t extensions_len = 0;
1720 const unsigned char *extensions_end;
1721 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001722
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001723 /* ...
1724 * opaque certificate_request_context<0..2^8-1>
1725 * ...
1726 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1728 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001729 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001730
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001731 if( certificate_request_context_len > 0 )
1732 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001733 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001734 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1735 p, certificate_request_context_len );
1736
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001737 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001738 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001739 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001740 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001741 {
1742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1743 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1744 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001745 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001746 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001747 p += certificate_request_context_len;
1748 }
1749
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001750 /* ...
1751 * Extension extensions<2..2^16-1>;
1752 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001753 */
1754 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1755 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1756 p += 2;
1757
1758 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1759 extensions_end = p + extensions_len;
1760
1761 while( p < extensions_end )
1762 {
1763 unsigned int extension_type;
1764 size_t extension_data_len;
1765
1766 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1767 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1768 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1769 p += 4;
1770
1771 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1772
1773 switch( extension_type )
1774 {
1775 case MBEDTLS_TLS_EXT_SIG_ALG:
1776 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001777 ( "found signature algorithms extension" ) );
1778 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001779 p + extension_data_len );
1780 if( ret != 0 )
1781 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001782 if( ! sig_alg_ext_found )
1783 sig_alg_ext_found = 1;
1784 else
1785 {
1786 MBEDTLS_SSL_DEBUG_MSG( 3,
1787 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001788 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001789 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001790 break;
1791
1792 default:
1793 MBEDTLS_SSL_DEBUG_MSG(
1794 3,
1795 ( "unknown extension found: %u ( ignoring )",
1796 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001797 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001798 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001799 p += extension_data_len;
1800 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001801 /* Check that we consumed all the message. */
1802 if( p != end )
1803 {
1804 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001805 ( "CertificateRequest misaligned" ) );
1806 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001807 }
1808 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001809 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001810 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001811 MBEDTLS_SSL_DEBUG_MSG( 3,
1812 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001813 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001814 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001815
Jerry Yu7840f812022-01-29 10:26:51 +08001816 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001817 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001818
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001819decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001820 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1821 MBEDTLS_ERR_SSL_DECODE_ERROR );
1822 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001823}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001824
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001825/*
1826 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1827 */
1828static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001829{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001831
1832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1833
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001834 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1835
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001836 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1837 {
1838 unsigned char *buf;
1839 size_t buf_len;
1840
1841 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1842 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1843 &buf, &buf_len ) );
1844
1845 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1846 buf, buf + buf_len ) );
1847
1848 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1849 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1850 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001851 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001852 {
1853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001854 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001855 }
1856 else
1857 {
1858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001859 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1860 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001861 }
1862
1863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001864 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001865
Jerry Yud2674312021-10-29 10:08:19 +08001866 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1867
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001868cleanup:
1869
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1871 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001872}
1873
1874/*
Jerry Yu687101b2021-09-14 16:03:56 +08001875 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1876 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001877static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001878{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001879 int ret;
1880
1881 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001882 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001883 return( ret );
1884
Jerry Yu687101b2021-09-14 16:03:56 +08001885 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1886 return( 0 );
1887}
1888
1889/*
1890 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1891 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001892static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001893{
Jerry Yu30b071c2021-09-12 20:16:03 +08001894 int ret;
1895
1896 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1897 if( ret != 0 )
1898 return( ret );
1899
Jerry Yu687101b2021-09-14 16:03:56 +08001900 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1901 return( 0 );
1902}
Jerry Yua93ac112021-10-27 16:31:48 +08001903#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001904
Jerry Yu687101b2021-09-14 16:03:56 +08001905/*
1906 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1907 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001908static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001909{
XiaokangQianac0385c2021-11-03 06:40:11 +00001910 int ret;
1911
XiaokangQianc5c39d52021-11-09 11:55:10 +00001912 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001913 if( ret != 0 )
1914 return( ret );
1915
Ronald Cron49ad6192021-11-24 16:25:31 +01001916#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1917 mbedtls_ssl_handshake_set_state(
1918 ssl,
1919 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1920#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001921 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001922#endif
1923
XiaokangQianac0385c2021-11-03 06:40:11 +00001924 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001925}
1926
1927/*
Ronald Crond4c64022021-12-06 09:06:46 +01001928 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1929 */
1930#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1931static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1932{
1933 int ret;
1934
1935 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1936 if( ret != 0 )
1937 return( ret );
1938
Ronald Crond4c64022021-12-06 09:06:46 +01001939 return( 0 );
1940}
1941#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1942
1943/*
Jerry Yu687101b2021-09-14 16:03:56 +08001944 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1945 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001946static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001947{
XiaokangQian0fa66432021-11-15 03:33:57 +00001948 int ret;
1949
Ronald Cron49ad6192021-11-24 16:25:31 +01001950 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1951
XiaokangQian0fa66432021-11-15 03:33:57 +00001952 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1953 if( ret != 0 )
1954 return( ret );
1955
1956 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1957 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001958}
1959
1960/*
1961 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1962 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001963static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001964{
Jerry Yu378254d2021-10-30 21:44:47 +08001965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001966 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001967 return( 0 );
1968}
1969
1970/*
1971 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1972 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001973static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001974{
Jerry Yu378254d2021-10-30 21:44:47 +08001975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1976 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1977
1978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1979 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1980
1981 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1982
1983 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1984 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001985}
1986
Jerry Yu92c6b402021-08-27 16:59:09 +08001987int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001988{
Jerry Yu92c6b402021-08-27 16:59:09 +08001989 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001990
Jerry Yue3b34122021-09-28 17:53:35 +08001991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1992 mbedtls_ssl_states_str( ssl->state ),
1993 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001994
1995 switch( ssl->state )
1996 {
1997 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001998 * ssl->state is initialized as HELLO_REQUEST. It is the same
1999 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002000 */
2001 case MBEDTLS_SSL_HELLO_REQUEST:
2002 case MBEDTLS_SSL_CLIENT_HELLO:
2003 ret = ssl_tls13_write_client_hello( ssl );
2004 break;
2005
2006 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002007 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002008 break;
2009
2010 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002011 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002012 break;
2013
Jerry Yua93ac112021-10-27 16:31:48 +08002014#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002015 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2016 ret = ssl_tls13_process_certificate_request( ssl );
2017 break;
2018
Jerry Yu687101b2021-09-14 16:03:56 +08002019 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002020 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002021 break;
2022
2023 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002024 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002025 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002026#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002027
2028 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002029 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002030 break;
2031
Jerry Yu687101b2021-09-14 16:03:56 +08002032 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002033 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002034 break;
2035
2036 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002037 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002038 break;
2039
2040 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002041 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002042 break;
2043
Ronald Cron49ad6192021-11-24 16:25:31 +01002044 /*
2045 * Injection of dummy-CCS's for middlebox compatibility
2046 */
2047#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2048 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002049 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01002050 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01002051 break;
2052#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2053
Jerry Yu92c6b402021-08-27 16:59:09 +08002054 default:
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2057 }
2058
2059 return( ret );
2060}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002061
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002062#endif /* MBEDTLS_SSL_CLI_C */
2063
Ronald Cron6f135e12021-12-08 16:57:54 +01002064#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */