blob: 88cd22ad4b53ee3b4547def50edbe90c72bf30a6 [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"
Gilles Peskine923d5c92021-12-15 12:56:54 +010036#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080037
Jerry Yubc20bdd2021-08-24 15:59:48 +080038/* Write extensions */
39
Jerry Yu92c6b402021-08-27 16:59:09 +080040/*
41 * ssl_tls13_write_supported_versions_ext():
42 *
43 * struct {
44 * ProtocolVersion versions<2..254>;
45 * } SupportedVersions;
46 */
Jerry Yuf4436812021-08-26 22:59:56 +080047static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080048 unsigned char *buf,
49 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000050 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080051{
52 unsigned char *p = buf;
53
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
62 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Jerry Yu92c6b402021-08-27 16:59:09 +080064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
65
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080066 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080068
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080069 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080070 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080071 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080073 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080074 *p++ = 0x2;
75
Jerry Yu0c63af62021-09-02 12:59:12 +080076 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080077 *
Jerry Yu0c63af62021-09-02 12:59:12 +080078 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 *
Jerry Yu0c63af62021-09-02 12:59:12 +080080 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080081 */
Jerry Yueecfbf02021-08-30 18:32:07 +080082 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
83 ssl->conf->max_minor_ver,
84 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080085
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080087 ssl->conf->max_major_ver,
88 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080089
Xiaofei Baid25fab62021-12-02 06:36:27 +000090 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuc068b662021-10-11 22:30:19 +080095static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
96 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080097 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080098{
Jerry Yue1b9c292021-09-10 10:08:31 +080099 ((void) ssl);
100
Jerry Yub85277e2021-10-13 13:36:05 +0800101 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
102 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800106
107 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
109 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800110 }
111
112 return( 0 );
113}
114
lhuang0486cacac2022-01-21 07:34:27 -0800115#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800116static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
117 const unsigned char *buf, size_t len )
118{
119 size_t list_len, name_len;
120 const unsigned char *p = buf;
121 const unsigned char *end = buf + len;
122
123 /* If we didn't send it, the server shouldn't send it */
124 if( ssl->conf->alpn_list == NULL )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
127 /*
128 * opaque ProtocolName<1..2^8-1>;
129 *
130 * struct {
131 * ProtocolName protocol_name_list<2..2^16-1>
132 * } ProtocolNameList;
133 *
134 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
135 */
136
137 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
138 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
139
140 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
141 p += 2;
142 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
143
144 name_len = *p++;
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
146
147 /* Check that the server chosen protocol was in our list and save it */
148 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
149 {
150 if( name_len == strlen( *alpn ) &&
151 memcmp( buf + 3, *alpn, name_len ) == 0 )
152 {
153 ssl->alpn_chosen = *alpn;
154 return( 0 );
155 }
156 }
157
158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159}
160#endif /* MBEDTLS_SSL_ALPN */
161
Jerry Yubc20bdd2021-08-24 15:59:48 +0800162#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
163
XiaokangQian16acd4b2022-01-14 07:35:47 +0000164static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000165{
166 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100167
XiaokangQian647719a2021-12-07 09:16:29 +0000168 if( group_id == 0 )
169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
170
XiaokangQian355e09a2022-01-20 11:14:50 +0000171#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000172 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000173 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
176
177 /* Destroy generated private key. */
178 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
179 if( status != PSA_SUCCESS )
180 {
181 ret = psa_ssl_status_to_mbedtls( status );
182 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
183 return( ret );
184 }
185
186 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000187 return( 0 );
188 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000189 else
190#endif /* MBEDTLS_ECDH_C */
191 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000192 {
193 /* Do something */
194 }
195
196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
197}
198
199/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200 * Functions for writing key_share extension.
201 */
202#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800203static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800204 mbedtls_ssl_context *ssl,
205 uint16_t named_group,
206 unsigned char *buf,
207 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000208 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800209{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100210 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
211 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
212 psa_key_attributes_t key_attributes;
213 size_t own_pubkey_len;
214 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
215 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100219 /* Convert EC group to PSA key type. */
220 if( ( handshake->ecdh_psa_type =
221 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
222 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800223
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100224 if( ecdh_bits > 0xffff )
225 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
226 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100228 key_attributes = psa_key_attributes_init();
229 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
230 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
231 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
232 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100233
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100234 /* Generate ECDH private key. */
235 status = psa_generate_key( &key_attributes,
236 &handshake->ecdh_psa_privkey );
237 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100239 ret = psa_ssl_status_to_mbedtls( status );
240 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100242
Jerry Yu56fc07f2021-09-01 17:48:49 +0800243 }
244
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100245 /* Export the public part of the ECDH private key from PSA. */
246 status = psa_export_public_key( handshake->ecdh_psa_privkey,
247 buf, (size_t)( end - buf ),
248 &own_pubkey_len );
249 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100251 ret = psa_ssl_status_to_mbedtls( status );
252 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100254
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 }
256
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100257 if( own_pubkey_len > (size_t)( end - buf ) )
258 {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
260 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
261 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100263 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100264
Jerry Yu75336352021-09-01 15:59:36 +0800265 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800266}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267#endif /* MBEDTLS_ECDH_C */
268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
270 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271{
272 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
273
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100276 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100278 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
280
Brett Warren14efd332021-10-06 09:32:11 +0100281 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800282 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000283 const mbedtls_ecp_curve_info *curve_info;
284 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
285 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100286 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 {
Brett Warren14efd332021-10-06 09:32:11 +0100288 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 return( 0 );
290 }
291 }
292#else
293 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800294 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295#endif /* MBEDTLS_ECDH_C */
296
297 /*
298 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800299 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300 */
301
302 return( ret );
303}
304
305/*
306 * ssl_tls13_write_key_share_ext
307 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800308 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309 *
310 * struct {
311 * NamedGroup group;
312 * opaque key_exchange<1..2^16-1>;
313 * } KeyShareEntry;
314 * struct {
315 * KeyShareEntry client_shares<0..2^16-1>;
316 * } KeyShareClientHello;
317 */
318static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000321 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322{
323 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000324 unsigned char *client_shares; /* Start of client_shares */
325 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800326 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
328
Xiaofei Baid25fab62021-12-02 06:36:27 +0000329 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800332 * - extension_type (2 bytes)
333 * - extension_data_length (2 bytes)
334 * - client_shares_length (2 bytes)
335 */
336 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
337 p += 6;
338
339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
340
341 /* HRR could already have requested something else. */
342 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
344 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800346 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 &group_id ) );
348 }
349
350 /*
351 * Dispatch to type-specific key generation function.
352 *
353 * So far, we're only supporting ECDHE. With the introduction
354 * of PQC KEMs, we'll want to have multiple branches, one per
355 * type of KEM, and dispatch to the corresponding crypto. And
356 * only one key share entry is allowed.
357 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000358 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800360 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800362 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000363 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800364 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100365 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800366
367 /* Check there is space for header of KeyShareEntry
368 * - group (2 bytes)
369 * - key_exchange_length (2 bytes)
370 */
371 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
372 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100373 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800374 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800375 p += key_exchange_len;
376 if( ret != 0 )
377 return( ret );
378
379 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000380 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000382 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800383 }
384 else
385#endif /* MBEDTLS_ECDH_C */
386 if( 0 /* other KEMs? */ )
387 {
388 /* Do something */
389 }
390 else
391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
392
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000394 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800395 if( client_shares_len == 0)
396 {
397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800399 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800400 /* Write extension_type */
401 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
402 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800404 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800405 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800406
407 /* Update offered_group_id field */
408 ssl->handshake->offered_group_id = group_id;
409
410 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000411 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412
Xiaofei Baid25fab62021-12-02 06:36:27 +0000413 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414
415 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
416
417cleanup:
418
419 return( ret );
420}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800421
Jerry Yue1b9c292021-09-10 10:08:31 +0800422#if defined(MBEDTLS_ECDH_C)
423
Jerry Yuc068b662021-10-11 22:30:19 +0800424static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
425 const unsigned char *buf,
426 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800427{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100428 uint8_t *p = (uint8_t*)buf;
429 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800430
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100431 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
432 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
433 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800434
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100435 /* Check if key size is consistent with given buffer length. */
436 if ( peerkey_len > ( buf_len - 2 ) )
437 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800438
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100439 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100440 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100441 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800442
443 return( 0 );
444}
Jerry Yu4a173382021-10-11 21:45:31 +0800445#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800446
XiaokangQiand59be772022-01-24 10:12:51 +0000447/*
448 * ssl_tls13_parse_hrr_key_share_ext()
449 * Parse key_share extension in Hello Retry Request
450 *
451 * struct {
452 * NamedGroup selected_group;
453 * } KeyShareHelloRetryRequest;
454 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000455static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000456 const unsigned char *buf,
457 const unsigned char *end )
458{
XiaokangQianb851da82022-01-14 04:03:11 +0000459 const mbedtls_ecp_curve_info *curve_info = NULL;
460 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000461 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000462 int found = 0;
463
464 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
465 if( group_list == NULL )
466 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
467
468 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
469
470 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000472 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000474
475 /* Upon receipt of this extension in a HelloRetryRequest, the client
476 * MUST first verify that the selected_group field corresponds to a
477 * group which was provided in the "supported_groups" extension in the
478 * original ClientHello.
479 * The supported_group was based on the info in ssl->conf->group_list.
480 *
481 * If the server provided a key share that was not sent in the ClientHello
482 * then the client MUST abort the handshake with an "illegal_parameter" alert.
483 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000484 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000485 {
486 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000487 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000488 continue;
489
490 /* We found a match */
491 found = 1;
492 break;
493 }
494
495 /* Client MUST verify that the selected_group field does not
496 * correspond to a group which was provided in the "key_share"
497 * extension in the original ClientHello. If the server sent an
498 * HRR message with a key share already provided in the
499 * ClientHello then the client MUST abort the handshake with
500 * an "illegal_parameter" alert.
501 */
XiaokangQiand59be772022-01-24 10:12:51 +0000502 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000503 {
504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
505 MBEDTLS_SSL_PEND_FATAL_ALERT(
506 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
507 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
508 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
509 }
510
511 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000512 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000513
514 return( 0 );
515}
516
Jerry Yue1b9c292021-09-10 10:08:31 +0800517/*
Jerry Yub85277e2021-10-13 13:36:05 +0800518 * ssl_tls13_parse_key_share_ext()
519 * Parse key_share extension in Server Hello
520 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 * struct {
522 * KeyShareEntry server_share;
523 * } KeyShareServerHello;
524 * struct {
525 * NamedGroup group;
526 * opaque key_exchange<1..2^16-1>;
527 * } KeyShareEntry;
528 */
Jerry Yu4a173382021-10-11 21:45:31 +0800529static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800530 const unsigned char *buf,
531 const unsigned char *end )
532{
Jerry Yub85277e2021-10-13 13:36:05 +0800533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800534 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800535 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800536
Jerry Yu4a173382021-10-11 21:45:31 +0800537 /* ...
538 * NamedGroup group; (2 bytes)
539 * ...
540 */
541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
542 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800543 p += 2;
544
Jerry Yu4a173382021-10-11 21:45:31 +0800545 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800547 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800548 {
549 MBEDTLS_SSL_DEBUG_MSG( 1,
550 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800551 (unsigned) offered_group, (unsigned) group ) );
552 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
553 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
554 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800555 }
556
557#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800558 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800559 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100560 const mbedtls_ecp_curve_info *curve_info =
561 mbedtls_ecp_curve_info_from_tls_id( group );
562 if( curve_info == NULL )
563 {
564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
566 }
567
568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
569
Jerry Yuc068b662021-10-11 22:30:19 +0800570 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800571 if( ret != 0 )
572 return( ret );
573 }
Jerry Yub85277e2021-10-13 13:36:05 +0800574 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800575#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800576 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800577 {
578 /* Do something */
579 }
580 else
581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
582
583 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
584 return( ret );
585}
586
Jerry Yubc20bdd2021-08-24 15:59:48 +0800587#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
588
XiaokangQiand59be772022-01-24 10:12:51 +0000589/*
590 * ssl_tls13_parse_cookie_ext()
591 * Parse cookie extension in Hello Retry Request
592 *
593 * struct {
594 * opaque cookie<1..2^16-1>;
595 * } Cookie;
596 *
597 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
598 * extension to the client (this is an exception to the usual rule that
599 * the only extensions that may be sent are those that appear in the
600 * ClientHello). When sending the new ClientHello, the client MUST copy
601 * the contents of the extension received in the HelloRetryRequest into
602 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
603 * cookies in their initial ClientHello in subsequent connections.
604 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000605static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
606 const unsigned char *buf,
607 const unsigned char *end )
608{
XiaokangQian25c9c902022-02-08 10:49:53 +0000609 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000610 const unsigned char *p = buf;
611 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
612
613 /* Retrieve length field of cookie */
614 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
615 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
616 p += 2;
617
618 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
619 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
620
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000621 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000622 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000623 handshake->cookie = mbedtls_calloc( 1, cookie_len );
624 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000625 {
626 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000627 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000628 cookie_len ) );
629 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
630 }
631
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000632 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000633 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000634
635 return( 0 );
636}
XiaokangQian43550bd2022-01-21 04:32:58 +0000637
XiaokangQian0b64eed2022-01-27 10:36:51 +0000638static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000639 unsigned char *buf,
640 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000641 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000642{
643 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000644 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000645 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000646
XiaokangQianc02768a2022-02-10 07:31:25 +0000647 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000648 {
649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
650 return( 0 );
651 }
652
653 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000654 handshake->cookie,
655 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000656
XiaokangQianc02768a2022-02-10 07:31:25 +0000657 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000658
659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
660
XiaokangQian0b64eed2022-01-27 10:36:51 +0000661 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000662 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
663 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000664 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000665
666 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000667 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000668
XiaokangQianc02768a2022-02-10 07:31:25 +0000669 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000670
671 return( 0 );
672}
673
Ronald Cron3d580bf2022-02-18 17:24:56 +0100674int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
675 unsigned char *buf,
676 unsigned char *end,
677 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100678{
679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
680 unsigned char *p = buf;
681 size_t ext_len;
682
683 *out_len = 0;
684
685 /* Write supported_versions extension
686 *
687 * Supported Versions Extension is mandatory with TLS 1.3.
688 */
689 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
690 if( ret != 0 )
691 return( ret );
692 p += ext_len;
693
694 /* Echo the cookie if the server provided one in its preceding
695 * HelloRetryRequest message.
696 */
697 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
698 if( ret != 0 )
699 return( ret );
700 p += ext_len;
701
702#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
703 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
704 {
705 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
706 if( ret != 0 )
707 return( ret );
708 p += ext_len;
709 }
710#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
711
712 *out_len = p - buf;
713
714 return( 0 );
715}
716
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800717/*
Jerry Yu4a173382021-10-11 21:45:31 +0800718 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800719 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800720/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800721 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
722 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000723 * to indicate which message is expected and to be parsed next.
724 */
Jerry Yub85277e2021-10-13 13:36:05 +0800725#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
726#define SSL_SERVER_HELLO_COORDINATE_HRR 1
727static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
728 const unsigned char *buf,
729 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800730{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800731 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800732 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
733 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
734 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
735 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
736
737 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
738 *
Jerry Yu4a173382021-10-11 21:45:31 +0800739 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800740 * special value of the SHA-256 of "HelloRetryRequest".
741 *
742 * struct {
743 * ProtocolVersion legacy_version = 0x0303;
744 * Random random;
745 * opaque legacy_session_id_echo<0..32>;
746 * CipherSuite cipher_suite;
747 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800748 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800749 * } ServerHello;
750 *
751 */
Jerry Yub85277e2021-10-13 13:36:05 +0800752 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800753
754 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800755 {
Jerry Yub85277e2021-10-13 13:36:05 +0800756 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800757 }
758
Jerry Yub85277e2021-10-13 13:36:05 +0800759 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800760}
761
Jerry Yu745bb612021-10-13 22:01:04 +0800762/* Fetch and preprocess
763 * Returns a negative value on failure, and otherwise
764 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
765 * - SSL_SERVER_HELLO_COORDINATE_HRR
766 */
Jerry Yub85277e2021-10-13 13:36:05 +0800767static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
768 unsigned char **buf,
769 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800770{
Jerry Yu4a173382021-10-11 21:45:31 +0800771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800772
XiaokangQian355e09a2022-01-20 11:14:50 +0000773 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
774 MBEDTLS_SSL_HS_SERVER_HELLO,
775 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800776
Jerry Yub85277e2021-10-13 13:36:05 +0800777 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
778 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800779 {
Jerry Yu745bb612021-10-13 22:01:04 +0800780 case SSL_SERVER_HELLO_COORDINATE_HELLO:
781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
782 break;
783 case SSL_SERVER_HELLO_COORDINATE_HRR:
784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000785 /* If a client receives a second
786 * HelloRetryRequest in the same connection (i.e., where the ClientHello
787 * was itself in response to a HelloRetryRequest), it MUST abort the
788 * handshake with an "unexpected_message" alert.
789 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000790 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000791 {
792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
793 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
794 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
795 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
796 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000797 /*
798 * Clients must abort the handshake with an "illegal_parameter"
799 * alert if the HelloRetryRequest would not result in any change
800 * in the ClientHello.
801 * In a PSK only key exchange that what we expect.
802 */
803 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
804 {
805 MBEDTLS_SSL_DEBUG_MSG( 1,
806 ( "Unexpected HRR in pure PSK key exchange." ) );
807 MBEDTLS_SSL_PEND_FATAL_ALERT(
808 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
809 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
810 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
811 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000812
XiaokangQiand9e068e2022-01-18 06:23:32 +0000813 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000814
Jerry Yu745bb612021-10-13 22:01:04 +0800815 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800816 }
817
818cleanup:
819
820 return( ret );
821}
822
Jerry Yu4a173382021-10-11 21:45:31 +0800823static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
824 const unsigned char **buf,
825 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800826{
827 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800828 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800829
Jerry Yude4fb2c2021-09-19 18:05:08 +0800830 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800831 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800832
Jerry Yu4a173382021-10-11 21:45:31 +0800833 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800834
835 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800836 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
837 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800839 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
840 ssl->session_negotiate->id,
841 ssl->session_negotiate->id_len );
842 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800843 legacy_session_id_echo_len );
844
845 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
846 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
847
Jerry Yue1b9c292021-09-10 10:08:31 +0800848 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
849 }
850
Jerry Yu4a173382021-10-11 21:45:31 +0800851 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800852 *buf = p;
853
854 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800855 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800856 return( 0 );
857}
858
Jerry Yu4a173382021-10-11 21:45:31 +0800859static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
860 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800861{
Jerry Yu4a173382021-10-11 21:45:31 +0800862 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
863
Jerry Yue1b9c292021-09-10 10:08:31 +0800864 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800865 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800866 {
Jerry Yu4a173382021-10-11 21:45:31 +0800867 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800868 {
869 return( 1 );
870 }
871 }
872 return( 0 );
873}
874
875/* Parse ServerHello message and configure context
876 *
877 * struct {
878 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
879 * Random random;
880 * opaque legacy_session_id_echo<0..32>;
881 * CipherSuite cipher_suite;
882 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800883 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800884 * } ServerHello;
885 */
Jerry Yuc068b662021-10-11 22:30:19 +0800886static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
887 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000888 const unsigned char *end,
889 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800890{
Jerry Yub85277e2021-10-13 13:36:05 +0800891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800892 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000893 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800894 size_t extensions_len;
895 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800896 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800897 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000898 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +0000899 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800900
901 /*
902 * Check there is space for minimal fields
903 *
904 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800905 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 * - legacy_session_id_echo ( 1 byte ), minimum size
907 * - cipher_suite ( 2 bytes)
908 * - legacy_compression_method ( 1 byte )
909 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800910 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800911
912 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800913 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
914
Jerry Yu4a173382021-10-11 21:45:31 +0800915 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800916 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800917 * ...
918 * with ProtocolVersion defined as:
919 * uint16 ProtocolVersion;
920 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
922 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
923 {
924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
925 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
926 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000927 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
928 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 }
930 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800931
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800932 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800933 * Random random;
934 * ...
935 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800936 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800937 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000938 if( !is_hrr )
939 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000940 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000941 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
942 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
943 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
944 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800945 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800946
Jerry Yu4a173382021-10-11 21:45:31 +0800947 /* ...
948 * opaque legacy_session_id_echo<0..32>;
949 * ...
950 */
951 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800952 {
XiaokangQian52da5582022-01-26 09:49:29 +0000953 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000954 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 }
956
Jerry Yu4a173382021-10-11 21:45:31 +0800957 /* ...
958 * CipherSuite cipher_suite;
959 * ...
960 * with CipherSuite defined as:
961 * uint8 CipherSuite[2];
962 */
963 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800964 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
965 p += 2;
966
Jerry Yu4a173382021-10-11 21:45:31 +0800967
XiaokangQian355e09a2022-01-20 11:14:50 +0000968 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +0800969 /*
970 * Check whether this ciphersuite is supported and offered.
971 * Via the force_ciphersuite version we may have instructed the client
972 * to use a different ciphersuite.
973 */
Jerry Yu4a173382021-10-11 21:45:31 +0800974 if( ciphersuite_info == NULL ||
975 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800976 {
XiaokangQian52da5582022-01-26 09:49:29 +0000977 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +0800978 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000979 /*
XiaokangQian355e09a2022-01-20 11:14:50 +0000980 * If we received an HRR before and that the proposed selected
981 * ciphersuite in this server hello is not the same as the one
982 * proposed in the HRR, we abort the handshake and send an
983 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +0000984 */
XiaokangQian355e09a2022-01-20 11:14:50 +0000985 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
986 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +0000987 {
XiaokangQian52da5582022-01-26 09:49:29 +0000988 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +0000989 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000990
XiaokangQian52da5582022-01-26 09:49:29 +0000991 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +0000992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
994 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +0000995 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +0000996 }
Jerry Yue1b9c292021-09-10 10:08:31 +0800997
Jerry Yu4a173382021-10-11 21:45:31 +0800998 /* Configure ciphersuites */
999 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1000
XiaokangQian355e09a2022-01-20 11:14:50 +00001001 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001002 ssl->session_negotiate->ciphersuite = cipher_suite;
1003
Jerry Yue1b9c292021-09-10 10:08:31 +08001004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1005 cipher_suite, ciphersuite_info->name ) );
1006
1007#if defined(MBEDTLS_HAVE_TIME)
1008 ssl->session_negotiate->start = time( NULL );
1009#endif /* MBEDTLS_HAVE_TIME */
1010
Jerry Yu4a173382021-10-11 21:45:31 +08001011 /* ...
1012 * uint8 legacy_compression_method = 0;
1013 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001015 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 if( p[0] != 0 )
1017 {
Jerry Yub85277e2021-10-13 13:36:05 +08001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001019 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001020 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 }
1022 p++;
1023
Jerry Yub85277e2021-10-13 13:36:05 +08001024 /* ...
1025 * Extension extensions<6..2^16-1>;
1026 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001027 * struct {
1028 * ExtensionType extension_type; (2 bytes)
1029 * opaque extension_data<0..2^16-1>;
1030 * } Extension;
1031 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001032 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001033 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001035
Jerry Yu4a173382021-10-11 21:45:31 +08001036 /* Check extensions do not go beyond the buffer of data. */
1037 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1038 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001039
Jerry Yu4a173382021-10-11 21:45:31 +08001040 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001041
Jerry Yu4a173382021-10-11 21:45:31 +08001042 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 {
1044 unsigned int extension_type;
1045 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001046 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001047
Jerry Yu4a173382021-10-11 21:45:31 +08001048 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001049 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1050 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1051 p += 4;
1052
Jerry Yu4a173382021-10-11 21:45:31 +08001053 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001054 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001055
1056 switch( extension_type )
1057 {
XiaokangQianb851da82022-01-14 04:03:11 +00001058 case MBEDTLS_TLS_EXT_COOKIE:
1059
XiaokangQiand9e068e2022-01-18 06:23:32 +00001060 if( !is_hrr )
1061 {
XiaokangQian52da5582022-01-26 09:49:29 +00001062 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001063 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001064 }
1065
XiaokangQian43550bd2022-01-21 04:32:58 +00001066 ret = ssl_tls13_parse_cookie_ext( ssl,
1067 p, extension_data_end );
1068 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001069 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001070 MBEDTLS_SSL_DEBUG_RET( 1,
1071 "ssl_tls13_parse_cookie_ext",
1072 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001073 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001074 }
XiaokangQianb851da82022-01-14 04:03:11 +00001075 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001076
Jerry Yue1b9c292021-09-10 10:08:31 +08001077 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001078 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 MBEDTLS_SSL_DEBUG_MSG( 3,
1080 ( "found supported_versions extension" ) );
1081
Jerry Yuc068b662021-10-11 22:30:19 +08001082 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001083 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001084 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001086 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 break;
1088
1089 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001092
XiaokangQian52da5582022-01-26 09:49:29 +00001093 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001094 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001095
1096#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1097 case MBEDTLS_TLS_EXT_KEY_SHARE:
1098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001099 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1100 {
XiaokangQian52da5582022-01-26 09:49:29 +00001101 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001102 goto cleanup;
1103 }
1104
XiaokangQian53f20b72022-01-18 10:47:33 +00001105 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001106 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001107 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001108 else
XiaokangQianb851da82022-01-14 04:03:11 +00001109 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001110 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001111 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001112 {
1113 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001114 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001115 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001116 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 }
1118 break;
1119#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1120
1121 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001122 MBEDTLS_SSL_DEBUG_MSG(
1123 3,
1124 ( "unknown extension found: %u ( ignoring )",
1125 extension_type ) );
1126
XiaokangQian52da5582022-01-26 09:49:29 +00001127 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001128 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001129 }
1130
1131 p += extension_data_len;
1132 }
1133
XiaokangQian78b1fa72022-01-19 06:56:30 +00001134 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001135 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001137 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001138 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001139 }
1140
XiaokangQiand59be772022-01-24 10:12:51 +00001141cleanup:
1142
XiaokangQian52da5582022-01-26 09:49:29 +00001143 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001144 {
1145 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1146 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1147 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1148 }
XiaokangQian52da5582022-01-26 09:49:29 +00001149 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001150 {
1151 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1152 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1153 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1154 }
1155 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001156}
1157
XiaokangQian355e09a2022-01-20 11:14:50 +00001158static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001159{
Jerry Yub85277e2021-10-13 13:36:05 +08001160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001161 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001162 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001163 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001164
Jerry Yub85277e2021-10-13 13:36:05 +08001165 /* Determine the key exchange mode:
1166 * 1) If both the pre_shared_key and key_share extensions were received
1167 * then the key exchange mode is PSK with EPHEMERAL.
1168 * 2) If only the pre_shared_key extension was received then the key
1169 * exchange mode is PSK-only.
1170 * 3) If only the key_share extension was received then the key
1171 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001172 */
Jerry Yub85277e2021-10-13 13:36:05 +08001173 switch( handshake->extensions_present &
1174 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001175 {
Jerry Yu745bb612021-10-13 22:01:04 +08001176 /* Only the pre_shared_key extension was received */
1177 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001178 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001179 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001180
Jerry Yu745bb612021-10-13 22:01:04 +08001181 /* Only the key_share extension was received */
1182 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001183 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001184 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001185
Jerry Yu745bb612021-10-13 22:01:04 +08001186 /* Both the pre_shared_key and key_share extensions were received */
1187 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001188 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001189 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001190
Jerry Yu745bb612021-10-13 22:01:04 +08001191 /* Neither pre_shared_key nor key_share extension was received */
1192 default:
1193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1194 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1195 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001196 }
1197
1198 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1199 *
1200 * TODO: We don't have to do this in case we offered 0-RTT and the
1201 * server accepted it. In this case, we could skip generating
1202 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001203 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001204 if( ret != 0 )
1205 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001207 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001208 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001209 }
1210
1211 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001212 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001213 if( ret != 0 )
1214 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001216 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001217 }
1218
1219 /* Next evolution in key schedule: Establish handshake secret and
1220 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001221 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001222 if( ret != 0 )
1223 {
Jerry Yuc068b662021-10-11 22:30:19 +08001224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1225 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001226 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001227 }
1228
Jerry Yub85277e2021-10-13 13:36:05 +08001229 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001230 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001231 {
1232 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1233 goto cleanup;
1234 }
Jerry Yu0b177842021-09-05 19:41:30 +08001235
1236 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1237 ssl->conf->endpoint,
1238 ssl->session_negotiate->ciphersuite,
1239 &traffic_keys,
1240 ssl );
1241 if( ret != 0 )
1242 {
1243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001244 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001245 }
1246
Jerry Yub85277e2021-10-13 13:36:05 +08001247 handshake->transform_handshake = transform_handshake;
1248 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001249
1250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1251 ssl->session_in = ssl->session_negotiate;
1252
1253 /*
1254 * State machine update
1255 */
1256 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1257
Jerry Yu4a173382021-10-11 21:45:31 +08001258cleanup:
1259
Jerry Yu0b177842021-09-05 19:41:30 +08001260 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001261 if( ret != 0 )
1262 {
Jerry Yub85277e2021-10-13 13:36:05 +08001263 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001264
Jerry Yu4a173382021-10-11 21:45:31 +08001265 MBEDTLS_SSL_PEND_FATAL_ALERT(
1266 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1267 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1268 }
1269 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001270}
1271
XiaokangQian355e09a2022-01-20 11:14:50 +00001272static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001273{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001274#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1276
XiaokangQian647719a2021-12-07 09:16:29 +00001277#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1278 /* If not offering early data, the client sends a dummy CCS record
1279 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001280 * its second ClientHello or before its encrypted handshake flight.
1281 */
XiaokangQian647719a2021-12-07 09:16:29 +00001282 mbedtls_ssl_handshake_set_state( ssl,
1283 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1284#else
1285 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1286#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1287
XiaokangQian78b1fa72022-01-19 06:56:30 +00001288 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001289
XiaokangQian78b1fa72022-01-19 06:56:30 +00001290 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001291 * We are going to re-generate a shared secret corresponding to the group
1292 * selected by the server, which is different from the group for which we
1293 * generated a shared secret in the first client hello.
1294 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001295 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001296 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001297 if( ret != 0 )
1298 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001299#else
1300 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001301#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001302
1303 return( 0 );
1304}
1305
Jerry Yue1b9c292021-09-10 10:08:31 +08001306/*
Jerry Yu4a173382021-10-11 21:45:31 +08001307 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001308 * Handler for MBEDTLS_SSL_SERVER_HELLO
1309 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001310static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001311{
Jerry Yu4a173382021-10-11 21:45:31 +08001312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001313 unsigned char *buf = NULL;
1314 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001315 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001316
1317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1318
1319 /* Coordination step
1320 * - Fetch record
1321 * - Make sure it's either a ServerHello or a HRR.
1322 * - Switch processing routine in case of HRR
1323 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001324 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1325 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1326
XiaokangQian16acd4b2022-01-14 07:35:47 +00001327 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001328 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001329 goto cleanup;
1330 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001331 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001332
XiaokangQianb851da82022-01-14 04:03:11 +00001333 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001334 buf + buf_len,
1335 is_hrr ) );
1336 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001337 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1338
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001339 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1340 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001341
XiaokangQiand9e068e2022-01-18 06:23:32 +00001342 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001343 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001344 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001345 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001346
1347cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1349 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001350 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001351}
1352
1353/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001354 *
1355 * EncryptedExtensions message
1356 *
1357 * The EncryptedExtensions message contains any extensions which
1358 * should be protected, i.e., any which are not needed to establish
1359 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001360 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001361
1362/*
1363 * Overview
1364 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001365
1366/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001367static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001368
XiaokangQian97799ac2021-10-11 10:05:54 +00001369static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1370 const unsigned char *buf,
1371 const unsigned char *end );
1372static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001373
1374/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001375 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001376 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001377static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001378{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001379 int ret;
1380 unsigned char *buf;
1381 size_t buf_len;
1382
1383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1384
Xiaofei Bai746f9482021-11-12 08:53:56 +00001385 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001386 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001387 &buf, &buf_len ) );
1388
1389 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001390 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001391 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001392
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001393 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1394 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001395
XiaokangQian97799ac2021-10-11 10:05:54 +00001396 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397
1398cleanup:
1399
1400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1401 return( ret );
1402
1403}
1404
XiaokangQian08da26c2021-10-09 10:12:11 +00001405/* Parse EncryptedExtensions message
1406 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001407 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001408 * } EncryptedExtensions;
1409 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001410static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1411 const unsigned char *buf,
1412 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001413{
1414 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001415 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001416 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001417 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001418
XiaokangQian08da26c2021-10-09 10:12:11 +00001419 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001420 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001421 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001422
XiaokangQian97799ac2021-10-11 10:05:54 +00001423 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001424 extensions_end = p + extensions_len;
1425 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001426
XiaokangQian8db25ff2021-10-13 05:56:18 +00001427 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001428 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001429 unsigned int extension_type;
1430 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001431
XiaokangQian08da26c2021-10-09 10:12:11 +00001432 /*
1433 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001434 * ExtensionType extension_type; (2 bytes)
1435 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001436 * } Extension;
1437 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001438 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001439 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1440 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1441 p += 4;
1442
XiaokangQian8db25ff2021-10-13 05:56:18 +00001443 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001444
XiaokangQian97799ac2021-10-11 10:05:54 +00001445 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001447 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001448 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001449 switch( extension_type )
1450 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451
XiaokangQian08da26c2021-10-09 10:12:11 +00001452 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1454 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001455
lhuang0486cacac2022-01-21 07:34:27 -08001456#if defined(MBEDTLS_SSL_ALPN)
1457 case MBEDTLS_TLS_EXT_ALPN:
1458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1459
1460 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1461 {
1462 return( ret );
1463 }
1464
1465 break;
1466#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001467 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001468 MBEDTLS_SSL_DEBUG_MSG(
1469 3, ( "unsupported extension found: %u ", extension_type) );
1470 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001471 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001472 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1473 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001474 }
1475
XiaokangQian08da26c2021-10-09 10:12:11 +00001476 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001477 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001478
XiaokangQian97799ac2021-10-11 10:05:54 +00001479 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001480 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001481 {
1482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001483 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001484 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001485 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001486 }
1487
1488 return( ret );
1489}
1490
XiaokangQian97799ac2021-10-11 10:05:54 +00001491static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492{
Jerry Yua93ac112021-10-27 16:31:48 +08001493#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001494 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001495 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1496 else
Jerry Yud2674312021-10-29 10:08:19 +08001497 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001498#else
1499 ((void) ssl);
1500 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1501#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001502 return( 0 );
1503}
1504
Jerry Yua93ac112021-10-27 16:31:48 +08001505#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001506/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001507 *
1508 * STATE HANDLING: CertificateRequest
1509 *
Jerry Yud2674312021-10-29 10:08:19 +08001510 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001511#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1512#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001513/* Coordination:
1514 * Deals with the ambiguity of not knowing if a CertificateRequest
1515 * will be sent. Returns a negative code on failure, or
1516 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1517 * - SSL_CERTIFICATE_REQUEST_SKIP
1518 * indicating if a Certificate Request is expected or not.
1519 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001520static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001521{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001523
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001524 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001525 {
1526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1527 return( SSL_CERTIFICATE_REQUEST_SKIP );
1528 }
1529
1530 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001531 {
1532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1533 return( ret );
1534 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001535 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001536
1537 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1538 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1539 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001540 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001541 }
1542
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001543 return( SSL_CERTIFICATE_REQUEST_SKIP );
1544}
1545
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001546/*
1547 * ssl_tls13_parse_certificate_request()
1548 * Parse certificate request
1549 * struct {
1550 * opaque certificate_request_context<0..2^8-1>;
1551 * Extension extensions<2..2^16-1>;
1552 * } CertificateRequest;
1553 */
1554static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1555 const unsigned char *buf,
1556 const unsigned char *end )
1557{
1558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1559 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001560 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001561 size_t extensions_len = 0;
1562 const unsigned char *extensions_end;
1563 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001565 /* ...
1566 * opaque certificate_request_context<0..2^8-1>
1567 * ...
1568 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001569 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1570 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001571 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001572
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001573 if( certificate_request_context_len > 0 )
1574 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001575 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001576 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1577 p, certificate_request_context_len );
1578
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001579 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001580 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001581 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001582 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001583 {
1584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1585 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1586 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001587 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001588 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001589 p += certificate_request_context_len;
1590 }
1591
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001592 /* ...
1593 * Extension extensions<2..2^16-1>;
1594 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001595 */
1596 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1597 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1598 p += 2;
1599
1600 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1601 extensions_end = p + extensions_len;
1602
1603 while( p < extensions_end )
1604 {
1605 unsigned int extension_type;
1606 size_t extension_data_len;
1607
1608 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1609 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1610 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1611 p += 4;
1612
1613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1614
1615 switch( extension_type )
1616 {
1617 case MBEDTLS_TLS_EXT_SIG_ALG:
1618 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001619 ( "found signature algorithms extension" ) );
1620 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001621 p + extension_data_len );
1622 if( ret != 0 )
1623 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001624 if( ! sig_alg_ext_found )
1625 sig_alg_ext_found = 1;
1626 else
1627 {
1628 MBEDTLS_SSL_DEBUG_MSG( 3,
1629 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001630 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001631 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001632 break;
1633
1634 default:
1635 MBEDTLS_SSL_DEBUG_MSG(
1636 3,
1637 ( "unknown extension found: %u ( ignoring )",
1638 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001639 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001641 p += extension_data_len;
1642 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001643 /* Check that we consumed all the message. */
1644 if( p != end )
1645 {
1646 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001647 ( "CertificateRequest misaligned" ) );
1648 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001649 }
1650 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001651 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001652 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001653 MBEDTLS_SSL_DEBUG_MSG( 3,
1654 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001655 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001656 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001657
Jerry Yu7840f812022-01-29 10:26:51 +08001658 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001660
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001661decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001662 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1663 MBEDTLS_ERR_SSL_DECODE_ERROR );
1664 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001667/*
1668 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1669 */
1670static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001671{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001673
1674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1675
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001676 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1677
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1679 {
1680 unsigned char *buf;
1681 size_t buf_len;
1682
1683 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1684 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1685 &buf, &buf_len ) );
1686
1687 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1688 buf, buf + buf_len ) );
1689
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001690 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1691 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001693 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694 {
1695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001696 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001697 }
1698 else
1699 {
1700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001701 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1702 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001703 }
1704
1705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001706 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001707
Jerry Yud2674312021-10-29 10:08:19 +08001708 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1709
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001710cleanup:
1711
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1713 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001714}
1715
1716/*
Jerry Yu687101b2021-09-14 16:03:56 +08001717 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1718 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001719static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001720{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001721 int ret;
1722
1723 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001724 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001725 return( ret );
1726
Jerry Yu687101b2021-09-14 16:03:56 +08001727 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1728 return( 0 );
1729}
1730
1731/*
1732 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1733 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001734static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001735{
Jerry Yu30b071c2021-09-12 20:16:03 +08001736 int ret;
1737
1738 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1739 if( ret != 0 )
1740 return( ret );
1741
Jerry Yu687101b2021-09-14 16:03:56 +08001742 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1743 return( 0 );
1744}
Jerry Yua93ac112021-10-27 16:31:48 +08001745#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001746
Jerry Yu687101b2021-09-14 16:03:56 +08001747/*
1748 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1749 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001750static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001751{
XiaokangQianac0385c2021-11-03 06:40:11 +00001752 int ret;
1753
XiaokangQianc5c39d52021-11-09 11:55:10 +00001754 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001755 if( ret != 0 )
1756 return( ret );
1757
Ronald Cron49ad6192021-11-24 16:25:31 +01001758#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1759 mbedtls_ssl_handshake_set_state(
1760 ssl,
1761 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1762#else
Jerry Yuca133a32022-02-15 14:22:05 +08001763 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001764#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001765
XiaokangQianac0385c2021-11-03 06:40:11 +00001766 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001767}
1768
1769/*
Jerry Yu566c7812022-01-26 15:41:22 +08001770 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1771 */
1772static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1773{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001774 int non_empty_certificate_msg = 0;
1775
Jerry Yu5cc35062022-01-28 16:16:08 +08001776 MBEDTLS_SSL_DEBUG_MSG( 1,
1777 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001778 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001779
Ronald Cron9df7c802022-03-08 18:38:54 +01001780#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001781 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001782 {
1783 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1784 if( ret != 0 )
1785 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001786
Ronald Cron7a94aca2022-03-09 07:44:27 +01001787 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1788 non_empty_certificate_msg = 1;
1789 }
1790 else
1791 {
1792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1793 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001794#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001795
Ronald Cron7a94aca2022-03-09 07:44:27 +01001796 if( non_empty_certificate_msg )
1797 {
1798 mbedtls_ssl_handshake_set_state( ssl,
1799 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1800 }
1801 else
1802 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1803
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001804 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001805}
1806
Ronald Cron9df7c802022-03-08 18:38:54 +01001807#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001808/*
1809 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1810 */
1811static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1812{
Ronald Crona8b38872022-03-09 07:59:25 +01001813 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1814
1815 if( ret == 0 )
1816 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1817
1818 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001819}
Jerry Yu90f152d2022-01-29 22:12:42 +08001820#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001821
1822/*
Jerry Yu687101b2021-09-14 16:03:56 +08001823 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1824 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001825static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001826{
XiaokangQian0fa66432021-11-15 03:33:57 +00001827 int ret;
1828
1829 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1830 if( ret != 0 )
1831 return( ret );
1832
1833 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1834 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001835}
1836
1837/*
1838 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1839 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001840static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001841{
Jerry Yu378254d2021-10-30 21:44:47 +08001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001843 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001844 return( 0 );
1845}
1846
1847/*
1848 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1849 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001850static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001851{
Jerry Yu378254d2021-10-30 21:44:47 +08001852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1853 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1854
1855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1856 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1857
1858 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1859
1860 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1861 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001862}
1863
Jerry Yu92c6b402021-08-27 16:59:09 +08001864int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001865{
Jerry Yu92c6b402021-08-27 16:59:09 +08001866 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001867
Jerry Yue3b34122021-09-28 17:53:35 +08001868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1869 mbedtls_ssl_states_str( ssl->state ),
1870 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001871
1872 switch( ssl->state )
1873 {
1874 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001875 * ssl->state is initialized as HELLO_REQUEST. It is the same
1876 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001877 */
1878 case MBEDTLS_SSL_HELLO_REQUEST:
1879 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001880 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001881 break;
1882
1883 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001884 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001885 break;
1886
1887 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001888 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001889 break;
1890
Jerry Yua93ac112021-10-27 16:31:48 +08001891#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001892 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1893 ret = ssl_tls13_process_certificate_request( ssl );
1894 break;
1895
Jerry Yu687101b2021-09-14 16:03:56 +08001896 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001897 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001898 break;
1899
1900 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001901 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001902 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001903#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001904
1905 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001906 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001907 break;
1908
Jerry Yu566c7812022-01-26 15:41:22 +08001909 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1910 ret = ssl_tls13_write_client_certificate( ssl );
1911 break;
1912
Ronald Cron9df7c802022-03-08 18:38:54 +01001913#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001914 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1915 ret = ssl_tls13_write_client_certificate_verify( ssl );
1916 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001917#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001918
Jerry Yu687101b2021-09-14 16:03:56 +08001919 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001920 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001921 break;
1922
1923 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001924 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001925 break;
1926
1927 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001928 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001929 break;
1930
Ronald Cron49ad6192021-11-24 16:25:31 +01001931 /*
1932 * Injection of dummy-CCS's for middlebox compatibility
1933 */
1934#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001935 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001936 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1937 if( ret == 0 )
1938 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1939 break;
1940
1941 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1942 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1943 if( ret == 0 )
1944 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001945 break;
1946#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1947
Jerry Yu92c6b402021-08-27 16:59:09 +08001948 default:
1949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1951 }
1952
1953 return( ret );
1954}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001955
Jerry Yufb4b6472022-01-27 15:03:26 +08001956#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001957
Jerry Yufb4b6472022-01-27 15:03:26 +08001958