blob: e4c3af5b7c0d9d7285c8a70e7ec0ef0d91e064d9 [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{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100149 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
150 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
151 psa_key_attributes_t key_attributes;
152 size_t own_pubkey_len;
153 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
154 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800155
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800157
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100158 /* Convert EC group to PSA key type. */
159 if( ( handshake->ecdh_psa_type =
160 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
161 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800162
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100163 if( ecdh_bits > 0xffff )
164 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
165 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800166
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100167 key_attributes = psa_key_attributes_init();
168 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
169 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
170 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
171 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100172
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100173 /* Generate ECDH private key. */
174 status = psa_generate_key( &key_attributes,
175 &handshake->ecdh_psa_privkey );
176 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800177 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100178 ret = psa_ssl_status_to_mbedtls( status );
179 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800180 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100181
Jerry Yu56fc07f2021-09-01 17:48:49 +0800182 }
183
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100184 /* Export the public part of the ECDH private key from PSA. */
185 status = psa_export_public_key( handshake->ecdh_psa_privkey,
186 buf, (size_t)( end - buf ),
187 &own_pubkey_len );
188 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800189 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100190 ret = psa_ssl_status_to_mbedtls( status );
191 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800192 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100193
Jerry Yu56fc07f2021-09-01 17:48:49 +0800194 }
195
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100196 if( own_pubkey_len > (size_t)( end - buf ) )
197 {
198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
200 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100201
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100202 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100203
Jerry Yu75336352021-09-01 15:59:36 +0800204 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800205}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800206#endif /* MBEDTLS_ECDH_C */
207
Jerry Yub60e3cf2021-09-08 16:41:02 +0800208static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
209 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800210{
211 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100215 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800216 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100217 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800218 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
219
Brett Warren14efd332021-10-06 09:32:11 +0100220 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000222 const mbedtls_ecp_curve_info *curve_info;
223 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
224 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100225 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800226 {
Brett Warren14efd332021-10-06 09:32:11 +0100227 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228 return( 0 );
229 }
230 }
231#else
232 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800233 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800234#endif /* MBEDTLS_ECDH_C */
235
236 /*
237 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800238 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239 */
240
241 return( ret );
242}
243
244/*
245 * ssl_tls13_write_key_share_ext
246 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800247 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800248 *
249 * struct {
250 * NamedGroup group;
251 * opaque key_exchange<1..2^16-1>;
252 * } KeyShareEntry;
253 * struct {
254 * KeyShareEntry client_shares<0..2^16-1>;
255 * } KeyShareClientHello;
256 */
257static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
258 unsigned char *buf,
259 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000260 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800261{
262 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000263 unsigned char *client_shares; /* Start of client_shares */
264 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800266 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
267
Xiaofei Baid25fab62021-12-02 06:36:27 +0000268 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269
Jerry Yub60e3cf2021-09-08 16:41:02 +0800270 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271 * - extension_type (2 bytes)
272 * - extension_data_length (2 bytes)
273 * - client_shares_length (2 bytes)
274 */
275 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
276 p += 6;
277
278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
279
280 /* HRR could already have requested something else. */
281 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800282 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
283 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800284 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800285 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 &group_id ) );
287 }
288
289 /*
290 * Dispatch to type-specific key generation function.
291 *
292 * So far, we're only supporting ECDHE. With the introduction
293 * of PQC KEMs, we'll want to have multiple branches, one per
294 * type of KEM, and dispatch to the corresponding crypto. And
295 * only one key share entry is allowed.
296 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000297 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800299 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800301 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000302 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800303 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100304 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305
306 /* Check there is space for header of KeyShareEntry
307 * - group (2 bytes)
308 * - key_exchange_length (2 bytes)
309 */
310 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
311 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100312 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800313 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314 p += key_exchange_len;
315 if( ret != 0 )
316 return( ret );
317
318 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000319 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000321 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 }
323 else
324#endif /* MBEDTLS_ECDH_C */
325 if( 0 /* other KEMs? */ )
326 {
327 /* Do something */
328 }
329 else
330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
331
Jerry Yub60e3cf2021-09-08 16:41:02 +0800332 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000333 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800334 if( client_shares_len == 0)
335 {
336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800338 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800339 /* Write extension_type */
340 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
341 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800342 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345
346 /* Update offered_group_id field */
347 ssl->handshake->offered_group_id = group_id;
348
349 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000350 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800351
Xiaofei Baid25fab62021-12-02 06:36:27 +0000352 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353
354 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
355
356cleanup:
357
358 return( ret );
359}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800360
Jerry Yue1b9c292021-09-10 10:08:31 +0800361#if defined(MBEDTLS_ECDH_C)
362
Jerry Yuc068b662021-10-11 22:30:19 +0800363static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
364 const unsigned char *buf,
365 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800366{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100367 uint8_t *p = (uint8_t*)buf;
368 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800369
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100370 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
371 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
372 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800373
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100374 /* Check if key size is consistent with given buffer length. */
375 if ( peerkey_len > ( buf_len - 2 ) )
376 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800377
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100378 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100379 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100380 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800381
382 return( 0 );
383}
Jerry Yu4a173382021-10-11 21:45:31 +0800384#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800385
XiaokangQiand59be772022-01-24 10:12:51 +0000386/*
387 * ssl_tls13_parse_hrr_key_share_ext()
388 * Parse key_share extension in Hello Retry Request
389 *
390 * struct {
391 * NamedGroup selected_group;
392 * } KeyShareHelloRetryRequest;
393 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000394static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000395 const unsigned char *buf,
396 const unsigned char *end )
397{
XiaokangQianb851da82022-01-14 04:03:11 +0000398 const mbedtls_ecp_curve_info *curve_info = NULL;
399 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000400 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000401 int found = 0;
402
403 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
404 if( group_list == NULL )
405 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
406
407 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
408
409 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000410 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000411 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000413
414 /* Upon receipt of this extension in a HelloRetryRequest, the client
415 * MUST first verify that the selected_group field corresponds to a
416 * group which was provided in the "supported_groups" extension in the
417 * original ClientHello.
418 * The supported_group was based on the info in ssl->conf->group_list.
419 *
420 * If the server provided a key share that was not sent in the ClientHello
421 * then the client MUST abort the handshake with an "illegal_parameter" alert.
422 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000423 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000424 {
425 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000426 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000427 continue;
428
429 /* We found a match */
430 found = 1;
431 break;
432 }
433
434 /* Client MUST verify that the selected_group field does not
435 * correspond to a group which was provided in the "key_share"
436 * extension in the original ClientHello. If the server sent an
437 * HRR message with a key share already provided in the
438 * ClientHello then the client MUST abort the handshake with
439 * an "illegal_parameter" alert.
440 */
XiaokangQiand59be772022-01-24 10:12:51 +0000441 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000442 {
443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
444 MBEDTLS_SSL_PEND_FATAL_ALERT(
445 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
446 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
447 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
448 }
449
450 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000451 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000452
453 return( 0 );
454}
455
Jerry Yue1b9c292021-09-10 10:08:31 +0800456/*
Jerry Yub85277e2021-10-13 13:36:05 +0800457 * ssl_tls13_parse_key_share_ext()
458 * Parse key_share extension in Server Hello
459 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800460 * struct {
461 * KeyShareEntry server_share;
462 * } KeyShareServerHello;
463 * struct {
464 * NamedGroup group;
465 * opaque key_exchange<1..2^16-1>;
466 * } KeyShareEntry;
467 */
Jerry Yu4a173382021-10-11 21:45:31 +0800468static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 const unsigned char *buf,
470 const unsigned char *end )
471{
Jerry Yub85277e2021-10-13 13:36:05 +0800472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800473 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800474 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800475
Jerry Yu4a173382021-10-11 21:45:31 +0800476 /* ...
477 * NamedGroup group; (2 bytes)
478 * ...
479 */
480 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
481 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800482 p += 2;
483
Jerry Yu4a173382021-10-11 21:45:31 +0800484 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800485 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800486 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1,
489 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800490 (unsigned) offered_group, (unsigned) group ) );
491 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
492 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
493 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800494 }
495
496#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800497 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800498 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100499 const mbedtls_ecp_curve_info *curve_info =
500 mbedtls_ecp_curve_info_from_tls_id( group );
501 if( curve_info == NULL )
502 {
503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
505 }
506
507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
508
Jerry Yuc068b662021-10-11 22:30:19 +0800509 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800510 if( ret != 0 )
511 return( ret );
512 }
Jerry Yub85277e2021-10-13 13:36:05 +0800513 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800514#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800515 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800516 {
517 /* Do something */
518 }
519 else
520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
521
522 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
523 return( ret );
524}
525
Jerry Yubc20bdd2021-08-24 15:59:48 +0800526#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
527
XiaokangQiand59be772022-01-24 10:12:51 +0000528/*
529 * ssl_tls13_parse_cookie_ext()
530 * Parse cookie extension in Hello Retry Request
531 *
532 * struct {
533 * opaque cookie<1..2^16-1>;
534 * } Cookie;
535 *
536 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
537 * extension to the client (this is an exception to the usual rule that
538 * the only extensions that may be sent are those that appear in the
539 * ClientHello). When sending the new ClientHello, the client MUST copy
540 * the contents of the extension received in the HelloRetryRequest into
541 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
542 * cookies in their initial ClientHello in subsequent connections.
543 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000544static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
545 const unsigned char *buf,
546 const unsigned char *end )
547{
548 size_t cookie_len;
549 const unsigned char *p = buf;
550 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
551
552 /* Retrieve length field of cookie */
553 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
554 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
555 p += 2;
556
557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
558 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
559
560 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000561 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000562 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
563 if( handshake->verify_cookie == NULL )
564 {
565 MBEDTLS_SSL_DEBUG_MSG( 1,
566 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
567 cookie_len ) );
568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
569 }
570
571 memcpy( handshake->verify_cookie, p, cookie_len );
572 handshake->verify_cookie_len = (unsigned char) cookie_len;
573
574 return( 0 );
575}
XiaokangQian43550bd2022-01-21 04:32:58 +0000576
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800577/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800578 * CipherSuite cipher_suites<2..2^16-2>;
579 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800580static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800581 mbedtls_ssl_context *ssl,
582 unsigned char *buf,
583 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000584 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800585{
Jerry Yufec982e2021-09-07 17:26:06 +0800586 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800587 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000588 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800589 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800590
Xiaofei Baid25fab62021-12-02 06:36:27 +0000591 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800592
593 /*
594 * Ciphersuite list
595 *
596 * This is a list of the symmetric cipher options supported by
597 * the client, specifically the record protection algorithm
598 * ( including secret key length ) and a hash to be used with
599 * HKDF, in descending order of client preference.
600 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800601 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800602
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800603 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800604 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
605 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800606
Jerry Yu0c63af62021-09-02 12:59:12 +0800607 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000608 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800609 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800610 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800611 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800612 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800613
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800614 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800615 if( ciphersuite_info == NULL )
616 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800617 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
618 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800619 continue;
620
621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800622 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800623 ciphersuite_info->name ) );
624
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800625 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800626 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
627 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
628 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800629 }
630
Jerry Yu0c63af62021-09-02 12:59:12 +0800631 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000632 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800633 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800634 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800635 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
636 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800637
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800638 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000639 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800640
Jerry Yu6a643102021-08-31 14:40:36 +0800641 return( 0 );
642}
643
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800644/*
645 * Structure of ClientHello message:
646 *
647 * struct {
648 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
649 * Random random;
650 * opaque legacy_session_id<0..32>;
651 * CipherSuite cipher_suites<2..2^16-2>;
652 * opaque legacy_compression_methods<1..2^8-1>;
653 * Extension extensions<8..2^16-1>;
654 * } ClientHello;
655 */
Jerry Yu08906d02021-08-31 11:05:27 +0800656static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800657 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800658 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000659 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800660{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800661
Jerry Yubc20bdd2021-08-24 15:59:48 +0800662 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000663 unsigned char *p_extensions_len; /* Pointer to extensions length */
664 size_t output_len; /* Length of buffer used by function */
665 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800666
Jerry Yubc20bdd2021-08-24 15:59:48 +0800667 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800668 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800669
Xiaofei Baid25fab62021-12-02 06:36:27 +0000670 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800671
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800672 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800673 ssl->major_ver = ssl->conf->min_major_ver;
674 ssl->minor_ver = ssl->conf->min_minor_ver;
675
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800676 /*
677 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800678 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800679 *
680 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800682 */
Jerry Yufec982e2021-09-07 17:26:06 +0800683 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800684 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800685 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800687 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800688 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
689 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800690 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800691 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
692 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800693
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800694 /*
695 * Write legacy_session_id
696 *
697 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
698 * which has been merged with pre-shared keys in this version. A client
699 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
700 * this field to that value. In compatibility mode, this field MUST be
701 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
702 * a new 32-byte value. This value need not be random but SHOULD be
703 * unpredictable to avoid implementations fixating on a specific value
704 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
705 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800706 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100707#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
708 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
709 *p++ = (unsigned char)ssl->session_negotiate->id_len;
710 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
711 p += ssl->session_negotiate->id_len;
712
713 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
714 ssl->session_negotiate->id_len );
715#else
Jerry Yubbe09522021-09-06 21:17:54 +0800716 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
717 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100718#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800719
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800720 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800721 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800722 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800723 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800724 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800725
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800726 /* Write legacy_compression_methods
727 *
728 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800729 * one byte set to zero, which corresponds to the 'null' compression
730 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800731 */
Jerry Yubbe09522021-09-06 21:17:54 +0800732 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
733 *p++ = 1;
734 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800735
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800736 /* Write extensions */
737
738 /* Keeping track of the included extensions */
739 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800740
741 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800742 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000743 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800744 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800745
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800746 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800747 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800748 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800749 */
Jerry Yubbe09522021-09-06 21:17:54 +0800750 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800751 if( ret != 0 )
752 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800753 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800754
755#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800756
Jerry Yub925f212022-01-12 11:17:02 +0800757 /*
758 * Add the extensions related to (EC)DHE ephemeral key establishment only if
759 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800760 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800761 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
762 {
763 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
764 if( ret != 0 )
765 return( ret );
766 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800767
Jerry Yuf46b0162022-01-11 16:28:00 +0800768 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
769 if( ret != 0 )
770 return( ret );
771 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800772
Jerry Yuf017ee42022-01-12 15:49:48 +0800773 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800774 if( ret != 0 )
775 return( ret );
776 p += output_len;
777 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800778#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
779
Xiaofei Bai15a56812021-11-05 10:52:12 +0000780#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000781 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000782 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
783 if( ret != 0 )
784 return( ret );
785 p += output_len;
786#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
787
Jerry Yubc20bdd2021-08-24 15:59:48 +0800788 /* Add more extensions here */
789
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800790 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000791 extensions_len = p - p_extensions_len - 2;
792 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800794 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000795 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800796
Xiaofei Baid25fab62021-12-02 06:36:27 +0000797 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800798 return( 0 );
799}
800
Jerry Yu92c6b402021-08-27 16:59:09 +0800801static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
802{
803 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800804
Jerry Yu92c6b402021-08-27 16:59:09 +0800805 if( ssl->conf->f_rng == NULL )
806 {
807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
808 return( MBEDTLS_ERR_SSL_NO_RNG );
809 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800810
Jerry Yu92c6b402021-08-27 16:59:09 +0800811 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
812 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800813 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800814 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800815 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800816 return( ret );
817 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800818
Ronald Cron49ad6192021-11-24 16:25:31 +0100819#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
820 /*
821 * Create a session identifier for the purpose of middlebox compatibility
822 * only if one has not been created already.
823 */
824 if( ssl->session_negotiate->id_len == 0 )
825 {
826 /* Creating a session id with 32 byte length */
827 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
828 ssl->session_negotiate->id, 32 ) ) != 0 )
829 {
830 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
831 return( ret );
832 }
833 ssl->session_negotiate->id_len = 32;
834 }
835#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
836
Jerry Yu6f13f642021-08-26 17:18:15 +0800837 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800838}
839
Jerry Yu92c6b402021-08-27 16:59:09 +0800840/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800841 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800842 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800843 */
844static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800845{
Jerry Yu92c6b402021-08-27 16:59:09 +0800846 int ret = 0;
847 unsigned char *buf;
848 size_t buf_len, msg_len;
849
850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
851
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800852 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800853
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800854 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
855 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
856 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800857
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800858 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800859 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800860 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800861
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800862 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
863 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800864 msg_len );
865 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800866
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800867 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
868 buf_len,
869 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800870
Ronald Cron3addfa42022-02-08 16:10:25 +0100871 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
872
Jerry Yu92c6b402021-08-27 16:59:09 +0800873cleanup:
874
875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
876 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800877}
878
Jerry Yu687101b2021-09-14 16:03:56 +0800879/*
Jerry Yu4a173382021-10-11 21:45:31 +0800880 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800881 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800882/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800883 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
884 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000885 * to indicate which message is expected and to be parsed next.
886 */
Jerry Yub85277e2021-10-13 13:36:05 +0800887#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
888#define SSL_SERVER_HELLO_COORDINATE_HRR 1
889static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
890 const unsigned char *buf,
891 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800892{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800893 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800894 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
895 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
896 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
897 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
898
899 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
900 *
Jerry Yu4a173382021-10-11 21:45:31 +0800901 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800902 * special value of the SHA-256 of "HelloRetryRequest".
903 *
904 * struct {
905 * ProtocolVersion legacy_version = 0x0303;
906 * Random random;
907 * opaque legacy_session_id_echo<0..32>;
908 * CipherSuite cipher_suite;
909 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800910 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 * } ServerHello;
912 *
913 */
Jerry Yub85277e2021-10-13 13:36:05 +0800914 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800915
916 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 {
Jerry Yub85277e2021-10-13 13:36:05 +0800918 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800919 }
920
Jerry Yub85277e2021-10-13 13:36:05 +0800921 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800922}
923
Jerry Yu745bb612021-10-13 22:01:04 +0800924/* Fetch and preprocess
925 * Returns a negative value on failure, and otherwise
926 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
927 * - SSL_SERVER_HELLO_COORDINATE_HRR
928 */
Jerry Yub85277e2021-10-13 13:36:05 +0800929static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
930 unsigned char **buf,
931 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800932{
Jerry Yu4a173382021-10-11 21:45:31 +0800933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800934
XiaokangQian355e09a2022-01-20 11:14:50 +0000935 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
936 MBEDTLS_SSL_HS_SERVER_HELLO,
937 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800938
Jerry Yub85277e2021-10-13 13:36:05 +0800939 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
940 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 {
Jerry Yu745bb612021-10-13 22:01:04 +0800942 case SSL_SERVER_HELLO_COORDINATE_HELLO:
943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
944 break;
945 case SSL_SERVER_HELLO_COORDINATE_HRR:
946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000947 /* If a client receives a second
948 * HelloRetryRequest in the same connection (i.e., where the ClientHello
949 * was itself in response to a HelloRetryRequest), it MUST abort the
950 * handshake with an "unexpected_message" alert.
951 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000952 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000953 {
954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
955 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
956 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
957 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
958 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000959 /*
960 * Clients must abort the handshake with an "illegal_parameter"
961 * alert if the HelloRetryRequest would not result in any change
962 * in the ClientHello.
963 * In a PSK only key exchange that what we expect.
964 */
965 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
966 {
967 MBEDTLS_SSL_DEBUG_MSG( 1,
968 ( "Unexpected HRR in pure PSK key exchange." ) );
969 MBEDTLS_SSL_PEND_FATAL_ALERT(
970 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
971 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
972 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
973 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000974
XiaokangQiand9e068e2022-01-18 06:23:32 +0000975 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000976
Jerry Yu745bb612021-10-13 22:01:04 +0800977 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800978 }
979
980cleanup:
981
982 return( ret );
983}
984
Jerry Yu4a173382021-10-11 21:45:31 +0800985static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
986 const unsigned char **buf,
987 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800988{
989 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800990 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800991
Jerry Yude4fb2c2021-09-19 18:05:08 +0800992 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800993 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800994
Jerry Yu4a173382021-10-11 21:45:31 +0800995 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800996
997 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800998 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
999 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001000 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001001 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1002 ssl->session_negotiate->id,
1003 ssl->session_negotiate->id_len );
1004 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001005 legacy_session_id_echo_len );
1006
1007 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1008 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1009
Jerry Yue1b9c292021-09-10 10:08:31 +08001010 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1011 }
1012
Jerry Yu4a173382021-10-11 21:45:31 +08001013 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 *buf = p;
1015
1016 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001017 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 return( 0 );
1019}
1020
Jerry Yu4a173382021-10-11 21:45:31 +08001021static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1022 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001023{
Jerry Yu4a173382021-10-11 21:45:31 +08001024 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1025
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001027 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 {
Jerry Yu4a173382021-10-11 21:45:31 +08001029 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001030 {
1031 return( 1 );
1032 }
1033 }
1034 return( 0 );
1035}
1036
1037/* Parse ServerHello message and configure context
1038 *
1039 * struct {
1040 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1041 * Random random;
1042 * opaque legacy_session_id_echo<0..32>;
1043 * CipherSuite cipher_suite;
1044 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001045 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 * } ServerHello;
1047 */
Jerry Yuc068b662021-10-11 22:30:19 +08001048static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1049 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001050 const unsigned char *end,
1051 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001052{
Jerry Yub85277e2021-10-13 13:36:05 +08001053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001054 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001055 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001056 size_t extensions_len;
1057 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001059 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001060 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001061 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062
1063 /*
1064 * Check there is space for minimal fields
1065 *
1066 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001067 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 * - legacy_session_id_echo ( 1 byte ), minimum size
1069 * - cipher_suite ( 2 bytes)
1070 * - legacy_compression_method ( 1 byte )
1071 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001073
1074 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001075 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1076
Jerry Yu4a173382021-10-11 21:45:31 +08001077 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001078 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001079 * ...
1080 * with ProtocolVersion defined as:
1081 * uint16 ProtocolVersion;
1082 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001083 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1084 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1085 {
1086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1087 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1088 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001089 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1090 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 }
1092 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001093
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001094 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001095 * Random random;
1096 * ...
1097 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001098 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001099 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001100 if( !is_hrr )
1101 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001102 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001103 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1104 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1105 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1106 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001107 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001108
Jerry Yu4a173382021-10-11 21:45:31 +08001109 /* ...
1110 * opaque legacy_session_id_echo<0..32>;
1111 * ...
1112 */
1113 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001114 {
XiaokangQian52da5582022-01-26 09:49:29 +00001115 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001116 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 }
1118
Jerry Yu4a173382021-10-11 21:45:31 +08001119 /* ...
1120 * CipherSuite cipher_suite;
1121 * ...
1122 * with CipherSuite defined as:
1123 * uint8 CipherSuite[2];
1124 */
1125 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1127 p += 2;
1128
Jerry Yu4a173382021-10-11 21:45:31 +08001129
XiaokangQian355e09a2022-01-20 11:14:50 +00001130 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001131 /*
1132 * Check whether this ciphersuite is supported and offered.
1133 * Via the force_ciphersuite version we may have instructed the client
1134 * to use a different ciphersuite.
1135 */
Jerry Yu4a173382021-10-11 21:45:31 +08001136 if( ciphersuite_info == NULL ||
1137 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001138 {
XiaokangQian52da5582022-01-26 09:49:29 +00001139 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001141 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001142 * If we received an HRR before and that the proposed selected
1143 * ciphersuite in this server hello is not the same as the one
1144 * proposed in the HRR, we abort the handshake and send an
1145 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001146 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001147 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1148 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001149 {
XiaokangQian52da5582022-01-26 09:49:29 +00001150 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001151 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001152
XiaokangQian52da5582022-01-26 09:49:29 +00001153 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001154 {
1155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1156 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001157 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001158 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001159
Jerry Yu4a173382021-10-11 21:45:31 +08001160 /* Configure ciphersuites */
1161 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1162
XiaokangQian355e09a2022-01-20 11:14:50 +00001163 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 ssl->session_negotiate->ciphersuite = cipher_suite;
1165
Jerry Yue1b9c292021-09-10 10:08:31 +08001166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1167 cipher_suite, ciphersuite_info->name ) );
1168
1169#if defined(MBEDTLS_HAVE_TIME)
1170 ssl->session_negotiate->start = time( NULL );
1171#endif /* MBEDTLS_HAVE_TIME */
1172
Jerry Yu4a173382021-10-11 21:45:31 +08001173 /* ...
1174 * uint8 legacy_compression_method = 0;
1175 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001177 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001178 if( p[0] != 0 )
1179 {
Jerry Yub85277e2021-10-13 13:36:05 +08001180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001181 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001182 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001183 }
1184 p++;
1185
Jerry Yub85277e2021-10-13 13:36:05 +08001186 /* ...
1187 * Extension extensions<6..2^16-1>;
1188 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001189 * struct {
1190 * ExtensionType extension_type; (2 bytes)
1191 * opaque extension_data<0..2^16-1>;
1192 * } Extension;
1193 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001194 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001195 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001196 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001197
Jerry Yu4a173382021-10-11 21:45:31 +08001198 /* Check extensions do not go beyond the buffer of data. */
1199 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1200 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
Jerry Yu4a173382021-10-11 21:45:31 +08001202 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001203
Jerry Yu4a173382021-10-11 21:45:31 +08001204 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 {
1206 unsigned int extension_type;
1207 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001208 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001209
Jerry Yu4a173382021-10-11 21:45:31 +08001210 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001211 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1212 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1213 p += 4;
1214
Jerry Yu4a173382021-10-11 21:45:31 +08001215 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001216 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001217
1218 switch( extension_type )
1219 {
XiaokangQianb851da82022-01-14 04:03:11 +00001220 case MBEDTLS_TLS_EXT_COOKIE:
1221
XiaokangQiand9e068e2022-01-18 06:23:32 +00001222 if( !is_hrr )
1223 {
XiaokangQian52da5582022-01-26 09:49:29 +00001224 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001225 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001226 }
1227
XiaokangQian43550bd2022-01-21 04:32:58 +00001228 ret = ssl_tls13_parse_cookie_ext( ssl,
1229 p, extension_data_end );
1230 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001231 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001232 MBEDTLS_SSL_DEBUG_RET( 1,
1233 "ssl_tls13_parse_cookie_ext",
1234 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001235 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001236 }
XiaokangQianb851da82022-01-14 04:03:11 +00001237 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001238
Jerry Yue1b9c292021-09-10 10:08:31 +08001239 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001240 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001241 MBEDTLS_SSL_DEBUG_MSG( 3,
1242 ( "found supported_versions extension" ) );
1243
Jerry Yuc068b662021-10-11 22:30:19 +08001244 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001245 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001246 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001248 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001249 break;
1250
1251 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001254
XiaokangQian52da5582022-01-26 09:49:29 +00001255 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001256 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001257
1258#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1259 case MBEDTLS_TLS_EXT_KEY_SHARE:
1260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001261 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1262 {
XiaokangQian52da5582022-01-26 09:49:29 +00001263 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001264 goto cleanup;
1265 }
1266
XiaokangQian53f20b72022-01-18 10:47:33 +00001267 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001268 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001269 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001270 else
XiaokangQianb851da82022-01-14 04:03:11 +00001271 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001272 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001273 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001274 {
1275 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001276 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001277 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001278 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001279 }
1280 break;
1281#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1282
1283 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001284 MBEDTLS_SSL_DEBUG_MSG(
1285 3,
1286 ( "unknown extension found: %u ( ignoring )",
1287 extension_type ) );
1288
XiaokangQian52da5582022-01-26 09:49:29 +00001289 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001290 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001291 }
1292
1293 p += extension_data_len;
1294 }
1295
XiaokangQian78b1fa72022-01-19 06:56:30 +00001296 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001297 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001299 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001300 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001301 }
1302
XiaokangQiand59be772022-01-24 10:12:51 +00001303cleanup:
1304
XiaokangQian52da5582022-01-26 09:49:29 +00001305 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001306 {
1307 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1308 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1309 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1310 }
XiaokangQian52da5582022-01-26 09:49:29 +00001311 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001312 {
1313 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1314 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1315 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1316 }
1317 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001318}
1319
XiaokangQian355e09a2022-01-20 11:14:50 +00001320static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001321{
Jerry Yub85277e2021-10-13 13:36:05 +08001322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001323 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001324 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001325 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001326
Jerry Yub85277e2021-10-13 13:36:05 +08001327 /* Determine the key exchange mode:
1328 * 1) If both the pre_shared_key and key_share extensions were received
1329 * then the key exchange mode is PSK with EPHEMERAL.
1330 * 2) If only the pre_shared_key extension was received then the key
1331 * exchange mode is PSK-only.
1332 * 3) If only the key_share extension was received then the key
1333 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001334 */
Jerry Yub85277e2021-10-13 13:36:05 +08001335 switch( handshake->extensions_present &
1336 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001337 {
Jerry Yu745bb612021-10-13 22:01:04 +08001338 /* Only the pre_shared_key extension was received */
1339 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001340 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001341 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001342
Jerry Yu745bb612021-10-13 22:01:04 +08001343 /* Only the key_share extension was received */
1344 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001345 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001346 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001347
Jerry Yu745bb612021-10-13 22:01:04 +08001348 /* Both the pre_shared_key and key_share extensions were received */
1349 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001350 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001351 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001352
Jerry Yu745bb612021-10-13 22:01:04 +08001353 /* Neither pre_shared_key nor key_share extension was received */
1354 default:
1355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1356 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1357 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001358 }
1359
1360 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1361 *
1362 * TODO: We don't have to do this in case we offered 0-RTT and the
1363 * server accepted it. In this case, we could skip generating
1364 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001365 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001366 if( ret != 0 )
1367 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001369 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001370 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001371 }
1372
1373 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001374 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001375 if( ret != 0 )
1376 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001378 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001379 }
1380
1381 /* Next evolution in key schedule: Establish handshake secret and
1382 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001383 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001384 if( ret != 0 )
1385 {
Jerry Yuc068b662021-10-11 22:30:19 +08001386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1387 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001388 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001389 }
1390
Jerry Yub85277e2021-10-13 13:36:05 +08001391 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001392 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001393 {
1394 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1395 goto cleanup;
1396 }
Jerry Yu0b177842021-09-05 19:41:30 +08001397
1398 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1399 ssl->conf->endpoint,
1400 ssl->session_negotiate->ciphersuite,
1401 &traffic_keys,
1402 ssl );
1403 if( ret != 0 )
1404 {
1405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001406 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001407 }
1408
Jerry Yub85277e2021-10-13 13:36:05 +08001409 handshake->transform_handshake = transform_handshake;
1410 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001411
1412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1413 ssl->session_in = ssl->session_negotiate;
1414
1415 /*
1416 * State machine update
1417 */
1418 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1419
Jerry Yu4a173382021-10-11 21:45:31 +08001420cleanup:
1421
Jerry Yu0b177842021-09-05 19:41:30 +08001422 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001423 if( ret != 0 )
1424 {
Jerry Yub85277e2021-10-13 13:36:05 +08001425 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001426
Jerry Yu4a173382021-10-11 21:45:31 +08001427 MBEDTLS_SSL_PEND_FATAL_ALERT(
1428 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1429 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1430 }
1431 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001432}
1433
XiaokangQian355e09a2022-01-20 11:14:50 +00001434static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001435{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001436#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1438
XiaokangQian647719a2021-12-07 09:16:29 +00001439#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1440 /* If not offering early data, the client sends a dummy CCS record
1441 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001442 * its second ClientHello or before its encrypted handshake flight.
1443 */
XiaokangQian647719a2021-12-07 09:16:29 +00001444 mbedtls_ssl_handshake_set_state( ssl,
1445 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1446#else
1447 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1448#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1449
XiaokangQian78b1fa72022-01-19 06:56:30 +00001450 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001451
XiaokangQian78b1fa72022-01-19 06:56:30 +00001452 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001453 * We are going to re-generate a shared secret corresponding to the group
1454 * selected by the server, which is different from the group for which we
1455 * generated a shared secret in the first client hello.
1456 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001457 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001458 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001459 if( ret != 0 )
1460 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001461#else
1462 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001463#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001464
1465 return( 0 );
1466}
1467
Jerry Yue1b9c292021-09-10 10:08:31 +08001468/*
Jerry Yu4a173382021-10-11 21:45:31 +08001469 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001470 * Handler for MBEDTLS_SSL_SERVER_HELLO
1471 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001472static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001473{
Jerry Yu4a173382021-10-11 21:45:31 +08001474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001475 unsigned char *buf = NULL;
1476 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001477 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001478
1479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1480
1481 /* Coordination step
1482 * - Fetch record
1483 * - Make sure it's either a ServerHello or a HRR.
1484 * - Switch processing routine in case of HRR
1485 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001486 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1487 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1488
XiaokangQian16acd4b2022-01-14 07:35:47 +00001489 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001490 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001491 goto cleanup;
1492 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001493 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001494
XiaokangQianb851da82022-01-14 04:03:11 +00001495 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001496 buf + buf_len,
1497 is_hrr ) );
1498 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001499 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1500
XiaokangQianb851da82022-01-14 04:03:11 +00001501 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1502 MBEDTLS_SSL_HS_SERVER_HELLO,
1503 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001504
XiaokangQiand9e068e2022-01-18 06:23:32 +00001505 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001506 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001507 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001508 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001509
1510cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1512 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001513 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001514}
1515
1516/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001517 *
1518 * EncryptedExtensions message
1519 *
1520 * The EncryptedExtensions message contains any extensions which
1521 * should be protected, i.e., any which are not needed to establish
1522 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001523 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524
1525/*
1526 * Overview
1527 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
1529/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001530static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001531
XiaokangQian97799ac2021-10-11 10:05:54 +00001532static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1533 const unsigned char *buf,
1534 const unsigned char *end );
1535static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001536
1537/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001538 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001539 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001540static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001541{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542 int ret;
1543 unsigned char *buf;
1544 size_t buf_len;
1545
1546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1547
Xiaofei Bai746f9482021-11-12 08:53:56 +00001548 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001549 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001550 &buf, &buf_len ) );
1551
1552 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001553 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001554 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001555
Xiaofei Bai746f9482021-11-12 08:53:56 +00001556 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001557 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001558
XiaokangQian97799ac2021-10-11 10:05:54 +00001559 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560
1561cleanup:
1562
1563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1564 return( ret );
1565
1566}
1567
XiaokangQian08da26c2021-10-09 10:12:11 +00001568/* Parse EncryptedExtensions message
1569 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001570 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001571 * } EncryptedExtensions;
1572 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001573static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1574 const unsigned char *buf,
1575 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001576{
1577 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001578 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001579 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001580 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001581
XiaokangQian08da26c2021-10-09 10:12:11 +00001582 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001583 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001584 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001585
XiaokangQian97799ac2021-10-11 10:05:54 +00001586 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001587 extensions_end = p + extensions_len;
1588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001589
XiaokangQian8db25ff2021-10-13 05:56:18 +00001590 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001592 unsigned int extension_type;
1593 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001594
XiaokangQian08da26c2021-10-09 10:12:11 +00001595 /*
1596 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001597 * ExtensionType extension_type; (2 bytes)
1598 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001599 * } Extension;
1600 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001601 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001602 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1603 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1604 p += 4;
1605
XiaokangQian8db25ff2021-10-13 05:56:18 +00001606 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001607
XiaokangQian97799ac2021-10-11 10:05:54 +00001608 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001609 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001610 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001611 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001612 switch( extension_type )
1613 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001614
XiaokangQian08da26c2021-10-09 10:12:11 +00001615 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1617 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001618
XiaokangQian08da26c2021-10-09 10:12:11 +00001619 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001620 MBEDTLS_SSL_DEBUG_MSG(
1621 3, ( "unsupported extension found: %u ", extension_type) );
1622 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001623 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001624 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1625 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001626 }
1627
XiaokangQian08da26c2021-10-09 10:12:11 +00001628 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001629 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001630
XiaokangQian97799ac2021-10-11 10:05:54 +00001631 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001632 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001633 {
1634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001635 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001636 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001637 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001638 }
1639
1640 return( ret );
1641}
1642
XiaokangQian97799ac2021-10-11 10:05:54 +00001643static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001644{
Jerry Yua93ac112021-10-27 16:31:48 +08001645#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001646 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001647 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1648 else
Jerry Yud2674312021-10-29 10:08:19 +08001649 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001650#else
1651 ((void) ssl);
1652 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1653#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001654 return( 0 );
1655}
1656
Jerry Yua93ac112021-10-27 16:31:48 +08001657#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001658/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659 *
1660 * STATE HANDLING: CertificateRequest
1661 *
Jerry Yud2674312021-10-29 10:08:19 +08001662 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001663#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1664#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665/* Coordination:
1666 * Deals with the ambiguity of not knowing if a CertificateRequest
1667 * will be sent. Returns a negative code on failure, or
1668 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1669 * - SSL_CERTIFICATE_REQUEST_SKIP
1670 * indicating if a Certificate Request is expected or not.
1671 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001673{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001675
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001676 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677 {
1678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1679 return( SSL_CERTIFICATE_REQUEST_SKIP );
1680 }
1681
1682 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001683 {
1684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1685 return( ret );
1686 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001687 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001688
1689 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1690 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1691 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001693 }
1694
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695 return( SSL_CERTIFICATE_REQUEST_SKIP );
1696}
1697
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698/*
1699 * ssl_tls13_parse_certificate_request()
1700 * Parse certificate request
1701 * struct {
1702 * opaque certificate_request_context<0..2^8-1>;
1703 * Extension extensions<2..2^16-1>;
1704 * } CertificateRequest;
1705 */
1706static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1707 const unsigned char *buf,
1708 const unsigned char *end )
1709{
1710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1711 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001712 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001713 size_t extensions_len = 0;
1714 const unsigned char *extensions_end;
1715 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001716
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001717 /* ...
1718 * opaque certificate_request_context<0..2^8-1>
1719 * ...
1720 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001721 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1722 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001723 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001724
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001725 if( certificate_request_context_len > 0 )
1726 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001728 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1729 p, certificate_request_context_len );
1730
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001731 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001732 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001733 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001734 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001735 {
1736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1737 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1738 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001739 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001740 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001741 p += certificate_request_context_len;
1742 }
1743
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001744 /* ...
1745 * Extension extensions<2..2^16-1>;
1746 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001747 */
1748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1749 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1750 p += 2;
1751
1752 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1753 extensions_end = p + extensions_len;
1754
1755 while( p < extensions_end )
1756 {
1757 unsigned int extension_type;
1758 size_t extension_data_len;
1759
1760 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1761 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1762 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1763 p += 4;
1764
1765 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1766
1767 switch( extension_type )
1768 {
1769 case MBEDTLS_TLS_EXT_SIG_ALG:
1770 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001771 ( "found signature algorithms extension" ) );
1772 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001773 p + extension_data_len );
1774 if( ret != 0 )
1775 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001776 if( ! sig_alg_ext_found )
1777 sig_alg_ext_found = 1;
1778 else
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 3,
1781 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001782 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001783 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001784 break;
1785
1786 default:
1787 MBEDTLS_SSL_DEBUG_MSG(
1788 3,
1789 ( "unknown extension found: %u ( ignoring )",
1790 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001791 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001792 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001793 p += extension_data_len;
1794 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001795 /* Check that we consumed all the message. */
1796 if( p != end )
1797 {
1798 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001799 ( "CertificateRequest misaligned" ) );
1800 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001801 }
1802 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001803 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001804 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001805 MBEDTLS_SSL_DEBUG_MSG( 3,
1806 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001807 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001808 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809
Jerry Yu7840f812022-01-29 10:26:51 +08001810 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001811 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001812
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001813decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001814 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1815 MBEDTLS_ERR_SSL_DECODE_ERROR );
1816 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001817}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001818
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001819/*
1820 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1821 */
1822static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001823{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001825
1826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1827
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001828 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1829
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001830 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1831 {
1832 unsigned char *buf;
1833 size_t buf_len;
1834
1835 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1836 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1837 &buf, &buf_len ) );
1838
1839 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1840 buf, buf + buf_len ) );
1841
1842 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1843 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1844 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001845 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001846 {
1847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001848 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001849 }
1850 else
1851 {
1852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001853 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1854 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001855 }
1856
1857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001858 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001859
Jerry Yud2674312021-10-29 10:08:19 +08001860 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1861
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001862cleanup:
1863
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1865 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001866}
1867
1868/*
Jerry Yu687101b2021-09-14 16:03:56 +08001869 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1870 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001871static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001872{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001873 int ret;
1874
1875 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001876 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001877 return( ret );
1878
Jerry Yu687101b2021-09-14 16:03:56 +08001879 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1880 return( 0 );
1881}
1882
1883/*
1884 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1885 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001886static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001887{
Jerry Yu30b071c2021-09-12 20:16:03 +08001888 int ret;
1889
1890 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1891 if( ret != 0 )
1892 return( ret );
1893
Jerry Yu687101b2021-09-14 16:03:56 +08001894 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1895 return( 0 );
1896}
Jerry Yua93ac112021-10-27 16:31:48 +08001897#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001898
Jerry Yu687101b2021-09-14 16:03:56 +08001899/*
1900 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1901 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001902static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001903{
XiaokangQianac0385c2021-11-03 06:40:11 +00001904 int ret;
1905
XiaokangQianc5c39d52021-11-09 11:55:10 +00001906 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001907 if( ret != 0 )
1908 return( ret );
1909
Ronald Cron49ad6192021-11-24 16:25:31 +01001910#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1911 mbedtls_ssl_handshake_set_state(
1912 ssl,
1913 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1914#else
Jerry Yuca133a32022-02-15 14:22:05 +08001915 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001916#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001917
XiaokangQianac0385c2021-11-03 06:40:11 +00001918 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001919}
1920
1921/*
Jerry Yu566c7812022-01-26 15:41:22 +08001922 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1923 */
1924static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1925{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001926 int non_empty_certificate_msg = 0;
1927
Jerry Yu5cc35062022-01-28 16:16:08 +08001928 MBEDTLS_SSL_DEBUG_MSG( 1,
1929 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001930 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001931
Ronald Cron9df7c802022-03-08 18:38:54 +01001932#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001933 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001934 {
1935 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1936 if( ret != 0 )
1937 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001938
Ronald Cron7a94aca2022-03-09 07:44:27 +01001939 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1940 non_empty_certificate_msg = 1;
1941 }
1942 else
1943 {
1944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1945 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001946#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001947
Ronald Cron7a94aca2022-03-09 07:44:27 +01001948 if( non_empty_certificate_msg )
1949 {
1950 mbedtls_ssl_handshake_set_state( ssl,
1951 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1952 }
1953 else
1954 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1955
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001956 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001957}
1958
Ronald Cron9df7c802022-03-08 18:38:54 +01001959#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001960/*
1961 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1962 */
1963static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1964{
Ronald Crona8b38872022-03-09 07:59:25 +01001965 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1966
1967 if( ret == 0 )
1968 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1969
1970 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001971}
Jerry Yu90f152d2022-01-29 22:12:42 +08001972#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001973
1974/*
Jerry Yu687101b2021-09-14 16:03:56 +08001975 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1976 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001977static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001978{
XiaokangQian0fa66432021-11-15 03:33:57 +00001979 int ret;
1980
1981 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1982 if( ret != 0 )
1983 return( ret );
1984
1985 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1986 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001987}
1988
1989/*
1990 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1991 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001992static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001993{
Jerry Yu378254d2021-10-30 21:44:47 +08001994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001995 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001996 return( 0 );
1997}
1998
1999/*
2000 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2001 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002002static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002003{
Jerry Yu378254d2021-10-30 21:44:47 +08002004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2005 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2006
2007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2008 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2009
2010 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2011
2012 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2013 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002014}
2015
Jerry Yu92c6b402021-08-27 16:59:09 +08002016int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002017{
Jerry Yu92c6b402021-08-27 16:59:09 +08002018 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002019
Jerry Yue3b34122021-09-28 17:53:35 +08002020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2021 mbedtls_ssl_states_str( ssl->state ),
2022 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002023
2024 switch( ssl->state )
2025 {
2026 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002027 * ssl->state is initialized as HELLO_REQUEST. It is the same
2028 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002029 */
2030 case MBEDTLS_SSL_HELLO_REQUEST:
2031 case MBEDTLS_SSL_CLIENT_HELLO:
2032 ret = ssl_tls13_write_client_hello( ssl );
2033 break;
2034
2035 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002036 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002037 break;
2038
2039 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002040 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002041 break;
2042
Jerry Yua93ac112021-10-27 16:31:48 +08002043#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002044 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2045 ret = ssl_tls13_process_certificate_request( ssl );
2046 break;
2047
Jerry Yu687101b2021-09-14 16:03:56 +08002048 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002049 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002050 break;
2051
2052 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002053 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002054 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002055#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002056
2057 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002058 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002059 break;
2060
Jerry Yu566c7812022-01-26 15:41:22 +08002061 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2062 ret = ssl_tls13_write_client_certificate( ssl );
2063 break;
2064
Ronald Cron9df7c802022-03-08 18:38:54 +01002065#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002066 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2067 ret = ssl_tls13_write_client_certificate_verify( ssl );
2068 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002069#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002070
Jerry Yu687101b2021-09-14 16:03:56 +08002071 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002072 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002073 break;
2074
2075 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002076 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002077 break;
2078
2079 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002080 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002081 break;
2082
Ronald Cron49ad6192021-11-24 16:25:31 +01002083 /*
2084 * Injection of dummy-CCS's for middlebox compatibility
2085 */
2086#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002087 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002088 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2089 if( ret == 0 )
2090 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2091 break;
2092
2093 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2094 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2095 if( ret == 0 )
2096 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002097 break;
2098#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2099
Jerry Yu92c6b402021-08-27 16:59:09 +08002100 default:
2101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2103 }
2104
2105 return( ret );
2106}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002107
Jerry Yufb4b6472022-01-27 15:03:26 +08002108#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002109
Jerry Yufb4b6472022-01-27 15:03:26 +08002110