blob: b608cd39a7f345216f09ebf600df98bf772a1436 [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
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
26#if defined(MBEDTLS_SSL_CLI_C)
27
Jerry Yubc20bdd2021-08-24 15:59:48 +080028#include <string.h>
29
Jerry Yu56fc07f2021-09-01 17:48:49 +080030#include "mbedtls/debug.h"
31#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080032#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yubdc71882021-09-14 19:30:36 +080034#include "ssl_misc.h"
35#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080036#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010037#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080038
Jerry Yubc20bdd2021-08-24 15:59:48 +080039/* Write extensions */
40
Jerry Yu92c6b402021-08-27 16:59:09 +080041/*
42 * ssl_tls13_write_supported_versions_ext():
43 *
44 * struct {
45 * ProtocolVersion versions<2..254>;
46 * } SupportedVersions;
47 */
Jerry Yuf4436812021-08-26 22:59:56 +080048static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080049 unsigned char *buf,
50 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000051 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080052{
53 unsigned char *p = buf;
54
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
63 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Jerry Yu92c6b402021-08-27 16:59:09 +080065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
66
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080067 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080071 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080072 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080074 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080075 *p++ = 0x2;
76
Jerry Yu0c63af62021-09-02 12:59:12 +080077 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 *
Jerry Yu0c63af62021-09-02 12:59:12 +080081 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080082 */
Jerry Yueecfbf02021-08-30 18:32:07 +080083 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
84 ssl->conf->max_minor_ver,
85 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080088 ssl->conf->max_major_ver,
89 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080090
Xiaofei Baid25fab62021-12-02 06:36:27 +000091 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080092
93 return( 0 );
94}
Jerry Yubc20bdd2021-08-24 15:59:48 +080095
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Jerry Yub85277e2021-10-13 13:36:05 +0800102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
103 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
113 return( 0 );
114}
115
Jerry Yubc20bdd2021-08-24 15:59:48 +0800116#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
117
XiaokangQian16acd4b2022-01-14 07:35:47 +0000118static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000119{
120 uint16_t group_id = ssl->handshake->offered_group_id;
121 if( group_id == 0 )
122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
123
XiaokangQian355e09a2022-01-20 11:14:50 +0000124#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000125 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000126 {
127 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
128 return( 0 );
129 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000130 else
131#endif /* MBEDTLS_ECDH_C */
132 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000133 {
134 /* Do something */
135 }
136
137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
138}
139
140/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800141 * Functions for writing key_share extension.
142 */
143#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800144static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800145 mbedtls_ssl_context *ssl,
146 uint16_t named_group,
147 unsigned char *buf,
148 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000149 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800150{
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100151 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
152 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
153 psa_key_attributes_t key_attributes;
154 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
155 size_t own_pubkey_len;
156 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
157 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800158
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800160
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100161 // --- Just for now --- !!!
162 psa_crypto_init();
Jerry Yu56fc07f2021-09-01 17:48:49 +0800163
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100164 /* Convert EC group to PSA key type. */
165 if( ( handshake->ecdh_psa_type =
166 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
167 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800168
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100169 if( ecdh_bits > 0xffff )
170 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
171 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800172
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100173 key_attributes = psa_key_attributes_init();
174 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
175 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
176 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
177 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
178
179 /* Generate ECDH private key. */
180 status = psa_generate_key( &key_attributes,
181 &handshake->ecdh_psa_privkey );
182 if( status != PSA_SUCCESS )
183 {
184 ret = psa_ssl_status_to_mbedtls( status );
185 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
186 return( ret );
187
188 }
189
190 /* Export the public part of the ECDH private key from PSA. */
191 status = psa_export_public_key( handshake->ecdh_psa_privkey,
192 own_pubkey, sizeof( own_pubkey ),
193 &own_pubkey_len );
194 if( status != PSA_SUCCESS )
195 {
196 ret = psa_ssl_status_to_mbedtls( status );
197 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
198 return( ret );
199
200 }
201
202 if( own_pubkey_len > (size_t)( end - buf ) )
203 {
204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
205 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
206 }
207
208 *out_len = own_pubkey_len;
209
210 memcpy( buf, &own_pubkey, own_pubkey_len );
211
Jerry Yu75336352021-09-01 15:59:36 +0800212 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800213}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214#endif /* MBEDTLS_ECDH_C */
215
Jerry Yub60e3cf2021-09-08 16:41:02 +0800216static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
217 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218{
219 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
220
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100223 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800224 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100225 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800226 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
227
Brett Warren14efd332021-10-06 09:32:11 +0100228 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800229 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000230 const mbedtls_ecp_curve_info *curve_info;
231 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
232 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100233 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800234 {
Brett Warren14efd332021-10-06 09:32:11 +0100235 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800236 return( 0 );
237 }
238 }
239#else
240 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800241 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242#endif /* MBEDTLS_ECDH_C */
243
244 /*
245 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 */
248
249 return( ret );
250}
251
252/*
253 * ssl_tls13_write_key_share_ext
254 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800255 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800256 *
257 * struct {
258 * NamedGroup group;
259 * opaque key_exchange<1..2^16-1>;
260 * } KeyShareEntry;
261 * struct {
262 * KeyShareEntry client_shares<0..2^16-1>;
263 * } KeyShareClientHello;
264 */
265static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
266 unsigned char *buf,
267 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000268 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269{
270 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000271 unsigned char *client_shares; /* Start of client_shares */
272 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
275
Xiaofei Baid25fab62021-12-02 06:36:27 +0000276 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800277
Jerry Yub60e3cf2021-09-08 16:41:02 +0800278 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279 * - extension_type (2 bytes)
280 * - extension_data_length (2 bytes)
281 * - client_shares_length (2 bytes)
282 */
283 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
284 p += 6;
285
286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
287
288 /* HRR could already have requested something else. */
289 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800290 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
291 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800293 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294 &group_id ) );
295 }
296
297 /*
298 * Dispatch to type-specific key generation function.
299 *
300 * So far, we're only supporting ECDHE. With the introduction
301 * of PQC KEMs, we'll want to have multiple branches, one per
302 * type of KEM, and dispatch to the corresponding crypto. And
303 * only one key share entry is allowed.
304 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000305 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800307 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800309 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000310 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800311 /* Length of key_exchange */
312 size_t key_exchange_len;
313
314 /* Check there is space for header of KeyShareEntry
315 * - group (2 bytes)
316 * - key_exchange_length (2 bytes)
317 */
318 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
319 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100320 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800321 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 p += key_exchange_len;
323 if( ret != 0 )
324 return( ret );
325
326 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000327 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000329 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 }
331 else
332#endif /* MBEDTLS_ECDH_C */
333 if( 0 /* other KEMs? */ )
334 {
335 /* Do something */
336 }
337 else
338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
339
Jerry Yub60e3cf2021-09-08 16:41:02 +0800340 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000341 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800342 if( client_shares_len == 0)
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800346 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 /* Write extension_type */
348 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
349 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800350 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800351 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800352 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353
354 /* Update offered_group_id field */
355 ssl->handshake->offered_group_id = group_id;
356
357 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000358 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359
Xiaofei Baid25fab62021-12-02 06:36:27 +0000360 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361
362 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
363
364cleanup:
365
366 return( ret );
367}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800368
Jerry Yue1b9c292021-09-10 10:08:31 +0800369#if defined(MBEDTLS_ECDH_C)
370
Jerry Yuc068b662021-10-11 22:30:19 +0800371static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800372{
373 const mbedtls_ecp_curve_info *curve_info;
374 mbedtls_ecp_group_id grp_id;
375#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
376 grp_id = ssl->handshake->ecdh_ctx.grp.id;
377#else
378 grp_id = ssl->handshake->ecdh_ctx.grp_id;
379#endif
380
381 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
382 if( curve_info == NULL )
383 {
384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
386 }
387
388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
389
Jerry Yue1b9c292021-09-10 10:08:31 +0800390 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800391 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800392
393 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
394 MBEDTLS_DEBUG_ECDH_QP );
395
396 return( 0 );
397}
398
Jerry Yuc068b662021-10-11 22:30:19 +0800399static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
400 const unsigned char *buf,
401 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800402{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100403 uint8_t *p = (uint8_t*)buf;
404 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800405
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100406 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
407 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
408 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800409
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100410 /* Check if key size is consistent with given buffer length. */
411 if ( peerkey_len > ( buf_len - 2 ) )
412 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800413
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100414 /* Store peer's ECDH public key. */
415 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
416 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800417
418 return( 0 );
419}
Jerry Yu4a173382021-10-11 21:45:31 +0800420#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800421
XiaokangQiand59be772022-01-24 10:12:51 +0000422/*
423 * ssl_tls13_parse_hrr_key_share_ext()
424 * Parse key_share extension in Hello Retry Request
425 *
426 * struct {
427 * NamedGroup selected_group;
428 * } KeyShareHelloRetryRequest;
429 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000430static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000431 const unsigned char *buf,
432 const unsigned char *end )
433{
XiaokangQianb851da82022-01-14 04:03:11 +0000434 const mbedtls_ecp_curve_info *curve_info = NULL;
435 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000436 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000437 int found = 0;
438
439 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
440 if( group_list == NULL )
441 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
442
443 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
444
445 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000446 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000447 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000449
450 /* Upon receipt of this extension in a HelloRetryRequest, the client
451 * MUST first verify that the selected_group field corresponds to a
452 * group which was provided in the "supported_groups" extension in the
453 * original ClientHello.
454 * The supported_group was based on the info in ssl->conf->group_list.
455 *
456 * If the server provided a key share that was not sent in the ClientHello
457 * then the client MUST abort the handshake with an "illegal_parameter" alert.
458 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000459 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000460 {
461 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000462 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000463 continue;
464
465 /* We found a match */
466 found = 1;
467 break;
468 }
469
470 /* Client MUST verify that the selected_group field does not
471 * correspond to a group which was provided in the "key_share"
472 * extension in the original ClientHello. If the server sent an
473 * HRR message with a key share already provided in the
474 * ClientHello then the client MUST abort the handshake with
475 * an "illegal_parameter" alert.
476 */
XiaokangQiand59be772022-01-24 10:12:51 +0000477 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000478 {
479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
480 MBEDTLS_SSL_PEND_FATAL_ALERT(
481 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
482 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
483 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
484 }
485
486 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000487 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000488
489 return( 0 );
490}
491
Jerry Yue1b9c292021-09-10 10:08:31 +0800492/*
Jerry Yub85277e2021-10-13 13:36:05 +0800493 * ssl_tls13_parse_key_share_ext()
494 * Parse key_share extension in Server Hello
495 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800496 * struct {
497 * KeyShareEntry server_share;
498 * } KeyShareServerHello;
499 * struct {
500 * NamedGroup group;
501 * opaque key_exchange<1..2^16-1>;
502 * } KeyShareEntry;
503 */
Jerry Yu4a173382021-10-11 21:45:31 +0800504static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800505 const unsigned char *buf,
506 const unsigned char *end )
507{
Jerry Yub85277e2021-10-13 13:36:05 +0800508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800509 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800510 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800511
Jerry Yu4a173382021-10-11 21:45:31 +0800512 /* ...
513 * NamedGroup group; (2 bytes)
514 * ...
515 */
516 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
517 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800518 p += 2;
519
Jerry Yu4a173382021-10-11 21:45:31 +0800520 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800522 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800523 {
524 MBEDTLS_SSL_DEBUG_MSG( 1,
525 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800526 (unsigned) offered_group, (unsigned) group ) );
527 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
528 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
529 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800530 }
531
532#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800533 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800534 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100535 const mbedtls_ecp_curve_info *curve_info =
536 mbedtls_ecp_curve_info_from_tls_id( group );
537 if( curve_info == NULL )
538 {
539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
541 }
542
543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
544
Jerry Yuc068b662021-10-11 22:30:19 +0800545 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 if( ret != 0 )
547 return( ret );
548 }
Jerry Yub85277e2021-10-13 13:36:05 +0800549 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800550#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800551 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800552 {
553 /* Do something */
554 }
555 else
556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
557
558 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
559 return( ret );
560}
561
Jerry Yubc20bdd2021-08-24 15:59:48 +0800562#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
563
XiaokangQiand59be772022-01-24 10:12:51 +0000564/*
565 * ssl_tls13_parse_cookie_ext()
566 * Parse cookie extension in Hello Retry Request
567 *
568 * struct {
569 * opaque cookie<1..2^16-1>;
570 * } Cookie;
571 *
572 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
573 * extension to the client (this is an exception to the usual rule that
574 * the only extensions that may be sent are those that appear in the
575 * ClientHello). When sending the new ClientHello, the client MUST copy
576 * the contents of the extension received in the HelloRetryRequest into
577 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
578 * cookies in their initial ClientHello in subsequent connections.
579 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000580static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
581 const unsigned char *buf,
582 const unsigned char *end )
583{
584 size_t cookie_len;
585 const unsigned char *p = buf;
586 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
587
588 /* Retrieve length field of cookie */
589 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
590 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
591 p += 2;
592
593 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
594 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
595
596 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000597 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000598 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
599 if( handshake->verify_cookie == NULL )
600 {
601 MBEDTLS_SSL_DEBUG_MSG( 1,
602 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
603 cookie_len ) );
604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
605 }
606
607 memcpy( handshake->verify_cookie, p, cookie_len );
608 handshake->verify_cookie_len = (unsigned char) cookie_len;
609
610 return( 0 );
611}
XiaokangQian43550bd2022-01-21 04:32:58 +0000612
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800613/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800614 * CipherSuite cipher_suites<2..2^16-2>;
615 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800616static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800617 mbedtls_ssl_context *ssl,
618 unsigned char *buf,
619 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000620 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800621{
Jerry Yufec982e2021-09-07 17:26:06 +0800622 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800623 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000624 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800625 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800626
Xiaofei Baid25fab62021-12-02 06:36:27 +0000627 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800628
629 /*
630 * Ciphersuite list
631 *
632 * This is a list of the symmetric cipher options supported by
633 * the client, specifically the record protection algorithm
634 * ( including secret key length ) and a hash to be used with
635 * HKDF, in descending order of client preference.
636 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800637 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800638
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800639 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800640 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
641 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800642
Jerry Yu0c63af62021-09-02 12:59:12 +0800643 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000644 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800645 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800646 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800647 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800648 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800649
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800650 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800651 if( ciphersuite_info == NULL )
652 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800653 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
654 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800655 continue;
656
657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800658 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800659 ciphersuite_info->name ) );
660
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800661 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800662 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
663 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
664 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800665 }
666
Jerry Yu0c63af62021-09-02 12:59:12 +0800667 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000668 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800669 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800670 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800671 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
672 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800673
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800674 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000675 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800676
Jerry Yu6a643102021-08-31 14:40:36 +0800677 return( 0 );
678}
679
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800680/*
681 * Structure of ClientHello message:
682 *
683 * struct {
684 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
685 * Random random;
686 * opaque legacy_session_id<0..32>;
687 * CipherSuite cipher_suites<2..2^16-2>;
688 * opaque legacy_compression_methods<1..2^8-1>;
689 * Extension extensions<8..2^16-1>;
690 * } ClientHello;
691 */
Jerry Yu08906d02021-08-31 11:05:27 +0800692static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800693 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800694 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000695 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800696{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800697
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000699 unsigned char *p_extensions_len; /* Pointer to extensions length */
700 size_t output_len; /* Length of buffer used by function */
701 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800702
Jerry Yubc20bdd2021-08-24 15:59:48 +0800703 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800704 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705
Xiaofei Baid25fab62021-12-02 06:36:27 +0000706 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800707
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800708 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800709 ssl->major_ver = ssl->conf->min_major_ver;
710 ssl->minor_ver = ssl->conf->min_minor_ver;
711
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800712 /*
713 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800714 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800715 *
716 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800717 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800718 */
Jerry Yufec982e2021-09-07 17:26:06 +0800719 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800720 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800721 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800722
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800723 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800724 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
725 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800726 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800727 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
728 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800729
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800730 /*
731 * Write legacy_session_id
732 *
733 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
734 * which has been merged with pre-shared keys in this version. A client
735 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
736 * this field to that value. In compatibility mode, this field MUST be
737 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
738 * a new 32-byte value. This value need not be random but SHOULD be
739 * unpredictable to avoid implementations fixating on a specific value
740 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
741 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800742 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100743#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
744 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
745 *p++ = (unsigned char)ssl->session_negotiate->id_len;
746 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
747 p += ssl->session_negotiate->id_len;
748
749 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
750 ssl->session_negotiate->id_len );
751#else
Jerry Yubbe09522021-09-06 21:17:54 +0800752 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
753 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100754#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800756 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800757 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800758 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800759 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800760 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800761
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800762 /* Write legacy_compression_methods
763 *
764 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765 * one byte set to zero, which corresponds to the 'null' compression
766 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800767 */
Jerry Yubbe09522021-09-06 21:17:54 +0800768 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
769 *p++ = 1;
770 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800771
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800772 /* Write extensions */
773
774 /* Keeping track of the included extensions */
775 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800776
777 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800778 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000779 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800780 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800782 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800784 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800785 */
Jerry Yubbe09522021-09-06 21:17:54 +0800786 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800787 if( ret != 0 )
788 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800789 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800790
791#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800792
Jerry Yub925f212022-01-12 11:17:02 +0800793 /*
794 * Add the extensions related to (EC)DHE ephemeral key establishment only if
795 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800796 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800797 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
798 {
799 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
800 if( ret != 0 )
801 return( ret );
802 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800803
Jerry Yuf46b0162022-01-11 16:28:00 +0800804 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
805 if( ret != 0 )
806 return( ret );
807 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800808
Jerry Yuf017ee42022-01-12 15:49:48 +0800809 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800810 if( ret != 0 )
811 return( ret );
812 p += output_len;
813 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800814#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
815
Xiaofei Bai15a56812021-11-05 10:52:12 +0000816#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000817 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000818 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
819 if( ret != 0 )
820 return( ret );
821 p += output_len;
822#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
823
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824 /* Add more extensions here */
825
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800826 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000827 extensions_len = p - p_extensions_len - 2;
828 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800830 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000831 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800832
Xiaofei Baid25fab62021-12-02 06:36:27 +0000833 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800834 return( 0 );
835}
836
Jerry Yu335aca92021-09-12 20:18:56 +0800837static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800838{
Jerry Yu92c6b402021-08-27 16:59:09 +0800839 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
840 return( 0 );
841}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800842
Jerry Yu92c6b402021-08-27 16:59:09 +0800843static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
844{
845 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800846
Jerry Yu92c6b402021-08-27 16:59:09 +0800847 if( ssl->conf->f_rng == NULL )
848 {
849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
850 return( MBEDTLS_ERR_SSL_NO_RNG );
851 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800852
Jerry Yu92c6b402021-08-27 16:59:09 +0800853 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
854 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800855 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800856 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800857 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800858 return( ret );
859 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800860
Ronald Cron49ad6192021-11-24 16:25:31 +0100861#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
862 /*
863 * Create a session identifier for the purpose of middlebox compatibility
864 * only if one has not been created already.
865 */
866 if( ssl->session_negotiate->id_len == 0 )
867 {
868 /* Creating a session id with 32 byte length */
869 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
870 ssl->session_negotiate->id, 32 ) ) != 0 )
871 {
872 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
873 return( ret );
874 }
875 ssl->session_negotiate->id_len = 32;
876 }
877#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
878
Jerry Yu6f13f642021-08-26 17:18:15 +0800879 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800880}
881
Jerry Yu92c6b402021-08-27 16:59:09 +0800882/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800883 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800884 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800885 */
886static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800887{
Jerry Yu92c6b402021-08-27 16:59:09 +0800888 int ret = 0;
889 unsigned char *buf;
890 size_t buf_len, msg_len;
891
892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
893
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800894 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800895
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800896 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
897 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
898 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800899
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800900 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800901 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800902 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800903
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800904 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
905 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800906 msg_len );
907 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800908
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800909 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
910 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
911 buf_len,
912 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800913
914cleanup:
915
916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
917 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800918}
919
Jerry Yu687101b2021-09-14 16:03:56 +0800920/*
Jerry Yu4a173382021-10-11 21:45:31 +0800921 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800922 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800923/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800924 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
925 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000926 * to indicate which message is expected and to be parsed next.
927 */
Jerry Yub85277e2021-10-13 13:36:05 +0800928#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
929#define SSL_SERVER_HELLO_COORDINATE_HRR 1
930static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
931 const unsigned char *buf,
932 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800933{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800934 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800935 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
936 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
937 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
938 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
939
940 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
941 *
Jerry Yu4a173382021-10-11 21:45:31 +0800942 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800943 * special value of the SHA-256 of "HelloRetryRequest".
944 *
945 * struct {
946 * ProtocolVersion legacy_version = 0x0303;
947 * Random random;
948 * opaque legacy_session_id_echo<0..32>;
949 * CipherSuite cipher_suite;
950 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800951 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800952 * } ServerHello;
953 *
954 */
Jerry Yub85277e2021-10-13 13:36:05 +0800955 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800956
957 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 {
Jerry Yub85277e2021-10-13 13:36:05 +0800959 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800960 }
961
Jerry Yub85277e2021-10-13 13:36:05 +0800962 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800963}
964
Jerry Yu745bb612021-10-13 22:01:04 +0800965/* Fetch and preprocess
966 * Returns a negative value on failure, and otherwise
967 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
968 * - SSL_SERVER_HELLO_COORDINATE_HRR
969 */
Jerry Yub85277e2021-10-13 13:36:05 +0800970static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
971 unsigned char **buf,
972 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800973{
Jerry Yu4a173382021-10-11 21:45:31 +0800974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800975
XiaokangQian355e09a2022-01-20 11:14:50 +0000976 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
977 MBEDTLS_SSL_HS_SERVER_HELLO,
978 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800979
Jerry Yub85277e2021-10-13 13:36:05 +0800980 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
981 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800982 {
Jerry Yu745bb612021-10-13 22:01:04 +0800983 case SSL_SERVER_HELLO_COORDINATE_HELLO:
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
985 break;
986 case SSL_SERVER_HELLO_COORDINATE_HRR:
987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000988 /* If a client receives a second
989 * HelloRetryRequest in the same connection (i.e., where the ClientHello
990 * was itself in response to a HelloRetryRequest), it MUST abort the
991 * handshake with an "unexpected_message" alert.
992 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000993 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000994 {
995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
996 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
997 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
998 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
999 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001000 /*
1001 * Clients must abort the handshake with an "illegal_parameter"
1002 * alert if the HelloRetryRequest would not result in any change
1003 * in the ClientHello.
1004 * In a PSK only key exchange that what we expect.
1005 */
1006 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1007 {
1008 MBEDTLS_SSL_DEBUG_MSG( 1,
1009 ( "Unexpected HRR in pure PSK key exchange." ) );
1010 MBEDTLS_SSL_PEND_FATAL_ALERT(
1011 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1012 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1013 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1014 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001015
XiaokangQiand9e068e2022-01-18 06:23:32 +00001016 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001017
Jerry Yu745bb612021-10-13 22:01:04 +08001018 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 }
1020
1021cleanup:
1022
1023 return( ret );
1024}
1025
Jerry Yu4a173382021-10-11 21:45:31 +08001026static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1027 const unsigned char **buf,
1028 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001029{
1030 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001031 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001032
Jerry Yude4fb2c2021-09-19 18:05:08 +08001033 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001034 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001035
Jerry Yu4a173382021-10-11 21:45:31 +08001036 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001037
1038 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001039 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1040 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001041 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1043 ssl->session_negotiate->id,
1044 ssl->session_negotiate->id_len );
1045 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001046 legacy_session_id_echo_len );
1047
1048 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1049 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1050
Jerry Yue1b9c292021-09-10 10:08:31 +08001051 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1052 }
1053
Jerry Yu4a173382021-10-11 21:45:31 +08001054 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 *buf = p;
1056
1057 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001058 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001059 return( 0 );
1060}
1061
Jerry Yu4a173382021-10-11 21:45:31 +08001062static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1063 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001064{
Jerry Yu4a173382021-10-11 21:45:31 +08001065 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1066
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001068 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001069 {
Jerry Yu4a173382021-10-11 21:45:31 +08001070 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 {
1072 return( 1 );
1073 }
1074 }
1075 return( 0 );
1076}
1077
1078/* Parse ServerHello message and configure context
1079 *
1080 * struct {
1081 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1082 * Random random;
1083 * opaque legacy_session_id_echo<0..32>;
1084 * CipherSuite cipher_suite;
1085 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001086 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 * } ServerHello;
1088 */
Jerry Yuc068b662021-10-11 22:30:19 +08001089static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1090 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001091 const unsigned char *end,
1092 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001093{
Jerry Yub85277e2021-10-13 13:36:05 +08001094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001096 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001097 size_t extensions_len;
1098 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001100 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001101 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001102 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001103
1104 /*
1105 * Check there is space for minimal fields
1106 *
1107 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001108 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001109 * - legacy_session_id_echo ( 1 byte ), minimum size
1110 * - cipher_suite ( 2 bytes)
1111 * - legacy_compression_method ( 1 byte )
1112 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001113 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001114
1115 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001116 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1117
Jerry Yu4a173382021-10-11 21:45:31 +08001118 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001119 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001120 * ...
1121 * with ProtocolVersion defined as:
1122 * uint16 ProtocolVersion;
1123 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1125 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1126 {
1127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1128 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1129 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001130 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1131 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001132 }
1133 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001134
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001135 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001136 * Random random;
1137 * ...
1138 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001139 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001140 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001141 if( !is_hrr )
1142 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001143 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001144 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1145 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1146 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1147 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001148 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001149
Jerry Yu4a173382021-10-11 21:45:31 +08001150 /* ...
1151 * opaque legacy_session_id_echo<0..32>;
1152 * ...
1153 */
1154 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001155 {
XiaokangQian52da5582022-01-26 09:49:29 +00001156 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001157 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001158 }
1159
Jerry Yu4a173382021-10-11 21:45:31 +08001160 /* ...
1161 * CipherSuite cipher_suite;
1162 * ...
1163 * with CipherSuite defined as:
1164 * uint8 CipherSuite[2];
1165 */
1166 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1168 p += 2;
1169
Jerry Yu4a173382021-10-11 21:45:31 +08001170
XiaokangQian355e09a2022-01-20 11:14:50 +00001171 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001172 /*
1173 * Check whether this ciphersuite is supported and offered.
1174 * Via the force_ciphersuite version we may have instructed the client
1175 * to use a different ciphersuite.
1176 */
Jerry Yu4a173382021-10-11 21:45:31 +08001177 if( ciphersuite_info == NULL ||
1178 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 {
XiaokangQian52da5582022-01-26 09:49:29 +00001180 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001181 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001182 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001183 * If we received an HRR before and that the proposed selected
1184 * ciphersuite in this server hello is not the same as the one
1185 * proposed in the HRR, we abort the handshake and send an
1186 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001187 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001188 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1189 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001190 {
XiaokangQian52da5582022-01-26 09:49:29 +00001191 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001192 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001193
XiaokangQian52da5582022-01-26 09:49:29 +00001194 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001195 {
1196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1197 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001198 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001199 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001200
Jerry Yu4a173382021-10-11 21:45:31 +08001201 /* Configure ciphersuites */
1202 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1203
XiaokangQian355e09a2022-01-20 11:14:50 +00001204 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 ssl->session_negotiate->ciphersuite = cipher_suite;
1206
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1208 cipher_suite, ciphersuite_info->name ) );
1209
1210#if defined(MBEDTLS_HAVE_TIME)
1211 ssl->session_negotiate->start = time( NULL );
1212#endif /* MBEDTLS_HAVE_TIME */
1213
Jerry Yu4a173382021-10-11 21:45:31 +08001214 /* ...
1215 * uint8 legacy_compression_method = 0;
1216 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001218 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001219 if( p[0] != 0 )
1220 {
Jerry Yub85277e2021-10-13 13:36:05 +08001221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001222 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001223 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 }
1225 p++;
1226
Jerry Yub85277e2021-10-13 13:36:05 +08001227 /* ...
1228 * Extension extensions<6..2^16-1>;
1229 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001230 * struct {
1231 * ExtensionType extension_type; (2 bytes)
1232 * opaque extension_data<0..2^16-1>;
1233 * } Extension;
1234 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001235 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001236 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001237 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001238
Jerry Yu4a173382021-10-11 21:45:31 +08001239 /* Check extensions do not go beyond the buffer of data. */
1240 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1241 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001242
Jerry Yu4a173382021-10-11 21:45:31 +08001243 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001244
Jerry Yu4a173382021-10-11 21:45:31 +08001245 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001246 {
1247 unsigned int extension_type;
1248 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001249 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001250
Jerry Yu4a173382021-10-11 21:45:31 +08001251 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001252 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1253 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1254 p += 4;
1255
Jerry Yu4a173382021-10-11 21:45:31 +08001256 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001257 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001258
1259 switch( extension_type )
1260 {
XiaokangQianb851da82022-01-14 04:03:11 +00001261 case MBEDTLS_TLS_EXT_COOKIE:
1262
XiaokangQiand9e068e2022-01-18 06:23:32 +00001263 if( !is_hrr )
1264 {
XiaokangQian52da5582022-01-26 09:49:29 +00001265 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001266 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001267 }
1268
XiaokangQian43550bd2022-01-21 04:32:58 +00001269 ret = ssl_tls13_parse_cookie_ext( ssl,
1270 p, extension_data_end );
1271 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001272 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001273 MBEDTLS_SSL_DEBUG_RET( 1,
1274 "ssl_tls13_parse_cookie_ext",
1275 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001276 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001277 }
XiaokangQianb851da82022-01-14 04:03:11 +00001278 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001279
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001281 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001282 MBEDTLS_SSL_DEBUG_MSG( 3,
1283 ( "found supported_versions extension" ) );
1284
Jerry Yuc068b662021-10-11 22:30:19 +08001285 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001286 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001287 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001288 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001289 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001290 break;
1291
1292 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001295
XiaokangQian52da5582022-01-26 09:49:29 +00001296 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001297 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001298
1299#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1300 case MBEDTLS_TLS_EXT_KEY_SHARE:
1301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001302 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1303 {
XiaokangQian52da5582022-01-26 09:49:29 +00001304 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001305 goto cleanup;
1306 }
1307
XiaokangQian53f20b72022-01-18 10:47:33 +00001308 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001309 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001310 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001311 else
XiaokangQianb851da82022-01-14 04:03:11 +00001312 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001313 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001314 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001315 {
1316 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001317 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001318 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001319 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001320 }
1321 break;
1322#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1323
1324 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001325 MBEDTLS_SSL_DEBUG_MSG(
1326 3,
1327 ( "unknown extension found: %u ( ignoring )",
1328 extension_type ) );
1329
XiaokangQian52da5582022-01-26 09:49:29 +00001330 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001331 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 }
1333
1334 p += extension_data_len;
1335 }
1336
XiaokangQian78b1fa72022-01-19 06:56:30 +00001337 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001338 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001340 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001341 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001342 }
1343
XiaokangQiand59be772022-01-24 10:12:51 +00001344cleanup:
1345
XiaokangQian52da5582022-01-26 09:49:29 +00001346 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001347 {
1348 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1349 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1350 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1351 }
XiaokangQian52da5582022-01-26 09:49:29 +00001352 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001353 {
1354 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1355 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1356 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1357 }
1358 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001359}
1360
XiaokangQian355e09a2022-01-20 11:14:50 +00001361static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001362{
Jerry Yub85277e2021-10-13 13:36:05 +08001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001364 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001365 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001366 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001367
Jerry Yub85277e2021-10-13 13:36:05 +08001368 /* Determine the key exchange mode:
1369 * 1) If both the pre_shared_key and key_share extensions were received
1370 * then the key exchange mode is PSK with EPHEMERAL.
1371 * 2) If only the pre_shared_key extension was received then the key
1372 * exchange mode is PSK-only.
1373 * 3) If only the key_share extension was received then the key
1374 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001375 */
Jerry Yub85277e2021-10-13 13:36:05 +08001376 switch( handshake->extensions_present &
1377 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001378 {
Jerry Yu745bb612021-10-13 22:01:04 +08001379 /* Only the pre_shared_key extension was received */
1380 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001381 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001382 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001383
Jerry Yu745bb612021-10-13 22:01:04 +08001384 /* Only the key_share extension was received */
1385 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001386 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001387 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001388
Jerry Yu745bb612021-10-13 22:01:04 +08001389 /* Both the pre_shared_key and key_share extensions were received */
1390 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001391 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001392 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001393
Jerry Yu745bb612021-10-13 22:01:04 +08001394 /* Neither pre_shared_key nor key_share extension was received */
1395 default:
1396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1397 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1398 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001399 }
1400
1401 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1402 *
1403 * TODO: We don't have to do this in case we offered 0-RTT and the
1404 * server accepted it. In this case, we could skip generating
1405 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001406 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001407 if( ret != 0 )
1408 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001410 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001411 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001412 }
1413
1414 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001415 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001416 if( ret != 0 )
1417 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001419 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001420 }
1421
1422 /* Next evolution in key schedule: Establish handshake secret and
1423 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001424 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001425 if( ret != 0 )
1426 {
Jerry Yuc068b662021-10-11 22:30:19 +08001427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1428 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001429 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001430 }
1431
Jerry Yub85277e2021-10-13 13:36:05 +08001432 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001433 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001434 {
1435 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1436 goto cleanup;
1437 }
Jerry Yu0b177842021-09-05 19:41:30 +08001438
1439 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1440 ssl->conf->endpoint,
1441 ssl->session_negotiate->ciphersuite,
1442 &traffic_keys,
1443 ssl );
1444 if( ret != 0 )
1445 {
1446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001447 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001448 }
1449
Jerry Yub85277e2021-10-13 13:36:05 +08001450 handshake->transform_handshake = transform_handshake;
1451 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001452
1453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1454 ssl->session_in = ssl->session_negotiate;
1455
1456 /*
1457 * State machine update
1458 */
1459 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1460
Jerry Yu4a173382021-10-11 21:45:31 +08001461cleanup:
1462
Jerry Yu0b177842021-09-05 19:41:30 +08001463 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001464 if( ret != 0 )
1465 {
Jerry Yub85277e2021-10-13 13:36:05 +08001466 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001467
Jerry Yu4a173382021-10-11 21:45:31 +08001468 MBEDTLS_SSL_PEND_FATAL_ALERT(
1469 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1470 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1471 }
1472 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001473}
1474
XiaokangQian355e09a2022-01-20 11:14:50 +00001475static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001476{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001477#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1479
XiaokangQian647719a2021-12-07 09:16:29 +00001480#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1481 /* If not offering early data, the client sends a dummy CCS record
1482 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001483 * its second ClientHello or before its encrypted handshake flight.
1484 */
XiaokangQian647719a2021-12-07 09:16:29 +00001485 mbedtls_ssl_handshake_set_state( ssl,
1486 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1487#else
1488 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1489#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1490
XiaokangQian78b1fa72022-01-19 06:56:30 +00001491 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001492
XiaokangQian78b1fa72022-01-19 06:56:30 +00001493 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001494 * We are going to re-generate a shared secret corresponding to the group
1495 * selected by the server, which is different from the group for which we
1496 * generated a shared secret in the first client hello.
1497 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001498 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001499 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001500 if( ret != 0 )
1501 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001502#else
1503 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001504#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001505
1506 return( 0 );
1507}
1508
Jerry Yue1b9c292021-09-10 10:08:31 +08001509/*
Jerry Yu4a173382021-10-11 21:45:31 +08001510 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001511 * Handler for MBEDTLS_SSL_SERVER_HELLO
1512 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001513static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001514{
Jerry Yu4a173382021-10-11 21:45:31 +08001515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001516 unsigned char *buf = NULL;
1517 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001518 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001519
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1521
1522 /* Coordination step
1523 * - Fetch record
1524 * - Make sure it's either a ServerHello or a HRR.
1525 * - Switch processing routine in case of HRR
1526 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001527 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1528 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1529
XiaokangQian16acd4b2022-01-14 07:35:47 +00001530 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001531 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001532 goto cleanup;
1533 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001534 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001535
XiaokangQianb851da82022-01-14 04:03:11 +00001536 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001537 buf + buf_len,
1538 is_hrr ) );
1539 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001540 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1541
XiaokangQianb851da82022-01-14 04:03:11 +00001542 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1543 MBEDTLS_SSL_HS_SERVER_HELLO,
1544 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001545
XiaokangQiand9e068e2022-01-18 06:23:32 +00001546 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001547 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001548 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001549 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001550
1551cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1553 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001554 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001555}
1556
1557/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001558 *
1559 * EncryptedExtensions message
1560 *
1561 * The EncryptedExtensions message contains any extensions which
1562 * should be protected, i.e., any which are not needed to establish
1563 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001564 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001565
1566/*
1567 * Overview
1568 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001569
1570/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001571static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001572
XiaokangQian97799ac2021-10-11 10:05:54 +00001573static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1574 const unsigned char *buf,
1575 const unsigned char *end );
1576static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001577
1578/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001579 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001581static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001582{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001583 int ret;
1584 unsigned char *buf;
1585 size_t buf_len;
1586
1587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1588
Xiaofei Bai746f9482021-11-12 08:53:56 +00001589 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001590 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591 &buf, &buf_len ) );
1592
1593 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001594 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001595 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001596
Xiaofei Bai746f9482021-11-12 08:53:56 +00001597 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001598 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001599
XiaokangQian97799ac2021-10-11 10:05:54 +00001600 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001601
1602cleanup:
1603
1604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1605 return( ret );
1606
1607}
1608
XiaokangQian08da26c2021-10-09 10:12:11 +00001609/* Parse EncryptedExtensions message
1610 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001611 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001612 * } EncryptedExtensions;
1613 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001614static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1615 const unsigned char *buf,
1616 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001617{
1618 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001619 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001620 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001621 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001622
XiaokangQian08da26c2021-10-09 10:12:11 +00001623 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001624 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001625 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001626
XiaokangQian97799ac2021-10-11 10:05:54 +00001627 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001628 extensions_end = p + extensions_len;
1629 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001630
XiaokangQian8db25ff2021-10-13 05:56:18 +00001631 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001632 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001633 unsigned int extension_type;
1634 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001635
XiaokangQian08da26c2021-10-09 10:12:11 +00001636 /*
1637 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001638 * ExtensionType extension_type; (2 bytes)
1639 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001640 * } Extension;
1641 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001642 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001643 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1644 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1645 p += 4;
1646
XiaokangQian8db25ff2021-10-13 05:56:18 +00001647 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001648
XiaokangQian97799ac2021-10-11 10:05:54 +00001649 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001650 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001651 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001652 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001653 switch( extension_type )
1654 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001655
XiaokangQian08da26c2021-10-09 10:12:11 +00001656 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1658 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001659
XiaokangQian08da26c2021-10-09 10:12:11 +00001660 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001661 MBEDTLS_SSL_DEBUG_MSG(
1662 3, ( "unsupported extension found: %u ", extension_type) );
1663 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001664 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001665 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1666 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001667 }
1668
XiaokangQian08da26c2021-10-09 10:12:11 +00001669 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001670 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001671
XiaokangQian97799ac2021-10-11 10:05:54 +00001672 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001673 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001674 {
1675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001676 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001677 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001678 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001679 }
1680
1681 return( ret );
1682}
1683
XiaokangQian97799ac2021-10-11 10:05:54 +00001684static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001685{
Jerry Yua93ac112021-10-27 16:31:48 +08001686#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001687 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001688 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1689 else
Jerry Yud2674312021-10-29 10:08:19 +08001690 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001691#else
1692 ((void) ssl);
1693 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1694#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001695 return( 0 );
1696}
1697
Jerry Yua93ac112021-10-27 16:31:48 +08001698#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001699/*
Jerry Yud2674312021-10-29 10:08:19 +08001700 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1701 */
1702static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1703{
1704 int ret = mbedtls_ssl_read_record( ssl, 0 );
1705
1706 if( ret != 0 )
1707 {
1708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1709 return( ret );
1710 }
1711
1712 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1713 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1714 {
1715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1716 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1717 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1718 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1719 }
1720
1721 ssl->keep_current_message = 1;
1722 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1723
1724 return( 0 );
1725}
1726
1727/*
Jerry Yu687101b2021-09-14 16:03:56 +08001728 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1729 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001730static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001731{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001732 int ret;
1733
1734 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001735 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001736 return( ret );
1737
Jerry Yu687101b2021-09-14 16:03:56 +08001738 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1739 return( 0 );
1740}
1741
1742/*
1743 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1744 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001745static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001746{
Jerry Yu30b071c2021-09-12 20:16:03 +08001747 int ret;
1748
1749 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1750 if( ret != 0 )
1751 return( ret );
1752
Jerry Yu687101b2021-09-14 16:03:56 +08001753 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1754 return( 0 );
1755}
Jerry Yua93ac112021-10-27 16:31:48 +08001756#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001757
Jerry Yu687101b2021-09-14 16:03:56 +08001758/*
1759 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1760 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001761static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001762{
XiaokangQianac0385c2021-11-03 06:40:11 +00001763 int ret;
1764
XiaokangQianc5c39d52021-11-09 11:55:10 +00001765 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001766 if( ret != 0 )
1767 return( ret );
1768
Ronald Cron49ad6192021-11-24 16:25:31 +01001769#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1770 mbedtls_ssl_handshake_set_state(
1771 ssl,
1772 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1773#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001774 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001775#endif
1776
XiaokangQianac0385c2021-11-03 06:40:11 +00001777 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001778}
1779
1780/*
Ronald Crond4c64022021-12-06 09:06:46 +01001781 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1782 */
1783#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1784static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1785{
1786 int ret;
1787
1788 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1789 if( ret != 0 )
1790 return( ret );
1791
Ronald Crond4c64022021-12-06 09:06:46 +01001792 return( 0 );
1793}
1794#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1795
1796/*
Jerry Yu687101b2021-09-14 16:03:56 +08001797 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1798 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001799static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001800{
XiaokangQian0fa66432021-11-15 03:33:57 +00001801 int ret;
1802
Ronald Cron49ad6192021-11-24 16:25:31 +01001803 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1804
XiaokangQian0fa66432021-11-15 03:33:57 +00001805 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1806 if( ret != 0 )
1807 return( ret );
1808
1809 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1810 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001811}
1812
1813/*
1814 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1815 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001816static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001817{
Jerry Yu378254d2021-10-30 21:44:47 +08001818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001819 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001820 return( 0 );
1821}
1822
1823/*
1824 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1825 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001826static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001827{
Jerry Yu378254d2021-10-30 21:44:47 +08001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1829 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1830
1831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1832 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1833
1834 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1835
1836 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1837 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001838}
1839
Jerry Yu92c6b402021-08-27 16:59:09 +08001840int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001841{
Jerry Yu92c6b402021-08-27 16:59:09 +08001842 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001843
Jerry Yue3b34122021-09-28 17:53:35 +08001844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1845 mbedtls_ssl_states_str( ssl->state ),
1846 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001847
1848 switch( ssl->state )
1849 {
1850 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001851 * ssl->state is initialized as HELLO_REQUEST. It is the same
1852 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001853 */
1854 case MBEDTLS_SSL_HELLO_REQUEST:
1855 case MBEDTLS_SSL_CLIENT_HELLO:
1856 ret = ssl_tls13_write_client_hello( ssl );
1857 break;
1858
1859 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001860 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001861 break;
1862
1863 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001864 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001865 break;
1866
Jerry Yua93ac112021-10-27 16:31:48 +08001867#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001868 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1869 ret = ssl_tls13_process_certificate_request( ssl );
1870 break;
1871
Jerry Yu687101b2021-09-14 16:03:56 +08001872 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001873 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001874 break;
1875
1876 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001877 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001878 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001879#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001880
1881 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001882 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001883 break;
1884
Jerry Yu687101b2021-09-14 16:03:56 +08001885 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001886 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001887 break;
1888
1889 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001890 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001891 break;
1892
1893 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001894 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001895 break;
1896
Ronald Cron49ad6192021-11-24 16:25:31 +01001897 /*
1898 * Injection of dummy-CCS's for middlebox compatibility
1899 */
1900#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1901 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001902 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001903 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001904 break;
1905#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1906
Jerry Yu92c6b402021-08-27 16:59:09 +08001907 default:
1908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1910 }
1911
1912 return( ret );
1913}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001914
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001915#endif /* MBEDTLS_SSL_CLI_C */
1916
Ronald Cron6f135e12021-12-08 16:57:54 +01001917#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */