blob: fc8ffa7cb2702a403baa2bbb25adf82269aa2456 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
52
Xiaofei Baid25fab62021-12-02 06:36:27 +000053 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Jerry Yu159c5a02021-08-31 12:51:25 +080055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu388bd0d2021-09-15 18:41:02 +080057 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080058 * - extension_type (2 bytes)
59 * - extension_data_length (2 bytes)
60 * - versions_length (1 byte )
61 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080062 */
Jerry Yu92c6b402021-08-27 16:59:09 +080063 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
64
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080065 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080067
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080068 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080069 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080073 *p++ = 0x2;
74
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080076 *
Jerry Yu0c63af62021-09-02 12:59:12 +080077 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080080 */
Jerry Yueecfbf02021-08-30 18:32:07 +080081 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
82 ssl->conf->max_minor_ver,
83 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080086 ssl->conf->max_major_ver,
87 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080088
Xiaofei Baid25fab62021-12-02 06:36:27 +000089 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Jerry Yub85277e2021-10-13 13:36:05 +0800100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
101 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800102 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
111 return( 0 );
112}
113
lhuang0486cacac2022-01-21 07:34:27 -0800114#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800115static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
116 const unsigned char *buf, size_t len )
117{
118 size_t list_len, name_len;
119 const unsigned char *p = buf;
120 const unsigned char *end = buf + len;
121
122 /* If we didn't send it, the server shouldn't send it */
123 if( ssl->conf->alpn_list == NULL )
124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125
126 /*
127 * opaque ProtocolName<1..2^8-1>;
128 *
129 * struct {
130 * ProtocolName protocol_name_list<2..2^16-1>
131 * } ProtocolNameList;
132 *
133 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
134 */
135
136 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
137 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
138
139 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
140 p += 2;
141 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
142
143 name_len = *p++;
144 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
145
146 /* Check that the server chosen protocol was in our list and save it */
147 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
148 {
149 if( name_len == strlen( *alpn ) &&
150 memcmp( buf + 3, *alpn, name_len ) == 0 )
151 {
152 ssl->alpn_chosen = *alpn;
153 return( 0 );
154 }
155 }
156
157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
158}
159#endif /* MBEDTLS_SSL_ALPN */
160
XiaokangQian16acd4b2022-01-14 07:35:47 +0000161static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000162{
163 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100164
XiaokangQian647719a2021-12-07 09:16:29 +0000165 if( group_id == 0 )
166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
167
XiaokangQian355e09a2022-01-20 11:14:50 +0000168#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000169 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000170 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
173
174 /* Destroy generated private key. */
175 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
176 if( status != PSA_SUCCESS )
177 {
178 ret = psa_ssl_status_to_mbedtls( status );
179 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
180 return( ret );
181 }
182
183 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000184 return( 0 );
185 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000186 else
187#endif /* MBEDTLS_ECDH_C */
188 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000189 {
190 /* Do something */
191 }
192
193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
194}
195
196/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800197 * Functions for writing key_share extension.
198 */
199#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800200static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800201 mbedtls_ssl_context *ssl,
202 uint16_t named_group,
203 unsigned char *buf,
204 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000205 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800206{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100207 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
208 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
209 psa_key_attributes_t key_attributes;
210 size_t own_pubkey_len;
211 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
212 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100216 /* Convert EC group to PSA key type. */
217 if( ( handshake->ecdh_psa_type =
218 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
219 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100221 if( ecdh_bits > 0xffff )
222 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
223 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 key_attributes = psa_key_attributes_init();
226 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
227 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
228 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
229 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100230
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100231 /* Generate ECDH private key. */
232 status = psa_generate_key( &key_attributes,
233 &handshake->ecdh_psa_privkey );
234 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800235 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100236 ret = psa_ssl_status_to_mbedtls( status );
237 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100239
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240 }
241
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100242 /* Export the public part of the ECDH private key from PSA. */
243 status = psa_export_public_key( handshake->ecdh_psa_privkey,
244 buf, (size_t)( end - buf ),
245 &own_pubkey_len );
246 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100248 ret = psa_ssl_status_to_mbedtls( status );
249 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100251
Jerry Yu56fc07f2021-09-01 17:48:49 +0800252 }
253
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100254 if( own_pubkey_len > (size_t)( end - buf ) )
255 {
256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
257 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
258 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100259
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100260 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100261
Jerry Yu75336352021-09-01 15:59:36 +0800262 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800263}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264#endif /* MBEDTLS_ECDH_C */
265
Jerry Yub60e3cf2021-09-08 16:41:02 +0800266static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
267 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268{
269 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
270
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100273 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800274 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100275 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800276 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
277
Brett Warren14efd332021-10-06 09:32:11 +0100278 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000280 const mbedtls_ecp_curve_info *curve_info;
281 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
282 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100283 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800284 {
Brett Warren14efd332021-10-06 09:32:11 +0100285 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800286 return( 0 );
287 }
288 }
289#else
290 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800291 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292#endif /* MBEDTLS_ECDH_C */
293
294 /*
295 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800296 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 */
298
299 return( ret );
300}
301
302/*
303 * ssl_tls13_write_key_share_ext
304 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800305 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306 *
307 * struct {
308 * NamedGroup group;
309 * opaque key_exchange<1..2^16-1>;
310 * } KeyShareEntry;
311 * struct {
312 * KeyShareEntry client_shares<0..2^16-1>;
313 * } KeyShareClientHello;
314 */
315static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
316 unsigned char *buf,
317 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000318 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319{
320 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000321 unsigned char *client_shares; /* Start of client_shares */
322 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
325
Xiaofei Baid25fab62021-12-02 06:36:27 +0000326 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327
Jerry Yub60e3cf2021-09-08 16:41:02 +0800328 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 * - extension_type (2 bytes)
330 * - extension_data_length (2 bytes)
331 * - client_shares_length (2 bytes)
332 */
333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
334 p += 6;
335
336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
337
338 /* HRR could already have requested something else. */
339 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800340 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
341 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344 &group_id ) );
345 }
346
347 /*
348 * Dispatch to type-specific key generation function.
349 *
350 * So far, we're only supporting ECDHE. With the introduction
351 * of PQC KEMs, we'll want to have multiple branches, one per
352 * type of KEM, and dispatch to the corresponding crypto. And
353 * only one key share entry is allowed.
354 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000355 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800357 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800359 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000360 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100362 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800363
364 /* Check there is space for header of KeyShareEntry
365 * - group (2 bytes)
366 * - key_exchange_length (2 bytes)
367 */
368 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
369 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100370 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800371 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800372 p += key_exchange_len;
373 if( ret != 0 )
374 return( ret );
375
376 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000377 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800378 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000379 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800380 }
381 else
382#endif /* MBEDTLS_ECDH_C */
383 if( 0 /* other KEMs? */ )
384 {
385 /* Do something */
386 }
387 else
388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
389
Jerry Yub60e3cf2021-09-08 16:41:02 +0800390 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000391 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800392 if( client_shares_len == 0)
393 {
394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800396 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397 /* Write extension_type */
398 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
399 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800400 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800401 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800402 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800403
404 /* Update offered_group_id field */
405 ssl->handshake->offered_group_id = group_id;
406
407 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000408 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800409
Xiaofei Baid25fab62021-12-02 06:36:27 +0000410 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800411
412 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
413
414cleanup:
415
416 return( ret );
417}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800418
Jerry Yue1b9c292021-09-10 10:08:31 +0800419#if defined(MBEDTLS_ECDH_C)
420
Jerry Yuc068b662021-10-11 22:30:19 +0800421static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
422 const unsigned char *buf,
423 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800424{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100425 uint8_t *p = (uint8_t*)buf;
426 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800427
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100428 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
429 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
430 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800431
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100432 /* Check if key size is consistent with given buffer length. */
433 if ( peerkey_len > ( buf_len - 2 ) )
434 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800435
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100436 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100437 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100438 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800439
440 return( 0 );
441}
Jerry Yu4a173382021-10-11 21:45:31 +0800442#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800443
XiaokangQiand59be772022-01-24 10:12:51 +0000444/*
445 * ssl_tls13_parse_hrr_key_share_ext()
446 * Parse key_share extension in Hello Retry Request
447 *
448 * struct {
449 * NamedGroup selected_group;
450 * } KeyShareHelloRetryRequest;
451 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000452static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000453 const unsigned char *buf,
454 const unsigned char *end )
455{
XiaokangQianb851da82022-01-14 04:03:11 +0000456 const mbedtls_ecp_curve_info *curve_info = NULL;
457 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000458 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000459 int found = 0;
460
461 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
462 if( group_list == NULL )
463 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
464
465 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
466
467 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000468 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000469 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000471
472 /* Upon receipt of this extension in a HelloRetryRequest, the client
473 * MUST first verify that the selected_group field corresponds to a
474 * group which was provided in the "supported_groups" extension in the
475 * original ClientHello.
476 * The supported_group was based on the info in ssl->conf->group_list.
477 *
478 * If the server provided a key share that was not sent in the ClientHello
479 * then the client MUST abort the handshake with an "illegal_parameter" alert.
480 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000481 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000482 {
483 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000484 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000485 continue;
486
487 /* We found a match */
488 found = 1;
489 break;
490 }
491
492 /* Client MUST verify that the selected_group field does not
493 * correspond to a group which was provided in the "key_share"
494 * extension in the original ClientHello. If the server sent an
495 * HRR message with a key share already provided in the
496 * ClientHello then the client MUST abort the handshake with
497 * an "illegal_parameter" alert.
498 */
XiaokangQiand59be772022-01-24 10:12:51 +0000499 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000500 {
501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
502 MBEDTLS_SSL_PEND_FATAL_ALERT(
503 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
504 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
505 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
506 }
507
508 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000509 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000510
511 return( 0 );
512}
513
Jerry Yue1b9c292021-09-10 10:08:31 +0800514/*
Jerry Yub85277e2021-10-13 13:36:05 +0800515 * ssl_tls13_parse_key_share_ext()
516 * Parse key_share extension in Server Hello
517 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800518 * struct {
519 * KeyShareEntry server_share;
520 * } KeyShareServerHello;
521 * struct {
522 * NamedGroup group;
523 * opaque key_exchange<1..2^16-1>;
524 * } KeyShareEntry;
525 */
Jerry Yu4a173382021-10-11 21:45:31 +0800526static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800527 const unsigned char *buf,
528 const unsigned char *end )
529{
Jerry Yub85277e2021-10-13 13:36:05 +0800530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800531 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800532 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800533
Jerry Yu4a173382021-10-11 21:45:31 +0800534 /* ...
535 * NamedGroup group; (2 bytes)
536 * ...
537 */
538 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
539 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800540 p += 2;
541
Jerry Yu4a173382021-10-11 21:45:31 +0800542 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800543 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800544 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800545 {
546 MBEDTLS_SSL_DEBUG_MSG( 1,
547 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800548 (unsigned) offered_group, (unsigned) group ) );
549 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
550 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
551 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800552 }
553
554#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800555 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800556 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100557 const mbedtls_ecp_curve_info *curve_info =
558 mbedtls_ecp_curve_info_from_tls_id( group );
559 if( curve_info == NULL )
560 {
561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
563 }
564
565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
566
Jerry Yuc068b662021-10-11 22:30:19 +0800567 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800568 if( ret != 0 )
569 return( ret );
570 }
Jerry Yub85277e2021-10-13 13:36:05 +0800571 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800572#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800573 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800574 {
575 /* Do something */
576 }
577 else
578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
579
580 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
581 return( ret );
582}
583
XiaokangQiand59be772022-01-24 10:12:51 +0000584/*
585 * ssl_tls13_parse_cookie_ext()
586 * Parse cookie extension in Hello Retry Request
587 *
588 * struct {
589 * opaque cookie<1..2^16-1>;
590 * } Cookie;
591 *
592 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
593 * extension to the client (this is an exception to the usual rule that
594 * the only extensions that may be sent are those that appear in the
595 * ClientHello). When sending the new ClientHello, the client MUST copy
596 * the contents of the extension received in the HelloRetryRequest into
597 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
598 * cookies in their initial ClientHello in subsequent connections.
599 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000600static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
601 const unsigned char *buf,
602 const unsigned char *end )
603{
XiaokangQian25c9c902022-02-08 10:49:53 +0000604 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000605 const unsigned char *p = buf;
606 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
607
608 /* Retrieve length field of cookie */
609 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
610 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
611 p += 2;
612
613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
614 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
615
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000616 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000617 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000618 handshake->cookie = mbedtls_calloc( 1, cookie_len );
619 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000620 {
621 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000622 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000623 cookie_len ) );
624 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
625 }
626
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000627 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000628 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000629
630 return( 0 );
631}
XiaokangQian43550bd2022-01-21 04:32:58 +0000632
XiaokangQian0b64eed2022-01-27 10:36:51 +0000633static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000634 unsigned char *buf,
635 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000636 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000637{
638 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000639 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000640 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000641
XiaokangQianc02768a2022-02-10 07:31:25 +0000642 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000643 {
644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
645 return( 0 );
646 }
647
648 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000649 handshake->cookie,
650 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000651
XiaokangQianc02768a2022-02-10 07:31:25 +0000652 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000653
654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
655
XiaokangQian0b64eed2022-01-27 10:36:51 +0000656 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000657 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
658 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000659 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000660
661 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000662 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000663
XiaokangQianc02768a2022-02-10 07:31:25 +0000664 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000665
666 return( 0 );
667}
668
Ronald Cron3d580bf2022-02-18 17:24:56 +0100669int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
670 unsigned char *buf,
671 unsigned char *end,
672 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100673{
674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
675 unsigned char *p = buf;
676 size_t ext_len;
677
678 *out_len = 0;
679
680 /* Write supported_versions extension
681 *
682 * Supported Versions Extension is mandatory with TLS 1.3.
683 */
684 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
685 if( ret != 0 )
686 return( ret );
687 p += ext_len;
688
689 /* Echo the cookie if the server provided one in its preceding
690 * HelloRetryRequest message.
691 */
692 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
693 if( ret != 0 )
694 return( ret );
695 p += ext_len;
696
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100697 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
698 {
699 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
700 if( ret != 0 )
701 return( ret );
702 p += ext_len;
703 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100704
705 *out_len = p - buf;
706
707 return( 0 );
708}
709
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800710/*
Jerry Yu4a173382021-10-11 21:45:31 +0800711 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800712 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800713/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800714 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
715 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000716 * to indicate which message is expected and to be parsed next.
717 */
Jerry Yub85277e2021-10-13 13:36:05 +0800718#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
719#define SSL_SERVER_HELLO_COORDINATE_HRR 1
720static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
721 const unsigned char *buf,
722 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800723{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800724 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800725 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
726 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
727 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
728 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
729
730 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
731 *
Jerry Yu4a173382021-10-11 21:45:31 +0800732 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800733 * special value of the SHA-256 of "HelloRetryRequest".
734 *
735 * struct {
736 * ProtocolVersion legacy_version = 0x0303;
737 * Random random;
738 * opaque legacy_session_id_echo<0..32>;
739 * CipherSuite cipher_suite;
740 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800741 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800742 * } ServerHello;
743 *
744 */
Jerry Yub85277e2021-10-13 13:36:05 +0800745 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800746
747 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800748 {
Jerry Yub85277e2021-10-13 13:36:05 +0800749 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800750 }
751
Jerry Yub85277e2021-10-13 13:36:05 +0800752 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800753}
754
Jerry Yu745bb612021-10-13 22:01:04 +0800755/* Fetch and preprocess
756 * Returns a negative value on failure, and otherwise
757 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
758 * - SSL_SERVER_HELLO_COORDINATE_HRR
759 */
Jerry Yub85277e2021-10-13 13:36:05 +0800760static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
761 unsigned char **buf,
762 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800763{
Jerry Yu4a173382021-10-11 21:45:31 +0800764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800765
XiaokangQian355e09a2022-01-20 11:14:50 +0000766 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
767 MBEDTLS_SSL_HS_SERVER_HELLO,
768 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800769
Jerry Yub85277e2021-10-13 13:36:05 +0800770 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
771 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800772 {
Jerry Yu745bb612021-10-13 22:01:04 +0800773 case SSL_SERVER_HELLO_COORDINATE_HELLO:
774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
775 break;
776 case SSL_SERVER_HELLO_COORDINATE_HRR:
777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000778 /* If a client receives a second
779 * HelloRetryRequest in the same connection (i.e., where the ClientHello
780 * was itself in response to a HelloRetryRequest), it MUST abort the
781 * handshake with an "unexpected_message" alert.
782 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000783 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000784 {
785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
786 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
787 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
788 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
789 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000790 /*
791 * Clients must abort the handshake with an "illegal_parameter"
792 * alert if the HelloRetryRequest would not result in any change
793 * in the ClientHello.
794 * In a PSK only key exchange that what we expect.
795 */
796 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
797 {
798 MBEDTLS_SSL_DEBUG_MSG( 1,
799 ( "Unexpected HRR in pure PSK key exchange." ) );
800 MBEDTLS_SSL_PEND_FATAL_ALERT(
801 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
802 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
803 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
804 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000805
XiaokangQiand9e068e2022-01-18 06:23:32 +0000806 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000807
Jerry Yu745bb612021-10-13 22:01:04 +0800808 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800809 }
810
811cleanup:
812
813 return( ret );
814}
815
Jerry Yu4a173382021-10-11 21:45:31 +0800816static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
817 const unsigned char **buf,
818 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800819{
820 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800821 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800822
Jerry Yude4fb2c2021-09-19 18:05:08 +0800823 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800824 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800825
Jerry Yu4a173382021-10-11 21:45:31 +0800826 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800827
828 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800829 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
830 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800831 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
833 ssl->session_negotiate->id,
834 ssl->session_negotiate->id_len );
835 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800836 legacy_session_id_echo_len );
837
838 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
839 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
840
Jerry Yue1b9c292021-09-10 10:08:31 +0800841 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
842 }
843
Jerry Yu4a173382021-10-11 21:45:31 +0800844 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800845 *buf = p;
846
847 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800848 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800849 return( 0 );
850}
851
Jerry Yu4a173382021-10-11 21:45:31 +0800852static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
853 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800854{
Jerry Yu4a173382021-10-11 21:45:31 +0800855 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
856
Jerry Yue1b9c292021-09-10 10:08:31 +0800857 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800858 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800859 {
Jerry Yu4a173382021-10-11 21:45:31 +0800860 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800861 {
862 return( 1 );
863 }
864 }
865 return( 0 );
866}
867
868/* Parse ServerHello message and configure context
869 *
870 * struct {
871 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
872 * Random random;
873 * opaque legacy_session_id_echo<0..32>;
874 * CipherSuite cipher_suite;
875 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800876 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800877 * } ServerHello;
878 */
Jerry Yuc068b662021-10-11 22:30:19 +0800879static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
880 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000881 const unsigned char *end,
882 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800883{
Jerry Yub85277e2021-10-13 13:36:05 +0800884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800885 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000886 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800887 size_t extensions_len;
888 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800889 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800890 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000891 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +0000892 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800893
894 /*
895 * Check there is space for minimal fields
896 *
897 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800898 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800899 * - legacy_session_id_echo ( 1 byte ), minimum size
900 * - cipher_suite ( 2 bytes)
901 * - legacy_compression_method ( 1 byte )
902 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800903 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800904
905 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
907
Jerry Yu4a173382021-10-11 21:45:31 +0800908 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800909 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800910 * ...
911 * with ProtocolVersion defined as:
912 * uint16 ProtocolVersion;
913 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
915 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
916 {
917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
918 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
919 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000920 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
921 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800922 }
923 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800924
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800925 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800926 * Random random;
927 * ...
928 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800929 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800930 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000931 if( !is_hrr )
932 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000933 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000934 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
935 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
936 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
937 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800938 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800939
Jerry Yu4a173382021-10-11 21:45:31 +0800940 /* ...
941 * opaque legacy_session_id_echo<0..32>;
942 * ...
943 */
944 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800945 {
XiaokangQian52da5582022-01-26 09:49:29 +0000946 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000947 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800948 }
949
Jerry Yu4a173382021-10-11 21:45:31 +0800950 /* ...
951 * CipherSuite cipher_suite;
952 * ...
953 * with CipherSuite defined as:
954 * uint8 CipherSuite[2];
955 */
956 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800957 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
958 p += 2;
959
Jerry Yu4a173382021-10-11 21:45:31 +0800960
XiaokangQian355e09a2022-01-20 11:14:50 +0000961 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +0800962 /*
963 * Check whether this ciphersuite is supported and offered.
964 * Via the force_ciphersuite version we may have instructed the client
965 * to use a different ciphersuite.
966 */
Jerry Yu4a173382021-10-11 21:45:31 +0800967 if( ciphersuite_info == NULL ||
968 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800969 {
XiaokangQian52da5582022-01-26 09:49:29 +0000970 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +0800971 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000972 /*
XiaokangQian355e09a2022-01-20 11:14:50 +0000973 * If we received an HRR before and that the proposed selected
974 * ciphersuite in this server hello is not the same as the one
975 * proposed in the HRR, we abort the handshake and send an
976 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +0000977 */
XiaokangQian355e09a2022-01-20 11:14:50 +0000978 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
979 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +0000980 {
XiaokangQian52da5582022-01-26 09:49:29 +0000981 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +0000982 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000983
XiaokangQian52da5582022-01-26 09:49:29 +0000984 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +0000985 {
986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
987 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +0000988 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +0000989 }
Jerry Yue1b9c292021-09-10 10:08:31 +0800990
Jerry Yu4a173382021-10-11 21:45:31 +0800991 /* Configure ciphersuites */
992 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
993
XiaokangQian355e09a2022-01-20 11:14:50 +0000994 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800995 ssl->session_negotiate->ciphersuite = cipher_suite;
996
Jerry Yue1b9c292021-09-10 10:08:31 +0800997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
998 cipher_suite, ciphersuite_info->name ) );
999
1000#if defined(MBEDTLS_HAVE_TIME)
1001 ssl->session_negotiate->start = time( NULL );
1002#endif /* MBEDTLS_HAVE_TIME */
1003
Jerry Yu4a173382021-10-11 21:45:31 +08001004 /* ...
1005 * uint8 legacy_compression_method = 0;
1006 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001008 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 if( p[0] != 0 )
1010 {
Jerry Yub85277e2021-10-13 13:36:05 +08001011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001012 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001013 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 }
1015 p++;
1016
Jerry Yub85277e2021-10-13 13:36:05 +08001017 /* ...
1018 * Extension extensions<6..2^16-1>;
1019 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001020 * struct {
1021 * ExtensionType extension_type; (2 bytes)
1022 * opaque extension_data<0..2^16-1>;
1023 * } Extension;
1024 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001025 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001026 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001027 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001028
Jerry Yu4a173382021-10-11 21:45:31 +08001029 /* Check extensions do not go beyond the buffer of data. */
1030 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1031 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001032
Jerry Yu4a173382021-10-11 21:45:31 +08001033 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001034
Jerry Yu4a173382021-10-11 21:45:31 +08001035 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001036 {
1037 unsigned int extension_type;
1038 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001039 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001040
Jerry Yu4a173382021-10-11 21:45:31 +08001041 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1043 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1044 p += 4;
1045
Jerry Yu4a173382021-10-11 21:45:31 +08001046 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001047 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001048
1049 switch( extension_type )
1050 {
XiaokangQianb851da82022-01-14 04:03:11 +00001051 case MBEDTLS_TLS_EXT_COOKIE:
1052
XiaokangQiand9e068e2022-01-18 06:23:32 +00001053 if( !is_hrr )
1054 {
XiaokangQian52da5582022-01-26 09:49:29 +00001055 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001056 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001057 }
1058
XiaokangQian43550bd2022-01-21 04:32:58 +00001059 ret = ssl_tls13_parse_cookie_ext( ssl,
1060 p, extension_data_end );
1061 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001062 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001063 MBEDTLS_SSL_DEBUG_RET( 1,
1064 "ssl_tls13_parse_cookie_ext",
1065 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001066 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001067 }
XiaokangQianb851da82022-01-14 04:03:11 +00001068 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001069
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001071 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 MBEDTLS_SSL_DEBUG_MSG( 3,
1073 ( "found supported_versions extension" ) );
1074
Jerry Yuc068b662021-10-11 22:30:19 +08001075 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001076 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001077 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001079 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001080 break;
1081
1082 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001085
XiaokangQian52da5582022-01-26 09:49:29 +00001086 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001087 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001088
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 case MBEDTLS_TLS_EXT_KEY_SHARE:
1090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001091 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1092 {
XiaokangQian52da5582022-01-26 09:49:29 +00001093 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001094 goto cleanup;
1095 }
1096
XiaokangQian53f20b72022-01-18 10:47:33 +00001097 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001098 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001099 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001100 else
XiaokangQianb851da82022-01-14 04:03:11 +00001101 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001102 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001103 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001104 {
1105 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001106 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001108 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001109 }
1110 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001111
1112 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001113 MBEDTLS_SSL_DEBUG_MSG(
1114 3,
1115 ( "unknown extension found: %u ( ignoring )",
1116 extension_type ) );
1117
XiaokangQian52da5582022-01-26 09:49:29 +00001118 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001119 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 }
1121
1122 p += extension_data_len;
1123 }
1124
XiaokangQian78b1fa72022-01-19 06:56:30 +00001125 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001126 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001128 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001129 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001130 }
1131
XiaokangQiand59be772022-01-24 10:12:51 +00001132cleanup:
1133
XiaokangQian52da5582022-01-26 09:49:29 +00001134 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001135 {
1136 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1137 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1138 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1139 }
XiaokangQian52da5582022-01-26 09:49:29 +00001140 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001141 {
1142 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1143 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1144 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1145 }
1146 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001147}
1148
XiaokangQian355e09a2022-01-20 11:14:50 +00001149static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001150{
Jerry Yub85277e2021-10-13 13:36:05 +08001151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001152 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001153 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001154 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001155
Jerry Yub85277e2021-10-13 13:36:05 +08001156 /* Determine the key exchange mode:
1157 * 1) If both the pre_shared_key and key_share extensions were received
1158 * then the key exchange mode is PSK with EPHEMERAL.
1159 * 2) If only the pre_shared_key extension was received then the key
1160 * exchange mode is PSK-only.
1161 * 3) If only the key_share extension was received then the key
1162 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001163 */
Jerry Yub85277e2021-10-13 13:36:05 +08001164 switch( handshake->extensions_present &
1165 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001166 {
Jerry Yu745bb612021-10-13 22:01:04 +08001167 /* Only the pre_shared_key extension was received */
1168 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001169 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001170 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001171
Jerry Yu745bb612021-10-13 22:01:04 +08001172 /* Only the key_share extension was received */
1173 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001174 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001175 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001176
Jerry Yu745bb612021-10-13 22:01:04 +08001177 /* Both the pre_shared_key and key_share extensions were received */
1178 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001179 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001180 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001181
Jerry Yu745bb612021-10-13 22:01:04 +08001182 /* Neither pre_shared_key nor key_share extension was received */
1183 default:
1184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1185 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1186 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001187 }
1188
1189 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1190 *
1191 * TODO: We don't have to do this in case we offered 0-RTT and the
1192 * server accepted it. In this case, we could skip generating
1193 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001194 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001195 if( ret != 0 )
1196 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001198 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001199 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001200 }
1201
1202 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001203 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( 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_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001207 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001208 }
1209
1210 /* Next evolution in key schedule: Establish handshake secret and
1211 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001212 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001213 if( ret != 0 )
1214 {
Jerry Yuc068b662021-10-11 22:30:19 +08001215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1216 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001217 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001218 }
1219
Jerry Yub85277e2021-10-13 13:36:05 +08001220 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001221 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001222 {
1223 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1224 goto cleanup;
1225 }
Jerry Yu0b177842021-09-05 19:41:30 +08001226
1227 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1228 ssl->conf->endpoint,
1229 ssl->session_negotiate->ciphersuite,
1230 &traffic_keys,
1231 ssl );
1232 if( ret != 0 )
1233 {
1234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001235 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001236 }
1237
Jerry Yub85277e2021-10-13 13:36:05 +08001238 handshake->transform_handshake = transform_handshake;
1239 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001240
1241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1242 ssl->session_in = ssl->session_negotiate;
1243
1244 /*
1245 * State machine update
1246 */
1247 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1248
Jerry Yu4a173382021-10-11 21:45:31 +08001249cleanup:
1250
Jerry Yu0b177842021-09-05 19:41:30 +08001251 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001252 if( ret != 0 )
1253 {
Jerry Yub85277e2021-10-13 13:36:05 +08001254 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001255
Jerry Yu4a173382021-10-11 21:45:31 +08001256 MBEDTLS_SSL_PEND_FATAL_ALERT(
1257 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1258 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1259 }
1260 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001261}
1262
XiaokangQian355e09a2022-01-20 11:14:50 +00001263static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001264{
1265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1266
XiaokangQian647719a2021-12-07 09:16:29 +00001267#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1268 /* If not offering early data, the client sends a dummy CCS record
1269 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001270 * its second ClientHello or before its encrypted handshake flight.
1271 */
XiaokangQian647719a2021-12-07 09:16:29 +00001272 mbedtls_ssl_handshake_set_state( ssl,
1273 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1274#else
1275 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1276#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1277
XiaokangQian78b1fa72022-01-19 06:56:30 +00001278 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001279
XiaokangQian78b1fa72022-01-19 06:56:30 +00001280 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001281 * We are going to re-generate a shared secret corresponding to the group
1282 * selected by the server, which is different from the group for which we
1283 * generated a shared secret in the first client hello.
1284 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001285 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001286 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001287 if( ret != 0 )
1288 return( ret );
1289
1290 return( 0 );
1291}
1292
Jerry Yue1b9c292021-09-10 10:08:31 +08001293/*
Jerry Yu4a173382021-10-11 21:45:31 +08001294 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001295 * Handler for MBEDTLS_SSL_SERVER_HELLO
1296 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001297static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001298{
Jerry Yu4a173382021-10-11 21:45:31 +08001299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001300 unsigned char *buf = NULL;
1301 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001302 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001303
1304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1305
1306 /* Coordination step
1307 * - Fetch record
1308 * - Make sure it's either a ServerHello or a HRR.
1309 * - Switch processing routine in case of HRR
1310 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001311 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1312 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1313
XiaokangQian16acd4b2022-01-14 07:35:47 +00001314 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001315 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001316 goto cleanup;
1317 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001318 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001319
XiaokangQianb851da82022-01-14 04:03:11 +00001320 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001321 buf + buf_len,
1322 is_hrr ) );
1323 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001324 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1325
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001326 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1327 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001328
XiaokangQiand9e068e2022-01-18 06:23:32 +00001329 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001330 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001331 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001332 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001333
1334cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1336 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001337 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001338}
1339
1340/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001341 *
1342 * EncryptedExtensions message
1343 *
1344 * The EncryptedExtensions message contains any extensions which
1345 * should be protected, i.e., any which are not needed to establish
1346 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001347 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001348
1349/*
1350 * Overview
1351 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001352
1353/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001354static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001355
XiaokangQian97799ac2021-10-11 10:05:54 +00001356static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1357 const unsigned char *buf,
1358 const unsigned char *end );
1359static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001360
1361/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001362 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001363 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001364static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001365{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001366 int ret;
1367 unsigned char *buf;
1368 size_t buf_len;
1369
1370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1371
Xiaofei Bai746f9482021-11-12 08:53:56 +00001372 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001373 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001374 &buf, &buf_len ) );
1375
1376 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001377 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001378 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001379
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001380 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1381 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001382
XiaokangQian97799ac2021-10-11 10:05:54 +00001383 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001384
1385cleanup:
1386
1387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1388 return( ret );
1389
1390}
1391
XiaokangQian08da26c2021-10-09 10:12:11 +00001392/* Parse EncryptedExtensions message
1393 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001394 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001395 * } EncryptedExtensions;
1396 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001397static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1398 const unsigned char *buf,
1399 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001400{
1401 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001402 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001403 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001404 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001405
XiaokangQian08da26c2021-10-09 10:12:11 +00001406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001407 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001408 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001409
XiaokangQian97799ac2021-10-11 10:05:54 +00001410 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001411 extensions_end = p + extensions_len;
1412 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001413
XiaokangQian8db25ff2021-10-13 05:56:18 +00001414 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001415 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001416 unsigned int extension_type;
1417 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001418
XiaokangQian08da26c2021-10-09 10:12:11 +00001419 /*
1420 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001421 * ExtensionType extension_type; (2 bytes)
1422 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001423 * } Extension;
1424 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001425 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001426 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1427 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1428 p += 4;
1429
XiaokangQian8db25ff2021-10-13 05:56:18 +00001430 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001431
XiaokangQian97799ac2021-10-11 10:05:54 +00001432 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001433 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001434 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001435 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001436 switch( extension_type )
1437 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001438
XiaokangQian08da26c2021-10-09 10:12:11 +00001439 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1441 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001442
lhuang0486cacac2022-01-21 07:34:27 -08001443#if defined(MBEDTLS_SSL_ALPN)
1444 case MBEDTLS_TLS_EXT_ALPN:
1445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1446
1447 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1448 {
1449 return( ret );
1450 }
1451
1452 break;
1453#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001454 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001455 MBEDTLS_SSL_DEBUG_MSG(
1456 3, ( "unsupported extension found: %u ", extension_type) );
1457 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001458 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001459 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1460 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001461 }
1462
XiaokangQian08da26c2021-10-09 10:12:11 +00001463 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001464 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001465
XiaokangQian97799ac2021-10-11 10:05:54 +00001466 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001467 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001470 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001471 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001472 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473 }
1474
1475 return( ret );
1476}
1477
XiaokangQian97799ac2021-10-11 10:05:54 +00001478static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001479{
Jerry Yua93ac112021-10-27 16:31:48 +08001480#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001481 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001482 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1483 else
Jerry Yud2674312021-10-29 10:08:19 +08001484 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001485#else
1486 ((void) ssl);
1487 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1488#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001489 return( 0 );
1490}
1491
Jerry Yua93ac112021-10-27 16:31:48 +08001492#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001493/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001494 *
1495 * STATE HANDLING: CertificateRequest
1496 *
Jerry Yud2674312021-10-29 10:08:19 +08001497 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001498#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1499#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001500/* Coordination:
1501 * Deals with the ambiguity of not knowing if a CertificateRequest
1502 * will be sent. Returns a negative code on failure, or
1503 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1504 * - SSL_CERTIFICATE_REQUEST_SKIP
1505 * indicating if a Certificate Request is expected or not.
1506 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001507static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001508{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001510
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001511 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001512 {
1513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1514 return( SSL_CERTIFICATE_REQUEST_SKIP );
1515 }
1516
1517 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001518 {
1519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1520 return( ret );
1521 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001522 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001523
1524 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1525 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1526 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001527 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001528 }
1529
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001530 return( SSL_CERTIFICATE_REQUEST_SKIP );
1531}
1532
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001533/*
1534 * ssl_tls13_parse_certificate_request()
1535 * Parse certificate request
1536 * struct {
1537 * opaque certificate_request_context<0..2^8-1>;
1538 * Extension extensions<2..2^16-1>;
1539 * } CertificateRequest;
1540 */
1541static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1542 const unsigned char *buf,
1543 const unsigned char *end )
1544{
1545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1546 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001547 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001548 size_t extensions_len = 0;
1549 const unsigned char *extensions_end;
1550 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001551
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001552 /* ...
1553 * opaque certificate_request_context<0..2^8-1>
1554 * ...
1555 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001556 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1557 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001558 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001560 if( certificate_request_context_len > 0 )
1561 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001563 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1564 p, certificate_request_context_len );
1565
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001566 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001567 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001568 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001569 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001570 {
1571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1572 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1573 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001574 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001575 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001576 p += certificate_request_context_len;
1577 }
1578
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001579 /* ...
1580 * Extension extensions<2..2^16-1>;
1581 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001582 */
1583 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1584 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1585 p += 2;
1586
1587 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1588 extensions_end = p + extensions_len;
1589
1590 while( p < extensions_end )
1591 {
1592 unsigned int extension_type;
1593 size_t extension_data_len;
1594
1595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1596 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1597 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1598 p += 4;
1599
1600 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1601
1602 switch( extension_type )
1603 {
1604 case MBEDTLS_TLS_EXT_SIG_ALG:
1605 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001606 ( "found signature algorithms extension" ) );
1607 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001608 p + extension_data_len );
1609 if( ret != 0 )
1610 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001611 if( ! sig_alg_ext_found )
1612 sig_alg_ext_found = 1;
1613 else
1614 {
1615 MBEDTLS_SSL_DEBUG_MSG( 3,
1616 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001617 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001618 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001619 break;
1620
1621 default:
1622 MBEDTLS_SSL_DEBUG_MSG(
1623 3,
1624 ( "unknown extension found: %u ( ignoring )",
1625 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001626 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001627 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001628 p += extension_data_len;
1629 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001630 /* Check that we consumed all the message. */
1631 if( p != end )
1632 {
1633 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001634 ( "CertificateRequest misaligned" ) );
1635 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001636 }
1637 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001638 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001639 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001640 MBEDTLS_SSL_DEBUG_MSG( 3,
1641 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001642 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001643 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001644
Jerry Yu7840f812022-01-29 10:26:51 +08001645 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001646 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001647
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001648decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001649 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1650 MBEDTLS_ERR_SSL_DECODE_ERROR );
1651 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001653
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001654/*
1655 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1656 */
1657static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001658{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001660
1661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1662
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001663 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1664
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1666 {
1667 unsigned char *buf;
1668 size_t buf_len;
1669
1670 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1671 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1672 &buf, &buf_len ) );
1673
1674 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1675 buf, buf + buf_len ) );
1676
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001677 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1678 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001680 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001681 {
1682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001683 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001684 }
1685 else
1686 {
1687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001688 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1689 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001690 }
1691
1692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001693 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694
Jerry Yud2674312021-10-29 10:08:19 +08001695 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1696
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001697cleanup:
1698
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1700 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001701}
1702
1703/*
Jerry Yu687101b2021-09-14 16:03:56 +08001704 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1705 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001706static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001707{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001708 int ret;
1709
1710 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001711 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001712 return( ret );
1713
Jerry Yu687101b2021-09-14 16:03:56 +08001714 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1715 return( 0 );
1716}
1717
1718/*
1719 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1720 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001721static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001722{
Jerry Yu30b071c2021-09-12 20:16:03 +08001723 int ret;
1724
1725 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1726 if( ret != 0 )
1727 return( ret );
1728
Jerry Yu687101b2021-09-14 16:03:56 +08001729 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1730 return( 0 );
1731}
Jerry Yua93ac112021-10-27 16:31:48 +08001732#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001733
Jerry Yu687101b2021-09-14 16:03:56 +08001734/*
1735 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1736 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001737static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001738{
XiaokangQianac0385c2021-11-03 06:40:11 +00001739 int ret;
1740
XiaokangQianc5c39d52021-11-09 11:55:10 +00001741 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001742 if( ret != 0 )
1743 return( ret );
1744
Ronald Cron49ad6192021-11-24 16:25:31 +01001745#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1746 mbedtls_ssl_handshake_set_state(
1747 ssl,
1748 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1749#else
Jerry Yuca133a32022-02-15 14:22:05 +08001750 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001751#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001752
XiaokangQianac0385c2021-11-03 06:40:11 +00001753 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001754}
1755
1756/*
Jerry Yu566c7812022-01-26 15:41:22 +08001757 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1758 */
1759static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1760{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001761 int non_empty_certificate_msg = 0;
1762
Jerry Yu5cc35062022-01-28 16:16:08 +08001763 MBEDTLS_SSL_DEBUG_MSG( 1,
1764 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001765 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001766
Ronald Cron9df7c802022-03-08 18:38:54 +01001767#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001768 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001769 {
1770 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1771 if( ret != 0 )
1772 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001773
Ronald Cron7a94aca2022-03-09 07:44:27 +01001774 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1775 non_empty_certificate_msg = 1;
1776 }
1777 else
1778 {
1779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1780 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001781#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001782
Ronald Cron7a94aca2022-03-09 07:44:27 +01001783 if( non_empty_certificate_msg )
1784 {
1785 mbedtls_ssl_handshake_set_state( ssl,
1786 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1787 }
1788 else
1789 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1790
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001791 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001792}
1793
Ronald Cron9df7c802022-03-08 18:38:54 +01001794#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001795/*
1796 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1797 */
1798static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1799{
Ronald Crona8b38872022-03-09 07:59:25 +01001800 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1801
1802 if( ret == 0 )
1803 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1804
1805 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001806}
Jerry Yu90f152d2022-01-29 22:12:42 +08001807#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001808
1809/*
Jerry Yu687101b2021-09-14 16:03:56 +08001810 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1811 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001812static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001813{
XiaokangQian0fa66432021-11-15 03:33:57 +00001814 int ret;
1815
1816 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1817 if( ret != 0 )
1818 return( ret );
1819
1820 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1821 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001822}
1823
1824/*
1825 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1826 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001827static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001828{
Jerry Yu378254d2021-10-30 21:44:47 +08001829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001830 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001831 return( 0 );
1832}
1833
1834/*
1835 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1836 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001837static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001838{
Jerry Yu378254d2021-10-30 21:44:47 +08001839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1840 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1841
1842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1843 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1844
1845 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1846
1847 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1848 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001849}
1850
Jerry Yu92c6b402021-08-27 16:59:09 +08001851int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001852{
Jerry Yu92c6b402021-08-27 16:59:09 +08001853 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001854
Jerry Yu92c6b402021-08-27 16:59:09 +08001855 switch( ssl->state )
1856 {
1857 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001858 * ssl->state is initialized as HELLO_REQUEST. It is the same
1859 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001860 */
1861 case MBEDTLS_SSL_HELLO_REQUEST:
1862 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001863 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001864 break;
1865
1866 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001867 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001868 break;
1869
1870 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001871 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001872 break;
1873
Jerry Yua93ac112021-10-27 16:31:48 +08001874#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001875 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1876 ret = ssl_tls13_process_certificate_request( ssl );
1877 break;
1878
Jerry Yu687101b2021-09-14 16:03:56 +08001879 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001880 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001881 break;
1882
1883 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001884 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001885 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001886#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001887
1888 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001889 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001890 break;
1891
Jerry Yu566c7812022-01-26 15:41:22 +08001892 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1893 ret = ssl_tls13_write_client_certificate( ssl );
1894 break;
1895
Ronald Cron9df7c802022-03-08 18:38:54 +01001896#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001897 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1898 ret = ssl_tls13_write_client_certificate_verify( ssl );
1899 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001900#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001901
Jerry Yu687101b2021-09-14 16:03:56 +08001902 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001903 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001904 break;
1905
1906 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001907 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001908 break;
1909
1910 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001911 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001912 break;
1913
Ronald Cron49ad6192021-11-24 16:25:31 +01001914 /*
1915 * Injection of dummy-CCS's for middlebox compatibility
1916 */
1917#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001918 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001919 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1920 if( ret == 0 )
1921 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1922 break;
1923
1924 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1925 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1926 if( ret == 0 )
1927 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001928 break;
1929#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1930
Jerry Yu92c6b402021-08-27 16:59:09 +08001931 default:
1932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934 }
1935
1936 return( ret );
1937}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001938
Jerry Yufb4b6472022-01-27 15:03:26 +08001939#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001940
Jerry Yufb4b6472022-01-27 15:03:26 +08001941