blob: 97cf61bbb956b7b616bcb30ab53d7ef0d35ba633 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
Ronald Crondbe87f02022-02-10 14:35:27 +010052 unsigned char versions_len = ( ssl->handshake->min_minor_ver <=
53 MBEDTLS_SSL_MINOR_VERSION_3 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crondbe87f02022-02-10 14:35:27 +010063 * - versions (2 to 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Jerry Yu92c6b402021-08-27 16:59:09 +080065
Ronald Crondbe87f02022-02-10 14:35:27 +010066 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 9 );
67
Jerry Yueecfbf02021-08-30 18:32:07 +080068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010069 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010073 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080074
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080076 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010077 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080078 */
Ronald Crondbe87f02022-02-10 14:35:27 +010079 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
80 MBEDTLS_SSL_MINOR_VERSION_4,
81 MBEDTLS_SSL_TRANSPORT_STREAM, p );
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080083
Jerry Yu92c6b402021-08-27 16:59:09 +080084
Ronald Crondbe87f02022-02-10 14:35:27 +010085 if( ssl->handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_3 )
86 {
87 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
88 MBEDTLS_SSL_MINOR_VERSION_3,
89 MBEDTLS_SSL_TRANSPORT_STREAM, p + 2 );
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
91 }
92
93 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080094
95 return( 0 );
96}
Jerry Yubc20bdd2021-08-24 15:59:48 +080097
Jerry Yuc068b662021-10-11 22:30:19 +080098static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
99 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +0800100 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800101{
Jerry Yue1b9c292021-09-10 10:08:31 +0800102 ((void) ssl);
103
Jerry Yub85277e2021-10-13 13:36:05 +0800104 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
105 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800106 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
107 {
108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800109
110 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
111 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
112 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800113 }
114
115 return( 0 );
116}
117
lhuang0486cacac2022-01-21 07:34:27 -0800118#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800119static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
120 const unsigned char *buf, size_t len )
121{
122 size_t list_len, name_len;
123 const unsigned char *p = buf;
124 const unsigned char *end = buf + len;
125
126 /* If we didn't send it, the server shouldn't send it */
127 if( ssl->conf->alpn_list == NULL )
128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
129
130 /*
131 * opaque ProtocolName<1..2^8-1>;
132 *
133 * struct {
134 * ProtocolName protocol_name_list<2..2^16-1>
135 * } ProtocolNameList;
136 *
137 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
138 */
139
140 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
141 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
142
143 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
144 p += 2;
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
146
147 name_len = *p++;
148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
149
150 /* Check that the server chosen protocol was in our list and save it */
151 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
152 {
153 if( name_len == strlen( *alpn ) &&
154 memcmp( buf + 3, *alpn, name_len ) == 0 )
155 {
156 ssl->alpn_chosen = *alpn;
157 return( 0 );
158 }
159 }
160
161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
162}
163#endif /* MBEDTLS_SSL_ALPN */
164
XiaokangQian16acd4b2022-01-14 07:35:47 +0000165static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000166{
167 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100168
XiaokangQian647719a2021-12-07 09:16:29 +0000169 if( group_id == 0 )
170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
171
XiaokangQian355e09a2022-01-20 11:14:50 +0000172#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000174 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
177
178 /* Destroy generated private key. */
179 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
180 if( status != PSA_SUCCESS )
181 {
182 ret = psa_ssl_status_to_mbedtls( status );
183 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
184 return( ret );
185 }
186
187 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000188 return( 0 );
189 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000190 else
191#endif /* MBEDTLS_ECDH_C */
192 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000193 {
194 /* Do something */
195 }
196
197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
198}
199
200/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800201 * Functions for writing key_share extension.
202 */
203#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800204static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800205 mbedtls_ssl_context *ssl,
206 uint16_t named_group,
207 unsigned char *buf,
208 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000209 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800210{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100211 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
212 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
213 psa_key_attributes_t key_attributes;
214 size_t own_pubkey_len;
215 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
216 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800217
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800219
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100220 /* Convert EC group to PSA key type. */
221 if( ( handshake->ecdh_psa_type =
222 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
223 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 if( ecdh_bits > 0xffff )
226 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
227 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100229 key_attributes = psa_key_attributes_init();
230 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
231 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
232 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
233 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100234
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100235 /* Generate ECDH private key. */
236 status = psa_generate_key( &key_attributes,
237 &handshake->ecdh_psa_privkey );
238 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100240 ret = psa_ssl_status_to_mbedtls( status );
241 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100243
Jerry Yu56fc07f2021-09-01 17:48:49 +0800244 }
245
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100246 /* Export the public part of the ECDH private key from PSA. */
247 status = psa_export_public_key( handshake->ecdh_psa_privkey,
248 buf, (size_t)( end - buf ),
249 &own_pubkey_len );
250 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800251 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100252 ret = psa_ssl_status_to_mbedtls( status );
253 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100255
Jerry Yu56fc07f2021-09-01 17:48:49 +0800256 }
257
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100258 if( own_pubkey_len > (size_t)( end - buf ) )
259 {
260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
261 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
262 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100263
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100264 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100265
Jerry Yu75336352021-09-01 15:59:36 +0800266 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800267}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268#endif /* MBEDTLS_ECDH_C */
269
Jerry Yub60e3cf2021-09-08 16:41:02 +0800270static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
271 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272{
273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100277 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800278 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100279 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800280 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
281
Brett Warren14efd332021-10-06 09:32:11 +0100282 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000284 const mbedtls_ecp_curve_info *curve_info;
285 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
286 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100287 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800288 {
Brett Warren14efd332021-10-06 09:32:11 +0100289 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800290 return( 0 );
291 }
292 }
293#else
294 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800295 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800296#endif /* MBEDTLS_ECDH_C */
297
298 /*
299 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800301 */
302
303 return( ret );
304}
305
306/*
307 * ssl_tls13_write_key_share_ext
308 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800309 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 *
311 * struct {
312 * NamedGroup group;
313 * opaque key_exchange<1..2^16-1>;
314 * } KeyShareEntry;
315 * struct {
316 * KeyShareEntry client_shares<0..2^16-1>;
317 * } KeyShareClientHello;
318 */
319static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
320 unsigned char *buf,
321 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000322 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323{
324 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000325 unsigned char *client_shares; /* Start of client_shares */
326 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
329
Xiaofei Baid25fab62021-12-02 06:36:27 +0000330 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800331
Jerry Yub60e3cf2021-09-08 16:41:02 +0800332 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800333 * - extension_type (2 bytes)
334 * - extension_data_length (2 bytes)
335 * - client_shares_length (2 bytes)
336 */
337 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
338 p += 6;
339
340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
341
342 /* HRR could already have requested something else. */
343 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
345 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800347 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348 &group_id ) );
349 }
350
351 /*
352 * Dispatch to type-specific key generation function.
353 *
354 * So far, we're only supporting ECDHE. With the introduction
355 * of PQC KEMs, we'll want to have multiple branches, one per
356 * type of KEM, and dispatch to the corresponding crypto. And
357 * only one key share entry is allowed.
358 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000359 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800360#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800361 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800363 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000364 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800365 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100366 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800367
368 /* Check there is space for header of KeyShareEntry
369 * - group (2 bytes)
370 * - key_exchange_length (2 bytes)
371 */
372 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
373 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100374 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800375 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800376 p += key_exchange_len;
377 if( ret != 0 )
378 return( ret );
379
380 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000381 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800382 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000383 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800384 }
385 else
386#endif /* MBEDTLS_ECDH_C */
387 if( 0 /* other KEMs? */ )
388 {
389 /* Do something */
390 }
391 else
392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
393
Jerry Yub60e3cf2021-09-08 16:41:02 +0800394 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000395 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800396 if( client_shares_len == 0)
397 {
398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800400 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800401 /* Write extension_type */
402 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
403 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800404 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800405 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800406 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800407
408 /* Update offered_group_id field */
409 ssl->handshake->offered_group_id = group_id;
410
411 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000412 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800413
Xiaofei Baid25fab62021-12-02 06:36:27 +0000414 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800415
416 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
417
418cleanup:
419
420 return( ret );
421}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800422
Jerry Yue1b9c292021-09-10 10:08:31 +0800423#if defined(MBEDTLS_ECDH_C)
424
Jerry Yuc068b662021-10-11 22:30:19 +0800425static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
426 const unsigned char *buf,
427 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800428{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100429 uint8_t *p = (uint8_t*)buf;
430 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800431
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100432 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
433 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
434 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800435
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100436 /* Check if key size is consistent with given buffer length. */
437 if ( peerkey_len > ( buf_len - 2 ) )
438 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800439
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100440 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100441 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100442 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800443
444 return( 0 );
445}
Jerry Yu4a173382021-10-11 21:45:31 +0800446#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800447
XiaokangQiand59be772022-01-24 10:12:51 +0000448/*
449 * ssl_tls13_parse_hrr_key_share_ext()
450 * Parse key_share extension in Hello Retry Request
451 *
452 * struct {
453 * NamedGroup selected_group;
454 * } KeyShareHelloRetryRequest;
455 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000456static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000457 const unsigned char *buf,
458 const unsigned char *end )
459{
XiaokangQianb851da82022-01-14 04:03:11 +0000460 const mbedtls_ecp_curve_info *curve_info = NULL;
461 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000462 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000463 int found = 0;
464
465 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
466 if( group_list == NULL )
467 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
468
469 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
470
471 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000472 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000473 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000475
476 /* Upon receipt of this extension in a HelloRetryRequest, the client
477 * MUST first verify that the selected_group field corresponds to a
478 * group which was provided in the "supported_groups" extension in the
479 * original ClientHello.
480 * The supported_group was based on the info in ssl->conf->group_list.
481 *
482 * If the server provided a key share that was not sent in the ClientHello
483 * then the client MUST abort the handshake with an "illegal_parameter" alert.
484 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000485 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000486 {
487 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000488 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000489 continue;
490
491 /* We found a match */
492 found = 1;
493 break;
494 }
495
496 /* Client MUST verify that the selected_group field does not
497 * correspond to a group which was provided in the "key_share"
498 * extension in the original ClientHello. If the server sent an
499 * HRR message with a key share already provided in the
500 * ClientHello then the client MUST abort the handshake with
501 * an "illegal_parameter" alert.
502 */
XiaokangQiand59be772022-01-24 10:12:51 +0000503 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000504 {
505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
506 MBEDTLS_SSL_PEND_FATAL_ALERT(
507 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
508 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
509 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
510 }
511
512 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000513 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000514
515 return( 0 );
516}
517
Jerry Yue1b9c292021-09-10 10:08:31 +0800518/*
Jerry Yub85277e2021-10-13 13:36:05 +0800519 * ssl_tls13_parse_key_share_ext()
520 * Parse key_share extension in Server Hello
521 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800522 * struct {
523 * KeyShareEntry server_share;
524 * } KeyShareServerHello;
525 * struct {
526 * NamedGroup group;
527 * opaque key_exchange<1..2^16-1>;
528 * } KeyShareEntry;
529 */
Jerry Yu4a173382021-10-11 21:45:31 +0800530static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800531 const unsigned char *buf,
532 const unsigned char *end )
533{
Jerry Yub85277e2021-10-13 13:36:05 +0800534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800535 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800536 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800537
Jerry Yu4a173382021-10-11 21:45:31 +0800538 /* ...
539 * NamedGroup group; (2 bytes)
540 * ...
541 */
542 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
543 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800544 p += 2;
545
Jerry Yu4a173382021-10-11 21:45:31 +0800546 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800547 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800548 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800549 {
550 MBEDTLS_SSL_DEBUG_MSG( 1,
551 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800552 (unsigned) offered_group, (unsigned) group ) );
553 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
554 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
555 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800556 }
557
558#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800559 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800560 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100561 const mbedtls_ecp_curve_info *curve_info =
562 mbedtls_ecp_curve_info_from_tls_id( group );
563 if( curve_info == NULL )
564 {
565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
567 }
568
569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
570
Jerry Yuc068b662021-10-11 22:30:19 +0800571 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800572 if( ret != 0 )
573 return( ret );
574 }
Jerry Yub85277e2021-10-13 13:36:05 +0800575 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800576#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800577 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800578 {
579 /* Do something */
580 }
581 else
582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
583
584 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
585 return( ret );
586}
587
XiaokangQiand59be772022-01-24 10:12:51 +0000588/*
589 * ssl_tls13_parse_cookie_ext()
590 * Parse cookie extension in Hello Retry Request
591 *
592 * struct {
593 * opaque cookie<1..2^16-1>;
594 * } Cookie;
595 *
596 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
597 * extension to the client (this is an exception to the usual rule that
598 * the only extensions that may be sent are those that appear in the
599 * ClientHello). When sending the new ClientHello, the client MUST copy
600 * the contents of the extension received in the HelloRetryRequest into
601 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
602 * cookies in their initial ClientHello in subsequent connections.
603 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000604static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
605 const unsigned char *buf,
606 const unsigned char *end )
607{
XiaokangQian25c9c902022-02-08 10:49:53 +0000608 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000609 const unsigned char *p = buf;
610 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
611
612 /* Retrieve length field of cookie */
613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
614 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
615 p += 2;
616
617 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
618 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
619
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000620 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000621 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000622 handshake->cookie = mbedtls_calloc( 1, cookie_len );
623 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000624 {
625 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000626 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000627 cookie_len ) );
628 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
629 }
630
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000631 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000632 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000633
634 return( 0 );
635}
XiaokangQian43550bd2022-01-21 04:32:58 +0000636
XiaokangQian0b64eed2022-01-27 10:36:51 +0000637static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000638 unsigned char *buf,
639 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000640 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000641{
642 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000643 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000644 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000645
XiaokangQianc02768a2022-02-10 07:31:25 +0000646 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000647 {
648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
649 return( 0 );
650 }
651
652 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000653 handshake->cookie,
654 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000655
XiaokangQianc02768a2022-02-10 07:31:25 +0000656 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000657
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
659
XiaokangQian0b64eed2022-01-27 10:36:51 +0000660 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000661 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
662 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000663 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000664
665 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000666 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000667
XiaokangQianc02768a2022-02-10 07:31:25 +0000668 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000669
670 return( 0 );
671}
672
Ronald Cron3d580bf2022-02-18 17:24:56 +0100673int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
674 unsigned char *buf,
675 unsigned char *end,
676 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100677{
678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
679 unsigned char *p = buf;
680 size_t ext_len;
681
682 *out_len = 0;
683
684 /* Write supported_versions extension
685 *
686 * Supported Versions Extension is mandatory with TLS 1.3.
687 */
688 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
689 if( ret != 0 )
690 return( ret );
691 p += ext_len;
692
693 /* Echo the cookie if the server provided one in its preceding
694 * HelloRetryRequest message.
695 */
696 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
697 if( ret != 0 )
698 return( ret );
699 p += ext_len;
700
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100701 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
702 {
703 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
704 if( ret != 0 )
705 return( ret );
706 p += ext_len;
707 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100708
709 *out_len = p - buf;
710
711 return( 0 );
712}
713
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800714/*
Jerry Yu4a173382021-10-11 21:45:31 +0800715 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800716 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100717static int ssl_tls13_is_supported_versions_ext_present(
718 mbedtls_ssl_context *ssl,
719 const unsigned char *buf,
720 const unsigned char *end )
721{
722 const unsigned char *p = buf;
723 size_t legacy_session_id_echo_len;
724 size_t extensions_len;
725 const unsigned char *extensions_end;
726
727 /*
728 * Check there is enough data to access the legacy_session_id_echo vector
729 * length.
730 * - legacy_version, 2 bytes
731 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
732 * - legacy_session_id_echo 1 byte
733 */
734 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
735 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
736 legacy_session_id_echo_len = *p;
737
738 /*
739 * Jump to the extensions, jumping over:
740 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
741 * - cipher_suite 2 bytes
742 * - legacy_compression_method 1 byte
743 */
744 p += legacy_session_id_echo_len + 4;
745
746 /* Case of no extension */
747 if( p == end )
748 return( 0 );
749
750 /* ...
751 * Extension extensions<6..2^16-1>;
752 * ...
753 * struct {
754 * ExtensionType extension_type; (2 bytes)
755 * opaque extension_data<0..2^16-1>;
756 * } Extension;
757 */
758 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
759 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
760 p += 2;
761
762 /* Check extensions do not go beyond the buffer of data. */
763 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
764 extensions_end = p + extensions_len;
765
766 while( p < extensions_end )
767 {
768 unsigned int extension_type;
769 size_t extension_data_len;
770
771 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
772 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
773 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
774 p += 4;
775
776 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
777 return( 1 );
778
779 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
780 p += extension_data_len;
781 }
782
783 return( 0 );
784}
785
Jerry Yu7a186a02021-10-15 18:46:14 +0800786/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800787 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
788 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000789 * to indicate which message is expected and to be parsed next.
790 */
Jerry Yub85277e2021-10-13 13:36:05 +0800791#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
792#define SSL_SERVER_HELLO_COORDINATE_HRR 1
793static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800796{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800797 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800798 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
799 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
800 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
801 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
802
803 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
804 *
Jerry Yu4a173382021-10-11 21:45:31 +0800805 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800806 * special value of the SHA-256 of "HelloRetryRequest".
807 *
808 * struct {
809 * ProtocolVersion legacy_version = 0x0303;
810 * Random random;
811 * opaque legacy_session_id_echo<0..32>;
812 * CipherSuite cipher_suite;
813 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800814 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800815 * } ServerHello;
816 *
817 */
Jerry Yub85277e2021-10-13 13:36:05 +0800818 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800819
820 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800821 {
Jerry Yub85277e2021-10-13 13:36:05 +0800822 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800823 }
824
Jerry Yub85277e2021-10-13 13:36:05 +0800825 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800826}
827
Jerry Yu745bb612021-10-13 22:01:04 +0800828/* Fetch and preprocess
829 * Returns a negative value on failure, and otherwise
830 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100831 * - SSL_SERVER_HELLO_COORDINATE_HRR or
832 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800833 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100834#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800835static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
836 unsigned char **buf,
837 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800838{
Jerry Yu4a173382021-10-11 21:45:31 +0800839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800840
XiaokangQian355e09a2022-01-20 11:14:50 +0000841 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
842 MBEDTLS_SSL_HS_SERVER_HELLO,
843 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800844
Ronald Cron9f0fba32022-02-10 16:45:15 +0100845 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
846 ssl, *buf, *buf + *buf_len ) );
847 if( ret == 0 )
848 {
849 /* If the supported versions extension is not present but we were
850 * expecting it, abort the handshake. Otherwise, switch to TLS 1.2
851 * handshake.
852 */
853 if( ssl->handshake->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 )
854 {
855 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
856 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
857 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
858 }
859
860 ssl->keep_current_message = 1;
861 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
862 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
863 *buf, *buf_len );
864
865 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
866 {
867 ret = ssl_tls13_reset_key_share( ssl );
868 if( ret != 0 )
869 return( ret );
870 }
871
872 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
873 }
874
Jerry Yub85277e2021-10-13 13:36:05 +0800875 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
876 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800877 {
Jerry Yu745bb612021-10-13 22:01:04 +0800878 case SSL_SERVER_HELLO_COORDINATE_HELLO:
879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
880 break;
881 case SSL_SERVER_HELLO_COORDINATE_HRR:
882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000883 /* If a client receives a second
884 * HelloRetryRequest in the same connection (i.e., where the ClientHello
885 * was itself in response to a HelloRetryRequest), it MUST abort the
886 * handshake with an "unexpected_message" alert.
887 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000888 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000889 {
890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
891 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
892 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
893 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
894 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000895 /*
896 * Clients must abort the handshake with an "illegal_parameter"
897 * alert if the HelloRetryRequest would not result in any change
898 * in the ClientHello.
899 * In a PSK only key exchange that what we expect.
900 */
901 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
902 {
903 MBEDTLS_SSL_DEBUG_MSG( 1,
904 ( "Unexpected HRR in pure PSK key exchange." ) );
905 MBEDTLS_SSL_PEND_FATAL_ALERT(
906 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
907 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
908 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
909 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000910
XiaokangQiand9e068e2022-01-18 06:23:32 +0000911 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000912
Jerry Yu745bb612021-10-13 22:01:04 +0800913 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 }
915
916cleanup:
917
918 return( ret );
919}
920
Jerry Yu4a173382021-10-11 21:45:31 +0800921static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
922 const unsigned char **buf,
923 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800924{
925 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800926 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800927
Jerry Yude4fb2c2021-09-19 18:05:08 +0800928 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800929 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800930
Jerry Yu4a173382021-10-11 21:45:31 +0800931 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800932
933 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800934 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
935 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800936 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800937 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
938 ssl->session_negotiate->id,
939 ssl->session_negotiate->id_len );
940 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800941 legacy_session_id_echo_len );
942
943 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
944 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
945
Jerry Yue1b9c292021-09-10 10:08:31 +0800946 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
947 }
948
Jerry Yu4a173382021-10-11 21:45:31 +0800949 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800950 *buf = p;
951
952 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800953 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 return( 0 );
955}
956
Jerry Yu4a173382021-10-11 21:45:31 +0800957static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
958 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800959{
Jerry Yu4a173382021-10-11 21:45:31 +0800960 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
961
Jerry Yue1b9c292021-09-10 10:08:31 +0800962 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800963 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800964 {
Jerry Yu4a173382021-10-11 21:45:31 +0800965 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800966 {
967 return( 1 );
968 }
969 }
970 return( 0 );
971}
972
973/* Parse ServerHello message and configure context
974 *
975 * struct {
976 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
977 * Random random;
978 * opaque legacy_session_id_echo<0..32>;
979 * CipherSuite cipher_suite;
980 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800981 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800982 * } ServerHello;
983 */
Jerry Yuc068b662021-10-11 22:30:19 +0800984static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
985 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000986 const unsigned char *end,
987 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800988{
Jerry Yub85277e2021-10-13 13:36:05 +0800989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800990 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000991 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800992 size_t extensions_len;
993 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800995 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000996 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997
998 /*
999 * Check there is space for minimal fields
1000 *
1001 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001002 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001003 * - legacy_session_id_echo ( 1 byte ), minimum size
1004 * - cipher_suite ( 2 bytes)
1005 * - legacy_compression_method ( 1 byte )
1006 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001007 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001008
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001010 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1011
Jerry Yu4a173382021-10-11 21:45:31 +08001012 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001013 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001014 * ...
1015 * with ProtocolVersion defined as:
1016 * uint16 ProtocolVersion;
1017 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1019 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1020 {
1021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1022 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1023 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001024 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1025 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 }
1027 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001028
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001029 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001030 * Random random;
1031 * ...
1032 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001033 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001034 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001035 if( !is_hrr )
1036 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001037 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001038 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1039 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1040 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1041 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001042 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001043
Jerry Yu4a173382021-10-11 21:45:31 +08001044 /* ...
1045 * opaque legacy_session_id_echo<0..32>;
1046 * ...
1047 */
1048 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001049 {
XiaokangQian52da5582022-01-26 09:49:29 +00001050 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001051 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001052 }
1053
Jerry Yu4a173382021-10-11 21:45:31 +08001054 /* ...
1055 * CipherSuite cipher_suite;
1056 * ...
1057 * with CipherSuite defined as:
1058 * uint8 CipherSuite[2];
1059 */
1060 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001061 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1062 p += 2;
1063
Jerry Yu4a173382021-10-11 21:45:31 +08001064
XiaokangQian355e09a2022-01-20 11:14:50 +00001065 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001066 /*
1067 * Check whether this ciphersuite is supported and offered.
1068 * Via the force_ciphersuite version we may have instructed the client
1069 * to use a different ciphersuite.
1070 */
Jerry Yu4a173382021-10-11 21:45:31 +08001071 if( ciphersuite_info == NULL ||
1072 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 {
XiaokangQian52da5582022-01-26 09:49:29 +00001074 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001075 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001076 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001077 * If we received an HRR before and that the proposed selected
1078 * ciphersuite in this server hello is not the same as the one
1079 * proposed in the HRR, we abort the handshake and send an
1080 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001081 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001082 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1083 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001084 {
XiaokangQian52da5582022-01-26 09:49:29 +00001085 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001086 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001087
XiaokangQian52da5582022-01-26 09:49:29 +00001088 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001089 {
1090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1091 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001092 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001093 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001094
Jerry Yu4a173382021-10-11 21:45:31 +08001095 /* Configure ciphersuites */
1096 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1097
XiaokangQian355e09a2022-01-20 11:14:50 +00001098 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 ssl->session_negotiate->ciphersuite = cipher_suite;
1100
Jerry Yue1b9c292021-09-10 10:08:31 +08001101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1102 cipher_suite, ciphersuite_info->name ) );
1103
1104#if defined(MBEDTLS_HAVE_TIME)
1105 ssl->session_negotiate->start = time( NULL );
1106#endif /* MBEDTLS_HAVE_TIME */
1107
Jerry Yu4a173382021-10-11 21:45:31 +08001108 /* ...
1109 * uint8 legacy_compression_method = 0;
1110 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001111 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001112 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001113 if( p[0] != 0 )
1114 {
Jerry Yub85277e2021-10-13 13:36:05 +08001115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001116 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001117 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 }
1119 p++;
1120
Jerry Yub85277e2021-10-13 13:36:05 +08001121 /* ...
1122 * Extension extensions<6..2^16-1>;
1123 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001124 * struct {
1125 * ExtensionType extension_type; (2 bytes)
1126 * opaque extension_data<0..2^16-1>;
1127 * } Extension;
1128 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001129 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001130 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001131 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001132
Jerry Yu4a173382021-10-11 21:45:31 +08001133 /* Check extensions do not go beyond the buffer of data. */
1134 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1135 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001136
Jerry Yu4a173382021-10-11 21:45:31 +08001137 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001138
Jerry Yu4a173382021-10-11 21:45:31 +08001139 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 {
1141 unsigned int extension_type;
1142 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001143 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001144
Jerry Yu4a173382021-10-11 21:45:31 +08001145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1147 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1148 p += 4;
1149
Jerry Yu4a173382021-10-11 21:45:31 +08001150 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001151 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001152
1153 switch( extension_type )
1154 {
XiaokangQianb851da82022-01-14 04:03:11 +00001155 case MBEDTLS_TLS_EXT_COOKIE:
1156
XiaokangQiand9e068e2022-01-18 06:23:32 +00001157 if( !is_hrr )
1158 {
XiaokangQian52da5582022-01-26 09:49:29 +00001159 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001160 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001161 }
1162
XiaokangQian43550bd2022-01-21 04:32:58 +00001163 ret = ssl_tls13_parse_cookie_ext( ssl,
1164 p, extension_data_end );
1165 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001166 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001167 MBEDTLS_SSL_DEBUG_RET( 1,
1168 "ssl_tls13_parse_cookie_ext",
1169 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001170 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001171 }
XiaokangQianb851da82022-01-14 04:03:11 +00001172 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001173
Jerry Yue1b9c292021-09-10 10:08:31 +08001174 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001175 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001176 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001177 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001178 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 break;
1181
1182 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001185
XiaokangQian52da5582022-01-26 09:49:29 +00001186 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001187 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001188
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 case MBEDTLS_TLS_EXT_KEY_SHARE:
1190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001191 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1192 {
XiaokangQian52da5582022-01-26 09:49:29 +00001193 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001194 goto cleanup;
1195 }
1196
XiaokangQian53f20b72022-01-18 10:47:33 +00001197 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001198 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001199 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001200 else
XiaokangQianb851da82022-01-14 04:03:11 +00001201 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001202 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001203 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001204 {
1205 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001206 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001208 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 }
1210 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001211
1212 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001213 MBEDTLS_SSL_DEBUG_MSG(
1214 3,
1215 ( "unknown extension found: %u ( ignoring )",
1216 extension_type ) );
1217
XiaokangQian52da5582022-01-26 09:49:29 +00001218 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001219 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 }
1221
1222 p += extension_data_len;
1223 }
1224
XiaokangQiand59be772022-01-24 10:12:51 +00001225cleanup:
1226
XiaokangQian52da5582022-01-26 09:49:29 +00001227 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001228 {
1229 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1230 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1231 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1232 }
XiaokangQian52da5582022-01-26 09:49:29 +00001233 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001234 {
1235 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1236 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1237 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1238 }
1239 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001240}
1241
XiaokangQian355e09a2022-01-20 11:14:50 +00001242static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001243{
Jerry Yub85277e2021-10-13 13:36:05 +08001244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001245 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001246 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001247 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001248
Jerry Yub85277e2021-10-13 13:36:05 +08001249 /* Determine the key exchange mode:
1250 * 1) If both the pre_shared_key and key_share extensions were received
1251 * then the key exchange mode is PSK with EPHEMERAL.
1252 * 2) If only the pre_shared_key extension was received then the key
1253 * exchange mode is PSK-only.
1254 * 3) If only the key_share extension was received then the key
1255 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001256 */
Jerry Yub85277e2021-10-13 13:36:05 +08001257 switch( handshake->extensions_present &
1258 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001259 {
Jerry Yu745bb612021-10-13 22:01:04 +08001260 /* Only the pre_shared_key extension was received */
1261 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001262 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001263 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001264
Jerry Yu745bb612021-10-13 22:01:04 +08001265 /* Only the key_share extension was received */
1266 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001267 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001268 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001269
Jerry Yu745bb612021-10-13 22:01:04 +08001270 /* Both the pre_shared_key and key_share extensions were received */
1271 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001272 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001273 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001274
Jerry Yu745bb612021-10-13 22:01:04 +08001275 /* Neither pre_shared_key nor key_share extension was received */
1276 default:
1277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1278 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1279 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001280 }
1281
1282 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1283 *
1284 * TODO: We don't have to do this in case we offered 0-RTT and the
1285 * server accepted it. In this case, we could skip generating
1286 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001287 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001288 if( ret != 0 )
1289 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001291 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001292 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001293 }
1294
1295 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001296 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001297 if( ret != 0 )
1298 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001300 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001301 }
1302
1303 /* Next evolution in key schedule: Establish handshake secret and
1304 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001305 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001306 if( ret != 0 )
1307 {
Jerry Yuc068b662021-10-11 22:30:19 +08001308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1309 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001310 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001311 }
1312
Jerry Yub85277e2021-10-13 13:36:05 +08001313 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001314 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001315 {
1316 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1317 goto cleanup;
1318 }
Jerry Yu0b177842021-09-05 19:41:30 +08001319
1320 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1321 ssl->conf->endpoint,
1322 ssl->session_negotiate->ciphersuite,
1323 &traffic_keys,
1324 ssl );
1325 if( ret != 0 )
1326 {
1327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001328 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001329 }
1330
Jerry Yub85277e2021-10-13 13:36:05 +08001331 handshake->transform_handshake = transform_handshake;
1332 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001333
1334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1335 ssl->session_in = ssl->session_negotiate;
1336
1337 /*
1338 * State machine update
1339 */
1340 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1341
Jerry Yu4a173382021-10-11 21:45:31 +08001342cleanup:
1343
Jerry Yu0b177842021-09-05 19:41:30 +08001344 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001345 if( ret != 0 )
1346 {
Jerry Yub85277e2021-10-13 13:36:05 +08001347 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001348
Jerry Yu4a173382021-10-11 21:45:31 +08001349 MBEDTLS_SSL_PEND_FATAL_ALERT(
1350 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1351 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1352 }
1353 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001354}
1355
XiaokangQian355e09a2022-01-20 11:14:50 +00001356static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001357{
1358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1359
XiaokangQian647719a2021-12-07 09:16:29 +00001360#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1361 /* If not offering early data, the client sends a dummy CCS record
1362 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001363 * its second ClientHello or before its encrypted handshake flight.
1364 */
XiaokangQian647719a2021-12-07 09:16:29 +00001365 mbedtls_ssl_handshake_set_state( ssl,
1366 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1367#else
1368 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1369#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1370
XiaokangQian78b1fa72022-01-19 06:56:30 +00001371 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001372
XiaokangQian78b1fa72022-01-19 06:56:30 +00001373 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001374 * We are going to re-generate a shared secret corresponding to the group
1375 * selected by the server, which is different from the group for which we
1376 * generated a shared secret in the first client hello.
1377 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001378 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001379 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001380 if( ret != 0 )
1381 return( ret );
1382
1383 return( 0 );
1384}
1385
Jerry Yue1b9c292021-09-10 10:08:31 +08001386/*
Jerry Yu4a173382021-10-11 21:45:31 +08001387 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001388 * Handler for MBEDTLS_SSL_SERVER_HELLO
1389 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001390static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001391{
Jerry Yu4a173382021-10-11 21:45:31 +08001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001393 unsigned char *buf = NULL;
1394 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001395 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001396
1397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1398
1399 /* Coordination step
1400 * - Fetch record
1401 * - Make sure it's either a ServerHello or a HRR.
1402 * - Switch processing routine in case of HRR
1403 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001404 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1405 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1406
XiaokangQian16acd4b2022-01-14 07:35:47 +00001407 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001408 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001409 goto cleanup;
1410 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001411 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001412
Ronald Cron9f0fba32022-02-10 16:45:15 +01001413 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1414 {
1415 ret = 0;
1416 goto cleanup;
1417 }
1418
XiaokangQianb851da82022-01-14 04:03:11 +00001419 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001420 buf + buf_len,
1421 is_hrr ) );
1422 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001423 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1424
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001425 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1426 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001427
XiaokangQiand9e068e2022-01-18 06:23:32 +00001428 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001429 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001430 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001431 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001432
1433cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1435 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001436 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001437}
1438
1439/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001440 *
1441 * EncryptedExtensions message
1442 *
1443 * The EncryptedExtensions message contains any extensions which
1444 * should be protected, i.e., any which are not needed to establish
1445 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001446 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001447
1448/*
1449 * Overview
1450 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451
1452/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001453static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001454
XiaokangQian97799ac2021-10-11 10:05:54 +00001455static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1456 const unsigned char *buf,
1457 const unsigned char *end );
1458static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001459
1460/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001461 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001462 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001463static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001464{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465 int ret;
1466 unsigned char *buf;
1467 size_t buf_len;
1468
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1470
Xiaofei Bai746f9482021-11-12 08:53:56 +00001471 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001472 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473 &buf, &buf_len ) );
1474
1475 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001476 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001477 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001478
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001479 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1480 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001481
XiaokangQian97799ac2021-10-11 10:05:54 +00001482 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483
1484cleanup:
1485
1486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1487 return( ret );
1488
1489}
1490
XiaokangQian08da26c2021-10-09 10:12:11 +00001491/* Parse EncryptedExtensions message
1492 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001493 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001494 * } EncryptedExtensions;
1495 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001496static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1497 const unsigned char *buf,
1498 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001499{
1500 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001501 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001502 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001503 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001504
XiaokangQian08da26c2021-10-09 10:12:11 +00001505 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001506 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001507 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001508
XiaokangQian97799ac2021-10-11 10:05:54 +00001509 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001510 extensions_end = p + extensions_len;
1511 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001512
XiaokangQian8db25ff2021-10-13 05:56:18 +00001513 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001514 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001515 unsigned int extension_type;
1516 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001517
XiaokangQian08da26c2021-10-09 10:12:11 +00001518 /*
1519 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001520 * ExtensionType extension_type; (2 bytes)
1521 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001522 * } Extension;
1523 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001524 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001525 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1526 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1527 p += 4;
1528
XiaokangQian8db25ff2021-10-13 05:56:18 +00001529 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001530
XiaokangQian97799ac2021-10-11 10:05:54 +00001531 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001532 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001533 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001534 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001535 switch( extension_type )
1536 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001537
XiaokangQian08da26c2021-10-09 10:12:11 +00001538 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1540 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001541
lhuang0486cacac2022-01-21 07:34:27 -08001542#if defined(MBEDTLS_SSL_ALPN)
1543 case MBEDTLS_TLS_EXT_ALPN:
1544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1545
1546 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1547 {
1548 return( ret );
1549 }
1550
1551 break;
1552#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001553 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001554 MBEDTLS_SSL_DEBUG_MSG(
1555 3, ( "unsupported extension found: %u ", extension_type) );
1556 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001557 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001558 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1559 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001560 }
1561
XiaokangQian08da26c2021-10-09 10:12:11 +00001562 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001563 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001564
XiaokangQian97799ac2021-10-11 10:05:54 +00001565 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001566 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001567 {
1568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001569 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001570 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001571 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001572 }
1573
1574 return( ret );
1575}
1576
XiaokangQian97799ac2021-10-11 10:05:54 +00001577static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001578{
Jerry Yua93ac112021-10-27 16:31:48 +08001579#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001580 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001581 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1582 else
Jerry Yud2674312021-10-29 10:08:19 +08001583 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001584#else
1585 ((void) ssl);
1586 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1587#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001588 return( 0 );
1589}
1590
Jerry Yua93ac112021-10-27 16:31:48 +08001591#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001592/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001593 *
1594 * STATE HANDLING: CertificateRequest
1595 *
Jerry Yud2674312021-10-29 10:08:19 +08001596 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001597#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1598#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001599/* Coordination:
1600 * Deals with the ambiguity of not knowing if a CertificateRequest
1601 * will be sent. Returns a negative code on failure, or
1602 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1603 * - SSL_CERTIFICATE_REQUEST_SKIP
1604 * indicating if a Certificate Request is expected or not.
1605 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001606static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001607{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001609
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001610 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001611 {
1612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1613 return( SSL_CERTIFICATE_REQUEST_SKIP );
1614 }
1615
1616 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001617 {
1618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1619 return( ret );
1620 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001621 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001622
1623 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1624 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1625 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001626 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001627 }
1628
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001629 return( SSL_CERTIFICATE_REQUEST_SKIP );
1630}
1631
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001632/*
1633 * ssl_tls13_parse_certificate_request()
1634 * Parse certificate request
1635 * struct {
1636 * opaque certificate_request_context<0..2^8-1>;
1637 * Extension extensions<2..2^16-1>;
1638 * } CertificateRequest;
1639 */
1640static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1641 const unsigned char *buf,
1642 const unsigned char *end )
1643{
1644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1645 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001646 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001647 size_t extensions_len = 0;
1648 const unsigned char *extensions_end;
1649 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001650
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001651 /* ...
1652 * opaque certificate_request_context<0..2^8-1>
1653 * ...
1654 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001655 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1656 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001657 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001658
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659 if( certificate_request_context_len > 0 )
1660 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001661 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001662 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1663 p, certificate_request_context_len );
1664
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001665 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001666 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001667 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001668 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669 {
1670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1671 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1672 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001673 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 p += certificate_request_context_len;
1676 }
1677
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001678 /* ...
1679 * Extension extensions<2..2^16-1>;
1680 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001681 */
1682 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1683 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1684 p += 2;
1685
1686 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1687 extensions_end = p + extensions_len;
1688
1689 while( p < extensions_end )
1690 {
1691 unsigned int extension_type;
1692 size_t extension_data_len;
1693
1694 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1695 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1696 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1697 p += 4;
1698
1699 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1700
1701 switch( extension_type )
1702 {
1703 case MBEDTLS_TLS_EXT_SIG_ALG:
1704 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001705 ( "found signature algorithms extension" ) );
1706 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001707 p + extension_data_len );
1708 if( ret != 0 )
1709 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001710 if( ! sig_alg_ext_found )
1711 sig_alg_ext_found = 1;
1712 else
1713 {
1714 MBEDTLS_SSL_DEBUG_MSG( 3,
1715 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001716 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001717 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001718 break;
1719
1720 default:
1721 MBEDTLS_SSL_DEBUG_MSG(
1722 3,
1723 ( "unknown extension found: %u ( ignoring )",
1724 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001725 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727 p += extension_data_len;
1728 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001729 /* Check that we consumed all the message. */
1730 if( p != end )
1731 {
1732 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001733 ( "CertificateRequest misaligned" ) );
1734 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001735 }
1736 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001737 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001738 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001739 MBEDTLS_SSL_DEBUG_MSG( 3,
1740 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001741 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001742 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001743
Jerry Yu7840f812022-01-29 10:26:51 +08001744 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001745 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001746
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001747decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001748 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1749 MBEDTLS_ERR_SSL_DECODE_ERROR );
1750 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001751}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001752
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001753/*
1754 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1755 */
1756static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001757{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001759
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1761
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1763
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1765 {
1766 unsigned char *buf;
1767 size_t buf_len;
1768
1769 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1770 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1771 &buf, &buf_len ) );
1772
1773 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1774 buf, buf + buf_len ) );
1775
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001776 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1777 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001778 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001779 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001780 {
1781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001782 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001783 }
1784 else
1785 {
1786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001787 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1788 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001789 }
1790
1791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001792 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001793
Jerry Yud2674312021-10-29 10:08:19 +08001794 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1795
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001796cleanup:
1797
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1799 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001800}
1801
1802/*
Jerry Yu687101b2021-09-14 16:03:56 +08001803 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1804 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001805static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001806{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001807 int ret;
1808
1809 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001810 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001811 return( ret );
1812
Jerry Yu687101b2021-09-14 16:03:56 +08001813 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1814 return( 0 );
1815}
1816
1817/*
1818 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1819 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001820static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001821{
Jerry Yu30b071c2021-09-12 20:16:03 +08001822 int ret;
1823
1824 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1825 if( ret != 0 )
1826 return( ret );
1827
Jerry Yu687101b2021-09-14 16:03:56 +08001828 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1829 return( 0 );
1830}
Jerry Yua93ac112021-10-27 16:31:48 +08001831#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001832
Jerry Yu687101b2021-09-14 16:03:56 +08001833/*
1834 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1835 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001836static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001837{
XiaokangQianac0385c2021-11-03 06:40:11 +00001838 int ret;
1839
XiaokangQianc5c39d52021-11-09 11:55:10 +00001840 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001841 if( ret != 0 )
1842 return( ret );
1843
Ronald Cron49ad6192021-11-24 16:25:31 +01001844#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1845 mbedtls_ssl_handshake_set_state(
1846 ssl,
1847 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1848#else
Jerry Yuca133a32022-02-15 14:22:05 +08001849 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001850#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001851
XiaokangQianac0385c2021-11-03 06:40:11 +00001852 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001853}
1854
1855/*
Jerry Yu566c7812022-01-26 15:41:22 +08001856 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1857 */
1858static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1859{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001860 int non_empty_certificate_msg = 0;
1861
Jerry Yu5cc35062022-01-28 16:16:08 +08001862 MBEDTLS_SSL_DEBUG_MSG( 1,
1863 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001864 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001865
Ronald Cron9df7c802022-03-08 18:38:54 +01001866#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001867 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001868 {
1869 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1870 if( ret != 0 )
1871 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001872
Ronald Cron7a94aca2022-03-09 07:44:27 +01001873 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1874 non_empty_certificate_msg = 1;
1875 }
1876 else
1877 {
1878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1879 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001880#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001881
Ronald Cron7a94aca2022-03-09 07:44:27 +01001882 if( non_empty_certificate_msg )
1883 {
1884 mbedtls_ssl_handshake_set_state( ssl,
1885 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1886 }
1887 else
1888 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1889
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001890 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001891}
1892
Ronald Cron9df7c802022-03-08 18:38:54 +01001893#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001894/*
1895 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1896 */
1897static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1898{
Ronald Crona8b38872022-03-09 07:59:25 +01001899 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1900
1901 if( ret == 0 )
1902 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1903
1904 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001905}
Jerry Yu90f152d2022-01-29 22:12:42 +08001906#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001907
1908/*
Jerry Yu687101b2021-09-14 16:03:56 +08001909 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1910 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001911static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001912{
XiaokangQian0fa66432021-11-15 03:33:57 +00001913 int ret;
1914
1915 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1916 if( ret != 0 )
1917 return( ret );
1918
1919 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1920 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001921}
1922
1923/*
1924 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1925 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001926static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001927{
Jerry Yu378254d2021-10-30 21:44:47 +08001928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001929 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001930 return( 0 );
1931}
1932
1933/*
1934 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1935 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001936static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001937{
Jerry Yu378254d2021-10-30 21:44:47 +08001938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1939 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1940
1941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1942 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1943
1944 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1945
1946 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1947 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001948}
1949
Jerry Yu92c6b402021-08-27 16:59:09 +08001950int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001951{
Jerry Yu92c6b402021-08-27 16:59:09 +08001952 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001953
Jerry Yu92c6b402021-08-27 16:59:09 +08001954 switch( ssl->state )
1955 {
1956 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001957 * ssl->state is initialized as HELLO_REQUEST. It is the same
1958 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001959 */
1960 case MBEDTLS_SSL_HELLO_REQUEST:
1961 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001962 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001963 break;
1964
1965 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001966 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001967 break;
1968
1969 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001970 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001971 break;
1972
Jerry Yua93ac112021-10-27 16:31:48 +08001973#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001974 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1975 ret = ssl_tls13_process_certificate_request( ssl );
1976 break;
1977
Jerry Yu687101b2021-09-14 16:03:56 +08001978 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001979 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001980 break;
1981
1982 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001983 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001984 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001985#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001986
1987 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001988 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001989 break;
1990
Jerry Yu566c7812022-01-26 15:41:22 +08001991 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1992 ret = ssl_tls13_write_client_certificate( ssl );
1993 break;
1994
Ronald Cron9df7c802022-03-08 18:38:54 +01001995#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001996 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1997 ret = ssl_tls13_write_client_certificate_verify( ssl );
1998 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001999#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002000
Jerry Yu687101b2021-09-14 16:03:56 +08002001 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002002 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002003 break;
2004
2005 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002006 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002007 break;
2008
2009 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002010 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002011 break;
2012
Ronald Cron49ad6192021-11-24 16:25:31 +01002013 /*
2014 * Injection of dummy-CCS's for middlebox compatibility
2015 */
2016#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002017 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002018 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2019 if( ret == 0 )
2020 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2021 break;
2022
2023 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2024 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2025 if( ret == 0 )
2026 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002027 break;
2028#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2029
Jerry Yu92c6b402021-08-27 16:59:09 +08002030 default:
2031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2033 }
2034
2035 return( ret );
2036}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002037
Jerry Yufb4b6472022-01-27 15:03:26 +08002038#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002039
Jerry Yufb4b6472022-01-27 15:03:26 +08002040