blob: 34805021e98ed3861ce21755e5c186b7487295e5 [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"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.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;
Ronald Crondbe87f02022-02-10 14:35:27 +010052 unsigned char versions_len = ( ssl->handshake->min_minor_ver <=
53 MBEDTLS_SSL_MINOR_VERSION_3 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Ronald Crondbe87f02022-02-10 14:35:27 +010078 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
79 MBEDTLS_SSL_MINOR_VERSION_4,
80 MBEDTLS_SSL_TRANSPORT_STREAM, p );
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Jerry Yu92c6b402021-08-27 16:59:09 +080083
Ronald Crondbe87f02022-02-10 14:35:27 +010084 if( ssl->handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_3 )
85 {
86 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
87 MBEDTLS_SSL_MINOR_VERSION_3,
88 MBEDTLS_SSL_TRANSPORT_STREAM, p + 2 );
89 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
90 }
91
92 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080093
94 return( 0 );
95}
Jerry Yubc20bdd2021-08-24 15:59:48 +080096
Jerry Yuc068b662021-10-11 22:30:19 +080097static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
98 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080099 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800100{
Jerry Yue1b9c292021-09-10 10:08:31 +0800101 ((void) ssl);
102
Ronald Cron98473382022-03-30 20:04:10 +0200103 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Jerry Yub85277e2021-10-13 13:36:05 +0800104 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
106 {
107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800108
109 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
111 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800112 }
113
Ronald Cron98473382022-03-30 20:04:10 +0200114 if( &buf[2] != end )
115 {
116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
117 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
118 MBEDTLS_ERR_SSL_DECODE_ERROR );
119 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
120 }
121
Jerry Yue1b9c292021-09-10 10:08:31 +0800122 return( 0 );
123}
124
lhuang0486cacac2022-01-21 07:34:27 -0800125#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
127 const unsigned char *buf, size_t len )
128{
129 size_t list_len, name_len;
130 const unsigned char *p = buf;
131 const unsigned char *end = buf + len;
132
133 /* If we didn't send it, the server shouldn't send it */
134 if( ssl->conf->alpn_list == NULL )
135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
136
137 /*
138 * opaque ProtocolName<1..2^8-1>;
139 *
140 * struct {
141 * ProtocolName protocol_name_list<2..2^16-1>
142 * } ProtocolNameList;
143 *
144 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
145 */
146
147 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
149
150 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
151 p += 2;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
153
154 name_len = *p++;
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
156
157 /* Check that the server chosen protocol was in our list and save it */
158 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
159 {
160 if( name_len == strlen( *alpn ) &&
161 memcmp( buf + 3, *alpn, name_len ) == 0 )
162 {
163 ssl->alpn_chosen = *alpn;
164 return( 0 );
165 }
166 }
167
168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
169}
170#endif /* MBEDTLS_SSL_ALPN */
171
XiaokangQian16acd4b2022-01-14 07:35:47 +0000172static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000173{
174 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100175
XiaokangQian647719a2021-12-07 09:16:29 +0000176 if( group_id == 0 )
177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
178
XiaokangQian355e09a2022-01-20 11:14:50 +0000179#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000180 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000181 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
184
185 /* Destroy generated private key. */
186 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
187 if( status != PSA_SUCCESS )
188 {
189 ret = psa_ssl_status_to_mbedtls( status );
190 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
191 return( ret );
192 }
193
194 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000195 return( 0 );
196 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000197 else
198#endif /* MBEDTLS_ECDH_C */
199 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000200 {
201 /* Do something */
202 }
203
204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
205}
206
207/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208 * Functions for writing key_share extension.
209 */
210#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800211static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800212 mbedtls_ssl_context *ssl,
213 uint16_t named_group,
214 unsigned char *buf,
215 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000216 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800217{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100218 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
219 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
220 psa_key_attributes_t key_attributes;
221 size_t own_pubkey_len;
222 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
223 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800226
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100227 /* Convert EC group to PSA key type. */
228 if( ( handshake->ecdh_psa_type =
229 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
230 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100232 if( ecdh_bits > 0xffff )
233 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
234 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800235
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100236 key_attributes = psa_key_attributes_init();
237 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
238 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
239 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
240 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100241
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100242 /* Generate ECDH private key. */
243 status = psa_generate_key( &key_attributes,
244 &handshake->ecdh_psa_privkey );
245 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800246 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100247 ret = psa_ssl_status_to_mbedtls( status );
248 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800249 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100250
Jerry Yu56fc07f2021-09-01 17:48:49 +0800251 }
252
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100253 /* Export the public part of the ECDH private key from PSA. */
254 status = psa_export_public_key( handshake->ecdh_psa_privkey,
255 buf, (size_t)( end - buf ),
256 &own_pubkey_len );
257 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800258 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100259 ret = psa_ssl_status_to_mbedtls( status );
260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800261 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
Jerry Yu56fc07f2021-09-01 17:48:49 +0800263 }
264
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100265 if( own_pubkey_len > (size_t)( end - buf ) )
266 {
267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
268 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
269 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100270
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100271 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100272
Jerry Yu75336352021-09-01 15:59:36 +0800273 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800274}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275#endif /* MBEDTLS_ECDH_C */
276
Jerry Yub60e3cf2021-09-08 16:41:02 +0800277static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
278 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279{
280 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
281
Jerry Yu56fc07f2021-09-01 17:48:49 +0800282
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100284 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800285 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100286 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800287 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
288
Brett Warren14efd332021-10-06 09:32:11 +0100289 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800290 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000291 const mbedtls_ecp_curve_info *curve_info;
292 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
293 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100294 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295 {
Brett Warren14efd332021-10-06 09:32:11 +0100296 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 return( 0 );
298 }
299 }
300#else
301 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800302 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800303#endif /* MBEDTLS_ECDH_C */
304
305 /*
306 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800307 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 */
309
310 return( ret );
311}
312
313/*
314 * ssl_tls13_write_key_share_ext
315 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800316 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800317 *
318 * struct {
319 * NamedGroup group;
320 * opaque key_exchange<1..2^16-1>;
321 * } KeyShareEntry;
322 * struct {
323 * KeyShareEntry client_shares<0..2^16-1>;
324 * } KeyShareClientHello;
325 */
326static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
327 unsigned char *buf,
328 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000329 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330{
331 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000332 unsigned char *client_shares; /* Start of client_shares */
333 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800334 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800335 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
336
Xiaofei Baid25fab62021-12-02 06:36:27 +0000337 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340 * - extension_type (2 bytes)
341 * - extension_data_length (2 bytes)
342 * - client_shares_length (2 bytes)
343 */
344 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
345 p += 6;
346
347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
348
349 /* HRR could already have requested something else. */
350 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800351 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
352 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800354 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800355 &group_id ) );
356 }
357
358 /*
359 * Dispatch to type-specific key generation function.
360 *
361 * So far, we're only supporting ECDHE. With the introduction
362 * of PQC KEMs, we'll want to have multiple branches, one per
363 * type of KEM, and dispatch to the corresponding crypto. And
364 * only one key share entry is allowed.
365 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000366 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800367#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800368 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800369 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800370 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000371 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800372 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100373 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800374
375 /* Check there is space for header of KeyShareEntry
376 * - group (2 bytes)
377 * - key_exchange_length (2 bytes)
378 */
379 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
380 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100381 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800382 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800383 p += key_exchange_len;
384 if( ret != 0 )
385 return( ret );
386
387 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000388 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800389 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000390 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800391 }
392 else
393#endif /* MBEDTLS_ECDH_C */
394 if( 0 /* other KEMs? */ )
395 {
396 /* Do something */
397 }
398 else
399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
400
Jerry Yub60e3cf2021-09-08 16:41:02 +0800401 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000402 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 if( client_shares_len == 0)
404 {
405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800407 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800408 /* Write extension_type */
409 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
410 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800411 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800413 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414
415 /* Update offered_group_id field */
416 ssl->handshake->offered_group_id = group_id;
417
418 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000419 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800420
Xiaofei Baid25fab62021-12-02 06:36:27 +0000421 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800422
423 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
424
425cleanup:
426
427 return( ret );
428}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800429
Jerry Yue1b9c292021-09-10 10:08:31 +0800430#if defined(MBEDTLS_ECDH_C)
431
Jerry Yuc068b662021-10-11 22:30:19 +0800432static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
433 const unsigned char *buf,
434 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800435{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100436 uint8_t *p = (uint8_t*)buf;
437 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800438
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100439 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
440 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
441 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800442
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100443 /* Check if key size is consistent with given buffer length. */
444 if ( peerkey_len > ( buf_len - 2 ) )
445 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800446
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100447 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100448 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100449 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450
451 return( 0 );
452}
Jerry Yu4a173382021-10-11 21:45:31 +0800453#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800454
XiaokangQiand59be772022-01-24 10:12:51 +0000455/*
456 * ssl_tls13_parse_hrr_key_share_ext()
457 * Parse key_share extension in Hello Retry Request
458 *
459 * struct {
460 * NamedGroup selected_group;
461 * } KeyShareHelloRetryRequest;
462 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000463static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000464 const unsigned char *buf,
465 const unsigned char *end )
466{
XiaokangQianb851da82022-01-14 04:03:11 +0000467 const mbedtls_ecp_curve_info *curve_info = NULL;
468 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000469 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000470 int found = 0;
471
472 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
473 if( group_list == NULL )
474 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
475
476 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
477
478 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000479 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000480 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000482
483 /* Upon receipt of this extension in a HelloRetryRequest, the client
484 * MUST first verify that the selected_group field corresponds to a
485 * group which was provided in the "supported_groups" extension in the
486 * original ClientHello.
487 * The supported_group was based on the info in ssl->conf->group_list.
488 *
489 * If the server provided a key share that was not sent in the ClientHello
490 * then the client MUST abort the handshake with an "illegal_parameter" alert.
491 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000492 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000493 {
494 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000495 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000496 continue;
497
498 /* We found a match */
499 found = 1;
500 break;
501 }
502
503 /* Client MUST verify that the selected_group field does not
504 * correspond to a group which was provided in the "key_share"
505 * extension in the original ClientHello. If the server sent an
506 * HRR message with a key share already provided in the
507 * ClientHello then the client MUST abort the handshake with
508 * an "illegal_parameter" alert.
509 */
XiaokangQiand59be772022-01-24 10:12:51 +0000510 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000511 {
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
513 MBEDTLS_SSL_PEND_FATAL_ALERT(
514 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
515 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
516 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
517 }
518
519 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000520 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000521
522 return( 0 );
523}
524
Jerry Yue1b9c292021-09-10 10:08:31 +0800525/*
Jerry Yub85277e2021-10-13 13:36:05 +0800526 * ssl_tls13_parse_key_share_ext()
527 * Parse key_share extension in Server Hello
528 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800529 * struct {
530 * KeyShareEntry server_share;
531 * } KeyShareServerHello;
532 * struct {
533 * NamedGroup group;
534 * opaque key_exchange<1..2^16-1>;
535 * } KeyShareEntry;
536 */
Jerry Yu4a173382021-10-11 21:45:31 +0800537static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800538 const unsigned char *buf,
539 const unsigned char *end )
540{
Jerry Yub85277e2021-10-13 13:36:05 +0800541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800542 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800543 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800544
Jerry Yu4a173382021-10-11 21:45:31 +0800545 /* ...
546 * NamedGroup group; (2 bytes)
547 * ...
548 */
549 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
550 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800551 p += 2;
552
Jerry Yu4a173382021-10-11 21:45:31 +0800553 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800554 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800555 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800556 {
557 MBEDTLS_SSL_DEBUG_MSG( 1,
558 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800559 (unsigned) offered_group, (unsigned) group ) );
560 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
561 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
562 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800563 }
564
565#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800566 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800567 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100568 const mbedtls_ecp_curve_info *curve_info =
569 mbedtls_ecp_curve_info_from_tls_id( group );
570 if( curve_info == NULL )
571 {
572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
574 }
575
576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
577
Jerry Yuc068b662021-10-11 22:30:19 +0800578 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800579 if( ret != 0 )
580 return( ret );
581 }
Jerry Yub85277e2021-10-13 13:36:05 +0800582 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800583#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800584 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800585 {
586 /* Do something */
587 }
588 else
589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
590
591 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
592 return( ret );
593}
594
XiaokangQiand59be772022-01-24 10:12:51 +0000595/*
596 * ssl_tls13_parse_cookie_ext()
597 * Parse cookie extension in Hello Retry Request
598 *
599 * struct {
600 * opaque cookie<1..2^16-1>;
601 * } Cookie;
602 *
603 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
604 * extension to the client (this is an exception to the usual rule that
605 * the only extensions that may be sent are those that appear in the
606 * ClientHello). When sending the new ClientHello, the client MUST copy
607 * the contents of the extension received in the HelloRetryRequest into
608 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
609 * cookies in their initial ClientHello in subsequent connections.
610 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000611static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
612 const unsigned char *buf,
613 const unsigned char *end )
614{
XiaokangQian25c9c902022-02-08 10:49:53 +0000615 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000616 const unsigned char *p = buf;
617 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
618
619 /* Retrieve length field of cookie */
620 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
621 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
622 p += 2;
623
624 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
625 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
626
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000627 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000628 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000629 handshake->cookie = mbedtls_calloc( 1, cookie_len );
630 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000631 {
632 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000633 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000634 cookie_len ) );
635 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
636 }
637
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000638 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000639 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000640
641 return( 0 );
642}
XiaokangQian43550bd2022-01-21 04:32:58 +0000643
XiaokangQian0b64eed2022-01-27 10:36:51 +0000644static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000645 unsigned char *buf,
646 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000647 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000648{
649 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000650 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000651 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000652
XiaokangQianc02768a2022-02-10 07:31:25 +0000653 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000654 {
655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
656 return( 0 );
657 }
658
659 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000660 handshake->cookie,
661 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000662
XiaokangQianc02768a2022-02-10 07:31:25 +0000663 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000664
665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
666
XiaokangQian0b64eed2022-01-27 10:36:51 +0000667 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000668 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
669 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000670 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000671
672 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000673 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000674
XiaokangQianc02768a2022-02-10 07:31:25 +0000675 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000676
677 return( 0 );
678}
679
Ronald Cron3d580bf2022-02-18 17:24:56 +0100680int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
681 unsigned char *buf,
682 unsigned char *end,
683 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100684{
685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
686 unsigned char *p = buf;
687 size_t ext_len;
688
689 *out_len = 0;
690
691 /* Write supported_versions extension
692 *
693 * Supported Versions Extension is mandatory with TLS 1.3.
694 */
695 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
696 if( ret != 0 )
697 return( ret );
698 p += ext_len;
699
700 /* Echo the cookie if the server provided one in its preceding
701 * HelloRetryRequest message.
702 */
703 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
704 if( ret != 0 )
705 return( ret );
706 p += ext_len;
707
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100708 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
709 {
710 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
711 if( ret != 0 )
712 return( ret );
713 p += ext_len;
714 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100715
716 *out_len = p - buf;
717
718 return( 0 );
719}
720
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800721/*
Jerry Yu4a173382021-10-11 21:45:31 +0800722 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800723 */
Ronald Cronda41b382022-03-30 09:57:11 +0200724/**
725 * \brief Detect if the ServerHello contains a supported_versions extension
726 * or not.
727 *
728 * \param[in] ssl SSL context
729 * \param[in] buf Buffer containing the ServerHello message
730 * \param[in] end End of the buffer containing the ServerHello message
731 *
732 * \return 0 if the ServerHello does not contain a supported_versions extension
733 * \return 1 if the ServerHello contains a supported_versions extension
734 * \return A negative value if an error occurred while parsing the ServerHello.
735 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100736static int ssl_tls13_is_supported_versions_ext_present(
737 mbedtls_ssl_context *ssl,
738 const unsigned char *buf,
739 const unsigned char *end )
740{
741 const unsigned char *p = buf;
742 size_t legacy_session_id_echo_len;
743 size_t extensions_len;
744 const unsigned char *extensions_end;
745
746 /*
747 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200748 * length:
749 * - legacy_version 2 bytes
750 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
751 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100752 */
753 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
754 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
755 legacy_session_id_echo_len = *p;
756
757 /*
758 * Jump to the extensions, jumping over:
759 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
760 * - cipher_suite 2 bytes
761 * - legacy_compression_method 1 byte
762 */
763 p += legacy_session_id_echo_len + 4;
764
765 /* Case of no extension */
766 if( p == end )
767 return( 0 );
768
769 /* ...
770 * Extension extensions<6..2^16-1>;
771 * ...
772 * struct {
773 * ExtensionType extension_type; (2 bytes)
774 * opaque extension_data<0..2^16-1>;
775 * } Extension;
776 */
777 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
778 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
779 p += 2;
780
781 /* Check extensions do not go beyond the buffer of data. */
782 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
783 extensions_end = p + extensions_len;
784
785 while( p < extensions_end )
786 {
787 unsigned int extension_type;
788 size_t extension_data_len;
789
790 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
791 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
792 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
793 p += 4;
794
795 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
796 return( 1 );
797
798 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
799 p += extension_data_len;
800 }
801
802 return( 0 );
803}
804
Jerry Yu7a186a02021-10-15 18:46:14 +0800805/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800806 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
807 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000808 * to indicate which message is expected and to be parsed next.
809 */
Jerry Yub85277e2021-10-13 13:36:05 +0800810#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
811#define SSL_SERVER_HELLO_COORDINATE_HRR 1
812static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
813 const unsigned char *buf,
814 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800815{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800816 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800817 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
818 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
819 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
820 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
821
822 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
823 *
Jerry Yu4a173382021-10-11 21:45:31 +0800824 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800825 * special value of the SHA-256 of "HelloRetryRequest".
826 *
827 * struct {
828 * ProtocolVersion legacy_version = 0x0303;
829 * Random random;
830 * opaque legacy_session_id_echo<0..32>;
831 * CipherSuite cipher_suite;
832 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800833 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800834 * } ServerHello;
835 *
836 */
Jerry Yub85277e2021-10-13 13:36:05 +0800837 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800838
839 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800840 {
Jerry Yub85277e2021-10-13 13:36:05 +0800841 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800842 }
843
Jerry Yub85277e2021-10-13 13:36:05 +0800844 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800845}
846
Jerry Yu745bb612021-10-13 22:01:04 +0800847/* Fetch and preprocess
848 * Returns a negative value on failure, and otherwise
849 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100850 * - SSL_SERVER_HELLO_COORDINATE_HRR or
851 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800852 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100853#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800854static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
855 unsigned char **buf,
856 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800857{
Jerry Yu4a173382021-10-11 21:45:31 +0800858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800859
XiaokangQian355e09a2022-01-20 11:14:50 +0000860 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
861 MBEDTLS_SSL_HS_SERVER_HELLO,
862 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800863
Ronald Cron9f0fba32022-02-10 16:45:15 +0100864 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
865 ssl, *buf, *buf + *buf_len ) );
866 if( ret == 0 )
867 {
868 /* If the supported versions extension is not present but we were
869 * expecting it, abort the handshake. Otherwise, switch to TLS 1.2
870 * handshake.
871 */
872 if( ssl->handshake->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 )
873 {
874 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
875 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
876 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
877 }
878
879 ssl->keep_current_message = 1;
880 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
881 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
882 *buf, *buf_len );
883
884 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
885 {
886 ret = ssl_tls13_reset_key_share( ssl );
887 if( ret != 0 )
888 return( ret );
889 }
890
891 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
892 }
893
Jerry Yub85277e2021-10-13 13:36:05 +0800894 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
895 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800896 {
Jerry Yu745bb612021-10-13 22:01:04 +0800897 case SSL_SERVER_HELLO_COORDINATE_HELLO:
898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
899 break;
900 case SSL_SERVER_HELLO_COORDINATE_HRR:
901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000902 /* If a client receives a second
903 * HelloRetryRequest in the same connection (i.e., where the ClientHello
904 * was itself in response to a HelloRetryRequest), it MUST abort the
905 * handshake with an "unexpected_message" alert.
906 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000907 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000908 {
909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
910 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
911 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
912 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
913 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000914 /*
915 * Clients must abort the handshake with an "illegal_parameter"
916 * alert if the HelloRetryRequest would not result in any change
917 * in the ClientHello.
918 * In a PSK only key exchange that what we expect.
919 */
920 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
921 {
922 MBEDTLS_SSL_DEBUG_MSG( 1,
923 ( "Unexpected HRR in pure PSK key exchange." ) );
924 MBEDTLS_SSL_PEND_FATAL_ALERT(
925 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
926 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
927 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
928 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000929
XiaokangQiand9e068e2022-01-18 06:23:32 +0000930 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000931
Jerry Yu745bb612021-10-13 22:01:04 +0800932 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800933 }
934
935cleanup:
936
937 return( ret );
938}
939
Jerry Yu4a173382021-10-11 21:45:31 +0800940static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
941 const unsigned char **buf,
942 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800943{
944 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800945 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800946
Jerry Yude4fb2c2021-09-19 18:05:08 +0800947 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800948 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800949
Jerry Yu4a173382021-10-11 21:45:31 +0800950 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800951
952 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800953 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
954 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800956 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
957 ssl->session_negotiate->id,
958 ssl->session_negotiate->id_len );
959 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800960 legacy_session_id_echo_len );
961
962 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
963 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
964
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
966 }
967
Jerry Yu4a173382021-10-11 21:45:31 +0800968 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800969 *buf = p;
970
971 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800972 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 return( 0 );
974}
975
Jerry Yu4a173382021-10-11 21:45:31 +0800976static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
977 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800978{
Jerry Yu4a173382021-10-11 21:45:31 +0800979 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
980
Jerry Yue1b9c292021-09-10 10:08:31 +0800981 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800982 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800983 {
Jerry Yu4a173382021-10-11 21:45:31 +0800984 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800985 {
986 return( 1 );
987 }
988 }
989 return( 0 );
990}
991
992/* Parse ServerHello message and configure context
993 *
994 * struct {
995 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
996 * Random random;
997 * opaque legacy_session_id_echo<0..32>;
998 * CipherSuite cipher_suite;
999 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001000 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001001 * } ServerHello;
1002 */
Jerry Yuc068b662021-10-11 22:30:19 +08001003static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1004 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001005 const unsigned char *end,
1006 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001007{
Jerry Yub85277e2021-10-13 13:36:05 +08001008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001010 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001011 size_t extensions_len;
1012 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001013 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001014 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001015 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001016
1017 /*
1018 * Check there is space for minimal fields
1019 *
1020 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001021 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001022 * - legacy_session_id_echo ( 1 byte ), minimum size
1023 * - cipher_suite ( 2 bytes)
1024 * - legacy_compression_method ( 1 byte )
1025 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001026 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001027
1028 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001029 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1030
Jerry Yu4a173382021-10-11 21:45:31 +08001031 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001032 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001033 * ...
1034 * with ProtocolVersion defined as:
1035 * uint16 ProtocolVersion;
1036 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1038 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1039 {
1040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1041 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1042 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001043 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1044 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 }
1046 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001047
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001048 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001049 * Random random;
1050 * ...
1051 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001052 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001053 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001054 if( !is_hrr )
1055 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001056 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001057 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1058 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1059 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1060 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001061 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062
Jerry Yu4a173382021-10-11 21:45:31 +08001063 /* ...
1064 * opaque legacy_session_id_echo<0..32>;
1065 * ...
1066 */
1067 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 {
XiaokangQian52da5582022-01-26 09:49:29 +00001069 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001070 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 }
1072
Jerry Yu4a173382021-10-11 21:45:31 +08001073 /* ...
1074 * CipherSuite cipher_suite;
1075 * ...
1076 * with CipherSuite defined as:
1077 * uint8 CipherSuite[2];
1078 */
1079 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001080 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1081 p += 2;
1082
Jerry Yu4a173382021-10-11 21:45:31 +08001083
XiaokangQian355e09a2022-01-20 11:14:50 +00001084 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001085 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001086 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001087 * Via the force_ciphersuite version we may have instructed the client
1088 * to use a different ciphersuite.
1089 */
Ronald Cronba120bb2022-03-30 22:09:48 +02001090 if( ( mbedtls_ssl_validate_ciphersuite(
1091 ssl, ciphersuite_info, ssl->minor_ver, ssl->minor_ver ) != 0 ) ||
1092 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001093 {
XiaokangQian52da5582022-01-26 09:49:29 +00001094 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001096 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001097 * If we received an HRR before and that the proposed selected
1098 * ciphersuite in this server hello is not the same as the one
1099 * proposed in the HRR, we abort the handshake and send an
1100 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001101 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001102 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1103 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001104 {
XiaokangQian52da5582022-01-26 09:49:29 +00001105 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001106 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001107
XiaokangQian52da5582022-01-26 09:49:29 +00001108 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001109 {
1110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1111 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001112 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001113 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001114
Jerry Yu4a173382021-10-11 21:45:31 +08001115 /* Configure ciphersuites */
1116 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1117
XiaokangQian355e09a2022-01-20 11:14:50 +00001118 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 ssl->session_negotiate->ciphersuite = cipher_suite;
1120
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1122 cipher_suite, ciphersuite_info->name ) );
1123
1124#if defined(MBEDTLS_HAVE_TIME)
1125 ssl->session_negotiate->start = time( NULL );
1126#endif /* MBEDTLS_HAVE_TIME */
1127
Jerry Yu4a173382021-10-11 21:45:31 +08001128 /* ...
1129 * uint8 legacy_compression_method = 0;
1130 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001131 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001132 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001133 if( p[0] != 0 )
1134 {
Jerry Yub85277e2021-10-13 13:36:05 +08001135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001136 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001137 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001138 }
1139 p++;
1140
Jerry Yub85277e2021-10-13 13:36:05 +08001141 /* ...
1142 * Extension extensions<6..2^16-1>;
1143 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001144 * struct {
1145 * ExtensionType extension_type; (2 bytes)
1146 * opaque extension_data<0..2^16-1>;
1147 * } Extension;
1148 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001150 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001152
Jerry Yu4a173382021-10-11 21:45:31 +08001153 /* Check extensions do not go beyond the buffer of data. */
1154 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1155 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156
Jerry Yu4a173382021-10-11 21:45:31 +08001157 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001158
Jerry Yu4a173382021-10-11 21:45:31 +08001159 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001160 {
1161 unsigned int extension_type;
1162 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001163 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164
Jerry Yu4a173382021-10-11 21:45:31 +08001165 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001166 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1167 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1168 p += 4;
1169
Jerry Yu4a173382021-10-11 21:45:31 +08001170 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001171 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001172
1173 switch( extension_type )
1174 {
XiaokangQianb851da82022-01-14 04:03:11 +00001175 case MBEDTLS_TLS_EXT_COOKIE:
1176
XiaokangQiand9e068e2022-01-18 06:23:32 +00001177 if( !is_hrr )
1178 {
XiaokangQian52da5582022-01-26 09:49:29 +00001179 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001180 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001181 }
1182
XiaokangQian43550bd2022-01-21 04:32:58 +00001183 ret = ssl_tls13_parse_cookie_ext( ssl,
1184 p, extension_data_end );
1185 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001186 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001187 MBEDTLS_SSL_DEBUG_RET( 1,
1188 "ssl_tls13_parse_cookie_ext",
1189 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001190 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001191 }
XiaokangQianb851da82022-01-14 04:03:11 +00001192 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001193
Jerry Yue1b9c292021-09-10 10:08:31 +08001194 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001195 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001196 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001197 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001198 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001199 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001200 break;
1201
1202 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001205
XiaokangQian52da5582022-01-26 09:49:29 +00001206 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001207 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001208
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 case MBEDTLS_TLS_EXT_KEY_SHARE:
1210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001211 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1212 {
XiaokangQian52da5582022-01-26 09:49:29 +00001213 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001214 goto cleanup;
1215 }
1216
XiaokangQian53f20b72022-01-18 10:47:33 +00001217 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001218 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001219 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001220 else
XiaokangQianb851da82022-01-14 04:03:11 +00001221 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001222 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001223 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 {
1225 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001226 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001227 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001228 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001229 }
1230 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001231
1232 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001233 MBEDTLS_SSL_DEBUG_MSG(
1234 3,
1235 ( "unknown extension found: %u ( ignoring )",
1236 extension_type ) );
1237
XiaokangQian52da5582022-01-26 09:49:29 +00001238 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001239 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001240 }
1241
1242 p += extension_data_len;
1243 }
1244
XiaokangQiand59be772022-01-24 10:12:51 +00001245cleanup:
1246
XiaokangQian52da5582022-01-26 09:49:29 +00001247 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001248 {
1249 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1250 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1251 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1252 }
XiaokangQian52da5582022-01-26 09:49:29 +00001253 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001254 {
1255 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1256 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1257 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1258 }
1259 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001260}
1261
XiaokangQian355e09a2022-01-20 11:14:50 +00001262static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001263{
Jerry Yub85277e2021-10-13 13:36:05 +08001264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001265 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001266 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001267 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001268
Jerry Yub85277e2021-10-13 13:36:05 +08001269 /* Determine the key exchange mode:
1270 * 1) If both the pre_shared_key and key_share extensions were received
1271 * then the key exchange mode is PSK with EPHEMERAL.
1272 * 2) If only the pre_shared_key extension was received then the key
1273 * exchange mode is PSK-only.
1274 * 3) If only the key_share extension was received then the key
1275 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001276 */
Jerry Yub85277e2021-10-13 13:36:05 +08001277 switch( handshake->extensions_present &
1278 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001279 {
Jerry Yu745bb612021-10-13 22:01:04 +08001280 /* Only the pre_shared_key extension was received */
1281 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001282 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001283 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001284
Jerry Yu745bb612021-10-13 22:01:04 +08001285 /* Only the key_share extension was received */
1286 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001287 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001288 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001289
Jerry Yu745bb612021-10-13 22:01:04 +08001290 /* Both the pre_shared_key and key_share extensions were received */
1291 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001292 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001293 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001294
Jerry Yu745bb612021-10-13 22:01:04 +08001295 /* Neither pre_shared_key nor key_share extension was received */
1296 default:
1297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1298 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1299 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001300 }
1301
1302 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1303 *
1304 * TODO: We don't have to do this in case we offered 0-RTT and the
1305 * server accepted it. In this case, we could skip generating
1306 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001307 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001308 if( ret != 0 )
1309 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001311 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001312 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001313 }
1314
1315 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001316 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001317 if( ret != 0 )
1318 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001320 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001321 }
1322
1323 /* Next evolution in key schedule: Establish handshake secret and
1324 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001325 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001326 if( ret != 0 )
1327 {
Jerry Yuc068b662021-10-11 22:30:19 +08001328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1329 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001330 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001331 }
1332
Jerry Yub85277e2021-10-13 13:36:05 +08001333 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001334 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001335 {
1336 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1337 goto cleanup;
1338 }
Jerry Yu0b177842021-09-05 19:41:30 +08001339
1340 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1341 ssl->conf->endpoint,
1342 ssl->session_negotiate->ciphersuite,
1343 &traffic_keys,
1344 ssl );
1345 if( ret != 0 )
1346 {
1347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001348 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001349 }
1350
Jerry Yub85277e2021-10-13 13:36:05 +08001351 handshake->transform_handshake = transform_handshake;
1352 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001353
1354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1355 ssl->session_in = ssl->session_negotiate;
1356
1357 /*
1358 * State machine update
1359 */
1360 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1361
Jerry Yu4a173382021-10-11 21:45:31 +08001362cleanup:
1363
Jerry Yu0b177842021-09-05 19:41:30 +08001364 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001365 if( ret != 0 )
1366 {
Jerry Yub85277e2021-10-13 13:36:05 +08001367 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001368
Jerry Yu4a173382021-10-11 21:45:31 +08001369 MBEDTLS_SSL_PEND_FATAL_ALERT(
1370 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1371 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1372 }
1373 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001374}
1375
XiaokangQian355e09a2022-01-20 11:14:50 +00001376static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001377{
1378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1379
XiaokangQian647719a2021-12-07 09:16:29 +00001380#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1381 /* If not offering early data, the client sends a dummy CCS record
1382 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001383 * its second ClientHello or before its encrypted handshake flight.
1384 */
XiaokangQian647719a2021-12-07 09:16:29 +00001385 mbedtls_ssl_handshake_set_state( ssl,
1386 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1387#else
1388 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1389#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1390
XiaokangQian78b1fa72022-01-19 06:56:30 +00001391 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001392
XiaokangQian78b1fa72022-01-19 06:56:30 +00001393 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001394 * We are going to re-generate a shared secret corresponding to the group
1395 * selected by the server, which is different from the group for which we
1396 * generated a shared secret in the first client hello.
1397 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001398 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001399 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001400 if( ret != 0 )
1401 return( ret );
1402
1403 return( 0 );
1404}
1405
Jerry Yue1b9c292021-09-10 10:08:31 +08001406/*
Jerry Yu4a173382021-10-11 21:45:31 +08001407 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001408 * Handler for MBEDTLS_SSL_SERVER_HELLO
1409 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001410static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001411{
Jerry Yu4a173382021-10-11 21:45:31 +08001412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001413 unsigned char *buf = NULL;
1414 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001415 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001416
1417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1418
1419 /* Coordination step
1420 * - Fetch record
1421 * - Make sure it's either a ServerHello or a HRR.
1422 * - Switch processing routine in case of HRR
1423 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001424 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1425 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1426
XiaokangQian16acd4b2022-01-14 07:35:47 +00001427 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001428 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001429 goto cleanup;
1430 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001431 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001432
Ronald Cron9f0fba32022-02-10 16:45:15 +01001433 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1434 {
1435 ret = 0;
1436 goto cleanup;
1437 }
1438
XiaokangQianb851da82022-01-14 04:03:11 +00001439 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001440 buf + buf_len,
1441 is_hrr ) );
1442 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001443 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1444
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001445 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1446 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001447
XiaokangQiand9e068e2022-01-18 06:23:32 +00001448 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001449 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001450 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001451 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001452
1453cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1455 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001457}
1458
1459/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001460 *
1461 * EncryptedExtensions message
1462 *
1463 * The EncryptedExtensions message contains any extensions which
1464 * should be protected, i.e., any which are not needed to establish
1465 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001466 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467
1468/*
1469 * Overview
1470 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001471
1472/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001473static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001474
XiaokangQian97799ac2021-10-11 10:05:54 +00001475static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1476 const unsigned char *buf,
1477 const unsigned char *end );
1478static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001479
1480/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001481 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001482 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001483static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001484{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001485 int ret;
1486 unsigned char *buf;
1487 size_t buf_len;
1488
1489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1490
Xiaofei Bai746f9482021-11-12 08:53:56 +00001491 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001492 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001493 &buf, &buf_len ) );
1494
1495 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001496 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001497 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001498
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001499 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1500 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001501
XiaokangQian97799ac2021-10-11 10:05:54 +00001502 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001503
1504cleanup:
1505
1506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1507 return( ret );
1508
1509}
1510
XiaokangQian08da26c2021-10-09 10:12:11 +00001511/* Parse EncryptedExtensions message
1512 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001513 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001514 * } EncryptedExtensions;
1515 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001516static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1517 const unsigned char *buf,
1518 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001519{
1520 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001521 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001522 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001523 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524
XiaokangQian08da26c2021-10-09 10:12:11 +00001525 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001526 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001527 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
XiaokangQian97799ac2021-10-11 10:05:54 +00001529 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001530 extensions_end = p + extensions_len;
1531 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001532
XiaokangQian8db25ff2021-10-13 05:56:18 +00001533 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001534 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001535 unsigned int extension_type;
1536 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001537
XiaokangQian08da26c2021-10-09 10:12:11 +00001538 /*
1539 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001540 * ExtensionType extension_type; (2 bytes)
1541 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001542 * } Extension;
1543 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001544 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001545 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1546 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1547 p += 4;
1548
XiaokangQian8db25ff2021-10-13 05:56:18 +00001549 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001550
XiaokangQian97799ac2021-10-11 10:05:54 +00001551 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001552 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001553 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001554 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001555 switch( extension_type )
1556 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001557
XiaokangQian08da26c2021-10-09 10:12:11 +00001558 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1560 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001561
lhuang0486cacac2022-01-21 07:34:27 -08001562#if defined(MBEDTLS_SSL_ALPN)
1563 case MBEDTLS_TLS_EXT_ALPN:
1564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1565
1566 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1567 {
1568 return( ret );
1569 }
1570
1571 break;
1572#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001573 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001574 MBEDTLS_SSL_DEBUG_MSG(
1575 3, ( "unsupported extension found: %u ", extension_type) );
1576 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001577 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001578 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1579 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001580 }
1581
XiaokangQian08da26c2021-10-09 10:12:11 +00001582 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001583 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001584
XiaokangQian97799ac2021-10-11 10:05:54 +00001585 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001586 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001587 {
1588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001589 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001590 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001591 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001592 }
1593
1594 return( ret );
1595}
1596
XiaokangQian97799ac2021-10-11 10:05:54 +00001597static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001598{
Jerry Yua93ac112021-10-27 16:31:48 +08001599#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001600 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001601 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1602 else
Jerry Yud2674312021-10-29 10:08:19 +08001603 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001604#else
1605 ((void) ssl);
1606 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1607#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001608 return( 0 );
1609}
1610
Jerry Yua93ac112021-10-27 16:31:48 +08001611#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001612/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001613 *
1614 * STATE HANDLING: CertificateRequest
1615 *
Jerry Yud2674312021-10-29 10:08:19 +08001616 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001617#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1618#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001619/* Coordination:
1620 * Deals with the ambiguity of not knowing if a CertificateRequest
1621 * will be sent. Returns a negative code on failure, or
1622 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1623 * - SSL_CERTIFICATE_REQUEST_SKIP
1624 * indicating if a Certificate Request is expected or not.
1625 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001626static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001627{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001629
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001630 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001631 {
1632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1633 return( SSL_CERTIFICATE_REQUEST_SKIP );
1634 }
1635
1636 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001637 {
1638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1639 return( ret );
1640 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001641 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001642
1643 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1644 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1645 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001646 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001647 }
1648
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001649 return( SSL_CERTIFICATE_REQUEST_SKIP );
1650}
1651
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652/*
1653 * ssl_tls13_parse_certificate_request()
1654 * Parse certificate request
1655 * struct {
1656 * opaque certificate_request_context<0..2^8-1>;
1657 * Extension extensions<2..2^16-1>;
1658 * } CertificateRequest;
1659 */
1660static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1661 const unsigned char *buf,
1662 const unsigned char *end )
1663{
1664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1665 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001667 size_t extensions_len = 0;
1668 const unsigned char *extensions_end;
1669 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001670
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001671 /* ...
1672 * opaque certificate_request_context<0..2^8-1>
1673 * ...
1674 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1676 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001677 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679 if( certificate_request_context_len > 0 )
1680 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001681 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001682 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1683 p, certificate_request_context_len );
1684
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001685 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001686 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001687 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001688 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001689 {
1690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1691 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1692 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001693 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695 p += certificate_request_context_len;
1696 }
1697
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001698 /* ...
1699 * Extension extensions<2..2^16-1>;
1700 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001701 */
1702 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1703 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1704 p += 2;
1705
1706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1707 extensions_end = p + extensions_len;
1708
1709 while( p < extensions_end )
1710 {
1711 unsigned int extension_type;
1712 size_t extension_data_len;
1713
1714 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1715 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1716 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1717 p += 4;
1718
1719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1720
1721 switch( extension_type )
1722 {
1723 case MBEDTLS_TLS_EXT_SIG_ALG:
1724 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001725 ( "found signature algorithms extension" ) );
1726 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727 p + extension_data_len );
1728 if( ret != 0 )
1729 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001730 if( ! sig_alg_ext_found )
1731 sig_alg_ext_found = 1;
1732 else
1733 {
1734 MBEDTLS_SSL_DEBUG_MSG( 3,
1735 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001736 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001737 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001738 break;
1739
1740 default:
1741 MBEDTLS_SSL_DEBUG_MSG(
1742 3,
1743 ( "unknown extension found: %u ( ignoring )",
1744 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001745 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001746 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001747 p += extension_data_len;
1748 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001749 /* Check that we consumed all the message. */
1750 if( p != end )
1751 {
1752 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001753 ( "CertificateRequest misaligned" ) );
1754 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001755 }
1756 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001757 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001758 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001759 MBEDTLS_SSL_DEBUG_MSG( 3,
1760 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001761 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001762 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001763
Jerry Yu7840f812022-01-29 10:26:51 +08001764 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001765 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001766
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001767decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001768 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1769 MBEDTLS_ERR_SSL_DECODE_ERROR );
1770 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001771}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001772
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001773/*
1774 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1775 */
1776static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001777{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001779
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1781
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001782 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1783
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001784 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1785 {
1786 unsigned char *buf;
1787 size_t buf_len;
1788
1789 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1790 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1791 &buf, &buf_len ) );
1792
1793 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1794 buf, buf + buf_len ) );
1795
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001796 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1797 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001798 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001799 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001800 {
1801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001802 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001803 }
1804 else
1805 {
1806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001807 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1808 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809 }
1810
1811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001812 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001813
Jerry Yud2674312021-10-29 10:08:19 +08001814 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1815
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001816cleanup:
1817
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1819 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001820}
1821
1822/*
Jerry Yu687101b2021-09-14 16:03:56 +08001823 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1824 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001825static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001826{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001827 int ret;
1828
1829 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001830 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001831 return( ret );
1832
Jerry Yu687101b2021-09-14 16:03:56 +08001833 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1834 return( 0 );
1835}
1836
1837/*
1838 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1839 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001840static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001841{
Jerry Yu30b071c2021-09-12 20:16:03 +08001842 int ret;
1843
1844 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1845 if( ret != 0 )
1846 return( ret );
1847
Jerry Yu687101b2021-09-14 16:03:56 +08001848 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1849 return( 0 );
1850}
Jerry Yua93ac112021-10-27 16:31:48 +08001851#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001852
Jerry Yu687101b2021-09-14 16:03:56 +08001853/*
1854 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1855 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001856static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001857{
XiaokangQianac0385c2021-11-03 06:40:11 +00001858 int ret;
1859
XiaokangQianc5c39d52021-11-09 11:55:10 +00001860 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001861 if( ret != 0 )
1862 return( ret );
1863
Ronald Cron49ad6192021-11-24 16:25:31 +01001864#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1865 mbedtls_ssl_handshake_set_state(
1866 ssl,
1867 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1868#else
Jerry Yuca133a32022-02-15 14:22:05 +08001869 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001870#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001871
XiaokangQianac0385c2021-11-03 06:40:11 +00001872 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001873}
1874
1875/*
Jerry Yu566c7812022-01-26 15:41:22 +08001876 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1877 */
1878static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1879{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001880 int non_empty_certificate_msg = 0;
1881
Jerry Yu5cc35062022-01-28 16:16:08 +08001882 MBEDTLS_SSL_DEBUG_MSG( 1,
1883 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001884 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001885
Ronald Cron9df7c802022-03-08 18:38:54 +01001886#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001887 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001888 {
1889 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1890 if( ret != 0 )
1891 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001892
Ronald Cron7a94aca2022-03-09 07:44:27 +01001893 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1894 non_empty_certificate_msg = 1;
1895 }
1896 else
1897 {
1898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1899 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001900#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001901
Ronald Cron7a94aca2022-03-09 07:44:27 +01001902 if( non_empty_certificate_msg )
1903 {
1904 mbedtls_ssl_handshake_set_state( ssl,
1905 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1906 }
1907 else
1908 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1909
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001910 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001911}
1912
Ronald Cron9df7c802022-03-08 18:38:54 +01001913#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001914/*
1915 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1916 */
1917static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1918{
Ronald Crona8b38872022-03-09 07:59:25 +01001919 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1920
1921 if( ret == 0 )
1922 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1923
1924 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001925}
Jerry Yu90f152d2022-01-29 22:12:42 +08001926#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001927
1928/*
Jerry Yu687101b2021-09-14 16:03:56 +08001929 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1930 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001931static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001932{
XiaokangQian0fa66432021-11-15 03:33:57 +00001933 int ret;
1934
1935 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1936 if( ret != 0 )
1937 return( ret );
1938
1939 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1940 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001941}
1942
1943/*
1944 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1945 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001946static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001947{
Jerry Yu378254d2021-10-30 21:44:47 +08001948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001949 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001950 return( 0 );
1951}
1952
1953/*
1954 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1955 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001956static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001957{
Jerry Yu378254d2021-10-30 21:44:47 +08001958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1959 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1960
1961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1962 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1963
1964 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1965
1966 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1967 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001968}
1969
Jerry Yu92c6b402021-08-27 16:59:09 +08001970int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001971{
Jerry Yu92c6b402021-08-27 16:59:09 +08001972 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001973
Jerry Yu92c6b402021-08-27 16:59:09 +08001974 switch( ssl->state )
1975 {
1976 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001977 * ssl->state is initialized as HELLO_REQUEST. It is the same
1978 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001979 */
1980 case MBEDTLS_SSL_HELLO_REQUEST:
1981 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001982 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001983 break;
1984
1985 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001986 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001987 break;
1988
1989 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001990 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001991 break;
1992
Jerry Yua93ac112021-10-27 16:31:48 +08001993#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001994 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1995 ret = ssl_tls13_process_certificate_request( ssl );
1996 break;
1997
Jerry Yu687101b2021-09-14 16:03:56 +08001998 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001999 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002000 break;
2001
2002 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002003 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002004 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002005#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002006
2007 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002008 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002009 break;
2010
Jerry Yu566c7812022-01-26 15:41:22 +08002011 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2012 ret = ssl_tls13_write_client_certificate( ssl );
2013 break;
2014
Ronald Cron9df7c802022-03-08 18:38:54 +01002015#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002016 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2017 ret = ssl_tls13_write_client_certificate_verify( ssl );
2018 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002019#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002020
Jerry Yu687101b2021-09-14 16:03:56 +08002021 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002022 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002023 break;
2024
2025 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002026 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002027 break;
2028
2029 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002030 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002031 break;
2032
Ronald Cron49ad6192021-11-24 16:25:31 +01002033 /*
2034 * Injection of dummy-CCS's for middlebox compatibility
2035 */
2036#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002037 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002038 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2039 if( ret == 0 )
2040 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2041 break;
2042
2043 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2044 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2045 if( ret == 0 )
2046 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002047 break;
2048#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2049
Jerry Yu92c6b402021-08-27 16:59:09 +08002050 default:
2051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053 }
2054
2055 return( ret );
2056}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002057
Jerry Yufb4b6472022-01-27 15:03:26 +08002058#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002059
Jerry Yufb4b6472022-01-27 15:03:26 +08002060