blob: b7cb995d72590261f69b23b31a75031e2b20c450 [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{
403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
404
Jerry Yuc068b662021-10-11 22:30:19 +0800405 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800406 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800407 if( ret != 0 )
408 {
409 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800410
411 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
412 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
413 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800414 }
415
Jerry Yuc068b662021-10-11 22:30:19 +0800416 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800417 {
Jerry Yuc068b662021-10-11 22:30:19 +0800418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800419
Jerry Yu4a173382021-10-11 21:45:31 +0800420 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
421 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
422 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800423 }
424
425 return( 0 );
426}
Jerry Yu4a173382021-10-11 21:45:31 +0800427#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800428
XiaokangQiand59be772022-01-24 10:12:51 +0000429/*
430 * ssl_tls13_parse_hrr_key_share_ext()
431 * Parse key_share extension in Hello Retry Request
432 *
433 * struct {
434 * NamedGroup selected_group;
435 * } KeyShareHelloRetryRequest;
436 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000437static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000438 const unsigned char *buf,
439 const unsigned char *end )
440{
XiaokangQianb851da82022-01-14 04:03:11 +0000441 const mbedtls_ecp_curve_info *curve_info = NULL;
442 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000443 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000444 int found = 0;
445
446 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
447 if( group_list == NULL )
448 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
449
450 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
451
452 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000453 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000454 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000456
457 /* Upon receipt of this extension in a HelloRetryRequest, the client
458 * MUST first verify that the selected_group field corresponds to a
459 * group which was provided in the "supported_groups" extension in the
460 * original ClientHello.
461 * The supported_group was based on the info in ssl->conf->group_list.
462 *
463 * If the server provided a key share that was not sent in the ClientHello
464 * then the client MUST abort the handshake with an "illegal_parameter" alert.
465 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000466 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000467 {
468 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000469 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000470 continue;
471
472 /* We found a match */
473 found = 1;
474 break;
475 }
476
477 /* Client MUST verify that the selected_group field does not
478 * correspond to a group which was provided in the "key_share"
479 * extension in the original ClientHello. If the server sent an
480 * HRR message with a key share already provided in the
481 * ClientHello then the client MUST abort the handshake with
482 * an "illegal_parameter" alert.
483 */
XiaokangQiand59be772022-01-24 10:12:51 +0000484 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000485 {
486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
487 MBEDTLS_SSL_PEND_FATAL_ALERT(
488 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
489 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
490 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
491 }
492
493 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000494 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000495
496 return( 0 );
497}
498
Jerry Yue1b9c292021-09-10 10:08:31 +0800499/*
Jerry Yub85277e2021-10-13 13:36:05 +0800500 * ssl_tls13_parse_key_share_ext()
501 * Parse key_share extension in Server Hello
502 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800503 * struct {
504 * KeyShareEntry server_share;
505 * } KeyShareServerHello;
506 * struct {
507 * NamedGroup group;
508 * opaque key_exchange<1..2^16-1>;
509 * } KeyShareEntry;
510 */
Jerry Yu4a173382021-10-11 21:45:31 +0800511static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800512 const unsigned char *buf,
513 const unsigned char *end )
514{
Jerry Yub85277e2021-10-13 13:36:05 +0800515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800516 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800517 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800518
Jerry Yu4a173382021-10-11 21:45:31 +0800519 /* ...
520 * NamedGroup group; (2 bytes)
521 * ...
522 */
523 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
524 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800525 p += 2;
526
Jerry Yu4a173382021-10-11 21:45:31 +0800527 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800529 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800530 {
531 MBEDTLS_SSL_DEBUG_MSG( 1,
532 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800533 (unsigned) offered_group, (unsigned) group ) );
534 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
535 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
536 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800537 }
538
539#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800540 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800541 {
542 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800543 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800544 if( ret != 0 )
545 return( ret );
546 }
Jerry Yub85277e2021-10-13 13:36:05 +0800547 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800548#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800549 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800550 {
551 /* Do something */
552 }
553 else
554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
555
556 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
557 return( ret );
558}
559
Jerry Yubc20bdd2021-08-24 15:59:48 +0800560#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
561
XiaokangQiand59be772022-01-24 10:12:51 +0000562/*
563 * ssl_tls13_parse_cookie_ext()
564 * Parse cookie extension in Hello Retry Request
565 *
566 * struct {
567 * opaque cookie<1..2^16-1>;
568 * } Cookie;
569 *
570 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
571 * extension to the client (this is an exception to the usual rule that
572 * the only extensions that may be sent are those that appear in the
573 * ClientHello). When sending the new ClientHello, the client MUST copy
574 * the contents of the extension received in the HelloRetryRequest into
575 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
576 * cookies in their initial ClientHello in subsequent connections.
577 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000578static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
579 const unsigned char *buf,
580 const unsigned char *end )
581{
582 size_t cookie_len;
583 const unsigned char *p = buf;
584 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
585
586 /* Retrieve length field of cookie */
587 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
588 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
589 p += 2;
590
591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
592 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
593
594 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000595 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000596 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
597 if( handshake->verify_cookie == NULL )
598 {
599 MBEDTLS_SSL_DEBUG_MSG( 1,
600 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
601 cookie_len ) );
602 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
603 }
604
605 memcpy( handshake->verify_cookie, p, cookie_len );
606 handshake->verify_cookie_len = (unsigned char) cookie_len;
607
608 return( 0 );
609}
XiaokangQian43550bd2022-01-21 04:32:58 +0000610
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800611/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800612 * CipherSuite cipher_suites<2..2^16-2>;
613 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800614static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800615 mbedtls_ssl_context *ssl,
616 unsigned char *buf,
617 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000618 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800619{
Jerry Yufec982e2021-09-07 17:26:06 +0800620 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800621 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000622 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800623 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800624
Xiaofei Baid25fab62021-12-02 06:36:27 +0000625 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800626
627 /*
628 * Ciphersuite list
629 *
630 * This is a list of the symmetric cipher options supported by
631 * the client, specifically the record protection algorithm
632 * ( including secret key length ) and a hash to be used with
633 * HKDF, in descending order of client preference.
634 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800635 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800636
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800637 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800638 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
639 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800640
Jerry Yu0c63af62021-09-02 12:59:12 +0800641 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000642 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800643 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800644 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800645 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800646 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800647
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800648 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800649 if( ciphersuite_info == NULL )
650 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800651 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
652 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800653 continue;
654
655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800656 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800657 ciphersuite_info->name ) );
658
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800659 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800660 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
661 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
662 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800663 }
664
Jerry Yu0c63af62021-09-02 12:59:12 +0800665 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000666 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800667 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800668 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800669 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
670 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800671
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800672 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000673 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800674
Jerry Yu6a643102021-08-31 14:40:36 +0800675 return( 0 );
676}
677
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800678/*
679 * Structure of ClientHello message:
680 *
681 * struct {
682 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
683 * Random random;
684 * opaque legacy_session_id<0..32>;
685 * CipherSuite cipher_suites<2..2^16-2>;
686 * opaque legacy_compression_methods<1..2^8-1>;
687 * Extension extensions<8..2^16-1>;
688 * } ClientHello;
689 */
Jerry Yu08906d02021-08-31 11:05:27 +0800690static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800691 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800692 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000693 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800694{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800695
Jerry Yubc20bdd2021-08-24 15:59:48 +0800696 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000697 unsigned char *p_extensions_len; /* Pointer to extensions length */
698 size_t output_len; /* Length of buffer used by function */
699 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800700
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800702 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800703
Xiaofei Baid25fab62021-12-02 06:36:27 +0000704 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800706 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800707 ssl->major_ver = ssl->conf->min_major_ver;
708 ssl->minor_ver = ssl->conf->min_minor_ver;
709
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800710 /*
711 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800712 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800713 *
714 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800715 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800716 */
Jerry Yufec982e2021-09-07 17:26:06 +0800717 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800718 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800719 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800720
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800721 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800722 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
723 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800724 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800725 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
726 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800727
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800728 /*
729 * Write legacy_session_id
730 *
731 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
732 * which has been merged with pre-shared keys in this version. A client
733 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
734 * this field to that value. In compatibility mode, this field MUST be
735 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
736 * a new 32-byte value. This value need not be random but SHOULD be
737 * unpredictable to avoid implementations fixating on a specific value
738 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
739 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800740 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100741#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
742 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
743 *p++ = (unsigned char)ssl->session_negotiate->id_len;
744 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
745 p += ssl->session_negotiate->id_len;
746
747 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
748 ssl->session_negotiate->id_len );
749#else
Jerry Yubbe09522021-09-06 21:17:54 +0800750 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
751 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100752#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800753
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800754 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800755 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800756 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800757 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800758 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800760 /* Write legacy_compression_methods
761 *
762 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800763 * one byte set to zero, which corresponds to the 'null' compression
764 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765 */
Jerry Yubbe09522021-09-06 21:17:54 +0800766 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
767 *p++ = 1;
768 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800770 /* Write extensions */
771
772 /* Keeping track of the included extensions */
773 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800774
775 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800776 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000777 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800778 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800779
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800780 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800782 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783 */
Jerry Yubbe09522021-09-06 21:17:54 +0800784 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800785 if( ret != 0 )
786 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800787 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800788
789#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800790
Jerry Yub925f212022-01-12 11:17:02 +0800791 /*
792 * Add the extensions related to (EC)DHE ephemeral key establishment only if
793 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800794 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800795 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
796 {
797 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
798 if( ret != 0 )
799 return( ret );
800 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800801
Jerry Yuf46b0162022-01-11 16:28:00 +0800802 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
803 if( ret != 0 )
804 return( ret );
805 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800806
Jerry Yuf017ee42022-01-12 15:49:48 +0800807 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800808 if( ret != 0 )
809 return( ret );
810 p += output_len;
811 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800812#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
813
Xiaofei Bai15a56812021-11-05 10:52:12 +0000814#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000815 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000816 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
817 if( ret != 0 )
818 return( ret );
819 p += output_len;
820#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
821
Jerry Yubc20bdd2021-08-24 15:59:48 +0800822 /* Add more extensions here */
823
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800824 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000825 extensions_len = p - p_extensions_len - 2;
826 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800828 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000829 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800830
Xiaofei Baid25fab62021-12-02 06:36:27 +0000831 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800832 return( 0 );
833}
834
Jerry Yu335aca92021-09-12 20:18:56 +0800835static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800836{
Jerry Yu92c6b402021-08-27 16:59:09 +0800837 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
838 return( 0 );
839}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800840
Jerry Yu92c6b402021-08-27 16:59:09 +0800841static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
842{
843 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800844
Jerry Yu92c6b402021-08-27 16:59:09 +0800845 if( ssl->conf->f_rng == NULL )
846 {
847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
848 return( MBEDTLS_ERR_SSL_NO_RNG );
849 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800850
Jerry Yu92c6b402021-08-27 16:59:09 +0800851 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
852 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800853 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800854 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800855 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800856 return( ret );
857 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800858
Ronald Cron49ad6192021-11-24 16:25:31 +0100859#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
860 /*
861 * Create a session identifier for the purpose of middlebox compatibility
862 * only if one has not been created already.
863 */
864 if( ssl->session_negotiate->id_len == 0 )
865 {
866 /* Creating a session id with 32 byte length */
867 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
868 ssl->session_negotiate->id, 32 ) ) != 0 )
869 {
870 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
871 return( ret );
872 }
873 ssl->session_negotiate->id_len = 32;
874 }
875#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
876
Jerry Yu6f13f642021-08-26 17:18:15 +0800877 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800878}
879
Jerry Yu92c6b402021-08-27 16:59:09 +0800880/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800881 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800882 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800883 */
884static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800885{
Jerry Yu92c6b402021-08-27 16:59:09 +0800886 int ret = 0;
887 unsigned char *buf;
888 size_t buf_len, msg_len;
889
890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
891
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800892 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800893
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800894 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
895 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
896 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800897
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800898 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800899 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800900 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800901
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800902 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
903 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800904 msg_len );
905 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800906
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800907 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
908 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
909 buf_len,
910 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800911
912cleanup:
913
914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
915 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800916}
917
Jerry Yu687101b2021-09-14 16:03:56 +0800918/*
Jerry Yu4a173382021-10-11 21:45:31 +0800919 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800920 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800921/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800922 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
923 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000924 * to indicate which message is expected and to be parsed next.
925 */
Jerry Yub85277e2021-10-13 13:36:05 +0800926#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
927#define SSL_SERVER_HELLO_COORDINATE_HRR 1
928static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
929 const unsigned char *buf,
930 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800931{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800932 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800933 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
934 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
935 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
936 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
937
938 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
939 *
Jerry Yu4a173382021-10-11 21:45:31 +0800940 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 * special value of the SHA-256 of "HelloRetryRequest".
942 *
943 * struct {
944 * ProtocolVersion legacy_version = 0x0303;
945 * Random random;
946 * opaque legacy_session_id_echo<0..32>;
947 * CipherSuite cipher_suite;
948 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800949 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800950 * } ServerHello;
951 *
952 */
Jerry Yub85277e2021-10-13 13:36:05 +0800953 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800954
955 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800956 {
Jerry Yub85277e2021-10-13 13:36:05 +0800957 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 }
959
Jerry Yub85277e2021-10-13 13:36:05 +0800960 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800961}
962
Jerry Yu745bb612021-10-13 22:01:04 +0800963/* Fetch and preprocess
964 * Returns a negative value on failure, and otherwise
965 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
966 * - SSL_SERVER_HELLO_COORDINATE_HRR
967 */
Jerry Yub85277e2021-10-13 13:36:05 +0800968static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
969 unsigned char **buf,
970 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800971{
Jerry Yu4a173382021-10-11 21:45:31 +0800972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973
XiaokangQian355e09a2022-01-20 11:14:50 +0000974 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
975 MBEDTLS_SSL_HS_SERVER_HELLO,
976 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800977
Jerry Yub85277e2021-10-13 13:36:05 +0800978 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
979 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800980 {
Jerry Yu745bb612021-10-13 22:01:04 +0800981 case SSL_SERVER_HELLO_COORDINATE_HELLO:
982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
983 break;
984 case SSL_SERVER_HELLO_COORDINATE_HRR:
985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000986 /* If a client receives a second
987 * HelloRetryRequest in the same connection (i.e., where the ClientHello
988 * was itself in response to a HelloRetryRequest), it MUST abort the
989 * handshake with an "unexpected_message" alert.
990 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000991 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
994 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
995 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
996 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
997 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000998 /*
999 * Clients must abort the handshake with an "illegal_parameter"
1000 * alert if the HelloRetryRequest would not result in any change
1001 * in the ClientHello.
1002 * In a PSK only key exchange that what we expect.
1003 */
1004 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1005 {
1006 MBEDTLS_SSL_DEBUG_MSG( 1,
1007 ( "Unexpected HRR in pure PSK key exchange." ) );
1008 MBEDTLS_SSL_PEND_FATAL_ALERT(
1009 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1010 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1011 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1012 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001013
XiaokangQiand9e068e2022-01-18 06:23:32 +00001014 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001015
Jerry Yu745bb612021-10-13 22:01:04 +08001016 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 }
1018
1019cleanup:
1020
1021 return( ret );
1022}
1023
Jerry Yu4a173382021-10-11 21:45:31 +08001024static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1025 const unsigned char **buf,
1026 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001027{
1028 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001029 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001030
Jerry Yude4fb2c2021-09-19 18:05:08 +08001031 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001032 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001033
Jerry Yu4a173382021-10-11 21:45:31 +08001034 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001035
1036 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001037 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1038 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001039 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1041 ssl->session_negotiate->id,
1042 ssl->session_negotiate->id_len );
1043 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001044 legacy_session_id_echo_len );
1045
1046 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1047 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1048
Jerry Yue1b9c292021-09-10 10:08:31 +08001049 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1050 }
1051
Jerry Yu4a173382021-10-11 21:45:31 +08001052 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001053 *buf = p;
1054
1055 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001056 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001057 return( 0 );
1058}
1059
Jerry Yu4a173382021-10-11 21:45:31 +08001060static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1061 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001062{
Jerry Yu4a173382021-10-11 21:45:31 +08001063 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1064
Jerry Yue1b9c292021-09-10 10:08:31 +08001065 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001066 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 {
Jerry Yu4a173382021-10-11 21:45:31 +08001068 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001069 {
1070 return( 1 );
1071 }
1072 }
1073 return( 0 );
1074}
1075
1076/* Parse ServerHello message and configure context
1077 *
1078 * struct {
1079 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1080 * Random random;
1081 * opaque legacy_session_id_echo<0..32>;
1082 * CipherSuite cipher_suite;
1083 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001084 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 * } ServerHello;
1086 */
Jerry Yuc068b662021-10-11 22:30:19 +08001087static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1088 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001089 const unsigned char *end,
1090 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001091{
Jerry Yub85277e2021-10-13 13:36:05 +08001092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001093 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001094 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001095 size_t extensions_len;
1096 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001097 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001099 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001100 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001101
1102 /*
1103 * Check there is space for minimal fields
1104 *
1105 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001106 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 * - legacy_session_id_echo ( 1 byte ), minimum size
1108 * - cipher_suite ( 2 bytes)
1109 * - legacy_compression_method ( 1 byte )
1110 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001111 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
1113 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001114 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1115
Jerry Yu4a173382021-10-11 21:45:31 +08001116 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001117 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001118 * ...
1119 * with ProtocolVersion defined as:
1120 * uint16 ProtocolVersion;
1121 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1123 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1124 {
1125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1126 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1127 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001128 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1129 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001130 }
1131 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001132
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001133 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001134 * Random random;
1135 * ...
1136 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001137 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001138 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001139 if( !is_hrr )
1140 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001141 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001142 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1143 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1144 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1145 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001146 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001147
Jerry Yu4a173382021-10-11 21:45:31 +08001148 /* ...
1149 * opaque legacy_session_id_echo<0..32>;
1150 * ...
1151 */
1152 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001153 {
XiaokangQian52da5582022-01-26 09:49:29 +00001154 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001155 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 }
1157
Jerry Yu4a173382021-10-11 21:45:31 +08001158 /* ...
1159 * CipherSuite cipher_suite;
1160 * ...
1161 * with CipherSuite defined as:
1162 * uint8 CipherSuite[2];
1163 */
1164 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1166 p += 2;
1167
Jerry Yu4a173382021-10-11 21:45:31 +08001168
XiaokangQian355e09a2022-01-20 11:14:50 +00001169 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001170 /*
1171 * Check whether this ciphersuite is supported and offered.
1172 * Via the force_ciphersuite version we may have instructed the client
1173 * to use a different ciphersuite.
1174 */
Jerry Yu4a173382021-10-11 21:45:31 +08001175 if( ciphersuite_info == NULL ||
1176 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001177 {
XiaokangQian52da5582022-01-26 09:49:29 +00001178 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001180 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001181 * If we received an HRR before and that the proposed selected
1182 * ciphersuite in this server hello is not the same as the one
1183 * proposed in the HRR, we abort the handshake and send an
1184 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001185 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001186 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1187 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001188 {
XiaokangQian52da5582022-01-26 09:49:29 +00001189 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001190 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001191
XiaokangQian52da5582022-01-26 09:49:29 +00001192 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1195 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001196 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001197 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001198
Jerry Yu4a173382021-10-11 21:45:31 +08001199 /* Configure ciphersuites */
1200 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1201
XiaokangQian355e09a2022-01-20 11:14:50 +00001202 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001203 ssl->session_negotiate->ciphersuite = cipher_suite;
1204
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1206 cipher_suite, ciphersuite_info->name ) );
1207
1208#if defined(MBEDTLS_HAVE_TIME)
1209 ssl->session_negotiate->start = time( NULL );
1210#endif /* MBEDTLS_HAVE_TIME */
1211
Jerry Yu4a173382021-10-11 21:45:31 +08001212 /* ...
1213 * uint8 legacy_compression_method = 0;
1214 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001216 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 if( p[0] != 0 )
1218 {
Jerry Yub85277e2021-10-13 13:36:05 +08001219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001220 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001221 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001222 }
1223 p++;
1224
Jerry Yub85277e2021-10-13 13:36:05 +08001225 /* ...
1226 * Extension extensions<6..2^16-1>;
1227 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001228 * struct {
1229 * ExtensionType extension_type; (2 bytes)
1230 * opaque extension_data<0..2^16-1>;
1231 * } Extension;
1232 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001233 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001234 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001236
Jerry Yu4a173382021-10-11 21:45:31 +08001237 /* Check extensions do not go beyond the buffer of data. */
1238 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1239 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001240
Jerry Yu4a173382021-10-11 21:45:31 +08001241 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001242
Jerry Yu4a173382021-10-11 21:45:31 +08001243 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001244 {
1245 unsigned int extension_type;
1246 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001247 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001248
Jerry Yu4a173382021-10-11 21:45:31 +08001249 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001250 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1251 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1252 p += 4;
1253
Jerry Yu4a173382021-10-11 21:45:31 +08001254 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001255 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001256
1257 switch( extension_type )
1258 {
XiaokangQianb851da82022-01-14 04:03:11 +00001259 case MBEDTLS_TLS_EXT_COOKIE:
1260
XiaokangQiand9e068e2022-01-18 06:23:32 +00001261 if( !is_hrr )
1262 {
XiaokangQian52da5582022-01-26 09:49:29 +00001263 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001264 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001265 }
1266
XiaokangQian43550bd2022-01-21 04:32:58 +00001267 ret = ssl_tls13_parse_cookie_ext( ssl,
1268 p, extension_data_end );
1269 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001270 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001271 MBEDTLS_SSL_DEBUG_RET( 1,
1272 "ssl_tls13_parse_cookie_ext",
1273 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001274 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001275 }
XiaokangQianb851da82022-01-14 04:03:11 +00001276 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001277
Jerry Yue1b9c292021-09-10 10:08:31 +08001278 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001279 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 MBEDTLS_SSL_DEBUG_MSG( 3,
1281 ( "found supported_versions extension" ) );
1282
Jerry Yuc068b662021-10-11 22:30:19 +08001283 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001284 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001285 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001286 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001287 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001288 break;
1289
1290 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001293
XiaokangQian52da5582022-01-26 09:49:29 +00001294 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001295 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001296
1297#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1298 case MBEDTLS_TLS_EXT_KEY_SHARE:
1299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001300 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1301 {
XiaokangQian52da5582022-01-26 09:49:29 +00001302 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001303 goto cleanup;
1304 }
1305
XiaokangQian53f20b72022-01-18 10:47:33 +00001306 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001307 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001308 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001309 else
XiaokangQianb851da82022-01-14 04:03:11 +00001310 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001311 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001312 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001313 {
1314 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001315 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001316 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001317 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001318 }
1319 break;
1320#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1321
1322 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001323 MBEDTLS_SSL_DEBUG_MSG(
1324 3,
1325 ( "unknown extension found: %u ( ignoring )",
1326 extension_type ) );
1327
XiaokangQian52da5582022-01-26 09:49:29 +00001328 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001329 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001330 }
1331
1332 p += extension_data_len;
1333 }
1334
XiaokangQian78b1fa72022-01-19 06:56:30 +00001335 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001336 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001338 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001339 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001340 }
1341
XiaokangQiand59be772022-01-24 10:12:51 +00001342cleanup:
1343
XiaokangQian52da5582022-01-26 09:49:29 +00001344 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001345 {
1346 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1347 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1348 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1349 }
XiaokangQian52da5582022-01-26 09:49:29 +00001350 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001351 {
1352 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1353 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1354 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1355 }
1356 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001357}
1358
XiaokangQian355e09a2022-01-20 11:14:50 +00001359static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001360{
Jerry Yub85277e2021-10-13 13:36:05 +08001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001362 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001363 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001364 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001365
Jerry Yub85277e2021-10-13 13:36:05 +08001366 /* Determine the key exchange mode:
1367 * 1) If both the pre_shared_key and key_share extensions were received
1368 * then the key exchange mode is PSK with EPHEMERAL.
1369 * 2) If only the pre_shared_key extension was received then the key
1370 * exchange mode is PSK-only.
1371 * 3) If only the key_share extension was received then the key
1372 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001373 */
Jerry Yub85277e2021-10-13 13:36:05 +08001374 switch( handshake->extensions_present &
1375 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001376 {
Jerry Yu745bb612021-10-13 22:01:04 +08001377 /* Only the pre_shared_key extension was received */
1378 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001379 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001380 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001381
Jerry Yu745bb612021-10-13 22:01:04 +08001382 /* Only the key_share extension was received */
1383 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001384 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001385 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001386
Jerry Yu745bb612021-10-13 22:01:04 +08001387 /* Both the pre_shared_key and key_share extensions were received */
1388 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001389 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001390 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001391
Jerry Yu745bb612021-10-13 22:01:04 +08001392 /* Neither pre_shared_key nor key_share extension was received */
1393 default:
1394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1395 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1396 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001397 }
1398
1399 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1400 *
1401 * TODO: We don't have to do this in case we offered 0-RTT and the
1402 * server accepted it. In this case, we could skip generating
1403 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001404 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001405 if( ret != 0 )
1406 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001408 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001409 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001410 }
1411
1412 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001413 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001414 if( ret != 0 )
1415 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001417 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001418 }
1419
1420 /* Next evolution in key schedule: Establish handshake secret and
1421 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001422 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001423 if( ret != 0 )
1424 {
Jerry Yuc068b662021-10-11 22:30:19 +08001425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1426 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001427 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001428 }
1429
Jerry Yub85277e2021-10-13 13:36:05 +08001430 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001431 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001432 {
1433 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1434 goto cleanup;
1435 }
Jerry Yu0b177842021-09-05 19:41:30 +08001436
1437 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1438 ssl->conf->endpoint,
1439 ssl->session_negotiate->ciphersuite,
1440 &traffic_keys,
1441 ssl );
1442 if( ret != 0 )
1443 {
1444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001445 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001446 }
1447
Jerry Yub85277e2021-10-13 13:36:05 +08001448 handshake->transform_handshake = transform_handshake;
1449 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001450
1451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1452 ssl->session_in = ssl->session_negotiate;
1453
1454 /*
1455 * State machine update
1456 */
1457 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1458
Jerry Yu4a173382021-10-11 21:45:31 +08001459cleanup:
1460
Jerry Yu0b177842021-09-05 19:41:30 +08001461 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001462 if( ret != 0 )
1463 {
Jerry Yub85277e2021-10-13 13:36:05 +08001464 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001465
Jerry Yu4a173382021-10-11 21:45:31 +08001466 MBEDTLS_SSL_PEND_FATAL_ALERT(
1467 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1468 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1469 }
1470 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001471}
1472
XiaokangQian355e09a2022-01-20 11:14:50 +00001473static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001474{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001475#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1477
XiaokangQian647719a2021-12-07 09:16:29 +00001478#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1479 /* If not offering early data, the client sends a dummy CCS record
1480 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001481 * its second ClientHello or before its encrypted handshake flight.
1482 */
XiaokangQian647719a2021-12-07 09:16:29 +00001483 mbedtls_ssl_handshake_set_state( ssl,
1484 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1485#else
1486 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1487#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1488
XiaokangQian78b1fa72022-01-19 06:56:30 +00001489 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001490
XiaokangQian78b1fa72022-01-19 06:56:30 +00001491 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001492 * We are going to re-generate a shared secret corresponding to the group
1493 * selected by the server, which is different from the group for which we
1494 * generated a shared secret in the first client hello.
1495 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001496 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001497 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001498 if( ret != 0 )
1499 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001500#else
1501 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001502#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001503
1504 return( 0 );
1505}
1506
Jerry Yue1b9c292021-09-10 10:08:31 +08001507/*
Jerry Yu4a173382021-10-11 21:45:31 +08001508 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001509 * Handler for MBEDTLS_SSL_SERVER_HELLO
1510 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001511static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001512{
Jerry Yu4a173382021-10-11 21:45:31 +08001513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001514 unsigned char *buf = NULL;
1515 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001516 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001517
1518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1519
1520 /* Coordination step
1521 * - Fetch record
1522 * - Make sure it's either a ServerHello or a HRR.
1523 * - Switch processing routine in case of HRR
1524 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001525 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1526 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1527
XiaokangQian16acd4b2022-01-14 07:35:47 +00001528 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001529 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001530 goto cleanup;
1531 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001532 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001533
XiaokangQianb851da82022-01-14 04:03:11 +00001534 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001535 buf + buf_len,
1536 is_hrr ) );
1537 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001538 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1539
XiaokangQianb851da82022-01-14 04:03:11 +00001540 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1541 MBEDTLS_SSL_HS_SERVER_HELLO,
1542 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001543
XiaokangQiand9e068e2022-01-18 06:23:32 +00001544 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001545 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001546 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001547 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001548
1549cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1551 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001552 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001553}
1554
1555/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001556 *
1557 * EncryptedExtensions message
1558 *
1559 * The EncryptedExtensions message contains any extensions which
1560 * should be protected, i.e., any which are not needed to establish
1561 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001562 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001563
1564/*
1565 * Overview
1566 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001567
1568/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001569static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001570
XiaokangQian97799ac2021-10-11 10:05:54 +00001571static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1572 const unsigned char *buf,
1573 const unsigned char *end );
1574static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001575
1576/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001577 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001578 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001579static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001580{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001581 int ret;
1582 unsigned char *buf;
1583 size_t buf_len;
1584
1585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1586
Xiaofei Bai746f9482021-11-12 08:53:56 +00001587 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001588 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001589 &buf, &buf_len ) );
1590
1591 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001592 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001593 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001594
Xiaofei Bai746f9482021-11-12 08:53:56 +00001595 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001596 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001597
XiaokangQian97799ac2021-10-11 10:05:54 +00001598 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001599
1600cleanup:
1601
1602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1603 return( ret );
1604
1605}
1606
XiaokangQian08da26c2021-10-09 10:12:11 +00001607/* Parse EncryptedExtensions message
1608 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001609 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001610 * } EncryptedExtensions;
1611 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001612static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1613 const unsigned char *buf,
1614 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001615{
1616 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001617 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001618 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001619 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001620
XiaokangQian08da26c2021-10-09 10:12:11 +00001621 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001622 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001623 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001624
XiaokangQian97799ac2021-10-11 10:05:54 +00001625 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001626 extensions_end = p + extensions_len;
1627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001628
XiaokangQian8db25ff2021-10-13 05:56:18 +00001629 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001630 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001631 unsigned int extension_type;
1632 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001633
XiaokangQian08da26c2021-10-09 10:12:11 +00001634 /*
1635 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001636 * ExtensionType extension_type; (2 bytes)
1637 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001638 * } Extension;
1639 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001640 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001641 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1642 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1643 p += 4;
1644
XiaokangQian8db25ff2021-10-13 05:56:18 +00001645 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001646
XiaokangQian97799ac2021-10-11 10:05:54 +00001647 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001648 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001649 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001650 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001651 switch( extension_type )
1652 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001653
XiaokangQian08da26c2021-10-09 10:12:11 +00001654 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1656 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001657
XiaokangQian08da26c2021-10-09 10:12:11 +00001658 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001659 MBEDTLS_SSL_DEBUG_MSG(
1660 3, ( "unsupported extension found: %u ", extension_type) );
1661 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001662 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001663 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1664 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001665 }
1666
XiaokangQian08da26c2021-10-09 10:12:11 +00001667 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001668 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001669
XiaokangQian97799ac2021-10-11 10:05:54 +00001670 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001671 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001672 {
1673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001674 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001675 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001676 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001677 }
1678
1679 return( ret );
1680}
1681
XiaokangQian97799ac2021-10-11 10:05:54 +00001682static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001683{
Jerry Yua93ac112021-10-27 16:31:48 +08001684#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001685 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001686 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1687 else
Jerry Yud2674312021-10-29 10:08:19 +08001688 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001689#else
1690 ((void) ssl);
1691 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1692#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001693 return( 0 );
1694}
1695
Jerry Yua93ac112021-10-27 16:31:48 +08001696#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001697/*
Jerry Yud2674312021-10-29 10:08:19 +08001698 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1699 */
1700static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1701{
1702 int ret = mbedtls_ssl_read_record( ssl, 0 );
1703
1704 if( ret != 0 )
1705 {
1706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1707 return( ret );
1708 }
1709
1710 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1711 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1712 {
1713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1714 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1715 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1716 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1717 }
1718
1719 ssl->keep_current_message = 1;
1720 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1721
1722 return( 0 );
1723}
1724
1725/*
Jerry Yu687101b2021-09-14 16:03:56 +08001726 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1727 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001728static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001729{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001730 int ret;
1731
1732 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001733 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001734 return( ret );
1735
Jerry Yu687101b2021-09-14 16:03:56 +08001736 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1737 return( 0 );
1738}
1739
1740/*
1741 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1742 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001743static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001744{
Jerry Yu30b071c2021-09-12 20:16:03 +08001745 int ret;
1746
1747 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1748 if( ret != 0 )
1749 return( ret );
1750
Jerry Yu687101b2021-09-14 16:03:56 +08001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1752 return( 0 );
1753}
Jerry Yua93ac112021-10-27 16:31:48 +08001754#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001755
Jerry Yu687101b2021-09-14 16:03:56 +08001756/*
1757 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1758 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001759static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001760{
XiaokangQianac0385c2021-11-03 06:40:11 +00001761 int ret;
1762
XiaokangQianc5c39d52021-11-09 11:55:10 +00001763 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001764 if( ret != 0 )
1765 return( ret );
1766
Ronald Cron49ad6192021-11-24 16:25:31 +01001767#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1768 mbedtls_ssl_handshake_set_state(
1769 ssl,
1770 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1771#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001772 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001773#endif
1774
XiaokangQianac0385c2021-11-03 06:40:11 +00001775 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001776}
1777
1778/*
Ronald Crond4c64022021-12-06 09:06:46 +01001779 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1780 */
1781#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1782static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1783{
1784 int ret;
1785
1786 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1787 if( ret != 0 )
1788 return( ret );
1789
Ronald Crond4c64022021-12-06 09:06:46 +01001790 return( 0 );
1791}
1792#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1793
1794/*
Jerry Yu687101b2021-09-14 16:03:56 +08001795 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1796 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001797static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001798{
XiaokangQian0fa66432021-11-15 03:33:57 +00001799 int ret;
1800
Ronald Cron49ad6192021-11-24 16:25:31 +01001801 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1802
XiaokangQian0fa66432021-11-15 03:33:57 +00001803 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1804 if( ret != 0 )
1805 return( ret );
1806
1807 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1808 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001809}
1810
1811/*
1812 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1813 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001814static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001815{
Jerry Yu378254d2021-10-30 21:44:47 +08001816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001817 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001818 return( 0 );
1819}
1820
1821/*
1822 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1823 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001824static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001825{
Jerry Yu378254d2021-10-30 21:44:47 +08001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1827 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1828
1829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1830 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1831
1832 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1833
1834 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1835 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001836}
1837
Jerry Yu92c6b402021-08-27 16:59:09 +08001838int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001839{
Jerry Yu92c6b402021-08-27 16:59:09 +08001840 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001841
Jerry Yue3b34122021-09-28 17:53:35 +08001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1843 mbedtls_ssl_states_str( ssl->state ),
1844 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001845
1846 switch( ssl->state )
1847 {
1848 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001849 * ssl->state is initialized as HELLO_REQUEST. It is the same
1850 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001851 */
1852 case MBEDTLS_SSL_HELLO_REQUEST:
1853 case MBEDTLS_SSL_CLIENT_HELLO:
1854 ret = ssl_tls13_write_client_hello( ssl );
1855 break;
1856
1857 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001858 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001859 break;
1860
1861 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001862 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001863 break;
1864
Jerry Yua93ac112021-10-27 16:31:48 +08001865#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001866 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1867 ret = ssl_tls13_process_certificate_request( ssl );
1868 break;
1869
Jerry Yu687101b2021-09-14 16:03:56 +08001870 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001871 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001872 break;
1873
1874 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001875 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001876 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001877#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001878
1879 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001880 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001881 break;
1882
Jerry Yu687101b2021-09-14 16:03:56 +08001883 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001884 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001885 break;
1886
1887 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001888 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001889 break;
1890
1891 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001892 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001893 break;
1894
Ronald Cron49ad6192021-11-24 16:25:31 +01001895 /*
1896 * Injection of dummy-CCS's for middlebox compatibility
1897 */
1898#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1899 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001900 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001901 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001902 break;
1903#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1904
Jerry Yu92c6b402021-08-27 16:59:09 +08001905 default:
1906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1908 }
1909
1910 return( ret );
1911}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001912
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001913#endif /* MBEDTLS_SSL_CLI_C */
1914
Ronald Cron6f135e12021-12-08 16:57:54 +01001915#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */