blob: 77c79409c2f8a6d0cd1337f876a84571851f3d24 [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 /*
1086 * Check whether this ciphersuite is supported and offered.
1087 * Via the force_ciphersuite version we may have instructed the client
1088 * to use a different ciphersuite.
1089 */
Jerry Yu4a173382021-10-11 21:45:31 +08001090 if( ciphersuite_info == NULL ||
1091 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001092 {
XiaokangQian52da5582022-01-26 09:49:29 +00001093 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001094 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001095 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001096 * If we received an HRR before and that the proposed selected
1097 * ciphersuite in this server hello is not the same as the one
1098 * proposed in the HRR, we abort the handshake and send an
1099 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001100 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001101 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1102 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001103 {
XiaokangQian52da5582022-01-26 09:49:29 +00001104 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001105 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001106
XiaokangQian52da5582022-01-26 09:49:29 +00001107 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001108 {
1109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1110 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001111 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001112 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001113
Jerry Yu4a173382021-10-11 21:45:31 +08001114 /* Configure ciphersuites */
1115 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1116
XiaokangQian355e09a2022-01-20 11:14:50 +00001117 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 ssl->session_negotiate->ciphersuite = cipher_suite;
1119
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1121 cipher_suite, ciphersuite_info->name ) );
1122
1123#if defined(MBEDTLS_HAVE_TIME)
1124 ssl->session_negotiate->start = time( NULL );
1125#endif /* MBEDTLS_HAVE_TIME */
1126
Jerry Yu4a173382021-10-11 21:45:31 +08001127 /* ...
1128 * uint8 legacy_compression_method = 0;
1129 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001130 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001131 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001132 if( p[0] != 0 )
1133 {
Jerry Yub85277e2021-10-13 13:36:05 +08001134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001135 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001136 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001137 }
1138 p++;
1139
Jerry Yub85277e2021-10-13 13:36:05 +08001140 /* ...
1141 * Extension extensions<6..2^16-1>;
1142 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001143 * struct {
1144 * ExtensionType extension_type; (2 bytes)
1145 * opaque extension_data<0..2^16-1>;
1146 * } Extension;
1147 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001149 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001151
Jerry Yu4a173382021-10-11 21:45:31 +08001152 /* Check extensions do not go beyond the buffer of data. */
1153 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1154 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001155
Jerry Yu4a173382021-10-11 21:45:31 +08001156 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001157
Jerry Yu4a173382021-10-11 21:45:31 +08001158 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001159 {
1160 unsigned int extension_type;
1161 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001162 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001163
Jerry Yu4a173382021-10-11 21:45:31 +08001164 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1166 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1167 p += 4;
1168
Jerry Yu4a173382021-10-11 21:45:31 +08001169 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001170 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001171
1172 switch( extension_type )
1173 {
XiaokangQianb851da82022-01-14 04:03:11 +00001174 case MBEDTLS_TLS_EXT_COOKIE:
1175
XiaokangQiand9e068e2022-01-18 06:23:32 +00001176 if( !is_hrr )
1177 {
XiaokangQian52da5582022-01-26 09:49:29 +00001178 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001180 }
1181
XiaokangQian43550bd2022-01-21 04:32:58 +00001182 ret = ssl_tls13_parse_cookie_ext( ssl,
1183 p, extension_data_end );
1184 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001185 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001186 MBEDTLS_SSL_DEBUG_RET( 1,
1187 "ssl_tls13_parse_cookie_ext",
1188 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001189 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001190 }
XiaokangQianb851da82022-01-14 04:03:11 +00001191 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001192
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001194 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001195 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001196 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001197 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001198 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001199 break;
1200
1201 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001204
XiaokangQian52da5582022-01-26 09:49:29 +00001205 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001206 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001207
Jerry Yue1b9c292021-09-10 10:08:31 +08001208 case MBEDTLS_TLS_EXT_KEY_SHARE:
1209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001210 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1211 {
XiaokangQian52da5582022-01-26 09:49:29 +00001212 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001213 goto cleanup;
1214 }
1215
XiaokangQian53f20b72022-01-18 10:47:33 +00001216 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001217 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001218 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001219 else
XiaokangQianb851da82022-01-14 04:03:11 +00001220 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001221 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001222 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 {
1224 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001225 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001226 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001227 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001228 }
1229 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001230
1231 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001232 MBEDTLS_SSL_DEBUG_MSG(
1233 3,
1234 ( "unknown extension found: %u ( ignoring )",
1235 extension_type ) );
1236
XiaokangQian52da5582022-01-26 09:49:29 +00001237 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001238 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001239 }
1240
1241 p += extension_data_len;
1242 }
1243
XiaokangQiand59be772022-01-24 10:12:51 +00001244cleanup:
1245
XiaokangQian52da5582022-01-26 09:49:29 +00001246 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001247 {
1248 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1249 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1250 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1251 }
XiaokangQian52da5582022-01-26 09:49:29 +00001252 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001253 {
1254 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1255 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1256 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1257 }
1258 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001259}
1260
XiaokangQian355e09a2022-01-20 11:14:50 +00001261static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001262{
Jerry Yub85277e2021-10-13 13:36:05 +08001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001264 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001265 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001266 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001267
Jerry Yub85277e2021-10-13 13:36:05 +08001268 /* Determine the key exchange mode:
1269 * 1) If both the pre_shared_key and key_share extensions were received
1270 * then the key exchange mode is PSK with EPHEMERAL.
1271 * 2) If only the pre_shared_key extension was received then the key
1272 * exchange mode is PSK-only.
1273 * 3) If only the key_share extension was received then the key
1274 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001275 */
Jerry Yub85277e2021-10-13 13:36:05 +08001276 switch( handshake->extensions_present &
1277 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001278 {
Jerry Yu745bb612021-10-13 22:01:04 +08001279 /* Only the pre_shared_key extension was received */
1280 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001281 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001282 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001283
Jerry Yu745bb612021-10-13 22:01:04 +08001284 /* Only the key_share extension was received */
1285 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001286 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001287 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001288
Jerry Yu745bb612021-10-13 22:01:04 +08001289 /* Both the pre_shared_key and key_share extensions were received */
1290 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001291 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001292 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001293
Jerry Yu745bb612021-10-13 22:01:04 +08001294 /* Neither pre_shared_key nor key_share extension was received */
1295 default:
1296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1297 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1298 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001299 }
1300
1301 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1302 *
1303 * TODO: We don't have to do this in case we offered 0-RTT and the
1304 * server accepted it. In this case, we could skip generating
1305 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001306 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001307 if( ret != 0 )
1308 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001310 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001311 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001312 }
1313
1314 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001315 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001316 if( ret != 0 )
1317 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001319 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001320 }
1321
1322 /* Next evolution in key schedule: Establish handshake secret and
1323 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001324 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001325 if( ret != 0 )
1326 {
Jerry Yuc068b662021-10-11 22:30:19 +08001327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1328 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001329 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001330 }
1331
Jerry Yub85277e2021-10-13 13:36:05 +08001332 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001333 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001334 {
1335 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1336 goto cleanup;
1337 }
Jerry Yu0b177842021-09-05 19:41:30 +08001338
1339 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1340 ssl->conf->endpoint,
1341 ssl->session_negotiate->ciphersuite,
1342 &traffic_keys,
1343 ssl );
1344 if( ret != 0 )
1345 {
1346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001347 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001348 }
1349
Jerry Yub85277e2021-10-13 13:36:05 +08001350 handshake->transform_handshake = transform_handshake;
1351 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001352
1353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1354 ssl->session_in = ssl->session_negotiate;
1355
1356 /*
1357 * State machine update
1358 */
1359 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1360
Jerry Yu4a173382021-10-11 21:45:31 +08001361cleanup:
1362
Jerry Yu0b177842021-09-05 19:41:30 +08001363 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001364 if( ret != 0 )
1365 {
Jerry Yub85277e2021-10-13 13:36:05 +08001366 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001367
Jerry Yu4a173382021-10-11 21:45:31 +08001368 MBEDTLS_SSL_PEND_FATAL_ALERT(
1369 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1370 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1371 }
1372 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001373}
1374
XiaokangQian355e09a2022-01-20 11:14:50 +00001375static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001376{
1377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1378
XiaokangQian647719a2021-12-07 09:16:29 +00001379#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1380 /* If not offering early data, the client sends a dummy CCS record
1381 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001382 * its second ClientHello or before its encrypted handshake flight.
1383 */
XiaokangQian647719a2021-12-07 09:16:29 +00001384 mbedtls_ssl_handshake_set_state( ssl,
1385 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1386#else
1387 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1388#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1389
XiaokangQian78b1fa72022-01-19 06:56:30 +00001390 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001391
XiaokangQian78b1fa72022-01-19 06:56:30 +00001392 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001393 * We are going to re-generate a shared secret corresponding to the group
1394 * selected by the server, which is different from the group for which we
1395 * generated a shared secret in the first client hello.
1396 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001397 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001398 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001399 if( ret != 0 )
1400 return( ret );
1401
1402 return( 0 );
1403}
1404
Jerry Yue1b9c292021-09-10 10:08:31 +08001405/*
Jerry Yu4a173382021-10-11 21:45:31 +08001406 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001407 * Handler for MBEDTLS_SSL_SERVER_HELLO
1408 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001409static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001410{
Jerry Yu4a173382021-10-11 21:45:31 +08001411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001412 unsigned char *buf = NULL;
1413 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001414 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001415
1416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1417
1418 /* Coordination step
1419 * - Fetch record
1420 * - Make sure it's either a ServerHello or a HRR.
1421 * - Switch processing routine in case of HRR
1422 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001423 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1424 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1425
XiaokangQian16acd4b2022-01-14 07:35:47 +00001426 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001427 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001428 goto cleanup;
1429 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001430 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001431
Ronald Cron9f0fba32022-02-10 16:45:15 +01001432 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1433 {
1434 ret = 0;
1435 goto cleanup;
1436 }
1437
XiaokangQianb851da82022-01-14 04:03:11 +00001438 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001439 buf + buf_len,
1440 is_hrr ) );
1441 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001442 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1443
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001444 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1445 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001446
XiaokangQiand9e068e2022-01-18 06:23:32 +00001447 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001448 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001449 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001450 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001451
1452cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1454 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001455 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001456}
1457
1458/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001459 *
1460 * EncryptedExtensions message
1461 *
1462 * The EncryptedExtensions message contains any extensions which
1463 * should be protected, i.e., any which are not needed to establish
1464 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001465 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001466
1467/*
1468 * Overview
1469 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470
1471/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001472static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473
XiaokangQian97799ac2021-10-11 10:05:54 +00001474static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1475 const unsigned char *buf,
1476 const unsigned char *end );
1477static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001478
1479/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001480 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001481 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001482static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001483{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001484 int ret;
1485 unsigned char *buf;
1486 size_t buf_len;
1487
1488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1489
Xiaofei Bai746f9482021-11-12 08:53:56 +00001490 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001491 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492 &buf, &buf_len ) );
1493
1494 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001495 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001496 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001497
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001498 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1499 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001500
XiaokangQian97799ac2021-10-11 10:05:54 +00001501 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001502
1503cleanup:
1504
1505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1506 return( ret );
1507
1508}
1509
XiaokangQian08da26c2021-10-09 10:12:11 +00001510/* Parse EncryptedExtensions message
1511 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001512 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001513 * } EncryptedExtensions;
1514 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001515static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1516 const unsigned char *buf,
1517 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001518{
1519 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001520 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001521 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001522 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523
XiaokangQian08da26c2021-10-09 10:12:11 +00001524 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001525 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001526 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001527
XiaokangQian97799ac2021-10-11 10:05:54 +00001528 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001529 extensions_end = p + extensions_len;
1530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001531
XiaokangQian8db25ff2021-10-13 05:56:18 +00001532 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001533 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001534 unsigned int extension_type;
1535 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001536
XiaokangQian08da26c2021-10-09 10:12:11 +00001537 /*
1538 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001539 * ExtensionType extension_type; (2 bytes)
1540 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001541 * } Extension;
1542 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001543 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001544 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1545 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1546 p += 4;
1547
XiaokangQian8db25ff2021-10-13 05:56:18 +00001548 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549
XiaokangQian97799ac2021-10-11 10:05:54 +00001550 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001551 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001552 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001553 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001554 switch( extension_type )
1555 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001556
XiaokangQian08da26c2021-10-09 10:12:11 +00001557 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1559 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560
lhuang0486cacac2022-01-21 07:34:27 -08001561#if defined(MBEDTLS_SSL_ALPN)
1562 case MBEDTLS_TLS_EXT_ALPN:
1563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1564
1565 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1566 {
1567 return( ret );
1568 }
1569
1570 break;
1571#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001572 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001573 MBEDTLS_SSL_DEBUG_MSG(
1574 3, ( "unsupported extension found: %u ", extension_type) );
1575 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001576 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001577 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1578 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001579 }
1580
XiaokangQian08da26c2021-10-09 10:12:11 +00001581 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001582 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001583
XiaokangQian97799ac2021-10-11 10:05:54 +00001584 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001585 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001586 {
1587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001588 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001589 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001590 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591 }
1592
1593 return( ret );
1594}
1595
XiaokangQian97799ac2021-10-11 10:05:54 +00001596static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001597{
Jerry Yua93ac112021-10-27 16:31:48 +08001598#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001599 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001600 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1601 else
Jerry Yud2674312021-10-29 10:08:19 +08001602 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001603#else
1604 ((void) ssl);
1605 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1606#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001607 return( 0 );
1608}
1609
Jerry Yua93ac112021-10-27 16:31:48 +08001610#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001611/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001612 *
1613 * STATE HANDLING: CertificateRequest
1614 *
Jerry Yud2674312021-10-29 10:08:19 +08001615 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001616#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1617#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001618/* Coordination:
1619 * Deals with the ambiguity of not knowing if a CertificateRequest
1620 * will be sent. Returns a negative code on failure, or
1621 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1622 * - SSL_CERTIFICATE_REQUEST_SKIP
1623 * indicating if a Certificate Request is expected or not.
1624 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001625static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001626{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001628
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001629 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001630 {
1631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1632 return( SSL_CERTIFICATE_REQUEST_SKIP );
1633 }
1634
1635 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001636 {
1637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1638 return( ret );
1639 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001641
1642 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1643 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1644 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001645 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001646 }
1647
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001648 return( SSL_CERTIFICATE_REQUEST_SKIP );
1649}
1650
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001651/*
1652 * ssl_tls13_parse_certificate_request()
1653 * Parse certificate request
1654 * struct {
1655 * opaque certificate_request_context<0..2^8-1>;
1656 * Extension extensions<2..2^16-1>;
1657 * } CertificateRequest;
1658 */
1659static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1660 const unsigned char *buf,
1661 const unsigned char *end )
1662{
1663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1664 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001666 size_t extensions_len = 0;
1667 const unsigned char *extensions_end;
1668 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001670 /* ...
1671 * opaque certificate_request_context<0..2^8-1>
1672 * ...
1673 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1675 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001676 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678 if( certificate_request_context_len > 0 )
1679 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001680 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001681 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1682 p, certificate_request_context_len );
1683
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001684 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001685 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001686 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001687 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001688 {
1689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1690 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1691 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001692 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001693 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694 p += certificate_request_context_len;
1695 }
1696
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001697 /* ...
1698 * Extension extensions<2..2^16-1>;
1699 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001700 */
1701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1702 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1703 p += 2;
1704
1705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1706 extensions_end = p + extensions_len;
1707
1708 while( p < extensions_end )
1709 {
1710 unsigned int extension_type;
1711 size_t extension_data_len;
1712
1713 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1714 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1715 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1716 p += 4;
1717
1718 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1719
1720 switch( extension_type )
1721 {
1722 case MBEDTLS_TLS_EXT_SIG_ALG:
1723 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001724 ( "found signature algorithms extension" ) );
1725 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 p + extension_data_len );
1727 if( ret != 0 )
1728 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001729 if( ! sig_alg_ext_found )
1730 sig_alg_ext_found = 1;
1731 else
1732 {
1733 MBEDTLS_SSL_DEBUG_MSG( 3,
1734 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001735 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001736 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001737 break;
1738
1739 default:
1740 MBEDTLS_SSL_DEBUG_MSG(
1741 3,
1742 ( "unknown extension found: %u ( ignoring )",
1743 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001744 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001745 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001746 p += extension_data_len;
1747 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001748 /* Check that we consumed all the message. */
1749 if( p != end )
1750 {
1751 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001752 ( "CertificateRequest misaligned" ) );
1753 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001754 }
1755 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001756 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001757 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001758 MBEDTLS_SSL_DEBUG_MSG( 3,
1759 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001760 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001761 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762
Jerry Yu7840f812022-01-29 10:26:51 +08001763 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001765
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001766decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001767 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1768 MBEDTLS_ERR_SSL_DECODE_ERROR );
1769 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001770}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001771
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001772/*
1773 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1774 */
1775static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001776{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001778
1779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1780
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001781 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1782
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001783 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1784 {
1785 unsigned char *buf;
1786 size_t buf_len;
1787
1788 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1789 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1790 &buf, &buf_len ) );
1791
1792 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1793 buf, buf + buf_len ) );
1794
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001795 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1796 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001798 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001799 {
1800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001801 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001802 }
1803 else
1804 {
1805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001806 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1807 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001808 }
1809
1810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001811 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001812
Jerry Yud2674312021-10-29 10:08:19 +08001813 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1814
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001815cleanup:
1816
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1818 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001819}
1820
1821/*
Jerry Yu687101b2021-09-14 16:03:56 +08001822 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1823 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001824static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001825{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001826 int ret;
1827
1828 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001829 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001830 return( ret );
1831
Jerry Yu687101b2021-09-14 16:03:56 +08001832 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1833 return( 0 );
1834}
1835
1836/*
1837 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1838 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001839static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001840{
Jerry Yu30b071c2021-09-12 20:16:03 +08001841 int ret;
1842
1843 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1844 if( ret != 0 )
1845 return( ret );
1846
Jerry Yu687101b2021-09-14 16:03:56 +08001847 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1848 return( 0 );
1849}
Jerry Yua93ac112021-10-27 16:31:48 +08001850#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001851
Jerry Yu687101b2021-09-14 16:03:56 +08001852/*
1853 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1854 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001855static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001856{
XiaokangQianac0385c2021-11-03 06:40:11 +00001857 int ret;
1858
XiaokangQianc5c39d52021-11-09 11:55:10 +00001859 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001860 if( ret != 0 )
1861 return( ret );
1862
Ronald Cron49ad6192021-11-24 16:25:31 +01001863#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1864 mbedtls_ssl_handshake_set_state(
1865 ssl,
1866 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1867#else
Jerry Yuca133a32022-02-15 14:22:05 +08001868 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001869#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001870
XiaokangQianac0385c2021-11-03 06:40:11 +00001871 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001872}
1873
1874/*
Jerry Yu566c7812022-01-26 15:41:22 +08001875 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1876 */
1877static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1878{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001879 int non_empty_certificate_msg = 0;
1880
Jerry Yu5cc35062022-01-28 16:16:08 +08001881 MBEDTLS_SSL_DEBUG_MSG( 1,
1882 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001883 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001884
Ronald Cron9df7c802022-03-08 18:38:54 +01001885#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001886 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001887 {
1888 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1889 if( ret != 0 )
1890 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001891
Ronald Cron7a94aca2022-03-09 07:44:27 +01001892 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1893 non_empty_certificate_msg = 1;
1894 }
1895 else
1896 {
1897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1898 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001899#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001900
Ronald Cron7a94aca2022-03-09 07:44:27 +01001901 if( non_empty_certificate_msg )
1902 {
1903 mbedtls_ssl_handshake_set_state( ssl,
1904 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1905 }
1906 else
1907 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1908
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001909 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001910}
1911
Ronald Cron9df7c802022-03-08 18:38:54 +01001912#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001913/*
1914 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1915 */
1916static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1917{
Ronald Crona8b38872022-03-09 07:59:25 +01001918 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1919
1920 if( ret == 0 )
1921 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1922
1923 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001924}
Jerry Yu90f152d2022-01-29 22:12:42 +08001925#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001926
1927/*
Jerry Yu687101b2021-09-14 16:03:56 +08001928 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1929 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001930static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001931{
XiaokangQian0fa66432021-11-15 03:33:57 +00001932 int ret;
1933
1934 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1935 if( ret != 0 )
1936 return( ret );
1937
1938 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1939 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001940}
1941
1942/*
1943 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1944 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001945static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001946{
Jerry Yu378254d2021-10-30 21:44:47 +08001947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001948 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001949 return( 0 );
1950}
1951
1952/*
1953 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1954 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001955static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001956{
Jerry Yu378254d2021-10-30 21:44:47 +08001957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1958 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1959
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1961 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1962
1963 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1964
1965 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1966 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001967}
1968
Jerry Yu92c6b402021-08-27 16:59:09 +08001969int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001970{
Jerry Yu92c6b402021-08-27 16:59:09 +08001971 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001972
Jerry Yu92c6b402021-08-27 16:59:09 +08001973 switch( ssl->state )
1974 {
1975 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001976 * ssl->state is initialized as HELLO_REQUEST. It is the same
1977 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001978 */
1979 case MBEDTLS_SSL_HELLO_REQUEST:
1980 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001981 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001982 break;
1983
1984 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001985 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001986 break;
1987
1988 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001989 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001990 break;
1991
Jerry Yua93ac112021-10-27 16:31:48 +08001992#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001993 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1994 ret = ssl_tls13_process_certificate_request( ssl );
1995 break;
1996
Jerry Yu687101b2021-09-14 16:03:56 +08001997 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001998 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001999 break;
2000
2001 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002002 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002003 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002004#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002005
2006 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002007 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002008 break;
2009
Jerry Yu566c7812022-01-26 15:41:22 +08002010 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2011 ret = ssl_tls13_write_client_certificate( ssl );
2012 break;
2013
Ronald Cron9df7c802022-03-08 18:38:54 +01002014#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002015 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2016 ret = ssl_tls13_write_client_certificate_verify( ssl );
2017 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002018#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002019
Jerry Yu687101b2021-09-14 16:03:56 +08002020 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002021 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002022 break;
2023
2024 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002025 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002026 break;
2027
2028 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002029 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002030 break;
2031
Ronald Cron49ad6192021-11-24 16:25:31 +01002032 /*
2033 * Injection of dummy-CCS's for middlebox compatibility
2034 */
2035#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002036 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002037 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2038 if( ret == 0 )
2039 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2040 break;
2041
2042 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2043 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2044 if( ret == 0 )
2045 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002046 break;
2047#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2048
Jerry Yu92c6b402021-08-27 16:59:09 +08002049 default:
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2052 }
2053
2054 return( ret );
2055}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002056
Jerry Yufb4b6472022-01-27 15:03:26 +08002057#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002058
Jerry Yufb4b6472022-01-27 15:03:26 +08002059