blob: cf5b38285174d1ffdb629e76f712caafaa29f5ee [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"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000048 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040051 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
52 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080053
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020062 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Ronald Crona77fc272022-03-30 17:20:47 +020064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010065
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010067 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080068 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010071 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu0c63af62021-09-02 12:59:12 +080073 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080074 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010075 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080076 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040077 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Glenn Strausscd78df62022-04-07 19:07:11 -040082 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010083 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040084 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
87 }
88
89 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Ronald Cron98473382022-03-30 20:04:10 +0200100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400101 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
102 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
Ronald Cron98473382022-03-30 20:04:10 +0200111 if( &buf[2] != end )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
115 MBEDTLS_ERR_SSL_DECODE_ERROR );
116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
117 }
118
Jerry Yue1b9c292021-09-10 10:08:31 +0800119 return( 0 );
120}
121
lhuang0486cacac2022-01-21 07:34:27 -0800122#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800123static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
124 const unsigned char *buf, size_t len )
125{
126 size_t list_len, name_len;
127 const unsigned char *p = buf;
128 const unsigned char *end = buf + len;
129
130 /* If we didn't send it, the server shouldn't send it */
131 if( ssl->conf->alpn_list == NULL )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
134 /*
135 * opaque ProtocolName<1..2^8-1>;
136 *
137 * struct {
138 * ProtocolName protocol_name_list<2..2^16-1>
139 * } ProtocolNameList;
140 *
141 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
142 */
143
144 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
146
147 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
148 p += 2;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
150
151 name_len = *p++;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
153
154 /* Check that the server chosen protocol was in our list and save it */
155 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
156 {
157 if( name_len == strlen( *alpn ) &&
158 memcmp( buf + 3, *alpn, name_len ) == 0 )
159 {
160 ssl->alpn_chosen = *alpn;
161 return( 0 );
162 }
163 }
164
165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
166}
167#endif /* MBEDTLS_SSL_ALPN */
168
XiaokangQian16acd4b2022-01-14 07:35:47 +0000169static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000170{
171 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( group_id == 0 )
174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
175
XiaokangQian355e09a2022-01-20 11:14:50 +0000176#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000178 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
181
182 /* Destroy generated private key. */
183 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
184 if( status != PSA_SUCCESS )
185 {
186 ret = psa_ssl_status_to_mbedtls( status );
187 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
188 return( ret );
189 }
190
191 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000192 return( 0 );
193 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000194 else
195#endif /* MBEDTLS_ECDH_C */
196 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000197 {
198 /* Do something */
199 }
200
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202}
203
204/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205 * Functions for writing key_share extension.
206 */
207#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800208static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800209 mbedtls_ssl_context *ssl,
210 uint16_t named_group,
211 unsigned char *buf,
212 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000213 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800214{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100215 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
216 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
217 psa_key_attributes_t key_attributes;
218 size_t own_pubkey_len;
219 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
220 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800223
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100224 /* Convert EC group to PSA key type. */
225 if( ( handshake->ecdh_psa_type =
226 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
227 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228
Neil Armstrong91477a72022-03-25 15:42:20 +0100229 ssl->handshake->ecdh_bits = ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800230
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100231 key_attributes = psa_key_attributes_init();
232 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
233 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
234 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
235 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100236
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100237 /* Generate ECDH private key. */
238 status = psa_generate_key( &key_attributes,
239 &handshake->ecdh_psa_privkey );
240 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100242 ret = psa_ssl_status_to_mbedtls( status );
243 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800244 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100245
Jerry Yu56fc07f2021-09-01 17:48:49 +0800246 }
247
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100248 /* Export the public part of the ECDH private key from PSA. */
249 status = psa_export_public_key( handshake->ecdh_psa_privkey,
250 buf, (size_t)( end - buf ),
251 &own_pubkey_len );
252 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100254 ret = psa_ssl_status_to_mbedtls( status );
255 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800256 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100257
Jerry Yu56fc07f2021-09-01 17:48:49 +0800258 }
259
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100260 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100261
Jerry Yu75336352021-09-01 15:59:36 +0800262 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800263}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264#endif /* MBEDTLS_ECDH_C */
265
Jerry Yub60e3cf2021-09-08 16:41:02 +0800266static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
267 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268{
269 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
270
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100273 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800274 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100275 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800276 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
277
Brett Warren14efd332021-10-06 09:32:11 +0100278 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000280 const mbedtls_ecp_curve_info *curve_info;
281 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
282 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100283 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800284 {
Brett Warren14efd332021-10-06 09:32:11 +0100285 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 return( 0 );
287 }
288 }
289#else
290 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800291 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292#endif /* MBEDTLS_ECDH_C */
293
294 /*
295 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800296 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 */
298
299 return( ret );
300}
301
302/*
303 * ssl_tls13_write_key_share_ext
304 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800305 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306 *
307 * struct {
308 * NamedGroup group;
309 * opaque key_exchange<1..2^16-1>;
310 * } KeyShareEntry;
311 * struct {
312 * KeyShareEntry client_shares<0..2^16-1>;
313 * } KeyShareClientHello;
314 */
315static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
316 unsigned char *buf,
317 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000318 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319{
320 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000321 unsigned char *client_shares; /* Start of client_shares */
322 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
325
Xiaofei Baid25fab62021-12-02 06:36:27 +0000326 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327
Jerry Yub60e3cf2021-09-08 16:41:02 +0800328 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 * - extension_type (2 bytes)
330 * - extension_data_length (2 bytes)
331 * - client_shares_length (2 bytes)
332 */
333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
334 p += 6;
335
336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
337
338 /* HRR could already have requested something else. */
339 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800340 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
341 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344 &group_id ) );
345 }
346
347 /*
348 * Dispatch to type-specific key generation function.
349 *
350 * So far, we're only supporting ECDHE. With the introduction
351 * of PQC KEMs, we'll want to have multiple branches, one per
352 * type of KEM, and dispatch to the corresponding crypto. And
353 * only one key share entry is allowed.
354 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000355 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800357 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800359 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000360 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100362 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800363
364 /* Check there is space for header of KeyShareEntry
365 * - group (2 bytes)
366 * - key_exchange_length (2 bytes)
367 */
368 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
369 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100370 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800371 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800372 p += key_exchange_len;
373 if( ret != 0 )
374 return( ret );
375
376 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000377 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800378 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000379 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800380 }
381 else
382#endif /* MBEDTLS_ECDH_C */
383 if( 0 /* other KEMs? */ )
384 {
385 /* Do something */
386 }
387 else
388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
389
Jerry Yub60e3cf2021-09-08 16:41:02 +0800390 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000391 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800392 if( client_shares_len == 0)
393 {
394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800396 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397 /* Write extension_type */
398 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
399 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800400 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800401 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800402 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800403
404 /* Update offered_group_id field */
405 ssl->handshake->offered_group_id = group_id;
406
407 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000408 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800409
Xiaofei Baid25fab62021-12-02 06:36:27 +0000410 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800411
412 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
413
414cleanup:
415
416 return( ret );
417}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800418
Jerry Yue1b9c292021-09-10 10:08:31 +0800419
XiaokangQiand59be772022-01-24 10:12:51 +0000420/*
421 * ssl_tls13_parse_hrr_key_share_ext()
422 * Parse key_share extension in Hello Retry Request
423 *
424 * struct {
425 * NamedGroup selected_group;
426 * } KeyShareHelloRetryRequest;
427 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000428static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000429 const unsigned char *buf,
430 const unsigned char *end )
431{
XiaokangQianb851da82022-01-14 04:03:11 +0000432 const mbedtls_ecp_curve_info *curve_info = NULL;
433 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000434 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000435 int found = 0;
436
437 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
438 if( group_list == NULL )
439 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
440
441 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
442
443 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000444 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000445 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000447
448 /* Upon receipt of this extension in a HelloRetryRequest, the client
449 * MUST first verify that the selected_group field corresponds to a
450 * group which was provided in the "supported_groups" extension in the
451 * original ClientHello.
452 * The supported_group was based on the info in ssl->conf->group_list.
453 *
454 * If the server provided a key share that was not sent in the ClientHello
455 * then the client MUST abort the handshake with an "illegal_parameter" alert.
456 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000457 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000458 {
459 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000460 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000461 continue;
462
463 /* We found a match */
464 found = 1;
465 break;
466 }
467
468 /* Client MUST verify that the selected_group field does not
469 * correspond to a group which was provided in the "key_share"
470 * extension in the original ClientHello. If the server sent an
471 * HRR message with a key share already provided in the
472 * ClientHello then the client MUST abort the handshake with
473 * an "illegal_parameter" alert.
474 */
XiaokangQiand59be772022-01-24 10:12:51 +0000475 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000476 {
477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
478 MBEDTLS_SSL_PEND_FATAL_ALERT(
479 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
480 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
481 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
482 }
483
484 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000485 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000486
487 return( 0 );
488}
489
Jerry Yue1b9c292021-09-10 10:08:31 +0800490/*
Jerry Yub85277e2021-10-13 13:36:05 +0800491 * ssl_tls13_parse_key_share_ext()
492 * Parse key_share extension in Server Hello
493 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800494 * struct {
495 * KeyShareEntry server_share;
496 * } KeyShareServerHello;
497 * struct {
498 * NamedGroup group;
499 * opaque key_exchange<1..2^16-1>;
500 * } KeyShareEntry;
501 */
Jerry Yu4a173382021-10-11 21:45:31 +0800502static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800503 const unsigned char *buf,
504 const unsigned char *end )
505{
Jerry Yub85277e2021-10-13 13:36:05 +0800506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800507 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800508 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800509
Jerry Yu4a173382021-10-11 21:45:31 +0800510 /* ...
511 * NamedGroup group; (2 bytes)
512 * ...
513 */
514 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
515 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800516 p += 2;
517
Jerry Yu4a173382021-10-11 21:45:31 +0800518 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800519 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800520 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 {
522 MBEDTLS_SSL_DEBUG_MSG( 1,
523 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800524 (unsigned) offered_group, (unsigned) group ) );
525 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
526 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
527 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 }
529
530#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800531 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800532 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100533 const mbedtls_ecp_curve_info *curve_info =
534 mbedtls_ecp_curve_info_from_tls_id( group );
535 if( curve_info == NULL )
536 {
537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
539 }
540
541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
542
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000543 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800544 if( ret != 0 )
545 return( ret );
546 }
Jerry Yub85277e2021-10-13 13:36:05 +0800547 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800548#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800549 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800550 {
551 /* Do something */
552 }
553 else
554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
555
556 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
557 return( ret );
558}
559
XiaokangQiand59be772022-01-24 10:12:51 +0000560/*
561 * ssl_tls13_parse_cookie_ext()
562 * Parse cookie extension in Hello Retry Request
563 *
564 * struct {
565 * opaque cookie<1..2^16-1>;
566 * } Cookie;
567 *
568 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
569 * extension to the client (this is an exception to the usual rule that
570 * the only extensions that may be sent are those that appear in the
571 * ClientHello). When sending the new ClientHello, the client MUST copy
572 * the contents of the extension received in the HelloRetryRequest into
573 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
574 * cookies in their initial ClientHello in subsequent connections.
575 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000576static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
577 const unsigned char *buf,
578 const unsigned char *end )
579{
XiaokangQian25c9c902022-02-08 10:49:53 +0000580 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000581 const unsigned char *p = buf;
582 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
583
584 /* Retrieve length field of cookie */
585 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
586 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
587 p += 2;
588
589 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
590 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
591
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000592 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000593 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000594 handshake->cookie = mbedtls_calloc( 1, cookie_len );
595 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000596 {
597 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000598 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000599 cookie_len ) );
600 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
601 }
602
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000603 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000604 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000605
606 return( 0 );
607}
XiaokangQian43550bd2022-01-21 04:32:58 +0000608
XiaokangQian0b64eed2022-01-27 10:36:51 +0000609static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000610 unsigned char *buf,
611 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000612 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000613{
614 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000615 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000616 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000617
XiaokangQianc02768a2022-02-10 07:31:25 +0000618 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000619 {
620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
621 return( 0 );
622 }
623
624 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000625 handshake->cookie,
626 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000627
XiaokangQianc02768a2022-02-10 07:31:25 +0000628 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000629
630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
631
XiaokangQian0b64eed2022-01-27 10:36:51 +0000632 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000633 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
634 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000635 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000636
637 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000638 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000639
XiaokangQianc02768a2022-02-10 07:31:25 +0000640 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000641
642 return( 0 );
643}
644
Ronald Cron3d580bf2022-02-18 17:24:56 +0100645int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
646 unsigned char *buf,
647 unsigned char *end,
648 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100649{
650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
651 unsigned char *p = buf;
652 size_t ext_len;
653
654 *out_len = 0;
655
656 /* Write supported_versions extension
657 *
658 * Supported Versions Extension is mandatory with TLS 1.3.
659 */
660 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
661 if( ret != 0 )
662 return( ret );
663 p += ext_len;
664
665 /* Echo the cookie if the server provided one in its preceding
666 * HelloRetryRequest message.
667 */
668 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
669 if( ret != 0 )
670 return( ret );
671 p += ext_len;
672
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100673 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
674 {
675 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
676 if( ret != 0 )
677 return( ret );
678 p += ext_len;
679 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100680
681 *out_len = p - buf;
682
683 return( 0 );
684}
685
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800686/*
Jerry Yu4a173382021-10-11 21:45:31 +0800687 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800688 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200689
Ronald Cronda41b382022-03-30 09:57:11 +0200690/**
691 * \brief Detect if the ServerHello contains a supported_versions extension
692 * or not.
693 *
694 * \param[in] ssl SSL context
695 * \param[in] buf Buffer containing the ServerHello message
696 * \param[in] end End of the buffer containing the ServerHello message
697 *
698 * \return 0 if the ServerHello does not contain a supported_versions extension
699 * \return 1 if the ServerHello contains a supported_versions extension
700 * \return A negative value if an error occurred while parsing the ServerHello.
701 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100702static int ssl_tls13_is_supported_versions_ext_present(
703 mbedtls_ssl_context *ssl,
704 const unsigned char *buf,
705 const unsigned char *end )
706{
707 const unsigned char *p = buf;
708 size_t legacy_session_id_echo_len;
709 size_t extensions_len;
710 const unsigned char *extensions_end;
711
712 /*
713 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200714 * length:
715 * - legacy_version 2 bytes
716 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
717 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100718 */
719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
720 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
721 legacy_session_id_echo_len = *p;
722
723 /*
724 * Jump to the extensions, jumping over:
725 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
726 * - cipher_suite 2 bytes
727 * - legacy_compression_method 1 byte
728 */
729 p += legacy_session_id_echo_len + 4;
730
731 /* Case of no extension */
732 if( p == end )
733 return( 0 );
734
735 /* ...
736 * Extension extensions<6..2^16-1>;
737 * ...
738 * struct {
739 * ExtensionType extension_type; (2 bytes)
740 * opaque extension_data<0..2^16-1>;
741 * } Extension;
742 */
743 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
744 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
745 p += 2;
746
747 /* Check extensions do not go beyond the buffer of data. */
748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
749 extensions_end = p + extensions_len;
750
751 while( p < extensions_end )
752 {
753 unsigned int extension_type;
754 size_t extension_data_len;
755
756 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
757 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
758 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
759 p += 4;
760
761 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
762 return( 1 );
763
764 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
765 p += extension_data_len;
766 }
767
768 return( 0 );
769}
770
Jerry Yu7a186a02021-10-15 18:46:14 +0800771/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200772 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
773 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
774 * - 0 otherwise
775 */
776static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
777 const unsigned char *buf,
778 const unsigned char *end )
779{
780 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
781 static const unsigned char magic_downgrade_string[] =
782 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
783 const unsigned char *last_eight_bytes_of_random;
784 unsigned char last_byte_of_random;
785
786 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
787 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
788
789 if( memcmp( last_eight_bytes_of_random,
790 magic_downgrade_string,
791 sizeof( magic_downgrade_string ) ) == 0 )
792 {
793 last_byte_of_random = last_eight_bytes_of_random[7];
794 return( last_byte_of_random == 0 ||
795 last_byte_of_random == 1 );
796 }
797
798 return( 0 );
799}
800
801/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800802 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
803 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000804 * to indicate which message is expected and to be parsed next.
805 */
Jerry Yub85277e2021-10-13 13:36:05 +0800806#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
807#define SSL_SERVER_HELLO_COORDINATE_HRR 1
808static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
809 const unsigned char *buf,
810 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800811{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800812 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800813 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
814 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
815 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
816 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
817
818 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
819 *
Jerry Yu4a173382021-10-11 21:45:31 +0800820 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800821 * special value of the SHA-256 of "HelloRetryRequest".
822 *
823 * struct {
824 * ProtocolVersion legacy_version = 0x0303;
825 * Random random;
826 * opaque legacy_session_id_echo<0..32>;
827 * CipherSuite cipher_suite;
828 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800829 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800830 * } ServerHello;
831 *
832 */
Jerry Yub85277e2021-10-13 13:36:05 +0800833 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800834
835 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800836 {
Jerry Yub85277e2021-10-13 13:36:05 +0800837 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 }
839
Jerry Yub85277e2021-10-13 13:36:05 +0800840 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800841}
842
Jerry Yu745bb612021-10-13 22:01:04 +0800843/* Fetch and preprocess
844 * Returns a negative value on failure, and otherwise
845 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100846 * - SSL_SERVER_HELLO_COORDINATE_HRR or
847 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800848 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100849#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800850static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
851 unsigned char **buf,
852 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800853{
Jerry Yu4a173382021-10-11 21:45:31 +0800854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200855 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800856
XiaokangQian355e09a2022-01-20 11:14:50 +0000857 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
858 MBEDTLS_SSL_HS_SERVER_HELLO,
859 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200860 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800861
Ronald Cron9f0fba32022-02-10 16:45:15 +0100862 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200863 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100864 if( ret == 0 )
865 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200866 MBEDTLS_SSL_PROC_CHK_NEG(
867 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
868
869 /* If the server is negotiating TLS 1.2 or below and:
870 * . we did not propose TLS 1.2 or
871 * . the server responded it is TLS 1.3 capable but negotiating a lower
872 * version of the protocol and thus we are under downgrade attack
873 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100874 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200875 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100876 {
877 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
878 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
879 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
880 }
881
882 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400883 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100884 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
885 *buf, *buf_len );
886
887 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
888 {
889 ret = ssl_tls13_reset_key_share( ssl );
890 if( ret != 0 )
891 return( ret );
892 }
893
894 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
895 }
896
Ronald Cronfd6193c2022-04-05 11:04:20 +0200897 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800898 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800899 {
Jerry Yu745bb612021-10-13 22:01:04 +0800900 case SSL_SERVER_HELLO_COORDINATE_HELLO:
901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
902 break;
903 case SSL_SERVER_HELLO_COORDINATE_HRR:
904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000905 /* If a client receives a second
906 * HelloRetryRequest in the same connection (i.e., where the ClientHello
907 * was itself in response to a HelloRetryRequest), it MUST abort the
908 * handshake with an "unexpected_message" alert.
909 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000910 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000911 {
912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
913 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
914 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
915 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
916 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000917 /*
918 * Clients must abort the handshake with an "illegal_parameter"
919 * alert if the HelloRetryRequest would not result in any change
920 * in the ClientHello.
921 * In a PSK only key exchange that what we expect.
922 */
923 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
924 {
925 MBEDTLS_SSL_DEBUG_MSG( 1,
926 ( "Unexpected HRR in pure PSK key exchange." ) );
927 MBEDTLS_SSL_PEND_FATAL_ALERT(
928 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
929 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
930 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
931 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000932
XiaokangQiand9e068e2022-01-18 06:23:32 +0000933 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000934
Jerry Yu745bb612021-10-13 22:01:04 +0800935 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800936 }
937
938cleanup:
939
940 return( ret );
941}
942
Jerry Yu4a173382021-10-11 21:45:31 +0800943static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
944 const unsigned char **buf,
945 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800946{
947 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800948 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800949
Jerry Yude4fb2c2021-09-19 18:05:08 +0800950 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800951 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800952
Jerry Yu4a173382021-10-11 21:45:31 +0800953 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800954
955 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800956 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
957 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
960 ssl->session_negotiate->id,
961 ssl->session_negotiate->id_len );
962 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800963 legacy_session_id_echo_len );
964
965 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
966 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
967
Jerry Yue1b9c292021-09-10 10:08:31 +0800968 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
969 }
970
Jerry Yu4a173382021-10-11 21:45:31 +0800971 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800972 *buf = p;
973
974 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800975 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800976 return( 0 );
977}
978
Jerry Yue1b9c292021-09-10 10:08:31 +0800979/* Parse ServerHello message and configure context
980 *
981 * struct {
982 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
983 * Random random;
984 * opaque legacy_session_id_echo<0..32>;
985 * CipherSuite cipher_suite;
986 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800987 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 * } ServerHello;
989 */
Jerry Yuc068b662021-10-11 22:30:19 +0800990static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
991 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000992 const unsigned char *end,
993 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800994{
Jerry Yub85277e2021-10-13 13:36:05 +0800995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000997 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800998 size_t extensions_len;
999 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001000 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001002 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003
1004 /*
1005 * Check there is space for minimal fields
1006 *
1007 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001008 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 * - legacy_session_id_echo ( 1 byte ), minimum size
1010 * - cipher_suite ( 2 bytes)
1011 * - legacy_compression_method ( 1 byte )
1012 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001013 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001014
1015 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1017
Jerry Yu4a173382021-10-11 21:45:31 +08001018 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001019 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001020 * ...
1021 * with ProtocolVersion defined as:
1022 * uint16 ProtocolVersion;
1023 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001024 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1025 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 {
1027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1028 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1029 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001030 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1031 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001032 }
1033 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001034
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001035 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001036 * Random random;
1037 * ...
1038 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001039 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001040 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001041 if( !is_hrr )
1042 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001043 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001044 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1045 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1046 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1047 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001048 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001049
Jerry Yu4a173382021-10-11 21:45:31 +08001050 /* ...
1051 * opaque legacy_session_id_echo<0..32>;
1052 * ...
1053 */
1054 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 {
XiaokangQian52da5582022-01-26 09:49:29 +00001056 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001057 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 }
1059
Jerry Yu4a173382021-10-11 21:45:31 +08001060 /* ...
1061 * CipherSuite cipher_suite;
1062 * ...
1063 * with CipherSuite defined as:
1064 * uint8 CipherSuite[2];
1065 */
1066 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1068 p += 2;
1069
Jerry Yu4a173382021-10-11 21:45:31 +08001070
XiaokangQian355e09a2022-01-20 11:14:50 +00001071 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001072 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001073 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001074 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001075 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1076 ssl->tls_version,
1077 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001078 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 {
XiaokangQian52da5582022-01-26 09:49:29 +00001080 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001082 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001083 * If we received an HRR before and that the proposed selected
1084 * ciphersuite in this server hello is not the same as the one
1085 * proposed in the HRR, we abort the handshake and send an
1086 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001087 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001088 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1089 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001090 {
XiaokangQian52da5582022-01-26 09:49:29 +00001091 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001092 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001093
XiaokangQian52da5582022-01-26 09:49:29 +00001094 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001095 {
1096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1097 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001098 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001099 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001100
Jerry Yu4a173382021-10-11 21:45:31 +08001101 /* Configure ciphersuites */
1102 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1103
XiaokangQian355e09a2022-01-20 11:14:50 +00001104 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001105 ssl->session_negotiate->ciphersuite = cipher_suite;
1106
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1108 cipher_suite, ciphersuite_info->name ) );
1109
1110#if defined(MBEDTLS_HAVE_TIME)
1111 ssl->session_negotiate->start = time( NULL );
1112#endif /* MBEDTLS_HAVE_TIME */
1113
Jerry Yu4a173382021-10-11 21:45:31 +08001114 /* ...
1115 * uint8 legacy_compression_method = 0;
1116 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001118 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 if( p[0] != 0 )
1120 {
Jerry Yub85277e2021-10-13 13:36:05 +08001121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001122 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001123 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 }
1125 p++;
1126
Jerry Yub85277e2021-10-13 13:36:05 +08001127 /* ...
1128 * Extension extensions<6..2^16-1>;
1129 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001130 * struct {
1131 * ExtensionType extension_type; (2 bytes)
1132 * opaque extension_data<0..2^16-1>;
1133 * } Extension;
1134 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001135 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001136 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001137 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001138
Jerry Yu4a173382021-10-11 21:45:31 +08001139 /* Check extensions do not go beyond the buffer of data. */
1140 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1141 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001142
Jerry Yu4a173382021-10-11 21:45:31 +08001143 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001144
Jerry Yu4a173382021-10-11 21:45:31 +08001145 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 {
1147 unsigned int extension_type;
1148 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001149 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001150
Jerry Yu4a173382021-10-11 21:45:31 +08001151 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001152 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1153 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1154 p += 4;
1155
Jerry Yu4a173382021-10-11 21:45:31 +08001156 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001157 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001158
1159 switch( extension_type )
1160 {
XiaokangQianb851da82022-01-14 04:03:11 +00001161 case MBEDTLS_TLS_EXT_COOKIE:
1162
XiaokangQiand9e068e2022-01-18 06:23:32 +00001163 if( !is_hrr )
1164 {
XiaokangQian52da5582022-01-26 09:49:29 +00001165 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001166 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001167 }
1168
XiaokangQian43550bd2022-01-21 04:32:58 +00001169 ret = ssl_tls13_parse_cookie_ext( ssl,
1170 p, extension_data_end );
1171 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001172 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001173 MBEDTLS_SSL_DEBUG_RET( 1,
1174 "ssl_tls13_parse_cookie_ext",
1175 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001176 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001177 }
XiaokangQianb851da82022-01-14 04:03:11 +00001178 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001179
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001181 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001182 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001183 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001185 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001186 break;
1187
1188 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001191
XiaokangQian52da5582022-01-26 09:49:29 +00001192 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001193 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001194
Jerry Yue1b9c292021-09-10 10:08:31 +08001195 case MBEDTLS_TLS_EXT_KEY_SHARE:
1196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001197 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1198 {
XiaokangQian52da5582022-01-26 09:49:29 +00001199 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001200 goto cleanup;
1201 }
1202
XiaokangQian53f20b72022-01-18 10:47:33 +00001203 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001204 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001205 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001206 else
XiaokangQianb851da82022-01-14 04:03:11 +00001207 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001208 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001209 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001210 {
1211 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001212 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001213 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001214 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 }
1216 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001217
1218 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001219 MBEDTLS_SSL_DEBUG_MSG(
1220 3,
1221 ( "unknown extension found: %u ( ignoring )",
1222 extension_type ) );
1223
XiaokangQian52da5582022-01-26 09:49:29 +00001224 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001225 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001226 }
1227
1228 p += extension_data_len;
1229 }
1230
XiaokangQiand59be772022-01-24 10:12:51 +00001231cleanup:
1232
XiaokangQian52da5582022-01-26 09:49:29 +00001233 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001234 {
1235 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1236 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1237 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1238 }
XiaokangQian52da5582022-01-26 09:49:29 +00001239 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001240 {
1241 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1242 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1243 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1244 }
1245 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001246}
1247
XiaokangQian355e09a2022-01-20 11:14:50 +00001248static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001249{
Jerry Yub85277e2021-10-13 13:36:05 +08001250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001251 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001252 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001253 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001254
Jerry Yub85277e2021-10-13 13:36:05 +08001255 /* Determine the key exchange mode:
1256 * 1) If both the pre_shared_key and key_share extensions were received
1257 * then the key exchange mode is PSK with EPHEMERAL.
1258 * 2) If only the pre_shared_key extension was received then the key
1259 * exchange mode is PSK-only.
1260 * 3) If only the key_share extension was received then the key
1261 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001262 */
Jerry Yub85277e2021-10-13 13:36:05 +08001263 switch( handshake->extensions_present &
1264 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001265 {
Jerry Yu745bb612021-10-13 22:01:04 +08001266 /* Only the pre_shared_key extension was received */
1267 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001268 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001269 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001270
Jerry Yu745bb612021-10-13 22:01:04 +08001271 /* Only the key_share extension was received */
1272 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001273 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001274 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001275
Jerry Yu745bb612021-10-13 22:01:04 +08001276 /* Both the pre_shared_key and key_share extensions were received */
1277 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001278 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001279 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001280
Jerry Yu745bb612021-10-13 22:01:04 +08001281 /* Neither pre_shared_key nor key_share extension was received */
1282 default:
1283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1284 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1285 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001286 }
1287
1288 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1289 *
1290 * TODO: We don't have to do this in case we offered 0-RTT and the
1291 * server accepted it. In this case, we could skip generating
1292 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001293 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001294 if( ret != 0 )
1295 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001297 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001298 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001299 }
1300
1301 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001302 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001303 if( ret != 0 )
1304 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001306 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001307 }
1308
1309 /* Next evolution in key schedule: Establish handshake secret and
1310 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001311 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001312 if( ret != 0 )
1313 {
Jerry Yuc068b662021-10-11 22:30:19 +08001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1315 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001316 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001317 }
1318
Jerry Yub85277e2021-10-13 13:36:05 +08001319 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001320 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001321 {
1322 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1323 goto cleanup;
1324 }
Jerry Yu0b177842021-09-05 19:41:30 +08001325
1326 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1327 ssl->conf->endpoint,
1328 ssl->session_negotiate->ciphersuite,
1329 &traffic_keys,
1330 ssl );
1331 if( ret != 0 )
1332 {
1333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001334 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001335 }
1336
Jerry Yub85277e2021-10-13 13:36:05 +08001337 handshake->transform_handshake = transform_handshake;
1338 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001339
1340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1341 ssl->session_in = ssl->session_negotiate;
1342
1343 /*
1344 * State machine update
1345 */
1346 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1347
Jerry Yu4a173382021-10-11 21:45:31 +08001348cleanup:
1349
Jerry Yu0b177842021-09-05 19:41:30 +08001350 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001351 if( ret != 0 )
1352 {
Jerry Yub85277e2021-10-13 13:36:05 +08001353 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001354
Jerry Yu4a173382021-10-11 21:45:31 +08001355 MBEDTLS_SSL_PEND_FATAL_ALERT(
1356 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1357 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1358 }
1359 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001360}
1361
XiaokangQian355e09a2022-01-20 11:14:50 +00001362static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001363{
1364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1365
XiaokangQian647719a2021-12-07 09:16:29 +00001366#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1367 /* If not offering early data, the client sends a dummy CCS record
1368 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001369 * its second ClientHello or before its encrypted handshake flight.
1370 */
XiaokangQian647719a2021-12-07 09:16:29 +00001371 mbedtls_ssl_handshake_set_state( ssl,
1372 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1373#else
1374 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1375#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1376
XiaokangQian78b1fa72022-01-19 06:56:30 +00001377 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001378
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001380 * We are going to re-generate a shared secret corresponding to the group
1381 * selected by the server, which is different from the group for which we
1382 * generated a shared secret in the first client hello.
1383 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001384 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001385 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001386 if( ret != 0 )
1387 return( ret );
1388
1389 return( 0 );
1390}
1391
Jerry Yue1b9c292021-09-10 10:08:31 +08001392/*
Jerry Yu4a173382021-10-11 21:45:31 +08001393 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001394 * Handler for MBEDTLS_SSL_SERVER_HELLO
1395 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001396static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001397{
Jerry Yu4a173382021-10-11 21:45:31 +08001398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001399 unsigned char *buf = NULL;
1400 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001401 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001402
1403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1404
1405 /* Coordination step
1406 * - Fetch record
1407 * - Make sure it's either a ServerHello or a HRR.
1408 * - Switch processing routine in case of HRR
1409 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001410 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1411
XiaokangQian16acd4b2022-01-14 07:35:47 +00001412 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001413 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001414 goto cleanup;
1415 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001416 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001417
Ronald Cron9f0fba32022-02-10 16:45:15 +01001418 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1419 {
1420 ret = 0;
1421 goto cleanup;
1422 }
1423
XiaokangQianb851da82022-01-14 04:03:11 +00001424 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001425 buf + buf_len,
1426 is_hrr ) );
1427 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001428 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1429
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001430 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1431 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001432
XiaokangQiand9e068e2022-01-18 06:23:32 +00001433 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001434 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001435 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001436 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001437
1438cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1440 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001441 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001442}
1443
1444/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001445 *
1446 * EncryptedExtensions message
1447 *
1448 * The EncryptedExtensions message contains any extensions which
1449 * should be protected, i.e., any which are not needed to establish
1450 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001451 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001452
1453/*
1454 * Overview
1455 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001456
1457/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001458static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001459
XiaokangQian97799ac2021-10-11 10:05:54 +00001460static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1461 const unsigned char *buf,
1462 const unsigned char *end );
1463static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001464
1465/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001466 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001468static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001469{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470 int ret;
1471 unsigned char *buf;
1472 size_t buf_len;
1473
1474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1475
Xiaofei Bai746f9482021-11-12 08:53:56 +00001476 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001477 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001478 &buf, &buf_len ) );
1479
1480 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001481 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001482 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001484 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1485 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001486
XiaokangQian97799ac2021-10-11 10:05:54 +00001487 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001488
1489cleanup:
1490
1491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1492 return( ret );
1493
1494}
1495
XiaokangQian08da26c2021-10-09 10:12:11 +00001496/* Parse EncryptedExtensions message
1497 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001498 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001499 * } EncryptedExtensions;
1500 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001501static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1502 const unsigned char *buf,
1503 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001504{
1505 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001506 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001507 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001508 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001509
XiaokangQian08da26c2021-10-09 10:12:11 +00001510 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001511 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001512 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001513
XiaokangQian97799ac2021-10-11 10:05:54 +00001514 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001515 extensions_end = p + extensions_len;
1516 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001517
XiaokangQian8db25ff2021-10-13 05:56:18 +00001518 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001519 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001520 unsigned int extension_type;
1521 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001522
XiaokangQian08da26c2021-10-09 10:12:11 +00001523 /*
1524 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001525 * ExtensionType extension_type; (2 bytes)
1526 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001527 * } Extension;
1528 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001529 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001530 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1531 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1532 p += 4;
1533
XiaokangQian8db25ff2021-10-13 05:56:18 +00001534 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001535
XiaokangQian97799ac2021-10-11 10:05:54 +00001536 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001537 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001538 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001539 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001540 switch( extension_type )
1541 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542
XiaokangQian08da26c2021-10-09 10:12:11 +00001543 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1545 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001546
lhuang0486cacac2022-01-21 07:34:27 -08001547#if defined(MBEDTLS_SSL_ALPN)
1548 case MBEDTLS_TLS_EXT_ALPN:
1549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1550
1551 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1552 {
1553 return( ret );
1554 }
1555
1556 break;
1557#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001558 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001559 MBEDTLS_SSL_DEBUG_MSG(
1560 3, ( "unsupported extension found: %u ", extension_type) );
1561 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001562 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001563 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1564 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001565 }
1566
XiaokangQian08da26c2021-10-09 10:12:11 +00001567 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001568 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001569
XiaokangQian97799ac2021-10-11 10:05:54 +00001570 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001571 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001572 {
1573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001574 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001575 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001576 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001577 }
1578
1579 return( ret );
1580}
1581
XiaokangQian97799ac2021-10-11 10:05:54 +00001582static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001583{
Jerry Yua93ac112021-10-27 16:31:48 +08001584#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001585 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001586 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1587 else
Jerry Yud2674312021-10-29 10:08:19 +08001588 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001589#else
1590 ((void) ssl);
1591 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1592#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001593 return( 0 );
1594}
1595
Jerry Yua93ac112021-10-27 16:31:48 +08001596#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001597/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001598 *
1599 * STATE HANDLING: CertificateRequest
1600 *
Jerry Yud2674312021-10-29 10:08:19 +08001601 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001602#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1603#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001604/* Coordination:
1605 * Deals with the ambiguity of not knowing if a CertificateRequest
1606 * will be sent. Returns a negative code on failure, or
1607 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1608 * - SSL_CERTIFICATE_REQUEST_SKIP
1609 * indicating if a Certificate Request is expected or not.
1610 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001611static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001612{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001614
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001615 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001616 {
1617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1618 return( SSL_CERTIFICATE_REQUEST_SKIP );
1619 }
1620
1621 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001622 {
1623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1624 return( ret );
1625 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001626 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001627
1628 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1629 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1630 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001631 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001632 }
1633
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001634 return( SSL_CERTIFICATE_REQUEST_SKIP );
1635}
1636
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001637/*
1638 * ssl_tls13_parse_certificate_request()
1639 * Parse certificate request
1640 * struct {
1641 * opaque certificate_request_context<0..2^8-1>;
1642 * Extension extensions<2..2^16-1>;
1643 * } CertificateRequest;
1644 */
1645static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1646 const unsigned char *buf,
1647 const unsigned char *end )
1648{
1649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1650 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001651 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001652 size_t extensions_len = 0;
1653 const unsigned char *extensions_end;
1654 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001655
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001656 /* ...
1657 * opaque certificate_request_context<0..2^8-1>
1658 * ...
1659 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1661 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001662 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001663
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001664 if( certificate_request_context_len > 0 )
1665 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001666 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001667 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1668 p, certificate_request_context_len );
1669
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001670 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001671 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001672 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001673 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 {
1675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1676 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1677 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001678 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001680 p += certificate_request_context_len;
1681 }
1682
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001683 /* ...
1684 * Extension extensions<2..2^16-1>;
1685 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001686 */
1687 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1688 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1689 p += 2;
1690
1691 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1692 extensions_end = p + extensions_len;
1693
1694 while( p < extensions_end )
1695 {
1696 unsigned int extension_type;
1697 size_t extension_data_len;
1698
1699 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1700 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1701 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1702 p += 4;
1703
1704 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1705
1706 switch( extension_type )
1707 {
1708 case MBEDTLS_TLS_EXT_SIG_ALG:
1709 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001710 ( "found signature algorithms extension" ) );
1711 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001712 p + extension_data_len );
1713 if( ret != 0 )
1714 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001715 if( ! sig_alg_ext_found )
1716 sig_alg_ext_found = 1;
1717 else
1718 {
1719 MBEDTLS_SSL_DEBUG_MSG( 3,
1720 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001721 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001722 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001723 break;
1724
1725 default:
1726 MBEDTLS_SSL_DEBUG_MSG(
1727 3,
1728 ( "unknown extension found: %u ( ignoring )",
1729 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001730 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001731 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001732 p += extension_data_len;
1733 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001734 /* Check that we consumed all the message. */
1735 if( p != end )
1736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001738 ( "CertificateRequest misaligned" ) );
1739 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001740 }
1741 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001742 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001743 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001744 MBEDTLS_SSL_DEBUG_MSG( 3,
1745 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001746 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001747 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001748
Jerry Yu7840f812022-01-29 10:26:51 +08001749 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001750 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001751
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001752decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001753 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1754 MBEDTLS_ERR_SSL_DECODE_ERROR );
1755 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001756}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001757
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001758/*
1759 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1760 */
1761static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764
1765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1766
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001767 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1768
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001769 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1770 {
1771 unsigned char *buf;
1772 size_t buf_len;
1773
1774 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1775 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1776 &buf, &buf_len ) );
1777
1778 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1779 buf, buf + buf_len ) );
1780
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001781 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1782 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001783 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001784 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001785 {
1786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001787 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001788 }
1789 else
1790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001792 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1793 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001794 }
1795
1796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001797 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001798
Jerry Yud2674312021-10-29 10:08:19 +08001799 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1800
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001801cleanup:
1802
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1804 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001805}
1806
1807/*
Jerry Yu687101b2021-09-14 16:03:56 +08001808 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1809 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001810static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001811{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001812 int ret;
1813
1814 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001815 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001816 return( ret );
1817
Jerry Yu687101b2021-09-14 16:03:56 +08001818 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1819 return( 0 );
1820}
1821
1822/*
1823 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1824 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001825static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001826{
Jerry Yu30b071c2021-09-12 20:16:03 +08001827 int ret;
1828
1829 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1830 if( ret != 0 )
1831 return( ret );
1832
Jerry Yu687101b2021-09-14 16:03:56 +08001833 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1834 return( 0 );
1835}
Jerry Yua93ac112021-10-27 16:31:48 +08001836#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001837
Jerry Yu687101b2021-09-14 16:03:56 +08001838/*
1839 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1840 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001841static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001842{
XiaokangQianac0385c2021-11-03 06:40:11 +00001843 int ret;
1844
XiaokangQianc5c39d52021-11-09 11:55:10 +00001845 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001846 if( ret != 0 )
1847 return( ret );
1848
Ronald Cron49ad6192021-11-24 16:25:31 +01001849#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1850 mbedtls_ssl_handshake_set_state(
1851 ssl,
1852 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1853#else
Jerry Yuca133a32022-02-15 14:22:05 +08001854 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001855#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001856
XiaokangQianac0385c2021-11-03 06:40:11 +00001857 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001858}
1859
1860/*
Jerry Yu566c7812022-01-26 15:41:22 +08001861 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1862 */
1863static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1864{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001865 int non_empty_certificate_msg = 0;
1866
Jerry Yu5cc35062022-01-28 16:16:08 +08001867 MBEDTLS_SSL_DEBUG_MSG( 1,
1868 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001869 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001870
Ronald Cron9df7c802022-03-08 18:38:54 +01001871#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001872 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001873 {
1874 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1875 if( ret != 0 )
1876 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001877
Ronald Cron7a94aca2022-03-09 07:44:27 +01001878 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1879 non_empty_certificate_msg = 1;
1880 }
1881 else
1882 {
1883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1884 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001885#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001886
Ronald Cron7a94aca2022-03-09 07:44:27 +01001887 if( non_empty_certificate_msg )
1888 {
1889 mbedtls_ssl_handshake_set_state( ssl,
1890 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1891 }
1892 else
1893 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1894
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001895 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001896}
1897
Ronald Cron9df7c802022-03-08 18:38:54 +01001898#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001899/*
1900 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1901 */
1902static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1903{
Ronald Crona8b38872022-03-09 07:59:25 +01001904 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1905
1906 if( ret == 0 )
1907 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1908
1909 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001910}
Jerry Yu90f152d2022-01-29 22:12:42 +08001911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001912
1913/*
Jerry Yu687101b2021-09-14 16:03:56 +08001914 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1915 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001916static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001917{
XiaokangQian0fa66432021-11-15 03:33:57 +00001918 int ret;
1919
1920 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1921 if( ret != 0 )
1922 return( ret );
1923
1924 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1925 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001926}
1927
1928/*
1929 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1930 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001931static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001932{
Jerry Yu378254d2021-10-30 21:44:47 +08001933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001934 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001935 return( 0 );
1936}
1937
1938/*
1939 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1940 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001941static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001942{
Jerry Yu378254d2021-10-30 21:44:47 +08001943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1944 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1945
1946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1947 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1948
1949 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1950
1951 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1952 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001953}
1954
Jerry Yu92c6b402021-08-27 16:59:09 +08001955int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001956{
Jerry Yu92c6b402021-08-27 16:59:09 +08001957 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001958
Jerry Yu92c6b402021-08-27 16:59:09 +08001959 switch( ssl->state )
1960 {
1961 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001962 * ssl->state is initialized as HELLO_REQUEST. It is the same
1963 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001964 */
1965 case MBEDTLS_SSL_HELLO_REQUEST:
1966 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001967 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001968 break;
1969
1970 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001971 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001972 break;
1973
1974 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001975 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001976 break;
1977
Jerry Yua93ac112021-10-27 16:31:48 +08001978#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001979 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1980 ret = ssl_tls13_process_certificate_request( ssl );
1981 break;
1982
Jerry Yu687101b2021-09-14 16:03:56 +08001983 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001984 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001985 break;
1986
1987 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001988 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001989 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001990#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001991
1992 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001993 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001994 break;
1995
Jerry Yu566c7812022-01-26 15:41:22 +08001996 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1997 ret = ssl_tls13_write_client_certificate( ssl );
1998 break;
1999
Ronald Cron9df7c802022-03-08 18:38:54 +01002000#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002001 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2002 ret = ssl_tls13_write_client_certificate_verify( ssl );
2003 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002004#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002005
Jerry Yu687101b2021-09-14 16:03:56 +08002006 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002007 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002008 break;
2009
2010 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002011 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002012 break;
2013
2014 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002015 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002016 break;
2017
Ronald Cron49ad6192021-11-24 16:25:31 +01002018 /*
2019 * Injection of dummy-CCS's for middlebox compatibility
2020 */
2021#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002022 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002023 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2024 if( ret == 0 )
2025 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2026 break;
2027
2028 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2029 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2030 if( ret == 0 )
2031 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002032 break;
2033#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2034
Jerry Yu92c6b402021-08-27 16:59:09 +08002035 default:
2036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2038 }
2039
2040 return( ret );
2041}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002042
Jerry Yufb4b6472022-01-27 15:03:26 +08002043#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002044
Jerry Yufb4b6472022-01-27 15:03:26 +08002045