blob: f701215dae5c517dac4e681907f0add57fefa139 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010036#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080037
Jerry Yubc20bdd2021-08-24 15:59:48 +080038/* Write extensions */
39
Jerry Yu92c6b402021-08-27 16:59:09 +080040/*
41 * ssl_tls13_write_supported_versions_ext():
42 *
43 * struct {
44 * ProtocolVersion versions<2..254>;
45 * } SupportedVersions;
46 */
Jerry Yuf4436812021-08-26 22:59:56 +080047static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080048 unsigned char *buf,
49 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000050 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080051{
52 unsigned char *p = buf;
53
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
62 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Jerry Yu92c6b402021-08-27 16:59:09 +080064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
65
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080066 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080068
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080069 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080070 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080071 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080073 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080074 *p++ = 0x2;
75
Jerry Yu0c63af62021-09-02 12:59:12 +080076 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080077 *
Jerry Yu0c63af62021-09-02 12:59:12 +080078 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 *
Jerry Yu0c63af62021-09-02 12:59:12 +080080 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080081 */
Jerry Yueecfbf02021-08-30 18:32:07 +080082 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
83 ssl->conf->max_minor_ver,
84 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080085
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080087 ssl->conf->max_major_ver,
88 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080089
Xiaofei Baid25fab62021-12-02 06:36:27 +000090 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuc068b662021-10-11 22:30:19 +080095static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
96 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080097 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080098{
Jerry Yue1b9c292021-09-10 10:08:31 +080099 ((void) ssl);
100
Jerry Yub85277e2021-10-13 13:36:05 +0800101 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
102 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800106
107 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
109 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800110 }
111
112 return( 0 );
113}
114
lhuang0486cacac2022-01-21 07:34:27 -0800115#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800116static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
117 const unsigned char *buf, size_t len )
118{
119 size_t list_len, name_len;
120 const unsigned char *p = buf;
121 const unsigned char *end = buf + len;
122
123 /* If we didn't send it, the server shouldn't send it */
124 if( ssl->conf->alpn_list == NULL )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
127 /*
128 * opaque ProtocolName<1..2^8-1>;
129 *
130 * struct {
131 * ProtocolName protocol_name_list<2..2^16-1>
132 * } ProtocolNameList;
133 *
134 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
135 */
136
137 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
138 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
139
140 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
141 p += 2;
142 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
143
144 name_len = *p++;
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
146
147 /* Check that the server chosen protocol was in our list and save it */
148 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
149 {
150 if( name_len == strlen( *alpn ) &&
151 memcmp( buf + 3, *alpn, name_len ) == 0 )
152 {
153 ssl->alpn_chosen = *alpn;
154 return( 0 );
155 }
156 }
157
158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159}
160#endif /* MBEDTLS_SSL_ALPN */
161
XiaokangQian16acd4b2022-01-14 07:35:47 +0000162static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000163{
164 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100165
XiaokangQian647719a2021-12-07 09:16:29 +0000166 if( group_id == 0 )
167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
168
XiaokangQian355e09a2022-01-20 11:14:50 +0000169#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000170 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000171 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
174
175 /* Destroy generated private key. */
176 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
177 if( status != PSA_SUCCESS )
178 {
179 ret = psa_ssl_status_to_mbedtls( status );
180 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
181 return( ret );
182 }
183
184 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000185 return( 0 );
186 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000187 else
188#endif /* MBEDTLS_ECDH_C */
189 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000190 {
191 /* Do something */
192 }
193
194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
195}
196
197/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800198 * Functions for writing key_share extension.
199 */
200#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800201static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800202 mbedtls_ssl_context *ssl,
203 uint16_t named_group,
204 unsigned char *buf,
205 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000206 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800207{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100208 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
209 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
210 psa_key_attributes_t key_attributes;
211 size_t own_pubkey_len;
212 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
213 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100217 /* Convert EC group to PSA key type. */
218 if( ( handshake->ecdh_psa_type =
219 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
220 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100222 if( ecdh_bits > 0xffff )
223 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
224 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100226 key_attributes = psa_key_attributes_init();
227 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
228 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
229 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
230 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100231
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100232 /* Generate ECDH private key. */
233 status = psa_generate_key( &key_attributes,
234 &handshake->ecdh_psa_privkey );
235 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800236 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100237 ret = psa_ssl_status_to_mbedtls( status );
238 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100240
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 }
242
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100243 /* Export the public part of the ECDH private key from PSA. */
244 status = psa_export_public_key( handshake->ecdh_psa_privkey,
245 buf, (size_t)( end - buf ),
246 &own_pubkey_len );
247 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800248 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100249 ret = psa_ssl_status_to_mbedtls( status );
250 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800251 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100252
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 }
254
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100255 if( own_pubkey_len > (size_t)( end - buf ) )
256 {
257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
258 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
259 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100260
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100261 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
Jerry Yu75336352021-09-01 15:59:36 +0800263 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800264}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265#endif /* MBEDTLS_ECDH_C */
266
Jerry Yub60e3cf2021-09-08 16:41:02 +0800267static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
268 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269{
270 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
271
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100274 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800275 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100276 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
278
Brett Warren14efd332021-10-06 09:32:11 +0100279 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000281 const mbedtls_ecp_curve_info *curve_info;
282 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
283 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100284 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 {
Brett Warren14efd332021-10-06 09:32:11 +0100286 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 return( 0 );
288 }
289 }
290#else
291 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293#endif /* MBEDTLS_ECDH_C */
294
295 /*
296 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800297 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298 */
299
300 return( ret );
301}
302
303/*
304 * ssl_tls13_write_key_share_ext
305 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 *
308 * struct {
309 * NamedGroup group;
310 * opaque key_exchange<1..2^16-1>;
311 * } KeyShareEntry;
312 * struct {
313 * KeyShareEntry client_shares<0..2^16-1>;
314 * } KeyShareClientHello;
315 */
316static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
317 unsigned char *buf,
318 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000319 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320{
321 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000322 unsigned char *client_shares; /* Start of client_shares */
323 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
326
Xiaofei Baid25fab62021-12-02 06:36:27 +0000327 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328
Jerry Yub60e3cf2021-09-08 16:41:02 +0800329 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 * - extension_type (2 bytes)
331 * - extension_data_length (2 bytes)
332 * - client_shares_length (2 bytes)
333 */
334 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
335 p += 6;
336
337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
338
339 /* HRR could already have requested something else. */
340 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
342 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345 &group_id ) );
346 }
347
348 /*
349 * Dispatch to type-specific key generation function.
350 *
351 * So far, we're only supporting ECDHE. With the introduction
352 * of PQC KEMs, we'll want to have multiple branches, one per
353 * type of KEM, and dispatch to the corresponding crypto. And
354 * only one key share entry is allowed.
355 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000356 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800358 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800360 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000361 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100363 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800364
365 /* Check there is space for header of KeyShareEntry
366 * - group (2 bytes)
367 * - key_exchange_length (2 bytes)
368 */
369 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
370 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100371 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800372 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800373 p += key_exchange_len;
374 if( ret != 0 )
375 return( ret );
376
377 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000378 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800379 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000380 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 }
382 else
383#endif /* MBEDTLS_ECDH_C */
384 if( 0 /* other KEMs? */ )
385 {
386 /* Do something */
387 }
388 else
389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
390
Jerry Yub60e3cf2021-09-08 16:41:02 +0800391 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000392 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 if( client_shares_len == 0)
394 {
395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800397 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800398 /* Write extension_type */
399 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
400 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800401 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800402 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800404
405 /* Update offered_group_id field */
406 ssl->handshake->offered_group_id = group_id;
407
408 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000409 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410
Xiaofei Baid25fab62021-12-02 06:36:27 +0000411 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412
413 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
414
415cleanup:
416
417 return( ret );
418}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800419
Jerry Yue1b9c292021-09-10 10:08:31 +0800420#if defined(MBEDTLS_ECDH_C)
421
Jerry Yuc068b662021-10-11 22:30:19 +0800422static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
423 const unsigned char *buf,
424 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800425{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100426 uint8_t *p = (uint8_t*)buf;
427 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800428
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100429 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
430 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
431 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800432
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100433 /* Check if key size is consistent with given buffer length. */
434 if ( peerkey_len > ( buf_len - 2 ) )
435 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800436
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100437 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100438 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100439 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800440
441 return( 0 );
442}
Jerry Yu4a173382021-10-11 21:45:31 +0800443#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800444
XiaokangQiand59be772022-01-24 10:12:51 +0000445/*
446 * ssl_tls13_parse_hrr_key_share_ext()
447 * Parse key_share extension in Hello Retry Request
448 *
449 * struct {
450 * NamedGroup selected_group;
451 * } KeyShareHelloRetryRequest;
452 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000453static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000454 const unsigned char *buf,
455 const unsigned char *end )
456{
XiaokangQianb851da82022-01-14 04:03:11 +0000457 const mbedtls_ecp_curve_info *curve_info = NULL;
458 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000459 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000460 int found = 0;
461
462 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
463 if( group_list == NULL )
464 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
465
466 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
467
468 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000469 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000470 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000472
473 /* Upon receipt of this extension in a HelloRetryRequest, the client
474 * MUST first verify that the selected_group field corresponds to a
475 * group which was provided in the "supported_groups" extension in the
476 * original ClientHello.
477 * The supported_group was based on the info in ssl->conf->group_list.
478 *
479 * If the server provided a key share that was not sent in the ClientHello
480 * then the client MUST abort the handshake with an "illegal_parameter" alert.
481 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000482 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000483 {
484 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000485 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000486 continue;
487
488 /* We found a match */
489 found = 1;
490 break;
491 }
492
493 /* Client MUST verify that the selected_group field does not
494 * correspond to a group which was provided in the "key_share"
495 * extension in the original ClientHello. If the server sent an
496 * HRR message with a key share already provided in the
497 * ClientHello then the client MUST abort the handshake with
498 * an "illegal_parameter" alert.
499 */
XiaokangQiand59be772022-01-24 10:12:51 +0000500 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000501 {
502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
503 MBEDTLS_SSL_PEND_FATAL_ALERT(
504 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
505 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
506 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
507 }
508
509 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000510 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000511
512 return( 0 );
513}
514
Jerry Yue1b9c292021-09-10 10:08:31 +0800515/*
Jerry Yub85277e2021-10-13 13:36:05 +0800516 * ssl_tls13_parse_key_share_ext()
517 * Parse key_share extension in Server Hello
518 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800519 * struct {
520 * KeyShareEntry server_share;
521 * } KeyShareServerHello;
522 * struct {
523 * NamedGroup group;
524 * opaque key_exchange<1..2^16-1>;
525 * } KeyShareEntry;
526 */
Jerry Yu4a173382021-10-11 21:45:31 +0800527static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 const unsigned char *buf,
529 const unsigned char *end )
530{
Jerry Yub85277e2021-10-13 13:36:05 +0800531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800532 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800533 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800534
Jerry Yu4a173382021-10-11 21:45:31 +0800535 /* ...
536 * NamedGroup group; (2 bytes)
537 * ...
538 */
539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
540 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800541 p += 2;
542
Jerry Yu4a173382021-10-11 21:45:31 +0800543 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800544 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800545 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1,
548 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800549 (unsigned) offered_group, (unsigned) group ) );
550 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
551 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
552 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800553 }
554
555#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800556 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800557 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100558 const mbedtls_ecp_curve_info *curve_info =
559 mbedtls_ecp_curve_info_from_tls_id( group );
560 if( curve_info == NULL )
561 {
562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
564 }
565
566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
567
Jerry Yuc068b662021-10-11 22:30:19 +0800568 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800569 if( ret != 0 )
570 return( ret );
571 }
Jerry Yub85277e2021-10-13 13:36:05 +0800572 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800573#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800574 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800575 {
576 /* Do something */
577 }
578 else
579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
580
581 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
582 return( ret );
583}
584
XiaokangQiand59be772022-01-24 10:12:51 +0000585/*
586 * ssl_tls13_parse_cookie_ext()
587 * Parse cookie extension in Hello Retry Request
588 *
589 * struct {
590 * opaque cookie<1..2^16-1>;
591 * } Cookie;
592 *
593 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
594 * extension to the client (this is an exception to the usual rule that
595 * the only extensions that may be sent are those that appear in the
596 * ClientHello). When sending the new ClientHello, the client MUST copy
597 * the contents of the extension received in the HelloRetryRequest into
598 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
599 * cookies in their initial ClientHello in subsequent connections.
600 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000601static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
602 const unsigned char *buf,
603 const unsigned char *end )
604{
XiaokangQian25c9c902022-02-08 10:49:53 +0000605 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000606 const unsigned char *p = buf;
607 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
608
609 /* Retrieve length field of cookie */
610 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
611 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
612 p += 2;
613
614 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
615 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
616
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000617 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000618 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000619 handshake->cookie = mbedtls_calloc( 1, cookie_len );
620 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000621 {
622 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000623 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000624 cookie_len ) );
625 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
626 }
627
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000628 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000629 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000630
631 return( 0 );
632}
XiaokangQian43550bd2022-01-21 04:32:58 +0000633
XiaokangQian0b64eed2022-01-27 10:36:51 +0000634static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000635 unsigned char *buf,
636 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000637 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000638{
639 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000640 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000641 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000642
XiaokangQianc02768a2022-02-10 07:31:25 +0000643 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000644 {
645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
646 return( 0 );
647 }
648
649 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000650 handshake->cookie,
651 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000652
XiaokangQianc02768a2022-02-10 07:31:25 +0000653 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000654
655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
656
XiaokangQian0b64eed2022-01-27 10:36:51 +0000657 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000658 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
659 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000660 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000661
662 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000663 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000664
XiaokangQianc02768a2022-02-10 07:31:25 +0000665 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000666
667 return( 0 );
668}
669
Ronald Cron3d580bf2022-02-18 17:24:56 +0100670int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
671 unsigned char *buf,
672 unsigned char *end,
673 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100674{
675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
676 unsigned char *p = buf;
677 size_t ext_len;
678
679 *out_len = 0;
680
681 /* Write supported_versions extension
682 *
683 * Supported Versions Extension is mandatory with TLS 1.3.
684 */
685 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
686 if( ret != 0 )
687 return( ret );
688 p += ext_len;
689
690 /* Echo the cookie if the server provided one in its preceding
691 * HelloRetryRequest message.
692 */
693 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
694 if( ret != 0 )
695 return( ret );
696 p += ext_len;
697
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100698 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
699 {
700 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
701 if( ret != 0 )
702 return( ret );
703 p += ext_len;
704 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100705
706 *out_len = p - buf;
707
708 return( 0 );
709}
710
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800711/*
Jerry Yu4a173382021-10-11 21:45:31 +0800712 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800713 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800714/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800715 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
716 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000717 * to indicate which message is expected and to be parsed next.
718 */
Jerry Yub85277e2021-10-13 13:36:05 +0800719#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
720#define SSL_SERVER_HELLO_COORDINATE_HRR 1
721static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
722 const unsigned char *buf,
723 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800724{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800725 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800726 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
727 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
728 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
729 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
730
731 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
732 *
Jerry Yu4a173382021-10-11 21:45:31 +0800733 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800734 * special value of the SHA-256 of "HelloRetryRequest".
735 *
736 * struct {
737 * ProtocolVersion legacy_version = 0x0303;
738 * Random random;
739 * opaque legacy_session_id_echo<0..32>;
740 * CipherSuite cipher_suite;
741 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800742 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800743 * } ServerHello;
744 *
745 */
Jerry Yub85277e2021-10-13 13:36:05 +0800746 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800747
748 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800749 {
Jerry Yub85277e2021-10-13 13:36:05 +0800750 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800751 }
752
Jerry Yub85277e2021-10-13 13:36:05 +0800753 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800754}
755
Jerry Yu745bb612021-10-13 22:01:04 +0800756/* Fetch and preprocess
757 * Returns a negative value on failure, and otherwise
758 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
759 * - SSL_SERVER_HELLO_COORDINATE_HRR
760 */
Jerry Yub85277e2021-10-13 13:36:05 +0800761static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
762 unsigned char **buf,
763 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800764{
Jerry Yu4a173382021-10-11 21:45:31 +0800765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800766
XiaokangQian355e09a2022-01-20 11:14:50 +0000767 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
768 MBEDTLS_SSL_HS_SERVER_HELLO,
769 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800770
Jerry Yub85277e2021-10-13 13:36:05 +0800771 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
772 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800773 {
Jerry Yu745bb612021-10-13 22:01:04 +0800774 case SSL_SERVER_HELLO_COORDINATE_HELLO:
775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
776 break;
777 case SSL_SERVER_HELLO_COORDINATE_HRR:
778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000779 /* If a client receives a second
780 * HelloRetryRequest in the same connection (i.e., where the ClientHello
781 * was itself in response to a HelloRetryRequest), it MUST abort the
782 * handshake with an "unexpected_message" alert.
783 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000784 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000785 {
786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
787 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
788 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
789 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
790 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000791 /*
792 * Clients must abort the handshake with an "illegal_parameter"
793 * alert if the HelloRetryRequest would not result in any change
794 * in the ClientHello.
795 * In a PSK only key exchange that what we expect.
796 */
797 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
798 {
799 MBEDTLS_SSL_DEBUG_MSG( 1,
800 ( "Unexpected HRR in pure PSK key exchange." ) );
801 MBEDTLS_SSL_PEND_FATAL_ALERT(
802 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
803 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
804 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
805 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000806
XiaokangQiand9e068e2022-01-18 06:23:32 +0000807 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000808
Jerry Yu745bb612021-10-13 22:01:04 +0800809 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800810 }
811
812cleanup:
813
814 return( ret );
815}
816
Jerry Yu4a173382021-10-11 21:45:31 +0800817static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
818 const unsigned char **buf,
819 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800820{
821 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800822 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800823
Jerry Yude4fb2c2021-09-19 18:05:08 +0800824 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800825 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800826
Jerry Yu4a173382021-10-11 21:45:31 +0800827 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800828
829 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800830 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
831 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800833 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
834 ssl->session_negotiate->id,
835 ssl->session_negotiate->id_len );
836 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800837 legacy_session_id_echo_len );
838
839 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
840 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
841
Jerry Yue1b9c292021-09-10 10:08:31 +0800842 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
843 }
844
Jerry Yu4a173382021-10-11 21:45:31 +0800845 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800846 *buf = p;
847
848 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800849 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800850 return( 0 );
851}
852
Jerry Yu4a173382021-10-11 21:45:31 +0800853static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
854 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800855{
Jerry Yu4a173382021-10-11 21:45:31 +0800856 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
857
Jerry Yue1b9c292021-09-10 10:08:31 +0800858 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800859 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800860 {
Jerry Yu4a173382021-10-11 21:45:31 +0800861 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800862 {
863 return( 1 );
864 }
865 }
866 return( 0 );
867}
868
869/* Parse ServerHello message and configure context
870 *
871 * struct {
872 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
873 * Random random;
874 * opaque legacy_session_id_echo<0..32>;
875 * CipherSuite cipher_suite;
876 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800877 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800878 * } ServerHello;
879 */
Jerry Yuc068b662021-10-11 22:30:19 +0800880static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
881 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000882 const unsigned char *end,
883 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800884{
Jerry Yub85277e2021-10-13 13:36:05 +0800885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800886 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000887 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800888 size_t extensions_len;
889 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800890 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800891 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000892 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +0000893 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800894
895 /*
896 * Check there is space for minimal fields
897 *
898 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800899 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800900 * - legacy_session_id_echo ( 1 byte ), minimum size
901 * - cipher_suite ( 2 bytes)
902 * - legacy_compression_method ( 1 byte )
903 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800904 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800905
906 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800907 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
908
Jerry Yu4a173382021-10-11 21:45:31 +0800909 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800910 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800911 * ...
912 * with ProtocolVersion defined as:
913 * uint16 ProtocolVersion;
914 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
916 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
917 {
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
919 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
920 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000921 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
922 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800923 }
924 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800925
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800926 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800927 * Random random;
928 * ...
929 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800930 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800931 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000932 if( !is_hrr )
933 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000934 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000935 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
936 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
937 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
938 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800939 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800940
Jerry Yu4a173382021-10-11 21:45:31 +0800941 /* ...
942 * opaque legacy_session_id_echo<0..32>;
943 * ...
944 */
945 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800946 {
XiaokangQian52da5582022-01-26 09:49:29 +0000947 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000948 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800949 }
950
Jerry Yu4a173382021-10-11 21:45:31 +0800951 /* ...
952 * CipherSuite cipher_suite;
953 * ...
954 * with CipherSuite defined as:
955 * uint8 CipherSuite[2];
956 */
957 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
959 p += 2;
960
Jerry Yu4a173382021-10-11 21:45:31 +0800961
XiaokangQian355e09a2022-01-20 11:14:50 +0000962 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +0800963 /*
964 * Check whether this ciphersuite is supported and offered.
965 * Via the force_ciphersuite version we may have instructed the client
966 * to use a different ciphersuite.
967 */
Jerry Yu4a173382021-10-11 21:45:31 +0800968 if( ciphersuite_info == NULL ||
969 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 {
XiaokangQian52da5582022-01-26 09:49:29 +0000971 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +0800972 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000973 /*
XiaokangQian355e09a2022-01-20 11:14:50 +0000974 * If we received an HRR before and that the proposed selected
975 * ciphersuite in this server hello is not the same as the one
976 * proposed in the HRR, we abort the handshake and send an
977 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +0000978 */
XiaokangQian355e09a2022-01-20 11:14:50 +0000979 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
980 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +0000981 {
XiaokangQian52da5582022-01-26 09:49:29 +0000982 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +0000983 }
XiaokangQian53f20b72022-01-18 10:47:33 +0000984
XiaokangQian52da5582022-01-26 09:49:29 +0000985 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +0000986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
988 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +0000989 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +0000990 }
Jerry Yue1b9c292021-09-10 10:08:31 +0800991
Jerry Yu4a173382021-10-11 21:45:31 +0800992 /* Configure ciphersuites */
993 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
994
XiaokangQian355e09a2022-01-20 11:14:50 +0000995 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 ssl->session_negotiate->ciphersuite = cipher_suite;
997
Jerry Yue1b9c292021-09-10 10:08:31 +0800998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
999 cipher_suite, ciphersuite_info->name ) );
1000
1001#if defined(MBEDTLS_HAVE_TIME)
1002 ssl->session_negotiate->start = time( NULL );
1003#endif /* MBEDTLS_HAVE_TIME */
1004
Jerry Yu4a173382021-10-11 21:45:31 +08001005 /* ...
1006 * uint8 legacy_compression_method = 0;
1007 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001008 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001009 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001010 if( p[0] != 0 )
1011 {
Jerry Yub85277e2021-10-13 13:36:05 +08001012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001013 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001014 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001015 }
1016 p++;
1017
Jerry Yub85277e2021-10-13 13:36:05 +08001018 /* ...
1019 * Extension extensions<6..2^16-1>;
1020 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001021 * struct {
1022 * ExtensionType extension_type; (2 bytes)
1023 * opaque extension_data<0..2^16-1>;
1024 * } Extension;
1025 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001026 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001027 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001029
Jerry Yu4a173382021-10-11 21:45:31 +08001030 /* Check extensions do not go beyond the buffer of data. */
1031 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1032 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001033
Jerry Yu4a173382021-10-11 21:45:31 +08001034 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001035
Jerry Yu4a173382021-10-11 21:45:31 +08001036 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 {
1038 unsigned int extension_type;
1039 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001040 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001041
Jerry Yu4a173382021-10-11 21:45:31 +08001042 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1044 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1045 p += 4;
1046
Jerry Yu4a173382021-10-11 21:45:31 +08001047 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001048 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001049
1050 switch( extension_type )
1051 {
XiaokangQianb851da82022-01-14 04:03:11 +00001052 case MBEDTLS_TLS_EXT_COOKIE:
1053
XiaokangQiand9e068e2022-01-18 06:23:32 +00001054 if( !is_hrr )
1055 {
XiaokangQian52da5582022-01-26 09:49:29 +00001056 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001057 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001058 }
1059
XiaokangQian43550bd2022-01-21 04:32:58 +00001060 ret = ssl_tls13_parse_cookie_ext( ssl,
1061 p, extension_data_end );
1062 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001063 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001064 MBEDTLS_SSL_DEBUG_RET( 1,
1065 "ssl_tls13_parse_cookie_ext",
1066 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001067 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001068 }
XiaokangQianb851da82022-01-14 04:03:11 +00001069 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001070
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001072 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 MBEDTLS_SSL_DEBUG_MSG( 3,
1074 ( "found supported_versions extension" ) );
1075
Jerry Yuc068b662021-10-11 22:30:19 +08001076 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001077 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001078 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001080 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 break;
1082
1083 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001086
XiaokangQian52da5582022-01-26 09:49:29 +00001087 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001088 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001089
Jerry Yue1b9c292021-09-10 10:08:31 +08001090 case MBEDTLS_TLS_EXT_KEY_SHARE:
1091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001092 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1093 {
XiaokangQian52da5582022-01-26 09:49:29 +00001094 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001095 goto cleanup;
1096 }
1097
XiaokangQian53f20b72022-01-18 10:47:33 +00001098 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001099 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001100 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001101 else
XiaokangQianb851da82022-01-14 04:03:11 +00001102 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001103 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001104 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001105 {
1106 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001107 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001108 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001109 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001110 }
1111 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
1113 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001114 MBEDTLS_SSL_DEBUG_MSG(
1115 3,
1116 ( "unknown extension found: %u ( ignoring )",
1117 extension_type ) );
1118
XiaokangQian52da5582022-01-26 09:49:29 +00001119 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001120 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 }
1122
1123 p += extension_data_len;
1124 }
1125
XiaokangQian78b1fa72022-01-19 06:56:30 +00001126 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001127 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001129 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001130 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001131 }
1132
XiaokangQiand59be772022-01-24 10:12:51 +00001133cleanup:
1134
XiaokangQian52da5582022-01-26 09:49:29 +00001135 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001136 {
1137 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1138 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1139 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1140 }
XiaokangQian52da5582022-01-26 09:49:29 +00001141 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001142 {
1143 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1144 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1145 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1146 }
1147 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001148}
1149
XiaokangQian355e09a2022-01-20 11:14:50 +00001150static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001151{
Jerry Yub85277e2021-10-13 13:36:05 +08001152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001153 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001154 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001155 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001156
Jerry Yub85277e2021-10-13 13:36:05 +08001157 /* Determine the key exchange mode:
1158 * 1) If both the pre_shared_key and key_share extensions were received
1159 * then the key exchange mode is PSK with EPHEMERAL.
1160 * 2) If only the pre_shared_key extension was received then the key
1161 * exchange mode is PSK-only.
1162 * 3) If only the key_share extension was received then the key
1163 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001164 */
Jerry Yub85277e2021-10-13 13:36:05 +08001165 switch( handshake->extensions_present &
1166 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001167 {
Jerry Yu745bb612021-10-13 22:01:04 +08001168 /* Only the pre_shared_key extension was received */
1169 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001170 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001171 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001172
Jerry Yu745bb612021-10-13 22:01:04 +08001173 /* Only the key_share extension was received */
1174 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001175 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001176 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001177
Jerry Yu745bb612021-10-13 22:01:04 +08001178 /* Both the pre_shared_key and key_share extensions were received */
1179 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001180 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001181 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001182
Jerry Yu745bb612021-10-13 22:01:04 +08001183 /* Neither pre_shared_key nor key_share extension was received */
1184 default:
1185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1186 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1187 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001188 }
1189
1190 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1191 *
1192 * TODO: We don't have to do this in case we offered 0-RTT and the
1193 * server accepted it. In this case, we could skip generating
1194 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001195 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001196 if( ret != 0 )
1197 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001199 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001200 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001201 }
1202
1203 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001204 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001205 if( ret != 0 )
1206 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001208 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001209 }
1210
1211 /* Next evolution in key schedule: Establish handshake secret and
1212 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001213 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001214 if( ret != 0 )
1215 {
Jerry Yuc068b662021-10-11 22:30:19 +08001216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1217 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001218 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001219 }
1220
Jerry Yub85277e2021-10-13 13:36:05 +08001221 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001222 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001223 {
1224 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1225 goto cleanup;
1226 }
Jerry Yu0b177842021-09-05 19:41:30 +08001227
1228 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1229 ssl->conf->endpoint,
1230 ssl->session_negotiate->ciphersuite,
1231 &traffic_keys,
1232 ssl );
1233 if( ret != 0 )
1234 {
1235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001236 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001237 }
1238
Jerry Yub85277e2021-10-13 13:36:05 +08001239 handshake->transform_handshake = transform_handshake;
1240 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001241
1242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1243 ssl->session_in = ssl->session_negotiate;
1244
1245 /*
1246 * State machine update
1247 */
1248 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1249
Jerry Yu4a173382021-10-11 21:45:31 +08001250cleanup:
1251
Jerry Yu0b177842021-09-05 19:41:30 +08001252 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001253 if( ret != 0 )
1254 {
Jerry Yub85277e2021-10-13 13:36:05 +08001255 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001256
Jerry Yu4a173382021-10-11 21:45:31 +08001257 MBEDTLS_SSL_PEND_FATAL_ALERT(
1258 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1259 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1260 }
1261 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001262}
1263
XiaokangQian355e09a2022-01-20 11:14:50 +00001264static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001265{
1266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1267
XiaokangQian647719a2021-12-07 09:16:29 +00001268#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1269 /* If not offering early data, the client sends a dummy CCS record
1270 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001271 * its second ClientHello or before its encrypted handshake flight.
1272 */
XiaokangQian647719a2021-12-07 09:16:29 +00001273 mbedtls_ssl_handshake_set_state( ssl,
1274 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1275#else
1276 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1277#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1278
XiaokangQian78b1fa72022-01-19 06:56:30 +00001279 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001280
XiaokangQian78b1fa72022-01-19 06:56:30 +00001281 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001282 * We are going to re-generate a shared secret corresponding to the group
1283 * selected by the server, which is different from the group for which we
1284 * generated a shared secret in the first client hello.
1285 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001286 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001287 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001288 if( ret != 0 )
1289 return( ret );
1290
1291 return( 0 );
1292}
1293
Jerry Yue1b9c292021-09-10 10:08:31 +08001294/*
Jerry Yu4a173382021-10-11 21:45:31 +08001295 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001296 * Handler for MBEDTLS_SSL_SERVER_HELLO
1297 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001298static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001299{
Jerry Yu4a173382021-10-11 21:45:31 +08001300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001301 unsigned char *buf = NULL;
1302 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001303 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001304
1305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1306
1307 /* Coordination step
1308 * - Fetch record
1309 * - Make sure it's either a ServerHello or a HRR.
1310 * - Switch processing routine in case of HRR
1311 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001312 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1313 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1314
XiaokangQian16acd4b2022-01-14 07:35:47 +00001315 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001316 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001317 goto cleanup;
1318 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001319 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001320
XiaokangQianb851da82022-01-14 04:03:11 +00001321 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001322 buf + buf_len,
1323 is_hrr ) );
1324 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001325 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1326
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001327 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1328 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001329
XiaokangQiand9e068e2022-01-18 06:23:32 +00001330 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001331 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001332 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001333 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001334
1335cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1337 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001338 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001339}
1340
1341/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001342 *
1343 * EncryptedExtensions message
1344 *
1345 * The EncryptedExtensions message contains any extensions which
1346 * should be protected, i.e., any which are not needed to establish
1347 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001348 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001349
1350/*
1351 * Overview
1352 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001353
1354/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001355static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001356
XiaokangQian97799ac2021-10-11 10:05:54 +00001357static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1358 const unsigned char *buf,
1359 const unsigned char *end );
1360static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001361
1362/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001363 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001364 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001365static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001366{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001367 int ret;
1368 unsigned char *buf;
1369 size_t buf_len;
1370
1371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1372
Xiaofei Bai746f9482021-11-12 08:53:56 +00001373 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001374 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001375 &buf, &buf_len ) );
1376
1377 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001378 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001379 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001380
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001381 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1382 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001383
XiaokangQian97799ac2021-10-11 10:05:54 +00001384 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001385
1386cleanup:
1387
1388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1389 return( ret );
1390
1391}
1392
XiaokangQian08da26c2021-10-09 10:12:11 +00001393/* Parse EncryptedExtensions message
1394 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001395 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001396 * } EncryptedExtensions;
1397 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001398static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1399 const unsigned char *buf,
1400 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001401{
1402 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001403 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001404 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001405 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001406
XiaokangQian08da26c2021-10-09 10:12:11 +00001407 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001408 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001409 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001410
XiaokangQian97799ac2021-10-11 10:05:54 +00001411 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001412 extensions_end = p + extensions_len;
1413 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001414
XiaokangQian8db25ff2021-10-13 05:56:18 +00001415 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001416 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001417 unsigned int extension_type;
1418 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001419
XiaokangQian08da26c2021-10-09 10:12:11 +00001420 /*
1421 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001422 * ExtensionType extension_type; (2 bytes)
1423 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001424 * } Extension;
1425 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001426 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001427 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1428 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1429 p += 4;
1430
XiaokangQian8db25ff2021-10-13 05:56:18 +00001431 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001432
XiaokangQian97799ac2021-10-11 10:05:54 +00001433 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001434 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001435 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001436 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001437 switch( extension_type )
1438 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001439
XiaokangQian08da26c2021-10-09 10:12:11 +00001440 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1442 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001443
lhuang0486cacac2022-01-21 07:34:27 -08001444#if defined(MBEDTLS_SSL_ALPN)
1445 case MBEDTLS_TLS_EXT_ALPN:
1446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1447
1448 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1449 {
1450 return( ret );
1451 }
1452
1453 break;
1454#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001455 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001456 MBEDTLS_SSL_DEBUG_MSG(
1457 3, ( "unsupported extension found: %u ", extension_type) );
1458 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001459 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001460 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1461 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001462 }
1463
XiaokangQian08da26c2021-10-09 10:12:11 +00001464 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001465 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001466
XiaokangQian97799ac2021-10-11 10:05:54 +00001467 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001468 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001471 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001472 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001473 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001474 }
1475
1476 return( ret );
1477}
1478
XiaokangQian97799ac2021-10-11 10:05:54 +00001479static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001480{
Jerry Yua93ac112021-10-27 16:31:48 +08001481#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001482 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001483 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1484 else
Jerry Yud2674312021-10-29 10:08:19 +08001485 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001486#else
1487 ((void) ssl);
1488 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1489#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001490 return( 0 );
1491}
1492
Jerry Yua93ac112021-10-27 16:31:48 +08001493#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001494/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001495 *
1496 * STATE HANDLING: CertificateRequest
1497 *
Jerry Yud2674312021-10-29 10:08:19 +08001498 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001499#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1500#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001501/* Coordination:
1502 * Deals with the ambiguity of not knowing if a CertificateRequest
1503 * will be sent. Returns a negative code on failure, or
1504 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1505 * - SSL_CERTIFICATE_REQUEST_SKIP
1506 * indicating if a Certificate Request is expected or not.
1507 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001508static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001509{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001511
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001512 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001513 {
1514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1515 return( SSL_CERTIFICATE_REQUEST_SKIP );
1516 }
1517
1518 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001519 {
1520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1521 return( ret );
1522 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001523 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001524
1525 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1526 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1527 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001528 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001529 }
1530
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001531 return( SSL_CERTIFICATE_REQUEST_SKIP );
1532}
1533
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001534/*
1535 * ssl_tls13_parse_certificate_request()
1536 * Parse certificate request
1537 * struct {
1538 * opaque certificate_request_context<0..2^8-1>;
1539 * Extension extensions<2..2^16-1>;
1540 * } CertificateRequest;
1541 */
1542static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1543 const unsigned char *buf,
1544 const unsigned char *end )
1545{
1546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1547 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001548 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001549 size_t extensions_len = 0;
1550 const unsigned char *extensions_end;
1551 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001552
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001553 /* ...
1554 * opaque certificate_request_context<0..2^8-1>
1555 * ...
1556 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1558 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001559 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001560
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001561 if( certificate_request_context_len > 0 )
1562 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001563 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1565 p, certificate_request_context_len );
1566
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001567 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001568 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001569 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001570 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571 {
1572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1573 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1574 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001575 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001576 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001577 p += certificate_request_context_len;
1578 }
1579
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001580 /* ...
1581 * Extension extensions<2..2^16-1>;
1582 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001583 */
1584 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1585 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1586 p += 2;
1587
1588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1589 extensions_end = p + extensions_len;
1590
1591 while( p < extensions_end )
1592 {
1593 unsigned int extension_type;
1594 size_t extension_data_len;
1595
1596 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1597 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1598 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1599 p += 4;
1600
1601 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1602
1603 switch( extension_type )
1604 {
1605 case MBEDTLS_TLS_EXT_SIG_ALG:
1606 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001607 ( "found signature algorithms extension" ) );
1608 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001609 p + extension_data_len );
1610 if( ret != 0 )
1611 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001612 if( ! sig_alg_ext_found )
1613 sig_alg_ext_found = 1;
1614 else
1615 {
1616 MBEDTLS_SSL_DEBUG_MSG( 3,
1617 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001618 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001619 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001620 break;
1621
1622 default:
1623 MBEDTLS_SSL_DEBUG_MSG(
1624 3,
1625 ( "unknown extension found: %u ( ignoring )",
1626 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001627 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001628 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001629 p += extension_data_len;
1630 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001631 /* Check that we consumed all the message. */
1632 if( p != end )
1633 {
1634 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001635 ( "CertificateRequest misaligned" ) );
1636 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001637 }
1638 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001639 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001640 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001641 MBEDTLS_SSL_DEBUG_MSG( 3,
1642 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001643 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001644 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001645
Jerry Yu7840f812022-01-29 10:26:51 +08001646 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001647 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001648
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001649decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001650 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1651 MBEDTLS_ERR_SSL_DECODE_ERROR );
1652 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001653}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001655/*
1656 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1657 */
1658static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001661
1662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1663
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001664 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1665
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1667 {
1668 unsigned char *buf;
1669 size_t buf_len;
1670
1671 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1672 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1673 &buf, &buf_len ) );
1674
1675 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1676 buf, buf + buf_len ) );
1677
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001678 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1679 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001680 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001681 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001682 {
1683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001684 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001685 }
1686 else
1687 {
1688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001689 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1690 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 }
1692
1693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001694 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695
Jerry Yud2674312021-10-29 10:08:19 +08001696 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1697
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698cleanup:
1699
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1701 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001702}
1703
1704/*
Jerry Yu687101b2021-09-14 16:03:56 +08001705 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1706 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001707static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001708{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001709 int ret;
1710
1711 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001712 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001713 return( ret );
1714
Jerry Yu687101b2021-09-14 16:03:56 +08001715 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1716 return( 0 );
1717}
1718
1719/*
1720 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1721 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001722static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001723{
Jerry Yu30b071c2021-09-12 20:16:03 +08001724 int ret;
1725
1726 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1727 if( ret != 0 )
1728 return( ret );
1729
Jerry Yu687101b2021-09-14 16:03:56 +08001730 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1731 return( 0 );
1732}
Jerry Yua93ac112021-10-27 16:31:48 +08001733#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001734
Jerry Yu687101b2021-09-14 16:03:56 +08001735/*
1736 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1737 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001738static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001739{
XiaokangQianac0385c2021-11-03 06:40:11 +00001740 int ret;
1741
XiaokangQianc5c39d52021-11-09 11:55:10 +00001742 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001743 if( ret != 0 )
1744 return( ret );
1745
Ronald Cron49ad6192021-11-24 16:25:31 +01001746#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1747 mbedtls_ssl_handshake_set_state(
1748 ssl,
1749 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1750#else
Jerry Yuca133a32022-02-15 14:22:05 +08001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001752#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001753
XiaokangQianac0385c2021-11-03 06:40:11 +00001754 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001755}
1756
1757/*
Jerry Yu566c7812022-01-26 15:41:22 +08001758 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1759 */
1760static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1761{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001762 int non_empty_certificate_msg = 0;
1763
Jerry Yu5cc35062022-01-28 16:16:08 +08001764 MBEDTLS_SSL_DEBUG_MSG( 1,
1765 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001766 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001767
Ronald Cron9df7c802022-03-08 18:38:54 +01001768#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001769 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001770 {
1771 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1772 if( ret != 0 )
1773 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001774
Ronald Cron7a94aca2022-03-09 07:44:27 +01001775 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1776 non_empty_certificate_msg = 1;
1777 }
1778 else
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1781 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001782#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001783
Ronald Cron7a94aca2022-03-09 07:44:27 +01001784 if( non_empty_certificate_msg )
1785 {
1786 mbedtls_ssl_handshake_set_state( ssl,
1787 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1788 }
1789 else
1790 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1791
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001792 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001793}
1794
Ronald Cron9df7c802022-03-08 18:38:54 +01001795#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001796/*
1797 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1798 */
1799static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1800{
Ronald Crona8b38872022-03-09 07:59:25 +01001801 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1802
1803 if( ret == 0 )
1804 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1805
1806 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001807}
Jerry Yu90f152d2022-01-29 22:12:42 +08001808#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001809
1810/*
Jerry Yu687101b2021-09-14 16:03:56 +08001811 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1812 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001813static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001814{
XiaokangQian0fa66432021-11-15 03:33:57 +00001815 int ret;
1816
1817 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1818 if( ret != 0 )
1819 return( ret );
1820
1821 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1822 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001823}
1824
1825/*
1826 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1827 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001828static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001829{
Jerry Yu378254d2021-10-30 21:44:47 +08001830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001831 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001832 return( 0 );
1833}
1834
1835/*
1836 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1837 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001838static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001839{
Jerry Yu378254d2021-10-30 21:44:47 +08001840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1841 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1842
1843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1844 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1845
1846 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1847
1848 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1849 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001850}
1851
Jerry Yu92c6b402021-08-27 16:59:09 +08001852int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001853{
Jerry Yu92c6b402021-08-27 16:59:09 +08001854 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001855
Jerry Yue3b34122021-09-28 17:53:35 +08001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1857 mbedtls_ssl_states_str( ssl->state ),
1858 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001859
1860 switch( ssl->state )
1861 {
1862 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001863 * ssl->state is initialized as HELLO_REQUEST. It is the same
1864 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001865 */
1866 case MBEDTLS_SSL_HELLO_REQUEST:
1867 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001868 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001869 break;
1870
1871 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001872 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001873 break;
1874
1875 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001876 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001877 break;
1878
Jerry Yua93ac112021-10-27 16:31:48 +08001879#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001880 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1881 ret = ssl_tls13_process_certificate_request( ssl );
1882 break;
1883
Jerry Yu687101b2021-09-14 16:03:56 +08001884 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001885 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001886 break;
1887
1888 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001889 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001890 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001891#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001892
1893 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001894 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001895 break;
1896
Jerry Yu566c7812022-01-26 15:41:22 +08001897 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1898 ret = ssl_tls13_write_client_certificate( ssl );
1899 break;
1900
Ronald Cron9df7c802022-03-08 18:38:54 +01001901#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001902 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1903 ret = ssl_tls13_write_client_certificate_verify( ssl );
1904 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001905#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001906
Jerry Yu687101b2021-09-14 16:03:56 +08001907 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001908 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001909 break;
1910
1911 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001912 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001913 break;
1914
1915 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001916 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001917 break;
1918
Ronald Cron49ad6192021-11-24 16:25:31 +01001919 /*
1920 * Injection of dummy-CCS's for middlebox compatibility
1921 */
1922#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001923 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001924 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1925 if( ret == 0 )
1926 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1927 break;
1928
1929 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1930 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1931 if( ret == 0 )
1932 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001933 break;
1934#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1935
Jerry Yu92c6b402021-08-27 16:59:09 +08001936 default:
1937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1939 }
1940
1941 return( ret );
1942}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001943
Jerry Yufb4b6472022-01-27 15:03:26 +08001944#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001945
Jerry Yufb4b6472022-01-27 15:03:26 +08001946