blob: 7f120a28ef3866e7ce33ca32028a989291d97aaa [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
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010035#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
52
Xiaofei Baid25fab62021-12-02 06:36:27 +000053 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Jerry Yu159c5a02021-08-31 12:51:25 +080055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu388bd0d2021-09-15 18:41:02 +080057 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080058 * - extension_type (2 bytes)
59 * - extension_data_length (2 bytes)
60 * - versions_length (1 byte )
61 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080062 */
Jerry Yu92c6b402021-08-27 16:59:09 +080063 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
64
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080065 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080067
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080068 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080069 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080073 *p++ = 0x2;
74
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080076 *
Jerry Yu0c63af62021-09-02 12:59:12 +080077 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080080 */
Jerry Yueecfbf02021-08-30 18:32:07 +080081 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
82 ssl->conf->max_minor_ver,
83 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080086 ssl->conf->max_major_ver,
87 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080088
Xiaofei Baid25fab62021-12-02 06:36:27 +000089 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Jerry Yub85277e2021-10-13 13:36:05 +0800100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
101 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800102 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
111 return( 0 );
112}
113
Jerry Yubc20bdd2021-08-24 15:59:48 +0800114#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
115
XiaokangQian16acd4b2022-01-14 07:35:47 +0000116static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000117{
118 uint16_t group_id = ssl->handshake->offered_group_id;
119 if( group_id == 0 )
120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
121
XiaokangQian355e09a2022-01-20 11:14:50 +0000122#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000123 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000124 {
125 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
126 return( 0 );
127 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000128 else
129#endif /* MBEDTLS_ECDH_C */
130 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000131 {
132 /* Do something */
133 }
134
135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
136}
137
138/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800139 * Functions for writing key_share extension.
140 */
141#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800142static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800143 mbedtls_ssl_context *ssl,
144 uint16_t named_group,
145 unsigned char *buf,
146 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000147 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800148{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800150 const mbedtls_ecp_curve_info *curve_info =
151 mbedtls_ecp_curve_info_from_tls_id( named_group );
152
153 if( curve_info == NULL )
154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
155
156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
157
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800158 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
159 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800160 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800162 return( ret );
163 }
164
Xiaofei Baid25fab62021-12-02 06:36:27 +0000165 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
166 buf, end - buf,
167 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800168 if( ret != 0 )
169 {
170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
171 return( ret );
172 }
173
174 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
175 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800176 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800177}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800178#endif /* MBEDTLS_ECDH_C */
179
Jerry Yub60e3cf2021-09-08 16:41:02 +0800180static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
181 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800182{
183 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
184
Jerry Yu56fc07f2021-09-01 17:48:49 +0800185
Jerry Yu56fc07f2021-09-01 17:48:49 +0800186#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100187 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800188 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100189 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800190 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
191
Brett Warren14efd332021-10-06 09:32:11 +0100192 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800193 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000194 const mbedtls_ecp_curve_info *curve_info;
195 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
196 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100197 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800198 {
Brett Warren14efd332021-10-06 09:32:11 +0100199 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200 return( 0 );
201 }
202 }
203#else
204 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800205 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800206#endif /* MBEDTLS_ECDH_C */
207
208 /*
209 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800210 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211 */
212
213 return( ret );
214}
215
216/*
217 * ssl_tls13_write_key_share_ext
218 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800219 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 *
221 * struct {
222 * NamedGroup group;
223 * opaque key_exchange<1..2^16-1>;
224 * } KeyShareEntry;
225 * struct {
226 * KeyShareEntry client_shares<0..2^16-1>;
227 * } KeyShareClientHello;
228 */
229static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
230 unsigned char *buf,
231 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000232 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233{
234 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000235 unsigned char *client_shares; /* Start of client_shares */
236 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800237 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
239
Xiaofei Baid25fab62021-12-02 06:36:27 +0000240 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241
Jerry Yub60e3cf2021-09-08 16:41:02 +0800242 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800243 * - extension_type (2 bytes)
244 * - extension_data_length (2 bytes)
245 * - client_shares_length (2 bytes)
246 */
247 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
248 p += 6;
249
250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
251
252 /* HRR could already have requested something else. */
253 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800254 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
255 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800256 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800257 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800258 &group_id ) );
259 }
260
261 /*
262 * Dispatch to type-specific key generation function.
263 *
264 * So far, we're only supporting ECDHE. With the introduction
265 * of PQC KEMs, we'll want to have multiple branches, one per
266 * type of KEM, and dispatch to the corresponding crypto. And
267 * only one key share entry is allowed.
268 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000269 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800271 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800273 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000274 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275 /* Length of key_exchange */
276 size_t key_exchange_len;
277
278 /* Check there is space for header of KeyShareEntry
279 * - group (2 bytes)
280 * - key_exchange_length (2 bytes)
281 */
282 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
283 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
285 p, end,
286 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 p += key_exchange_len;
288 if( ret != 0 )
289 return( ret );
290
291 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000292 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000294 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295 }
296 else
297#endif /* MBEDTLS_ECDH_C */
298 if( 0 /* other KEMs? */ )
299 {
300 /* Do something */
301 }
302 else
303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
304
Jerry Yub60e3cf2021-09-08 16:41:02 +0800305 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000306 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800307 if( client_shares_len == 0)
308 {
309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800311 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312 /* Write extension_type */
313 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
314 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800315 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800316 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800317 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800318
319 /* Update offered_group_id field */
320 ssl->handshake->offered_group_id = group_id;
321
322 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000323 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324
Xiaofei Baid25fab62021-12-02 06:36:27 +0000325 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800326
327 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
328
329cleanup:
330
331 return( ret );
332}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800333
Jerry Yue1b9c292021-09-10 10:08:31 +0800334#if defined(MBEDTLS_ECDH_C)
335
Jerry Yuc068b662021-10-11 22:30:19 +0800336static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800337{
338 const mbedtls_ecp_curve_info *curve_info;
339 mbedtls_ecp_group_id grp_id;
340#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
341 grp_id = ssl->handshake->ecdh_ctx.grp.id;
342#else
343 grp_id = ssl->handshake->ecdh_ctx.grp_id;
344#endif
345
346 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
347 if( curve_info == NULL )
348 {
349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
351 }
352
353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
354
Jerry Yue1b9c292021-09-10 10:08:31 +0800355 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800356 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800357
358 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
359 MBEDTLS_DEBUG_ECDH_QP );
360
361 return( 0 );
362}
363
Jerry Yuc068b662021-10-11 22:30:19 +0800364static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
365 const unsigned char *buf,
366 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800367{
368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
369
Jerry Yuc068b662021-10-11 22:30:19 +0800370 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800371 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800372 if( ret != 0 )
373 {
374 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800375
376 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
377 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
378 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800379 }
380
Jerry Yuc068b662021-10-11 22:30:19 +0800381 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800382 {
Jerry Yuc068b662021-10-11 22:30:19 +0800383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800384
Jerry Yu4a173382021-10-11 21:45:31 +0800385 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
386 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
387 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800388 }
389
390 return( 0 );
391}
Jerry Yu4a173382021-10-11 21:45:31 +0800392#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800393
XiaokangQiand59be772022-01-24 10:12:51 +0000394/*
395 * ssl_tls13_parse_hrr_key_share_ext()
396 * Parse key_share extension in Hello Retry Request
397 *
398 * struct {
399 * NamedGroup selected_group;
400 * } KeyShareHelloRetryRequest;
401 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000402static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000403 const unsigned char *buf,
404 const unsigned char *end )
405{
XiaokangQianb851da82022-01-14 04:03:11 +0000406 const mbedtls_ecp_curve_info *curve_info = NULL;
407 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000408 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000409 int found = 0;
410
411 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
412 if( group_list == NULL )
413 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
414
415 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
416
417 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000418 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000419 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000421
422 /* Upon receipt of this extension in a HelloRetryRequest, the client
423 * MUST first verify that the selected_group field corresponds to a
424 * group which was provided in the "supported_groups" extension in the
425 * original ClientHello.
426 * The supported_group was based on the info in ssl->conf->group_list.
427 *
428 * If the server provided a key share that was not sent in the ClientHello
429 * then the client MUST abort the handshake with an "illegal_parameter" alert.
430 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000431 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000432 {
433 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000434 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000435 continue;
436
437 /* We found a match */
438 found = 1;
439 break;
440 }
441
442 /* Client MUST verify that the selected_group field does not
443 * correspond to a group which was provided in the "key_share"
444 * extension in the original ClientHello. If the server sent an
445 * HRR message with a key share already provided in the
446 * ClientHello then the client MUST abort the handshake with
447 * an "illegal_parameter" alert.
448 */
XiaokangQiand59be772022-01-24 10:12:51 +0000449 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000450 {
451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
452 MBEDTLS_SSL_PEND_FATAL_ALERT(
453 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
454 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
455 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
456 }
457
458 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000459 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000460
461 return( 0 );
462}
463
Jerry Yue1b9c292021-09-10 10:08:31 +0800464/*
Jerry Yub85277e2021-10-13 13:36:05 +0800465 * ssl_tls13_parse_key_share_ext()
466 * Parse key_share extension in Server Hello
467 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800468 * struct {
469 * KeyShareEntry server_share;
470 * } KeyShareServerHello;
471 * struct {
472 * NamedGroup group;
473 * opaque key_exchange<1..2^16-1>;
474 * } KeyShareEntry;
475 */
Jerry Yu4a173382021-10-11 21:45:31 +0800476static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800477 const unsigned char *buf,
478 const unsigned char *end )
479{
Jerry Yub85277e2021-10-13 13:36:05 +0800480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800481 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800482 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800483
Jerry Yu4a173382021-10-11 21:45:31 +0800484 /* ...
485 * NamedGroup group; (2 bytes)
486 * ...
487 */
488 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
489 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800490 p += 2;
491
Jerry Yu4a173382021-10-11 21:45:31 +0800492 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800493 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800494 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 {
496 MBEDTLS_SSL_DEBUG_MSG( 1,
497 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800498 (unsigned) offered_group, (unsigned) group ) );
499 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
500 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
501 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800502 }
503
504#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800505 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800506 {
507 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800508 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800509 if( ret != 0 )
510 return( ret );
511 }
Jerry Yub85277e2021-10-13 13:36:05 +0800512 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800513#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800514 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800515 {
516 /* Do something */
517 }
518 else
519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
520
521 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
522 return( ret );
523}
524
Jerry Yubc20bdd2021-08-24 15:59:48 +0800525#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
526
XiaokangQiand59be772022-01-24 10:12:51 +0000527/*
528 * ssl_tls13_parse_cookie_ext()
529 * Parse cookie extension in Hello Retry Request
530 *
531 * struct {
532 * opaque cookie<1..2^16-1>;
533 * } Cookie;
534 *
535 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
536 * extension to the client (this is an exception to the usual rule that
537 * the only extensions that may be sent are those that appear in the
538 * ClientHello). When sending the new ClientHello, the client MUST copy
539 * the contents of the extension received in the HelloRetryRequest into
540 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
541 * cookies in their initial ClientHello in subsequent connections.
542 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000543static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
544 const unsigned char *buf,
545 const unsigned char *end )
546{
547 size_t cookie_len;
548 const unsigned char *p = buf;
549 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
550
551 /* Retrieve length field of cookie */
552 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
553 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
554 p += 2;
555
556 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
557 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
558
559 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000560 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000561 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
562 if( handshake->verify_cookie == NULL )
563 {
564 MBEDTLS_SSL_DEBUG_MSG( 1,
565 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
566 cookie_len ) );
567 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
568 }
569
570 memcpy( handshake->verify_cookie, p, cookie_len );
571 handshake->verify_cookie_len = (unsigned char) cookie_len;
572
573 return( 0 );
574}
XiaokangQian43550bd2022-01-21 04:32:58 +0000575
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800576/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800577 * CipherSuite cipher_suites<2..2^16-2>;
578 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800579static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800580 mbedtls_ssl_context *ssl,
581 unsigned char *buf,
582 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000583 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800584{
Jerry Yufec982e2021-09-07 17:26:06 +0800585 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800586 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000587 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800588 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800589
Xiaofei Baid25fab62021-12-02 06:36:27 +0000590 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800591
592 /*
593 * Ciphersuite list
594 *
595 * This is a list of the symmetric cipher options supported by
596 * the client, specifically the record protection algorithm
597 * ( including secret key length ) and a hash to be used with
598 * HKDF, in descending order of client preference.
599 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800600 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800601
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800602 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800603 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
604 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800605
Jerry Yu0c63af62021-09-02 12:59:12 +0800606 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000607 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800608 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800609 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800610 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800611 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800612
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800613 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800614 if( ciphersuite_info == NULL )
615 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800616 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
617 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800618 continue;
619
620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800621 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800622 ciphersuite_info->name ) );
623
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800624 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800625 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
626 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
627 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800628 }
629
Jerry Yu0c63af62021-09-02 12:59:12 +0800630 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000631 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800632 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800633 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800634 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
635 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800636
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800637 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000638 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800639
Jerry Yu6a643102021-08-31 14:40:36 +0800640 return( 0 );
641}
642
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800643/*
644 * Structure of ClientHello message:
645 *
646 * struct {
647 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
648 * Random random;
649 * opaque legacy_session_id<0..32>;
650 * CipherSuite cipher_suites<2..2^16-2>;
651 * opaque legacy_compression_methods<1..2^8-1>;
652 * Extension extensions<8..2^16-1>;
653 * } ClientHello;
654 */
Jerry Yu08906d02021-08-31 11:05:27 +0800655static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800656 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800657 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000658 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800659{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800660
Jerry Yubc20bdd2021-08-24 15:59:48 +0800661 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000662 unsigned char *p_extensions_len; /* Pointer to extensions length */
663 size_t output_len; /* Length of buffer used by function */
664 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800665
Jerry Yubc20bdd2021-08-24 15:59:48 +0800666 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800667 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800668
Xiaofei Baid25fab62021-12-02 06:36:27 +0000669 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800670
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800671 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800672 ssl->major_ver = ssl->conf->min_major_ver;
673 ssl->minor_ver = ssl->conf->min_minor_ver;
674
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800675 /*
676 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800677 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800678 *
679 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800680 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681 */
Jerry Yufec982e2021-09-07 17:26:06 +0800682 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800683 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800684 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800685
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800686 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800687 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
688 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800689 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800690 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
691 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800692
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800693 /*
694 * Write legacy_session_id
695 *
696 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
697 * which has been merged with pre-shared keys in this version. A client
698 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
699 * this field to that value. In compatibility mode, this field MUST be
700 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
701 * a new 32-byte value. This value need not be random but SHOULD be
702 * unpredictable to avoid implementations fixating on a specific value
703 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
704 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100706#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
707 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
708 *p++ = (unsigned char)ssl->session_negotiate->id_len;
709 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
710 p += ssl->session_negotiate->id_len;
711
712 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
713 ssl->session_negotiate->id_len );
714#else
Jerry Yubbe09522021-09-06 21:17:54 +0800715 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
716 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100717#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800718
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800719 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800720 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800721 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800722 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800723 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800724
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800725 /* Write legacy_compression_methods
726 *
727 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800728 * one byte set to zero, which corresponds to the 'null' compression
729 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800730 */
Jerry Yubbe09522021-09-06 21:17:54 +0800731 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
732 *p++ = 1;
733 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800734
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800735 /* Write extensions */
736
737 /* Keeping track of the included extensions */
738 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800739
740 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800741 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000742 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800743 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800744
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800745 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800746 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800747 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800748 */
Jerry Yubbe09522021-09-06 21:17:54 +0800749 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800750 if( ret != 0 )
751 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800752 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800753
754#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
Jerry Yub925f212022-01-12 11:17:02 +0800756 /*
757 * Add the extensions related to (EC)DHE ephemeral key establishment only if
758 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800760 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
761 {
762 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
763 if( ret != 0 )
764 return( ret );
765 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800766
Jerry Yuf46b0162022-01-11 16:28:00 +0800767 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
768 if( ret != 0 )
769 return( ret );
770 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800771
Jerry Yuf017ee42022-01-12 15:49:48 +0800772 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800773 if( ret != 0 )
774 return( ret );
775 p += output_len;
776 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800777#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
778
Xiaofei Bai15a56812021-11-05 10:52:12 +0000779#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000780 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000781 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
782 if( ret != 0 )
783 return( ret );
784 p += output_len;
785#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
786
Jerry Yubc20bdd2021-08-24 15:59:48 +0800787 /* Add more extensions here */
788
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800789 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000790 extensions_len = p - p_extensions_len - 2;
791 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800793 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000794 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800795
Xiaofei Baid25fab62021-12-02 06:36:27 +0000796 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797 return( 0 );
798}
799
Jerry Yu335aca92021-09-12 20:18:56 +0800800static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800801{
Jerry Yu92c6b402021-08-27 16:59:09 +0800802 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
803 return( 0 );
804}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800805
Jerry Yu92c6b402021-08-27 16:59:09 +0800806static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
807{
808 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800809
Jerry Yu92c6b402021-08-27 16:59:09 +0800810 if( ssl->conf->f_rng == NULL )
811 {
812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
813 return( MBEDTLS_ERR_SSL_NO_RNG );
814 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800815
Jerry Yu92c6b402021-08-27 16:59:09 +0800816 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
817 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800818 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800819 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800820 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800821 return( ret );
822 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800823
Ronald Cron49ad6192021-11-24 16:25:31 +0100824#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
825 /*
826 * Create a session identifier for the purpose of middlebox compatibility
827 * only if one has not been created already.
828 */
829 if( ssl->session_negotiate->id_len == 0 )
830 {
831 /* Creating a session id with 32 byte length */
832 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
833 ssl->session_negotiate->id, 32 ) ) != 0 )
834 {
835 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
836 return( ret );
837 }
838 ssl->session_negotiate->id_len = 32;
839 }
840#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
841
Jerry Yu6f13f642021-08-26 17:18:15 +0800842 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800843}
844
Jerry Yu92c6b402021-08-27 16:59:09 +0800845/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800846 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800847 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800848 */
849static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800850{
Jerry Yu92c6b402021-08-27 16:59:09 +0800851 int ret = 0;
852 unsigned char *buf;
853 size_t buf_len, msg_len;
854
855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
856
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800857 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800858
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800859 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
860 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
861 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800862
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800863 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800864 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800865 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800866
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800867 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
868 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800869 msg_len );
870 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800871
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800872 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
873 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
874 buf_len,
875 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800876
877cleanup:
878
879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
880 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800881}
882
Jerry Yu687101b2021-09-14 16:03:56 +0800883/*
Jerry Yu4a173382021-10-11 21:45:31 +0800884 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800885 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800886/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800887 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
888 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000889 * to indicate which message is expected and to be parsed next.
890 */
Jerry Yub85277e2021-10-13 13:36:05 +0800891#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
892#define SSL_SERVER_HELLO_COORDINATE_HRR 1
893static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
894 const unsigned char *buf,
895 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800896{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800897 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800898 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
899 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
900 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
901 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
902
903 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
904 *
Jerry Yu4a173382021-10-11 21:45:31 +0800905 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 * special value of the SHA-256 of "HelloRetryRequest".
907 *
908 * struct {
909 * ProtocolVersion legacy_version = 0x0303;
910 * Random random;
911 * opaque legacy_session_id_echo<0..32>;
912 * CipherSuite cipher_suite;
913 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800914 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 * } ServerHello;
916 *
917 */
Jerry Yub85277e2021-10-13 13:36:05 +0800918 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800919
920 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 {
Jerry Yub85277e2021-10-13 13:36:05 +0800922 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800923 }
924
Jerry Yub85277e2021-10-13 13:36:05 +0800925 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800926}
927
Jerry Yu745bb612021-10-13 22:01:04 +0800928/* Fetch and preprocess
929 * Returns a negative value on failure, and otherwise
930 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
931 * - SSL_SERVER_HELLO_COORDINATE_HRR
932 */
Jerry Yub85277e2021-10-13 13:36:05 +0800933static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
934 unsigned char **buf,
935 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800936{
Jerry Yu4a173382021-10-11 21:45:31 +0800937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800938
XiaokangQian355e09a2022-01-20 11:14:50 +0000939 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
940 MBEDTLS_SSL_HS_SERVER_HELLO,
941 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800942
Jerry Yub85277e2021-10-13 13:36:05 +0800943 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
944 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800945 {
Jerry Yu745bb612021-10-13 22:01:04 +0800946 case SSL_SERVER_HELLO_COORDINATE_HELLO:
947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
948 break;
949 case SSL_SERVER_HELLO_COORDINATE_HRR:
950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000951 /* If a client receives a second
952 * HelloRetryRequest in the same connection (i.e., where the ClientHello
953 * was itself in response to a HelloRetryRequest), it MUST abort the
954 * handshake with an "unexpected_message" alert.
955 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000956 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000957 {
958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
959 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
960 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
962 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000963 /*
964 * Clients must abort the handshake with an "illegal_parameter"
965 * alert if the HelloRetryRequest would not result in any change
966 * in the ClientHello.
967 * In a PSK only key exchange that what we expect.
968 */
969 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
970 {
971 MBEDTLS_SSL_DEBUG_MSG( 1,
972 ( "Unexpected HRR in pure PSK key exchange." ) );
973 MBEDTLS_SSL_PEND_FATAL_ALERT(
974 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
975 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
976 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
977 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000978
XiaokangQiand9e068e2022-01-18 06:23:32 +0000979 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000980
Jerry Yu745bb612021-10-13 22:01:04 +0800981 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800982 }
983
984cleanup:
985
986 return( ret );
987}
988
Jerry Yu4a173382021-10-11 21:45:31 +0800989static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
990 const unsigned char **buf,
991 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800992{
993 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800994 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800995
Jerry Yude4fb2c2021-09-19 18:05:08 +0800996 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800997 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800998
Jerry Yu4a173382021-10-11 21:45:31 +0800999 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001000
1001 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001002 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1003 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001004 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1006 ssl->session_negotiate->id,
1007 ssl->session_negotiate->id_len );
1008 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001009 legacy_session_id_echo_len );
1010
1011 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1012 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1013
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1015 }
1016
Jerry Yu4a173382021-10-11 21:45:31 +08001017 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 *buf = p;
1019
1020 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001021 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001022 return( 0 );
1023}
1024
Jerry Yu4a173382021-10-11 21:45:31 +08001025static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1026 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001027{
Jerry Yu4a173382021-10-11 21:45:31 +08001028 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1029
Jerry Yue1b9c292021-09-10 10:08:31 +08001030 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001031 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001032 {
Jerry Yu4a173382021-10-11 21:45:31 +08001033 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 {
1035 return( 1 );
1036 }
1037 }
1038 return( 0 );
1039}
1040
1041/* Parse ServerHello message and configure context
1042 *
1043 * struct {
1044 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1045 * Random random;
1046 * opaque legacy_session_id_echo<0..32>;
1047 * CipherSuite cipher_suite;
1048 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001049 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001050 * } ServerHello;
1051 */
Jerry Yuc068b662021-10-11 22:30:19 +08001052static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1053 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001054 const unsigned char *end,
1055 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001056{
Jerry Yub85277e2021-10-13 13:36:05 +08001057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001059 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001060 size_t extensions_len;
1061 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001063 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001064 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001065 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001066
1067 /*
1068 * Check there is space for minimal fields
1069 *
1070 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001071 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 * - legacy_session_id_echo ( 1 byte ), minimum size
1073 * - cipher_suite ( 2 bytes)
1074 * - legacy_compression_method ( 1 byte )
1075 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001076 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001077
1078 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1080
Jerry Yu4a173382021-10-11 21:45:31 +08001081 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001082 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001083 * ...
1084 * with ProtocolVersion defined as:
1085 * uint16 ProtocolVersion;
1086 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1088 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1089 {
1090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1091 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1092 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001093 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1094 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 }
1096 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001097
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001098 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001099 * Random random;
1100 * ...
1101 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001102 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001103 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001104 if( !is_hrr )
1105 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001106 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001107 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1108 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1109 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1110 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001111 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
Jerry Yu4a173382021-10-11 21:45:31 +08001113 /* ...
1114 * opaque legacy_session_id_echo<0..32>;
1115 * ...
1116 */
1117 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 {
XiaokangQian52da5582022-01-26 09:49:29 +00001119 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001120 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 }
1122
Jerry Yu4a173382021-10-11 21:45:31 +08001123 /* ...
1124 * CipherSuite cipher_suite;
1125 * ...
1126 * with CipherSuite defined as:
1127 * uint8 CipherSuite[2];
1128 */
1129 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001130 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1131 p += 2;
1132
Jerry Yu4a173382021-10-11 21:45:31 +08001133
XiaokangQian355e09a2022-01-20 11:14:50 +00001134 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001135 /*
1136 * Check whether this ciphersuite is supported and offered.
1137 * Via the force_ciphersuite version we may have instructed the client
1138 * to use a different ciphersuite.
1139 */
Jerry Yu4a173382021-10-11 21:45:31 +08001140 if( ciphersuite_info == NULL ||
1141 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001142 {
XiaokangQian52da5582022-01-26 09:49:29 +00001143 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001145 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001146 * If we received an HRR before and that the proposed selected
1147 * ciphersuite in this server hello is not the same as the one
1148 * proposed in the HRR, we abort the handshake and send an
1149 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001150 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001151 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1152 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001153 {
XiaokangQian52da5582022-01-26 09:49:29 +00001154 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001155 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001156
XiaokangQian52da5582022-01-26 09:49:29 +00001157 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001158 {
1159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1160 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001161 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001162 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001163
Jerry Yu4a173382021-10-11 21:45:31 +08001164 /* Configure ciphersuites */
1165 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1166
XiaokangQian355e09a2022-01-20 11:14:50 +00001167 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001168 ssl->session_negotiate->ciphersuite = cipher_suite;
1169
Jerry Yue1b9c292021-09-10 10:08:31 +08001170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1171 cipher_suite, ciphersuite_info->name ) );
1172
1173#if defined(MBEDTLS_HAVE_TIME)
1174 ssl->session_negotiate->start = time( NULL );
1175#endif /* MBEDTLS_HAVE_TIME */
1176
Jerry Yu4a173382021-10-11 21:45:31 +08001177 /* ...
1178 * uint8 legacy_compression_method = 0;
1179 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001181 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001182 if( p[0] != 0 )
1183 {
Jerry Yub85277e2021-10-13 13:36:05 +08001184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001185 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001186 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001187 }
1188 p++;
1189
Jerry Yub85277e2021-10-13 13:36:05 +08001190 /* ...
1191 * Extension extensions<6..2^16-1>;
1192 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001193 * struct {
1194 * ExtensionType extension_type; (2 bytes)
1195 * opaque extension_data<0..2^16-1>;
1196 * } Extension;
1197 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001198 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001199 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001200 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001201
Jerry Yu4a173382021-10-11 21:45:31 +08001202 /* Check extensions do not go beyond the buffer of data. */
1203 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1204 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001205
Jerry Yu4a173382021-10-11 21:45:31 +08001206 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001207
Jerry Yu4a173382021-10-11 21:45:31 +08001208 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 {
1210 unsigned int extension_type;
1211 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001212 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001213
Jerry Yu4a173382021-10-11 21:45:31 +08001214 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1216 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1217 p += 4;
1218
Jerry Yu4a173382021-10-11 21:45:31 +08001219 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001220 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001221
1222 switch( extension_type )
1223 {
XiaokangQianb851da82022-01-14 04:03:11 +00001224 case MBEDTLS_TLS_EXT_COOKIE:
1225
XiaokangQiand9e068e2022-01-18 06:23:32 +00001226 if( !is_hrr )
1227 {
XiaokangQian52da5582022-01-26 09:49:29 +00001228 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001229 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001230 }
1231
XiaokangQian43550bd2022-01-21 04:32:58 +00001232 ret = ssl_tls13_parse_cookie_ext( ssl,
1233 p, extension_data_end );
1234 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001235 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001236 MBEDTLS_SSL_DEBUG_RET( 1,
1237 "ssl_tls13_parse_cookie_ext",
1238 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001239 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001240 }
XiaokangQianb851da82022-01-14 04:03:11 +00001241 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001242
Jerry Yue1b9c292021-09-10 10:08:31 +08001243 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001244 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001245 MBEDTLS_SSL_DEBUG_MSG( 3,
1246 ( "found supported_versions extension" ) );
1247
Jerry Yuc068b662021-10-11 22:30:19 +08001248 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001249 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001250 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001251 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001252 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 break;
1254
1255 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001258
XiaokangQian52da5582022-01-26 09:49:29 +00001259 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001260 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261
1262#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1263 case MBEDTLS_TLS_EXT_KEY_SHARE:
1264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001265 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1266 {
XiaokangQian52da5582022-01-26 09:49:29 +00001267 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001268 goto cleanup;
1269 }
1270
XiaokangQian53f20b72022-01-18 10:47:33 +00001271 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001272 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001273 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001274 else
XiaokangQianb851da82022-01-14 04:03:11 +00001275 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001276 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001277 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001278 {
1279 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001280 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001281 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001282 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001283 }
1284 break;
1285#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1286
1287 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001288 MBEDTLS_SSL_DEBUG_MSG(
1289 3,
1290 ( "unknown extension found: %u ( ignoring )",
1291 extension_type ) );
1292
XiaokangQian52da5582022-01-26 09:49:29 +00001293 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001294 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001295 }
1296
1297 p += extension_data_len;
1298 }
1299
XiaokangQian78b1fa72022-01-19 06:56:30 +00001300 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001301 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001303 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001304 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001305 }
1306
XiaokangQiand59be772022-01-24 10:12:51 +00001307cleanup:
1308
XiaokangQian52da5582022-01-26 09:49:29 +00001309 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001310 {
1311 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1312 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1313 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1314 }
XiaokangQian52da5582022-01-26 09:49:29 +00001315 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001316 {
1317 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1318 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1319 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1320 }
1321 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001322}
1323
XiaokangQian355e09a2022-01-20 11:14:50 +00001324static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001325{
Jerry Yub85277e2021-10-13 13:36:05 +08001326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001327 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001328 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001329 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001330
Jerry Yub85277e2021-10-13 13:36:05 +08001331 /* Determine the key exchange mode:
1332 * 1) If both the pre_shared_key and key_share extensions were received
1333 * then the key exchange mode is PSK with EPHEMERAL.
1334 * 2) If only the pre_shared_key extension was received then the key
1335 * exchange mode is PSK-only.
1336 * 3) If only the key_share extension was received then the key
1337 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001338 */
Jerry Yub85277e2021-10-13 13:36:05 +08001339 switch( handshake->extensions_present &
1340 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001341 {
Jerry Yu745bb612021-10-13 22:01:04 +08001342 /* Only the pre_shared_key extension was received */
1343 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001344 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001345 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001346
Jerry Yu745bb612021-10-13 22:01:04 +08001347 /* Only the key_share extension was received */
1348 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001349 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001350 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001351
Jerry Yu745bb612021-10-13 22:01:04 +08001352 /* Both the pre_shared_key and key_share extensions were received */
1353 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001354 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001355 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001356
Jerry Yu745bb612021-10-13 22:01:04 +08001357 /* Neither pre_shared_key nor key_share extension was received */
1358 default:
1359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1360 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1361 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001362 }
1363
1364 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1365 *
1366 * TODO: We don't have to do this in case we offered 0-RTT and the
1367 * server accepted it. In this case, we could skip generating
1368 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001369 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001370 if( ret != 0 )
1371 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001373 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001374 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001375 }
1376
1377 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001378 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001379 if( ret != 0 )
1380 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001382 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001383 }
1384
1385 /* Next evolution in key schedule: Establish handshake secret and
1386 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001387 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001388 if( ret != 0 )
1389 {
Jerry Yuc068b662021-10-11 22:30:19 +08001390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1391 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001392 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001393 }
1394
Jerry Yub85277e2021-10-13 13:36:05 +08001395 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001396 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001397 {
1398 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1399 goto cleanup;
1400 }
Jerry Yu0b177842021-09-05 19:41:30 +08001401
1402 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1403 ssl->conf->endpoint,
1404 ssl->session_negotiate->ciphersuite,
1405 &traffic_keys,
1406 ssl );
1407 if( ret != 0 )
1408 {
1409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001410 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001411 }
1412
Jerry Yub85277e2021-10-13 13:36:05 +08001413 handshake->transform_handshake = transform_handshake;
1414 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001415
1416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1417 ssl->session_in = ssl->session_negotiate;
1418
1419 /*
1420 * State machine update
1421 */
1422 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1423
Jerry Yu4a173382021-10-11 21:45:31 +08001424cleanup:
1425
Jerry Yu0b177842021-09-05 19:41:30 +08001426 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001427 if( ret != 0 )
1428 {
Jerry Yub85277e2021-10-13 13:36:05 +08001429 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001430
Jerry Yu4a173382021-10-11 21:45:31 +08001431 MBEDTLS_SSL_PEND_FATAL_ALERT(
1432 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1433 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1434 }
1435 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001436}
1437
XiaokangQian355e09a2022-01-20 11:14:50 +00001438static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001439{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001440#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1442
XiaokangQian647719a2021-12-07 09:16:29 +00001443#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1444 /* If not offering early data, the client sends a dummy CCS record
1445 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001446 * its second ClientHello or before its encrypted handshake flight.
1447 */
XiaokangQian647719a2021-12-07 09:16:29 +00001448 mbedtls_ssl_handshake_set_state( ssl,
1449 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1450#else
1451 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1452#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1453
XiaokangQian78b1fa72022-01-19 06:56:30 +00001454 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001455
XiaokangQian78b1fa72022-01-19 06:56:30 +00001456 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001457 * We are going to re-generate a shared secret corresponding to the group
1458 * selected by the server, which is different from the group for which we
1459 * generated a shared secret in the first client hello.
1460 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001461 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001462 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001463 if( ret != 0 )
1464 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001465#else
1466 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001467#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001468
1469 return( 0 );
1470}
1471
Jerry Yue1b9c292021-09-10 10:08:31 +08001472/*
Jerry Yu4a173382021-10-11 21:45:31 +08001473 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001474 * Handler for MBEDTLS_SSL_SERVER_HELLO
1475 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001476static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001477{
Jerry Yu4a173382021-10-11 21:45:31 +08001478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001479 unsigned char *buf = NULL;
1480 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001481 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001482
1483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1484
1485 /* Coordination step
1486 * - Fetch record
1487 * - Make sure it's either a ServerHello or a HRR.
1488 * - Switch processing routine in case of HRR
1489 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001490 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1491 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1492
XiaokangQian16acd4b2022-01-14 07:35:47 +00001493 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001494 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001495 goto cleanup;
1496 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001497 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001498
XiaokangQianb851da82022-01-14 04:03:11 +00001499 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001500 buf + buf_len,
1501 is_hrr ) );
1502 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001503 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1504
XiaokangQianb851da82022-01-14 04:03:11 +00001505 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1506 MBEDTLS_SSL_HS_SERVER_HELLO,
1507 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001508
XiaokangQiand9e068e2022-01-18 06:23:32 +00001509 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001510 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001511 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001512 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001513
1514cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1516 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001517 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001518}
1519
1520/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001521 *
1522 * EncryptedExtensions message
1523 *
1524 * The EncryptedExtensions message contains any extensions which
1525 * should be protected, i.e., any which are not needed to establish
1526 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001527 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
1529/*
1530 * Overview
1531 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001532
1533/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001534static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001535
XiaokangQian97799ac2021-10-11 10:05:54 +00001536static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1537 const unsigned char *buf,
1538 const unsigned char *end );
1539static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001540
1541/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001542 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001543 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001544static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001545{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001546 int ret;
1547 unsigned char *buf;
1548 size_t buf_len;
1549
1550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1551
Xiaofei Bai746f9482021-11-12 08:53:56 +00001552 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001553 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001554 &buf, &buf_len ) );
1555
1556 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001557 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001558 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001559
Xiaofei Bai746f9482021-11-12 08:53:56 +00001560 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001561 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001562
XiaokangQian97799ac2021-10-11 10:05:54 +00001563 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001564
1565cleanup:
1566
1567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1568 return( ret );
1569
1570}
1571
XiaokangQian08da26c2021-10-09 10:12:11 +00001572/* Parse EncryptedExtensions message
1573 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001574 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001575 * } EncryptedExtensions;
1576 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001577static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1578 const unsigned char *buf,
1579 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580{
1581 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001582 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001583 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001584 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001585
XiaokangQian08da26c2021-10-09 10:12:11 +00001586 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001587 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001588 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001589
XiaokangQian97799ac2021-10-11 10:05:54 +00001590 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001591 extensions_end = p + extensions_len;
1592 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001593
XiaokangQian8db25ff2021-10-13 05:56:18 +00001594 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001595 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001596 unsigned int extension_type;
1597 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001598
XiaokangQian08da26c2021-10-09 10:12:11 +00001599 /*
1600 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001601 * ExtensionType extension_type; (2 bytes)
1602 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001603 * } Extension;
1604 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001605 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001606 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1607 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1608 p += 4;
1609
XiaokangQian8db25ff2021-10-13 05:56:18 +00001610 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001611
XiaokangQian97799ac2021-10-11 10:05:54 +00001612 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001613 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001614 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001615 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001616 switch( extension_type )
1617 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001618
XiaokangQian08da26c2021-10-09 10:12:11 +00001619 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1621 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001622
XiaokangQian08da26c2021-10-09 10:12:11 +00001623 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001624 MBEDTLS_SSL_DEBUG_MSG(
1625 3, ( "unsupported extension found: %u ", extension_type) );
1626 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001627 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001628 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1629 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001630 }
1631
XiaokangQian08da26c2021-10-09 10:12:11 +00001632 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001633 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001634
XiaokangQian97799ac2021-10-11 10:05:54 +00001635 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001636 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001637 {
1638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001639 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001640 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001641 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001642 }
1643
1644 return( ret );
1645}
1646
XiaokangQian97799ac2021-10-11 10:05:54 +00001647static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001648{
Jerry Yua93ac112021-10-27 16:31:48 +08001649#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001650 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001651 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1652 else
Jerry Yud2674312021-10-29 10:08:19 +08001653 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001654#else
1655 ((void) ssl);
1656 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1657#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001658 return( 0 );
1659}
1660
Jerry Yua93ac112021-10-27 16:31:48 +08001661#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001662/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001663 *
1664 * STATE HANDLING: CertificateRequest
1665 *
Jerry Yud2674312021-10-29 10:08:19 +08001666 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001667#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1668#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669/* Coordination:
1670 * Deals with the ambiguity of not knowing if a CertificateRequest
1671 * will be sent. Returns a negative code on failure, or
1672 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1673 * - SSL_CERTIFICATE_REQUEST_SKIP
1674 * indicating if a Certificate Request is expected or not.
1675 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001676static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001677{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001679
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001680 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001681 {
1682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1683 return( SSL_CERTIFICATE_REQUEST_SKIP );
1684 }
1685
1686 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001687 {
1688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1689 return( ret );
1690 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001692
1693 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1694 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1695 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001696 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001697 }
1698
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001699 return( SSL_CERTIFICATE_REQUEST_SKIP );
1700}
1701
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001702/*
1703 * ssl_tls13_parse_certificate_request()
1704 * Parse certificate request
1705 * struct {
1706 * opaque certificate_request_context<0..2^8-1>;
1707 * Extension extensions<2..2^16-1>;
1708 * } CertificateRequest;
1709 */
1710static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1711 const unsigned char *buf,
1712 const unsigned char *end )
1713{
1714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1715 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001716 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001717 size_t extensions_len = 0;
1718 const unsigned char *extensions_end;
1719 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001720
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001721 /* ...
1722 * opaque certificate_request_context<0..2^8-1>
1723 * ...
1724 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001725 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1726 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001727 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001728
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001729 if( certificate_request_context_len > 0 )
1730 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001731 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001732 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1733 p, certificate_request_context_len );
1734
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001735 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001736 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001737 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001738 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001739 {
1740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1741 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1742 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001743 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001744 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001745 p += certificate_request_context_len;
1746 }
1747
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001748 /* ...
1749 * Extension extensions<2..2^16-1>;
1750 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001751 */
1752 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1753 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1754 p += 2;
1755
1756 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1757 extensions_end = p + extensions_len;
1758
1759 while( p < extensions_end )
1760 {
1761 unsigned int extension_type;
1762 size_t extension_data_len;
1763
1764 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1765 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1766 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1767 p += 4;
1768
1769 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1770
1771 switch( extension_type )
1772 {
1773 case MBEDTLS_TLS_EXT_SIG_ALG:
1774 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001775 ( "found signature algorithms extension" ) );
1776 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001777 p + extension_data_len );
1778 if( ret != 0 )
1779 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001780 if( ! sig_alg_ext_found )
1781 sig_alg_ext_found = 1;
1782 else
1783 {
1784 MBEDTLS_SSL_DEBUG_MSG( 3,
1785 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001786 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001787 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001788 break;
1789
1790 default:
1791 MBEDTLS_SSL_DEBUG_MSG(
1792 3,
1793 ( "unknown extension found: %u ( ignoring )",
1794 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001795 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001796 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797 p += extension_data_len;
1798 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001799 /* Check that we consumed all the message. */
1800 if( p != end )
1801 {
1802 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001803 ( "CertificateRequest misaligned" ) );
1804 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001805 }
1806 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001807 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001808 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001809 MBEDTLS_SSL_DEBUG_MSG( 3,
1810 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001811 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001812 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001813
Jerry Yu7840f812022-01-29 10:26:51 +08001814 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001815 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001816
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001817decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001818 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1819 MBEDTLS_ERR_SSL_DECODE_ERROR );
1820 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001821}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001822
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001823/*
1824 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1825 */
1826static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001827{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001829
1830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1831
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001832 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1833
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001834 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1835 {
1836 unsigned char *buf;
1837 size_t buf_len;
1838
1839 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1840 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1841 &buf, &buf_len ) );
1842
1843 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1844 buf, buf + buf_len ) );
1845
1846 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1847 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1848 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001849 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001850 {
1851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001852 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001853 }
1854 else
1855 {
1856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001857 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1858 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001859 }
1860
1861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001862 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001863
Jerry Yud2674312021-10-29 10:08:19 +08001864 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1865
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001866cleanup:
1867
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1869 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001870}
1871
1872/*
Jerry Yu687101b2021-09-14 16:03:56 +08001873 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1874 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001875static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001876{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001877 int ret;
1878
1879 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001880 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001881 return( ret );
1882
Jerry Yu687101b2021-09-14 16:03:56 +08001883 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1884 return( 0 );
1885}
1886
1887/*
1888 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1889 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001890static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001891{
Jerry Yu30b071c2021-09-12 20:16:03 +08001892 int ret;
1893
1894 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1895 if( ret != 0 )
1896 return( ret );
1897
Jerry Yu687101b2021-09-14 16:03:56 +08001898 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1899 return( 0 );
1900}
Jerry Yua93ac112021-10-27 16:31:48 +08001901#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001902
Jerry Yu687101b2021-09-14 16:03:56 +08001903/*
1904 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1905 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001906static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001907{
XiaokangQianac0385c2021-11-03 06:40:11 +00001908 int ret;
1909
XiaokangQianc5c39d52021-11-09 11:55:10 +00001910 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001911 if( ret != 0 )
1912 return( ret );
1913
Ronald Cron49ad6192021-11-24 16:25:31 +01001914#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1915 mbedtls_ssl_handshake_set_state(
1916 ssl,
1917 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1918#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001919 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001920#endif
1921
XiaokangQianac0385c2021-11-03 06:40:11 +00001922 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001923}
1924
1925/*
Ronald Crond4c64022021-12-06 09:06:46 +01001926 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1927 */
1928#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1929static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1930{
1931 int ret;
1932
1933 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1934 if( ret != 0 )
1935 return( ret );
1936
Ronald Crond4c64022021-12-06 09:06:46 +01001937 return( 0 );
1938}
1939#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1940
1941/*
Jerry Yu687101b2021-09-14 16:03:56 +08001942 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1943 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001944static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001945{
XiaokangQian0fa66432021-11-15 03:33:57 +00001946 int ret;
1947
Ronald Cron49ad6192021-11-24 16:25:31 +01001948 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1949
XiaokangQian0fa66432021-11-15 03:33:57 +00001950 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1951 if( ret != 0 )
1952 return( ret );
1953
1954 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1955 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001956}
1957
1958/*
1959 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1960 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001961static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001962{
Jerry Yu378254d2021-10-30 21:44:47 +08001963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001964 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001965 return( 0 );
1966}
1967
1968/*
1969 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1970 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001971static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001972{
Jerry Yu378254d2021-10-30 21:44:47 +08001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1974 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1975
1976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1977 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1978
1979 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1980
1981 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1982 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001983}
1984
Jerry Yu92c6b402021-08-27 16:59:09 +08001985int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001986{
Jerry Yu92c6b402021-08-27 16:59:09 +08001987 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001988
Jerry Yue3b34122021-09-28 17:53:35 +08001989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1990 mbedtls_ssl_states_str( ssl->state ),
1991 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001992
1993 switch( ssl->state )
1994 {
1995 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001996 * ssl->state is initialized as HELLO_REQUEST. It is the same
1997 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001998 */
1999 case MBEDTLS_SSL_HELLO_REQUEST:
2000 case MBEDTLS_SSL_CLIENT_HELLO:
2001 ret = ssl_tls13_write_client_hello( ssl );
2002 break;
2003
2004 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002005 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002006 break;
2007
2008 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002009 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002010 break;
2011
Jerry Yua93ac112021-10-27 16:31:48 +08002012#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002013 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2014 ret = ssl_tls13_process_certificate_request( ssl );
2015 break;
2016
Jerry Yu687101b2021-09-14 16:03:56 +08002017 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002018 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002019 break;
2020
2021 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002022 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002023 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002024#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002025
2026 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002027 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002028 break;
2029
Jerry Yu687101b2021-09-14 16:03:56 +08002030 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002031 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002032 break;
2033
2034 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002035 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002036 break;
2037
2038 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002039 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002040 break;
2041
Ronald Cron49ad6192021-11-24 16:25:31 +01002042 /*
2043 * Injection of dummy-CCS's for middlebox compatibility
2044 */
2045#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2046 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002047 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01002048 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01002049 break;
2050#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2051
Jerry Yu92c6b402021-08-27 16:59:09 +08002052 default:
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2055 }
2056
2057 return( ret );
2058}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002059
Jerry Yufb4b6472022-01-27 15:03:26 +08002060#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002061
Jerry Yufb4b6472022-01-27 15:03:26 +08002062