blob: 05c281a7a87d3a9a195aafa5a92913ecd6c500b4 [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#if defined(MBEDTLS_ECDH_C)
420
Jerry Yuc068b662021-10-11 22:30:19 +0800421static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
422 const unsigned char *buf,
423 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800424{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100425 uint8_t *p = (uint8_t*)buf;
426 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800427
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100428 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
429 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
430 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800431
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100432 /* Check if key size is consistent with given buffer length. */
433 if ( peerkey_len > ( buf_len - 2 ) )
434 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800435
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100436 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100437 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100438 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800439
440 return( 0 );
441}
Jerry Yu4a173382021-10-11 21:45:31 +0800442#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800443
XiaokangQiand59be772022-01-24 10:12:51 +0000444/*
445 * ssl_tls13_parse_hrr_key_share_ext()
446 * Parse key_share extension in Hello Retry Request
447 *
448 * struct {
449 * NamedGroup selected_group;
450 * } KeyShareHelloRetryRequest;
451 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000452static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000453 const unsigned char *buf,
454 const unsigned char *end )
455{
XiaokangQianb851da82022-01-14 04:03:11 +0000456 const mbedtls_ecp_curve_info *curve_info = NULL;
457 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000458 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000459 int found = 0;
460
461 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
462 if( group_list == NULL )
463 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
464
465 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
466
467 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000468 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000469 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000471
472 /* Upon receipt of this extension in a HelloRetryRequest, the client
473 * MUST first verify that the selected_group field corresponds to a
474 * group which was provided in the "supported_groups" extension in the
475 * original ClientHello.
476 * The supported_group was based on the info in ssl->conf->group_list.
477 *
478 * If the server provided a key share that was not sent in the ClientHello
479 * then the client MUST abort the handshake with an "illegal_parameter" alert.
480 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000481 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000482 {
483 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000484 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000485 continue;
486
487 /* We found a match */
488 found = 1;
489 break;
490 }
491
492 /* Client MUST verify that the selected_group field does not
493 * correspond to a group which was provided in the "key_share"
494 * extension in the original ClientHello. If the server sent an
495 * HRR message with a key share already provided in the
496 * ClientHello then the client MUST abort the handshake with
497 * an "illegal_parameter" alert.
498 */
XiaokangQiand59be772022-01-24 10:12:51 +0000499 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000500 {
501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
502 MBEDTLS_SSL_PEND_FATAL_ALERT(
503 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
504 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
505 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
506 }
507
508 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000509 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000510
511 return( 0 );
512}
513
Jerry Yue1b9c292021-09-10 10:08:31 +0800514/*
Jerry Yub85277e2021-10-13 13:36:05 +0800515 * ssl_tls13_parse_key_share_ext()
516 * Parse key_share extension in Server Hello
517 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800518 * struct {
519 * KeyShareEntry server_share;
520 * } KeyShareServerHello;
521 * struct {
522 * NamedGroup group;
523 * opaque key_exchange<1..2^16-1>;
524 * } KeyShareEntry;
525 */
Jerry Yu4a173382021-10-11 21:45:31 +0800526static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800527 const unsigned char *buf,
528 const unsigned char *end )
529{
Jerry Yub85277e2021-10-13 13:36:05 +0800530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800531 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800532 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800533
Jerry Yu4a173382021-10-11 21:45:31 +0800534 /* ...
535 * NamedGroup group; (2 bytes)
536 * ...
537 */
538 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
539 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800540 p += 2;
541
Jerry Yu4a173382021-10-11 21:45:31 +0800542 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800543 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800544 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800545 {
546 MBEDTLS_SSL_DEBUG_MSG( 1,
547 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800548 (unsigned) offered_group, (unsigned) group ) );
549 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
550 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
551 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800552 }
553
554#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800555 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800556 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100557 const mbedtls_ecp_curve_info *curve_info =
558 mbedtls_ecp_curve_info_from_tls_id( group );
559 if( curve_info == NULL )
560 {
561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
563 }
564
565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
566
Jerry Yuc068b662021-10-11 22:30:19 +0800567 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800568 if( ret != 0 )
569 return( ret );
570 }
Jerry Yub85277e2021-10-13 13:36:05 +0800571 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800572#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800573 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800574 {
575 /* Do something */
576 }
577 else
578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
579
580 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
581 return( ret );
582}
583
XiaokangQiand59be772022-01-24 10:12:51 +0000584/*
585 * ssl_tls13_parse_cookie_ext()
586 * Parse cookie extension in Hello Retry Request
587 *
588 * struct {
589 * opaque cookie<1..2^16-1>;
590 * } Cookie;
591 *
592 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
593 * extension to the client (this is an exception to the usual rule that
594 * the only extensions that may be sent are those that appear in the
595 * ClientHello). When sending the new ClientHello, the client MUST copy
596 * the contents of the extension received in the HelloRetryRequest into
597 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
598 * cookies in their initial ClientHello in subsequent connections.
599 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000600static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
601 const unsigned char *buf,
602 const unsigned char *end )
603{
XiaokangQian25c9c902022-02-08 10:49:53 +0000604 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000605 const unsigned char *p = buf;
606 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
607
608 /* Retrieve length field of cookie */
609 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
610 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
611 p += 2;
612
613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
614 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
615
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000616 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000617 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000618 handshake->cookie = mbedtls_calloc( 1, cookie_len );
619 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000620 {
621 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000622 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000623 cookie_len ) );
624 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
625 }
626
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000627 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000628 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000629
630 return( 0 );
631}
XiaokangQian43550bd2022-01-21 04:32:58 +0000632
XiaokangQian0b64eed2022-01-27 10:36:51 +0000633static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000634 unsigned char *buf,
635 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000636 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000637{
638 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000639 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000640 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000641
XiaokangQianc02768a2022-02-10 07:31:25 +0000642 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000643 {
644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
645 return( 0 );
646 }
647
648 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000649 handshake->cookie,
650 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000651
XiaokangQianc02768a2022-02-10 07:31:25 +0000652 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000653
654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
655
XiaokangQian0b64eed2022-01-27 10:36:51 +0000656 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000657 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
658 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000659 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000660
661 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000662 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000663
XiaokangQianc02768a2022-02-10 07:31:25 +0000664 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000665
666 return( 0 );
667}
668
Ronald Cron3d580bf2022-02-18 17:24:56 +0100669int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
670 unsigned char *buf,
671 unsigned char *end,
672 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100673{
674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
675 unsigned char *p = buf;
676 size_t ext_len;
677
678 *out_len = 0;
679
680 /* Write supported_versions extension
681 *
682 * Supported Versions Extension is mandatory with TLS 1.3.
683 */
684 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
685 if( ret != 0 )
686 return( ret );
687 p += ext_len;
688
689 /* Echo the cookie if the server provided one in its preceding
690 * HelloRetryRequest message.
691 */
692 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
693 if( ret != 0 )
694 return( ret );
695 p += ext_len;
696
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100697 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
698 {
699 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
700 if( ret != 0 )
701 return( ret );
702 p += ext_len;
703 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100704
705 *out_len = p - buf;
706
707 return( 0 );
708}
709
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800710/*
Jerry Yu4a173382021-10-11 21:45:31 +0800711 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800712 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200713
Ronald Cronda41b382022-03-30 09:57:11 +0200714/**
715 * \brief Detect if the ServerHello contains a supported_versions extension
716 * or not.
717 *
718 * \param[in] ssl SSL context
719 * \param[in] buf Buffer containing the ServerHello message
720 * \param[in] end End of the buffer containing the ServerHello message
721 *
722 * \return 0 if the ServerHello does not contain a supported_versions extension
723 * \return 1 if the ServerHello contains a supported_versions extension
724 * \return A negative value if an error occurred while parsing the ServerHello.
725 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100726static int ssl_tls13_is_supported_versions_ext_present(
727 mbedtls_ssl_context *ssl,
728 const unsigned char *buf,
729 const unsigned char *end )
730{
731 const unsigned char *p = buf;
732 size_t legacy_session_id_echo_len;
733 size_t extensions_len;
734 const unsigned char *extensions_end;
735
736 /*
737 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200738 * length:
739 * - legacy_version 2 bytes
740 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
741 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100742 */
743 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
744 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
745 legacy_session_id_echo_len = *p;
746
747 /*
748 * Jump to the extensions, jumping over:
749 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
750 * - cipher_suite 2 bytes
751 * - legacy_compression_method 1 byte
752 */
753 p += legacy_session_id_echo_len + 4;
754
755 /* Case of no extension */
756 if( p == end )
757 return( 0 );
758
759 /* ...
760 * Extension extensions<6..2^16-1>;
761 * ...
762 * struct {
763 * ExtensionType extension_type; (2 bytes)
764 * opaque extension_data<0..2^16-1>;
765 * } Extension;
766 */
767 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
768 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
769 p += 2;
770
771 /* Check extensions do not go beyond the buffer of data. */
772 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
773 extensions_end = p + extensions_len;
774
775 while( p < extensions_end )
776 {
777 unsigned int extension_type;
778 size_t extension_data_len;
779
780 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
781 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
782 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
783 p += 4;
784
785 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
786 return( 1 );
787
788 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
789 p += extension_data_len;
790 }
791
792 return( 0 );
793}
794
Jerry Yu7a186a02021-10-15 18:46:14 +0800795/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200796 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
797 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
798 * - 0 otherwise
799 */
800static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
801 const unsigned char *buf,
802 const unsigned char *end )
803{
804 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
805 static const unsigned char magic_downgrade_string[] =
806 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
807 const unsigned char *last_eight_bytes_of_random;
808 unsigned char last_byte_of_random;
809
810 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
811 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
812
813 if( memcmp( last_eight_bytes_of_random,
814 magic_downgrade_string,
815 sizeof( magic_downgrade_string ) ) == 0 )
816 {
817 last_byte_of_random = last_eight_bytes_of_random[7];
818 return( last_byte_of_random == 0 ||
819 last_byte_of_random == 1 );
820 }
821
822 return( 0 );
823}
824
825/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800826 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
827 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000828 * to indicate which message is expected and to be parsed next.
829 */
Jerry Yub85277e2021-10-13 13:36:05 +0800830#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
831#define SSL_SERVER_HELLO_COORDINATE_HRR 1
832static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
833 const unsigned char *buf,
834 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800835{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800836 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800837 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
838 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
839 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
840 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
841
842 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
843 *
Jerry Yu4a173382021-10-11 21:45:31 +0800844 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800845 * special value of the SHA-256 of "HelloRetryRequest".
846 *
847 * struct {
848 * ProtocolVersion legacy_version = 0x0303;
849 * Random random;
850 * opaque legacy_session_id_echo<0..32>;
851 * CipherSuite cipher_suite;
852 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800853 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800854 * } ServerHello;
855 *
856 */
Jerry Yub85277e2021-10-13 13:36:05 +0800857 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800858
859 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800860 {
Jerry Yub85277e2021-10-13 13:36:05 +0800861 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800862 }
863
Jerry Yub85277e2021-10-13 13:36:05 +0800864 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800865}
866
Jerry Yu745bb612021-10-13 22:01:04 +0800867/* Fetch and preprocess
868 * Returns a negative value on failure, and otherwise
869 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100870 * - SSL_SERVER_HELLO_COORDINATE_HRR or
871 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800872 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100873#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800874static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
875 unsigned char **buf,
876 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800877{
Jerry Yu4a173382021-10-11 21:45:31 +0800878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200879 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800880
XiaokangQian355e09a2022-01-20 11:14:50 +0000881 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
882 MBEDTLS_SSL_HS_SERVER_HELLO,
883 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200884 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800885
Ronald Cron9f0fba32022-02-10 16:45:15 +0100886 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200887 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100888 if( ret == 0 )
889 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200890 MBEDTLS_SSL_PROC_CHK_NEG(
891 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
892
893 /* If the server is negotiating TLS 1.2 or below and:
894 * . we did not propose TLS 1.2 or
895 * . the server responded it is TLS 1.3 capable but negotiating a lower
896 * version of the protocol and thus we are under downgrade attack
897 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100898 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200899 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100900 {
901 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
902 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
903 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
904 }
905
906 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400907 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100908 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
909 *buf, *buf_len );
910
911 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
912 {
913 ret = ssl_tls13_reset_key_share( ssl );
914 if( ret != 0 )
915 return( ret );
916 }
917
918 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
919 }
920
Ronald Cronfd6193c2022-04-05 11:04:20 +0200921 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800922 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800923 {
Jerry Yu745bb612021-10-13 22:01:04 +0800924 case SSL_SERVER_HELLO_COORDINATE_HELLO:
925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
926 break;
927 case SSL_SERVER_HELLO_COORDINATE_HRR:
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000929 /* If a client receives a second
930 * HelloRetryRequest in the same connection (i.e., where the ClientHello
931 * was itself in response to a HelloRetryRequest), it MUST abort the
932 * handshake with an "unexpected_message" alert.
933 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000934 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000935 {
936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
937 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
938 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
939 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
940 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000941 /*
942 * Clients must abort the handshake with an "illegal_parameter"
943 * alert if the HelloRetryRequest would not result in any change
944 * in the ClientHello.
945 * In a PSK only key exchange that what we expect.
946 */
947 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
948 {
949 MBEDTLS_SSL_DEBUG_MSG( 1,
950 ( "Unexpected HRR in pure PSK key exchange." ) );
951 MBEDTLS_SSL_PEND_FATAL_ALERT(
952 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
953 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
954 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
955 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000956
XiaokangQiand9e068e2022-01-18 06:23:32 +0000957 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000958
Jerry Yu745bb612021-10-13 22:01:04 +0800959 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800960 }
961
962cleanup:
963
964 return( ret );
965}
966
Jerry Yu4a173382021-10-11 21:45:31 +0800967static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
968 const unsigned char **buf,
969 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800970{
971 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800972 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973
Jerry Yude4fb2c2021-09-19 18:05:08 +0800974 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800975 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800976
Jerry Yu4a173382021-10-11 21:45:31 +0800977 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800978
979 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800980 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
981 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800982 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800983 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
984 ssl->session_negotiate->id,
985 ssl->session_negotiate->id_len );
986 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800987 legacy_session_id_echo_len );
988
989 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
990 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
991
Jerry Yue1b9c292021-09-10 10:08:31 +0800992 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
993 }
994
Jerry Yu4a173382021-10-11 21:45:31 +0800995 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 *buf = p;
997
998 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800999 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001000 return( 0 );
1001}
1002
Jerry Yu4a173382021-10-11 21:45:31 +08001003static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1004 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001005{
Jerry Yu4a173382021-10-11 21:45:31 +08001006 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1007
Jerry Yue1b9c292021-09-10 10:08:31 +08001008 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001009 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001010 {
Jerry Yu4a173382021-10-11 21:45:31 +08001011 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001012 {
1013 return( 1 );
1014 }
1015 }
1016 return( 0 );
1017}
1018
1019/* Parse ServerHello message and configure context
1020 *
1021 * struct {
1022 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1023 * Random random;
1024 * opaque legacy_session_id_echo<0..32>;
1025 * CipherSuite cipher_suite;
1026 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001027 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 * } ServerHello;
1029 */
Jerry Yuc068b662021-10-11 22:30:19 +08001030static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1031 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001032 const unsigned char *end,
1033 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001034{
Jerry Yub85277e2021-10-13 13:36:05 +08001035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001036 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001037 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001038 size_t extensions_len;
1039 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001041 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001042 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001043
1044 /*
1045 * Check there is space for minimal fields
1046 *
1047 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001048 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001049 * - legacy_session_id_echo ( 1 byte ), minimum size
1050 * - cipher_suite ( 2 bytes)
1051 * - legacy_compression_method ( 1 byte )
1052 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001053 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001054
1055 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001056 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1057
Jerry Yu4a173382021-10-11 21:45:31 +08001058 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001059 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001060 * ...
1061 * with ProtocolVersion defined as:
1062 * uint16 ProtocolVersion;
1063 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001064 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1065 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001066 {
1067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1068 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1069 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001070 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1071 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 }
1073 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001074
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001075 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001076 * Random random;
1077 * ...
1078 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001079 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001080 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001081 if( !is_hrr )
1082 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001083 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001084 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1085 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1086 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1087 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001088 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001089
Jerry Yu4a173382021-10-11 21:45:31 +08001090 /* ...
1091 * opaque legacy_session_id_echo<0..32>;
1092 * ...
1093 */
1094 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 {
XiaokangQian52da5582022-01-26 09:49:29 +00001096 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001097 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001098 }
1099
Jerry Yu4a173382021-10-11 21:45:31 +08001100 /* ...
1101 * CipherSuite cipher_suite;
1102 * ...
1103 * with CipherSuite defined as:
1104 * uint8 CipherSuite[2];
1105 */
1106 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1108 p += 2;
1109
Jerry Yu4a173382021-10-11 21:45:31 +08001110
XiaokangQian355e09a2022-01-20 11:14:50 +00001111 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001112 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001113 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001114 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001115 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1116 ssl->tls_version,
1117 ssl->tls_version ) != 0 ) ||
Ronald Cronba120bb2022-03-30 22:09:48 +02001118 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 {
XiaokangQian52da5582022-01-26 09:49:29 +00001120 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001122 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001123 * If we received an HRR before and that the proposed selected
1124 * ciphersuite in this server hello is not the same as the one
1125 * proposed in the HRR, we abort the handshake and send an
1126 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001127 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001128 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1129 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001130 {
XiaokangQian52da5582022-01-26 09:49:29 +00001131 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001132 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001133
XiaokangQian52da5582022-01-26 09:49:29 +00001134 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001135 {
1136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1137 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001138 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001139 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001140
Jerry Yu4a173382021-10-11 21:45:31 +08001141 /* Configure ciphersuites */
1142 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1143
XiaokangQian355e09a2022-01-20 11:14:50 +00001144 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001145 ssl->session_negotiate->ciphersuite = cipher_suite;
1146
Jerry Yue1b9c292021-09-10 10:08:31 +08001147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1148 cipher_suite, ciphersuite_info->name ) );
1149
1150#if defined(MBEDTLS_HAVE_TIME)
1151 ssl->session_negotiate->start = time( NULL );
1152#endif /* MBEDTLS_HAVE_TIME */
1153
Jerry Yu4a173382021-10-11 21:45:31 +08001154 /* ...
1155 * uint8 legacy_compression_method = 0;
1156 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001157 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001158 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001159 if( p[0] != 0 )
1160 {
Jerry Yub85277e2021-10-13 13:36:05 +08001161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001162 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001163 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 }
1165 p++;
1166
Jerry Yub85277e2021-10-13 13:36:05 +08001167 /* ...
1168 * Extension extensions<6..2^16-1>;
1169 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001170 * struct {
1171 * ExtensionType extension_type; (2 bytes)
1172 * opaque extension_data<0..2^16-1>;
1173 * } Extension;
1174 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001175 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001176 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001177 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001178
Jerry Yu4a173382021-10-11 21:45:31 +08001179 /* Check extensions do not go beyond the buffer of data. */
1180 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1181 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001182
Jerry Yu4a173382021-10-11 21:45:31 +08001183 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184
Jerry Yu4a173382021-10-11 21:45:31 +08001185 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001186 {
1187 unsigned int extension_type;
1188 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001189 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001190
Jerry Yu4a173382021-10-11 21:45:31 +08001191 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001192 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1193 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1194 p += 4;
1195
Jerry Yu4a173382021-10-11 21:45:31 +08001196 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001197 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001198
1199 switch( extension_type )
1200 {
XiaokangQianb851da82022-01-14 04:03:11 +00001201 case MBEDTLS_TLS_EXT_COOKIE:
1202
XiaokangQiand9e068e2022-01-18 06:23:32 +00001203 if( !is_hrr )
1204 {
XiaokangQian52da5582022-01-26 09:49:29 +00001205 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001206 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001207 }
1208
XiaokangQian43550bd2022-01-21 04:32:58 +00001209 ret = ssl_tls13_parse_cookie_ext( ssl,
1210 p, extension_data_end );
1211 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001212 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001213 MBEDTLS_SSL_DEBUG_RET( 1,
1214 "ssl_tls13_parse_cookie_ext",
1215 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001216 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001217 }
XiaokangQianb851da82022-01-14 04:03:11 +00001218 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001219
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001221 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001222 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001223 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001225 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001226 break;
1227
1228 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001231
XiaokangQian52da5582022-01-26 09:49:29 +00001232 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001233 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001234
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 case MBEDTLS_TLS_EXT_KEY_SHARE:
1236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001237 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1238 {
XiaokangQian52da5582022-01-26 09:49:29 +00001239 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001240 goto cleanup;
1241 }
1242
XiaokangQian53f20b72022-01-18 10:47:33 +00001243 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001244 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001245 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001246 else
XiaokangQianb851da82022-01-14 04:03:11 +00001247 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001248 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001249 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001250 {
1251 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001252 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001254 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001255 }
1256 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001257
1258 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001259 MBEDTLS_SSL_DEBUG_MSG(
1260 3,
1261 ( "unknown extension found: %u ( ignoring )",
1262 extension_type ) );
1263
XiaokangQian52da5582022-01-26 09:49:29 +00001264 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001265 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001266 }
1267
1268 p += extension_data_len;
1269 }
1270
XiaokangQiand59be772022-01-24 10:12:51 +00001271cleanup:
1272
XiaokangQian52da5582022-01-26 09:49:29 +00001273 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001274 {
1275 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1276 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1277 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1278 }
XiaokangQian52da5582022-01-26 09:49:29 +00001279 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001280 {
1281 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1282 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1283 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1284 }
1285 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001286}
1287
XiaokangQian355e09a2022-01-20 11:14:50 +00001288static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001289{
Jerry Yub85277e2021-10-13 13:36:05 +08001290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001291 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001292 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001293 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001294
Jerry Yub85277e2021-10-13 13:36:05 +08001295 /* Determine the key exchange mode:
1296 * 1) If both the pre_shared_key and key_share extensions were received
1297 * then the key exchange mode is PSK with EPHEMERAL.
1298 * 2) If only the pre_shared_key extension was received then the key
1299 * exchange mode is PSK-only.
1300 * 3) If only the key_share extension was received then the key
1301 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001302 */
Jerry Yub85277e2021-10-13 13:36:05 +08001303 switch( handshake->extensions_present &
1304 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001305 {
Jerry Yu745bb612021-10-13 22:01:04 +08001306 /* Only the pre_shared_key extension was received */
1307 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001308 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001309 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001310
Jerry Yu745bb612021-10-13 22:01:04 +08001311 /* Only the key_share extension was received */
1312 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001313 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001314 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001315
Jerry Yu745bb612021-10-13 22:01:04 +08001316 /* Both the pre_shared_key and key_share extensions were received */
1317 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001318 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001319 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001320
Jerry Yu745bb612021-10-13 22:01:04 +08001321 /* Neither pre_shared_key nor key_share extension was received */
1322 default:
1323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1324 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1325 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001326 }
1327
1328 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1329 *
1330 * TODO: We don't have to do this in case we offered 0-RTT and the
1331 * server accepted it. In this case, we could skip generating
1332 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001333 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001334 if( ret != 0 )
1335 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001337 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001338 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001339 }
1340
1341 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001342 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001343 if( ret != 0 )
1344 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001346 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001347 }
1348
1349 /* Next evolution in key schedule: Establish handshake secret and
1350 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001351 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001352 if( ret != 0 )
1353 {
Jerry Yuc068b662021-10-11 22:30:19 +08001354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1355 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001356 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001357 }
1358
Jerry Yub85277e2021-10-13 13:36:05 +08001359 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001360 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001361 {
1362 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1363 goto cleanup;
1364 }
Jerry Yu0b177842021-09-05 19:41:30 +08001365
1366 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1367 ssl->conf->endpoint,
1368 ssl->session_negotiate->ciphersuite,
1369 &traffic_keys,
1370 ssl );
1371 if( ret != 0 )
1372 {
1373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001374 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001375 }
1376
Jerry Yub85277e2021-10-13 13:36:05 +08001377 handshake->transform_handshake = transform_handshake;
1378 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001379
1380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1381 ssl->session_in = ssl->session_negotiate;
1382
1383 /*
1384 * State machine update
1385 */
1386 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1387
Jerry Yu4a173382021-10-11 21:45:31 +08001388cleanup:
1389
Jerry Yu0b177842021-09-05 19:41:30 +08001390 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001391 if( ret != 0 )
1392 {
Jerry Yub85277e2021-10-13 13:36:05 +08001393 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001394
Jerry Yu4a173382021-10-11 21:45:31 +08001395 MBEDTLS_SSL_PEND_FATAL_ALERT(
1396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1397 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1398 }
1399 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001400}
1401
XiaokangQian355e09a2022-01-20 11:14:50 +00001402static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001403{
1404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1405
XiaokangQian647719a2021-12-07 09:16:29 +00001406#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1407 /* If not offering early data, the client sends a dummy CCS record
1408 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001409 * its second ClientHello or before its encrypted handshake flight.
1410 */
XiaokangQian647719a2021-12-07 09:16:29 +00001411 mbedtls_ssl_handshake_set_state( ssl,
1412 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1413#else
1414 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1415#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1416
XiaokangQian78b1fa72022-01-19 06:56:30 +00001417 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001418
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001420 * We are going to re-generate a shared secret corresponding to the group
1421 * selected by the server, which is different from the group for which we
1422 * generated a shared secret in the first client hello.
1423 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001424 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001425 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001426 if( ret != 0 )
1427 return( ret );
1428
1429 return( 0 );
1430}
1431
Jerry Yue1b9c292021-09-10 10:08:31 +08001432/*
Jerry Yu4a173382021-10-11 21:45:31 +08001433 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001434 * Handler for MBEDTLS_SSL_SERVER_HELLO
1435 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001436static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001437{
Jerry Yu4a173382021-10-11 21:45:31 +08001438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001439 unsigned char *buf = NULL;
1440 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001441 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001442
1443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1444
1445 /* Coordination step
1446 * - Fetch record
1447 * - Make sure it's either a ServerHello or a HRR.
1448 * - Switch processing routine in case of HRR
1449 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001450 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1451
XiaokangQian16acd4b2022-01-14 07:35:47 +00001452 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001453 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001454 goto cleanup;
1455 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001456 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001457
Ronald Cron9f0fba32022-02-10 16:45:15 +01001458 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1459 {
1460 ret = 0;
1461 goto cleanup;
1462 }
1463
XiaokangQianb851da82022-01-14 04:03:11 +00001464 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001465 buf + buf_len,
1466 is_hrr ) );
1467 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001468 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1469
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001470 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1471 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001472
XiaokangQiand9e068e2022-01-18 06:23:32 +00001473 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001474 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001475 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001476 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001477
1478cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1480 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001481 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001482}
1483
1484/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001485 *
1486 * EncryptedExtensions message
1487 *
1488 * The EncryptedExtensions message contains any extensions which
1489 * should be protected, i.e., any which are not needed to establish
1490 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001491 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492
1493/*
1494 * Overview
1495 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001496
1497/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001498static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001499
XiaokangQian97799ac2021-10-11 10:05:54 +00001500static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1501 const unsigned char *buf,
1502 const unsigned char *end );
1503static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001504
1505/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001506 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001507 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001508static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001509{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001510 int ret;
1511 unsigned char *buf;
1512 size_t buf_len;
1513
1514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1515
Xiaofei Bai746f9482021-11-12 08:53:56 +00001516 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001517 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001518 &buf, &buf_len ) );
1519
1520 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001521 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001522 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001524 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1525 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001526
XiaokangQian97799ac2021-10-11 10:05:54 +00001527 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
1529cleanup:
1530
1531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1532 return( ret );
1533
1534}
1535
XiaokangQian08da26c2021-10-09 10:12:11 +00001536/* Parse EncryptedExtensions message
1537 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001538 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001539 * } EncryptedExtensions;
1540 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001541static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1542 const unsigned char *buf,
1543 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001544{
1545 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001546 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001547 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001548 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549
XiaokangQian08da26c2021-10-09 10:12:11 +00001550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001551 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001552 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001553
XiaokangQian97799ac2021-10-11 10:05:54 +00001554 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001555 extensions_end = p + extensions_len;
1556 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001557
XiaokangQian8db25ff2021-10-13 05:56:18 +00001558 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001559 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001560 unsigned int extension_type;
1561 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001562
XiaokangQian08da26c2021-10-09 10:12:11 +00001563 /*
1564 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001565 * ExtensionType extension_type; (2 bytes)
1566 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001567 * } Extension;
1568 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001569 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001570 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1571 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1572 p += 4;
1573
XiaokangQian8db25ff2021-10-13 05:56:18 +00001574 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001575
XiaokangQian97799ac2021-10-11 10:05:54 +00001576 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001577 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001578 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001579 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001580 switch( extension_type )
1581 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001582
XiaokangQian08da26c2021-10-09 10:12:11 +00001583 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1585 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001586
lhuang0486cacac2022-01-21 07:34:27 -08001587#if defined(MBEDTLS_SSL_ALPN)
1588 case MBEDTLS_TLS_EXT_ALPN:
1589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1590
1591 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1592 {
1593 return( ret );
1594 }
1595
1596 break;
1597#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001598 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001599 MBEDTLS_SSL_DEBUG_MSG(
1600 3, ( "unsupported extension found: %u ", extension_type) );
1601 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001602 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001603 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1604 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001605 }
1606
XiaokangQian08da26c2021-10-09 10:12:11 +00001607 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001608 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001609
XiaokangQian97799ac2021-10-11 10:05:54 +00001610 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001611 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001612 {
1613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001614 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001615 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001616 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001617 }
1618
1619 return( ret );
1620}
1621
XiaokangQian97799ac2021-10-11 10:05:54 +00001622static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001623{
Jerry Yua93ac112021-10-27 16:31:48 +08001624#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001625 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001626 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1627 else
Jerry Yud2674312021-10-29 10:08:19 +08001628 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001629#else
1630 ((void) ssl);
1631 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1632#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001633 return( 0 );
1634}
1635
Jerry Yua93ac112021-10-27 16:31:48 +08001636#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001637/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001638 *
1639 * STATE HANDLING: CertificateRequest
1640 *
Jerry Yud2674312021-10-29 10:08:19 +08001641 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001642#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1643#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001644/* Coordination:
1645 * Deals with the ambiguity of not knowing if a CertificateRequest
1646 * will be sent. Returns a negative code on failure, or
1647 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1648 * - SSL_CERTIFICATE_REQUEST_SKIP
1649 * indicating if a Certificate Request is expected or not.
1650 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001651static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001652{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001654
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001655 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001656 {
1657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1658 return( SSL_CERTIFICATE_REQUEST_SKIP );
1659 }
1660
1661 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001662 {
1663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1664 return( ret );
1665 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001667
1668 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1669 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1670 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001671 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001672 }
1673
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 return( SSL_CERTIFICATE_REQUEST_SKIP );
1675}
1676
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677/*
1678 * ssl_tls13_parse_certificate_request()
1679 * Parse certificate request
1680 * struct {
1681 * opaque certificate_request_context<0..2^8-1>;
1682 * Extension extensions<2..2^16-1>;
1683 * } CertificateRequest;
1684 */
1685static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1686 const unsigned char *buf,
1687 const unsigned char *end )
1688{
1689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1690 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001692 size_t extensions_len = 0;
1693 const unsigned char *extensions_end;
1694 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001696 /* ...
1697 * opaque certificate_request_context<0..2^8-1>
1698 * ...
1699 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001700 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1701 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001702 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001703
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001704 if( certificate_request_context_len > 0 )
1705 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001707 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1708 p, certificate_request_context_len );
1709
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001710 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001711 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001712 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001713 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001714 {
1715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1716 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1717 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001718 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001719 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001720 p += certificate_request_context_len;
1721 }
1722
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001723 /* ...
1724 * Extension extensions<2..2^16-1>;
1725 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 */
1727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1728 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1729 p += 2;
1730
1731 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1732 extensions_end = p + extensions_len;
1733
1734 while( p < extensions_end )
1735 {
1736 unsigned int extension_type;
1737 size_t extension_data_len;
1738
1739 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1740 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1741 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1742 p += 4;
1743
1744 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1745
1746 switch( extension_type )
1747 {
1748 case MBEDTLS_TLS_EXT_SIG_ALG:
1749 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001750 ( "found signature algorithms extension" ) );
1751 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001752 p + extension_data_len );
1753 if( ret != 0 )
1754 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001755 if( ! sig_alg_ext_found )
1756 sig_alg_ext_found = 1;
1757 else
1758 {
1759 MBEDTLS_SSL_DEBUG_MSG( 3,
1760 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001761 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001762 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001763 break;
1764
1765 default:
1766 MBEDTLS_SSL_DEBUG_MSG(
1767 3,
1768 ( "unknown extension found: %u ( ignoring )",
1769 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001770 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001771 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001772 p += extension_data_len;
1773 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001774 /* Check that we consumed all the message. */
1775 if( p != end )
1776 {
1777 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001778 ( "CertificateRequest misaligned" ) );
1779 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001780 }
1781 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001782 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001783 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001784 MBEDTLS_SSL_DEBUG_MSG( 3,
1785 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001786 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001787 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001788
Jerry Yu7840f812022-01-29 10:26:51 +08001789 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001790 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001791
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001792decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001793 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1794 MBEDTLS_ERR_SSL_DECODE_ERROR );
1795 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001796}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001798/*
1799 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1800 */
1801static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001802{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001804
1805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1806
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001807 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1808
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1810 {
1811 unsigned char *buf;
1812 size_t buf_len;
1813
1814 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1815 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1816 &buf, &buf_len ) );
1817
1818 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1819 buf, buf + buf_len ) );
1820
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001821 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1822 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001823 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001824 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001825 {
1826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001827 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001828 }
1829 else
1830 {
1831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001832 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1833 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001834 }
1835
1836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001837 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001838
Jerry Yud2674312021-10-29 10:08:19 +08001839 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1840
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001841cleanup:
1842
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1844 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001845}
1846
1847/*
Jerry Yu687101b2021-09-14 16:03:56 +08001848 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1849 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001850static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001851{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001852 int ret;
1853
1854 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001855 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001856 return( ret );
1857
Jerry Yu687101b2021-09-14 16:03:56 +08001858 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1859 return( 0 );
1860}
1861
1862/*
1863 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1864 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001865static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001866{
Jerry Yu30b071c2021-09-12 20:16:03 +08001867 int ret;
1868
1869 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1870 if( ret != 0 )
1871 return( ret );
1872
Jerry Yu687101b2021-09-14 16:03:56 +08001873 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1874 return( 0 );
1875}
Jerry Yua93ac112021-10-27 16:31:48 +08001876#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001877
Jerry Yu687101b2021-09-14 16:03:56 +08001878/*
1879 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1880 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001881static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001882{
XiaokangQianac0385c2021-11-03 06:40:11 +00001883 int ret;
1884
XiaokangQianc5c39d52021-11-09 11:55:10 +00001885 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001886 if( ret != 0 )
1887 return( ret );
1888
Ronald Cron49ad6192021-11-24 16:25:31 +01001889#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1890 mbedtls_ssl_handshake_set_state(
1891 ssl,
1892 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1893#else
Jerry Yuca133a32022-02-15 14:22:05 +08001894 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001895#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001896
XiaokangQianac0385c2021-11-03 06:40:11 +00001897 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001898}
1899
1900/*
Jerry Yu566c7812022-01-26 15:41:22 +08001901 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1902 */
1903static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1904{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001905 int non_empty_certificate_msg = 0;
1906
Jerry Yu5cc35062022-01-28 16:16:08 +08001907 MBEDTLS_SSL_DEBUG_MSG( 1,
1908 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001909 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001910
Ronald Cron9df7c802022-03-08 18:38:54 +01001911#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001912 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001913 {
1914 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1915 if( ret != 0 )
1916 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001917
Ronald Cron7a94aca2022-03-09 07:44:27 +01001918 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1919 non_empty_certificate_msg = 1;
1920 }
1921 else
1922 {
1923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1924 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001925#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001926
Ronald Cron7a94aca2022-03-09 07:44:27 +01001927 if( non_empty_certificate_msg )
1928 {
1929 mbedtls_ssl_handshake_set_state( ssl,
1930 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1931 }
1932 else
1933 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1934
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001935 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001936}
1937
Ronald Cron9df7c802022-03-08 18:38:54 +01001938#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001939/*
1940 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1941 */
1942static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1943{
Ronald Crona8b38872022-03-09 07:59:25 +01001944 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1945
1946 if( ret == 0 )
1947 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1948
1949 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001950}
Jerry Yu90f152d2022-01-29 22:12:42 +08001951#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001952
1953/*
Jerry Yu687101b2021-09-14 16:03:56 +08001954 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1955 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001956static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001957{
XiaokangQian0fa66432021-11-15 03:33:57 +00001958 int ret;
1959
1960 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1961 if( ret != 0 )
1962 return( ret );
1963
1964 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1965 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001966}
1967
1968/*
1969 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1970 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001971static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001972{
Jerry Yu378254d2021-10-30 21:44:47 +08001973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001974 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001975 return( 0 );
1976}
1977
1978/*
1979 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1980 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001981static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001982{
Jerry Yu378254d2021-10-30 21:44:47 +08001983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1984 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1985
1986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1987 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1988
1989 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1990
1991 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1992 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001993}
1994
Jerry Yu92c6b402021-08-27 16:59:09 +08001995int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001996{
Jerry Yu92c6b402021-08-27 16:59:09 +08001997 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001998
Jerry Yu92c6b402021-08-27 16:59:09 +08001999 switch( ssl->state )
2000 {
2001 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002002 * ssl->state is initialized as HELLO_REQUEST. It is the same
2003 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002004 */
2005 case MBEDTLS_SSL_HELLO_REQUEST:
2006 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002007 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002008 break;
2009
2010 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002011 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002012 break;
2013
2014 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002015 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002016 break;
2017
Jerry Yua93ac112021-10-27 16:31:48 +08002018#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002019 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2020 ret = ssl_tls13_process_certificate_request( ssl );
2021 break;
2022
Jerry Yu687101b2021-09-14 16:03:56 +08002023 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002024 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002025 break;
2026
2027 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002028 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002029 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002030#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002031
2032 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002033 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002034 break;
2035
Jerry Yu566c7812022-01-26 15:41:22 +08002036 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2037 ret = ssl_tls13_write_client_certificate( ssl );
2038 break;
2039
Ronald Cron9df7c802022-03-08 18:38:54 +01002040#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002041 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2042 ret = ssl_tls13_write_client_certificate_verify( ssl );
2043 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002044#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002045
Jerry Yu687101b2021-09-14 16:03:56 +08002046 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002047 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002048 break;
2049
2050 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002051 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002052 break;
2053
2054 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002055 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002056 break;
2057
Ronald Cron49ad6192021-11-24 16:25:31 +01002058 /*
2059 * Injection of dummy-CCS's for middlebox compatibility
2060 */
2061#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002062 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002063 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2064 if( ret == 0 )
2065 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2066 break;
2067
2068 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2069 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2070 if( ret == 0 )
2071 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002072 break;
2073#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2074
Jerry Yu92c6b402021-08-27 16:59:09 +08002075 default:
2076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078 }
2079
2080 return( ret );
2081}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002082
Jerry Yufb4b6472022-01-27 15:03:26 +08002083#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002084
Jerry Yufb4b6472022-01-27 15:03:26 +08002085