blob: 208f24a4bcffe38230ad0994ea32d90d34398335 [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 /* Convert EC group to PSA key type. */
162 if( ( handshake->ecdh_psa_type =
163 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
164 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800165
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100166 if( ecdh_bits > 0xffff )
167 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
168 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800169
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100170 key_attributes = psa_key_attributes_init();
171 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
172 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
173 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
174 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
175
176 /* Generate ECDH private key. */
177 status = psa_generate_key( &key_attributes,
178 &handshake->ecdh_psa_privkey );
179 if( status != PSA_SUCCESS )
180 {
181 ret = psa_ssl_status_to_mbedtls( status );
182 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
183 return( ret );
184
185 }
186
187 /* Export the public part of the ECDH private key from PSA. */
188 status = psa_export_public_key( handshake->ecdh_psa_privkey,
189 own_pubkey, sizeof( own_pubkey ),
190 &own_pubkey_len );
191 if( status != PSA_SUCCESS )
192 {
193 ret = psa_ssl_status_to_mbedtls( status );
194 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
195 return( ret );
196
197 }
198
199 if( own_pubkey_len > (size_t)( end - buf ) )
200 {
201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
202 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
203 }
204
205 *out_len = own_pubkey_len;
206
207 memcpy( buf, &own_pubkey, own_pubkey_len );
208
Jerry Yu75336352021-09-01 15:59:36 +0800209 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800210}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211#endif /* MBEDTLS_ECDH_C */
212
Jerry Yub60e3cf2021-09-08 16:41:02 +0800213static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
214 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215{
216 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
217
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218
Jerry Yu56fc07f2021-09-01 17:48:49 +0800219#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100220 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800221 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100222 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800223 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
224
Brett Warren14efd332021-10-06 09:32:11 +0100225 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800226 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000227 const mbedtls_ecp_curve_info *curve_info;
228 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
229 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100230 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231 {
Brett Warren14efd332021-10-06 09:32:11 +0100232 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233 return( 0 );
234 }
235 }
236#else
237 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800238 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239#endif /* MBEDTLS_ECDH_C */
240
241 /*
242 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800243 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800244 */
245
246 return( ret );
247}
248
249/*
250 * ssl_tls13_write_key_share_ext
251 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800252 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 *
254 * struct {
255 * NamedGroup group;
256 * opaque key_exchange<1..2^16-1>;
257 * } KeyShareEntry;
258 * struct {
259 * KeyShareEntry client_shares<0..2^16-1>;
260 * } KeyShareClientHello;
261 */
262static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
263 unsigned char *buf,
264 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000265 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800266{
267 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000268 unsigned char *client_shares; /* Start of client_shares */
269 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
272
Xiaofei Baid25fab62021-12-02 06:36:27 +0000273 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274
Jerry Yub60e3cf2021-09-08 16:41:02 +0800275 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276 * - extension_type (2 bytes)
277 * - extension_data_length (2 bytes)
278 * - client_shares_length (2 bytes)
279 */
280 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
281 p += 6;
282
283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
284
285 /* HRR could already have requested something else. */
286 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800287 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
288 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800290 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 &group_id ) );
292 }
293
294 /*
295 * Dispatch to type-specific key generation function.
296 *
297 * So far, we're only supporting ECDHE. With the introduction
298 * of PQC KEMs, we'll want to have multiple branches, one per
299 * type of KEM, and dispatch to the corresponding crypto. And
300 * only one key share entry is allowed.
301 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000302 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800303#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800304 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000307 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100309 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310
311 /* Check there is space for header of KeyShareEntry
312 * - group (2 bytes)
313 * - key_exchange_length (2 bytes)
314 */
315 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
316 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100317 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800318 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 p += key_exchange_len;
320 if( ret != 0 )
321 return( ret );
322
323 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000324 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000326 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 }
328 else
329#endif /* MBEDTLS_ECDH_C */
330 if( 0 /* other KEMs? */ )
331 {
332 /* Do something */
333 }
334 else
335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
336
Jerry Yub60e3cf2021-09-08 16:41:02 +0800337 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000338 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 if( client_shares_len == 0)
340 {
341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800343 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344 /* Write extension_type */
345 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
346 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800347 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800349 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
351 /* Update offered_group_id field */
352 ssl->handshake->offered_group_id = group_id;
353
354 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000355 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356
Xiaofei Baid25fab62021-12-02 06:36:27 +0000357 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358
359 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
360
361cleanup:
362
363 return( ret );
364}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800365
Jerry Yue1b9c292021-09-10 10:08:31 +0800366#if defined(MBEDTLS_ECDH_C)
367
Jerry Yuc068b662021-10-11 22:30:19 +0800368static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
369 const unsigned char *buf,
370 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800371{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100372 uint8_t *p = (uint8_t*)buf;
373 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800374
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100375 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
376 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
377 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800378
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100379 /* Check if key size is consistent with given buffer length. */
380 if ( peerkey_len > ( buf_len - 2 ) )
381 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800382
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100383 /* Store peer's ECDH public key. */
384 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
385 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800386
387 return( 0 );
388}
Jerry Yu4a173382021-10-11 21:45:31 +0800389#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800390
XiaokangQiand59be772022-01-24 10:12:51 +0000391/*
392 * ssl_tls13_parse_hrr_key_share_ext()
393 * Parse key_share extension in Hello Retry Request
394 *
395 * struct {
396 * NamedGroup selected_group;
397 * } KeyShareHelloRetryRequest;
398 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000399static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000400 const unsigned char *buf,
401 const unsigned char *end )
402{
XiaokangQianb851da82022-01-14 04:03:11 +0000403 const mbedtls_ecp_curve_info *curve_info = NULL;
404 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000405 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000406 int found = 0;
407
408 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
409 if( group_list == NULL )
410 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
411
412 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
413
414 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000415 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000416 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000418
419 /* Upon receipt of this extension in a HelloRetryRequest, the client
420 * MUST first verify that the selected_group field corresponds to a
421 * group which was provided in the "supported_groups" extension in the
422 * original ClientHello.
423 * The supported_group was based on the info in ssl->conf->group_list.
424 *
425 * If the server provided a key share that was not sent in the ClientHello
426 * then the client MUST abort the handshake with an "illegal_parameter" alert.
427 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000428 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000429 {
430 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000431 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000432 continue;
433
434 /* We found a match */
435 found = 1;
436 break;
437 }
438
439 /* Client MUST verify that the selected_group field does not
440 * correspond to a group which was provided in the "key_share"
441 * extension in the original ClientHello. If the server sent an
442 * HRR message with a key share already provided in the
443 * ClientHello then the client MUST abort the handshake with
444 * an "illegal_parameter" alert.
445 */
XiaokangQiand59be772022-01-24 10:12:51 +0000446 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000447 {
448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
449 MBEDTLS_SSL_PEND_FATAL_ALERT(
450 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
451 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
452 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
453 }
454
455 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000456 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000457
458 return( 0 );
459}
460
Jerry Yue1b9c292021-09-10 10:08:31 +0800461/*
Jerry Yub85277e2021-10-13 13:36:05 +0800462 * ssl_tls13_parse_key_share_ext()
463 * Parse key_share extension in Server Hello
464 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800465 * struct {
466 * KeyShareEntry server_share;
467 * } KeyShareServerHello;
468 * struct {
469 * NamedGroup group;
470 * opaque key_exchange<1..2^16-1>;
471 * } KeyShareEntry;
472 */
Jerry Yu4a173382021-10-11 21:45:31 +0800473static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800474 const unsigned char *buf,
475 const unsigned char *end )
476{
Jerry Yub85277e2021-10-13 13:36:05 +0800477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800478 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800479 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800480
Jerry Yu4a173382021-10-11 21:45:31 +0800481 /* ...
482 * NamedGroup group; (2 bytes)
483 * ...
484 */
485 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
486 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800487 p += 2;
488
Jerry Yu4a173382021-10-11 21:45:31 +0800489 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800490 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800491 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800492 {
493 MBEDTLS_SSL_DEBUG_MSG( 1,
494 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800495 (unsigned) offered_group, (unsigned) group ) );
496 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
497 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
498 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800499 }
500
501#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800502 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800503 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100504 const mbedtls_ecp_curve_info *curve_info =
505 mbedtls_ecp_curve_info_from_tls_id( group );
506 if( curve_info == NULL )
507 {
508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
510 }
511
512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
513
Jerry Yuc068b662021-10-11 22:30:19 +0800514 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800515 if( ret != 0 )
516 return( ret );
517 }
Jerry Yub85277e2021-10-13 13:36:05 +0800518 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800519#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800520 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 {
522 /* Do something */
523 }
524 else
525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
526
527 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
528 return( ret );
529}
530
Jerry Yubc20bdd2021-08-24 15:59:48 +0800531#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
532
XiaokangQiand59be772022-01-24 10:12:51 +0000533/*
534 * ssl_tls13_parse_cookie_ext()
535 * Parse cookie extension in Hello Retry Request
536 *
537 * struct {
538 * opaque cookie<1..2^16-1>;
539 * } Cookie;
540 *
541 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
542 * extension to the client (this is an exception to the usual rule that
543 * the only extensions that may be sent are those that appear in the
544 * ClientHello). When sending the new ClientHello, the client MUST copy
545 * the contents of the extension received in the HelloRetryRequest into
546 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
547 * cookies in their initial ClientHello in subsequent connections.
548 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000549static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
550 const unsigned char *buf,
551 const unsigned char *end )
552{
553 size_t cookie_len;
554 const unsigned char *p = buf;
555 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
556
557 /* Retrieve length field of cookie */
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
559 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
560 p += 2;
561
562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
563 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
564
565 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000566 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000567 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
568 if( handshake->verify_cookie == NULL )
569 {
570 MBEDTLS_SSL_DEBUG_MSG( 1,
571 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
572 cookie_len ) );
573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
574 }
575
576 memcpy( handshake->verify_cookie, p, cookie_len );
577 handshake->verify_cookie_len = (unsigned char) cookie_len;
578
579 return( 0 );
580}
XiaokangQian43550bd2022-01-21 04:32:58 +0000581
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800582/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800583 * CipherSuite cipher_suites<2..2^16-2>;
584 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800585static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800586 mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000589 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800590{
Jerry Yufec982e2021-09-07 17:26:06 +0800591 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800592 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000593 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800594 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800595
Xiaofei Baid25fab62021-12-02 06:36:27 +0000596 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800597
598 /*
599 * Ciphersuite list
600 *
601 * This is a list of the symmetric cipher options supported by
602 * the client, specifically the record protection algorithm
603 * ( including secret key length ) and a hash to be used with
604 * HKDF, in descending order of client preference.
605 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800606 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800607
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800608 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800609 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
610 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800611
Jerry Yu0c63af62021-09-02 12:59:12 +0800612 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000613 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800614 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800615 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800616 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800617 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800618
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800619 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800620 if( ciphersuite_info == NULL )
621 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800622 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
623 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800624 continue;
625
626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800627 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800628 ciphersuite_info->name ) );
629
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800630 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800631 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
632 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
633 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800634 }
635
Jerry Yu0c63af62021-09-02 12:59:12 +0800636 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000637 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800638 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800639 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800640 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
641 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800642
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800643 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000644 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800645
Jerry Yu6a643102021-08-31 14:40:36 +0800646 return( 0 );
647}
648
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800649/*
650 * Structure of ClientHello message:
651 *
652 * struct {
653 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
654 * Random random;
655 * opaque legacy_session_id<0..32>;
656 * CipherSuite cipher_suites<2..2^16-2>;
657 * opaque legacy_compression_methods<1..2^8-1>;
658 * Extension extensions<8..2^16-1>;
659 * } ClientHello;
660 */
Jerry Yu08906d02021-08-31 11:05:27 +0800661static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800662 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800663 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000664 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800665{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800666
Jerry Yubc20bdd2021-08-24 15:59:48 +0800667 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000668 unsigned char *p_extensions_len; /* Pointer to extensions length */
669 size_t output_len; /* Length of buffer used by function */
670 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800671
Jerry Yubc20bdd2021-08-24 15:59:48 +0800672 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800673 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800674
Xiaofei Baid25fab62021-12-02 06:36:27 +0000675 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800676
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800677 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800678 ssl->major_ver = ssl->conf->min_major_ver;
679 ssl->minor_ver = ssl->conf->min_minor_ver;
680
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800681 /*
682 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800683 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800684 *
685 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800687 */
Jerry Yufec982e2021-09-07 17:26:06 +0800688 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800689 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800690 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800691
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800692 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800693 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
694 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800695 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800696 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
697 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800699 /*
700 * Write legacy_session_id
701 *
702 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
703 * which has been merged with pre-shared keys in this version. A client
704 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
705 * this field to that value. In compatibility mode, this field MUST be
706 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
707 * a new 32-byte value. This value need not be random but SHOULD be
708 * unpredictable to avoid implementations fixating on a specific value
709 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
710 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800711 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100712#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
713 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
714 *p++ = (unsigned char)ssl->session_negotiate->id_len;
715 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
716 p += ssl->session_negotiate->id_len;
717
718 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
719 ssl->session_negotiate->id_len );
720#else
Jerry Yubbe09522021-09-06 21:17:54 +0800721 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
722 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100723#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800724
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800725 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800726 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800727 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800728 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800729 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800730
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800731 /* Write legacy_compression_methods
732 *
733 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800734 * one byte set to zero, which corresponds to the 'null' compression
735 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800736 */
Jerry Yubbe09522021-09-06 21:17:54 +0800737 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
738 *p++ = 1;
739 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800740
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800741 /* Write extensions */
742
743 /* Keeping track of the included extensions */
744 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800745
746 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800747 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000748 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800749 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800751 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800752 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800753 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800754 */
Jerry Yubbe09522021-09-06 21:17:54 +0800755 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800756 if( ret != 0 )
757 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800758 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759
760#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800761
Jerry Yub925f212022-01-12 11:17:02 +0800762 /*
763 * Add the extensions related to (EC)DHE ephemeral key establishment only if
764 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800766 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
767 {
768 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
769 if( ret != 0 )
770 return( ret );
771 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800772
Jerry Yuf46b0162022-01-11 16:28:00 +0800773 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
774 if( ret != 0 )
775 return( ret );
776 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800777
Jerry Yuf017ee42022-01-12 15:49:48 +0800778 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800779 if( ret != 0 )
780 return( ret );
781 p += output_len;
782 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
784
Xiaofei Bai15a56812021-11-05 10:52:12 +0000785#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000786 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000787 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
788 if( ret != 0 )
789 return( ret );
790 p += output_len;
791#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
792
Jerry Yubc20bdd2021-08-24 15:59:48 +0800793 /* Add more extensions here */
794
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800795 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000796 extensions_len = p - p_extensions_len - 2;
797 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800799 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000800 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800801
Xiaofei Baid25fab62021-12-02 06:36:27 +0000802 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800803 return( 0 );
804}
805
Jerry Yu335aca92021-09-12 20:18:56 +0800806static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800807{
Jerry Yu92c6b402021-08-27 16:59:09 +0800808 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
809 return( 0 );
810}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800811
Jerry Yu92c6b402021-08-27 16:59:09 +0800812static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
813{
814 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800815
Jerry Yu92c6b402021-08-27 16:59:09 +0800816 if( ssl->conf->f_rng == NULL )
817 {
818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
819 return( MBEDTLS_ERR_SSL_NO_RNG );
820 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800821
Jerry Yu92c6b402021-08-27 16:59:09 +0800822 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
823 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800824 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800825 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800826 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800827 return( ret );
828 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800829
Ronald Cron49ad6192021-11-24 16:25:31 +0100830#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
831 /*
832 * Create a session identifier for the purpose of middlebox compatibility
833 * only if one has not been created already.
834 */
835 if( ssl->session_negotiate->id_len == 0 )
836 {
837 /* Creating a session id with 32 byte length */
838 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
839 ssl->session_negotiate->id, 32 ) ) != 0 )
840 {
841 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
842 return( ret );
843 }
844 ssl->session_negotiate->id_len = 32;
845 }
846#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
847
Jerry Yu6f13f642021-08-26 17:18:15 +0800848 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800849}
850
Jerry Yu92c6b402021-08-27 16:59:09 +0800851/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800852 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800853 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800854 */
855static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800856{
Jerry Yu92c6b402021-08-27 16:59:09 +0800857 int ret = 0;
858 unsigned char *buf;
859 size_t buf_len, msg_len;
860
861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
862
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800863 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800864
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800865 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
866 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
867 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800868
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800869 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800870 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800871 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800872
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800873 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
874 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800875 msg_len );
876 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800877
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800878 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
879 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
880 buf_len,
881 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800882
883cleanup:
884
885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
886 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800887}
888
Jerry Yu687101b2021-09-14 16:03:56 +0800889/*
Jerry Yu4a173382021-10-11 21:45:31 +0800890 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800891 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800892/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800893 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
894 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000895 * to indicate which message is expected and to be parsed next.
896 */
Jerry Yub85277e2021-10-13 13:36:05 +0800897#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
898#define SSL_SERVER_HELLO_COORDINATE_HRR 1
899static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
900 const unsigned char *buf,
901 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800902{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800903 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800904 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
905 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
906 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
907 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
908
909 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
910 *
Jerry Yu4a173382021-10-11 21:45:31 +0800911 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800912 * special value of the SHA-256 of "HelloRetryRequest".
913 *
914 * struct {
915 * ProtocolVersion legacy_version = 0x0303;
916 * Random random;
917 * opaque legacy_session_id_echo<0..32>;
918 * CipherSuite cipher_suite;
919 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800920 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 * } ServerHello;
922 *
923 */
Jerry Yub85277e2021-10-13 13:36:05 +0800924 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800925
926 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800927 {
Jerry Yub85277e2021-10-13 13:36:05 +0800928 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 }
930
Jerry Yub85277e2021-10-13 13:36:05 +0800931 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800932}
933
Jerry Yu745bb612021-10-13 22:01:04 +0800934/* Fetch and preprocess
935 * Returns a negative value on failure, and otherwise
936 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
937 * - SSL_SERVER_HELLO_COORDINATE_HRR
938 */
Jerry Yub85277e2021-10-13 13:36:05 +0800939static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
940 unsigned char **buf,
941 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800942{
Jerry Yu4a173382021-10-11 21:45:31 +0800943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800944
XiaokangQian355e09a2022-01-20 11:14:50 +0000945 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
946 MBEDTLS_SSL_HS_SERVER_HELLO,
947 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800948
Jerry Yub85277e2021-10-13 13:36:05 +0800949 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
950 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800951 {
Jerry Yu745bb612021-10-13 22:01:04 +0800952 case SSL_SERVER_HELLO_COORDINATE_HELLO:
953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
954 break;
955 case SSL_SERVER_HELLO_COORDINATE_HRR:
956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000957 /* If a client receives a second
958 * HelloRetryRequest in the same connection (i.e., where the ClientHello
959 * was itself in response to a HelloRetryRequest), it MUST abort the
960 * handshake with an "unexpected_message" alert.
961 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000962 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
965 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
966 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
967 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
968 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000969 /*
970 * Clients must abort the handshake with an "illegal_parameter"
971 * alert if the HelloRetryRequest would not result in any change
972 * in the ClientHello.
973 * In a PSK only key exchange that what we expect.
974 */
975 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
976 {
977 MBEDTLS_SSL_DEBUG_MSG( 1,
978 ( "Unexpected HRR in pure PSK key exchange." ) );
979 MBEDTLS_SSL_PEND_FATAL_ALERT(
980 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
981 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
982 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
983 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000984
XiaokangQiand9e068e2022-01-18 06:23:32 +0000985 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000986
Jerry Yu745bb612021-10-13 22:01:04 +0800987 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 }
989
990cleanup:
991
992 return( ret );
993}
994
Jerry Yu4a173382021-10-11 21:45:31 +0800995static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
996 const unsigned char **buf,
997 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800998{
999 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001000 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001001
Jerry Yude4fb2c2021-09-19 18:05:08 +08001002 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001003 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001004
Jerry Yu4a173382021-10-11 21:45:31 +08001005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001006
1007 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001008 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1009 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001010 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001011 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1012 ssl->session_negotiate->id,
1013 ssl->session_negotiate->id_len );
1014 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001015 legacy_session_id_echo_len );
1016
1017 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1018 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1019
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1021 }
1022
Jerry Yu4a173382021-10-11 21:45:31 +08001023 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001024 *buf = p;
1025
1026 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001027 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 return( 0 );
1029}
1030
Jerry Yu4a173382021-10-11 21:45:31 +08001031static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1032 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001033{
Jerry Yu4a173382021-10-11 21:45:31 +08001034 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1035
Jerry Yue1b9c292021-09-10 10:08:31 +08001036 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001037 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 {
Jerry Yu4a173382021-10-11 21:45:31 +08001039 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 {
1041 return( 1 );
1042 }
1043 }
1044 return( 0 );
1045}
1046
1047/* Parse ServerHello message and configure context
1048 *
1049 * struct {
1050 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1051 * Random random;
1052 * opaque legacy_session_id_echo<0..32>;
1053 * CipherSuite cipher_suite;
1054 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001055 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001056 * } ServerHello;
1057 */
Jerry Yuc068b662021-10-11 22:30:19 +08001058static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1059 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001060 const unsigned char *end,
1061 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001062{
Jerry Yub85277e2021-10-13 13:36:05 +08001063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001064 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001065 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001066 size_t extensions_len;
1067 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001069 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001070 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001071 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001072
1073 /*
1074 * Check there is space for minimal fields
1075 *
1076 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001077 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 * - legacy_session_id_echo ( 1 byte ), minimum size
1079 * - cipher_suite ( 2 bytes)
1080 * - legacy_compression_method ( 1 byte )
1081 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001082 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001083
1084 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1086
Jerry Yu4a173382021-10-11 21:45:31 +08001087 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001088 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001089 * ...
1090 * with ProtocolVersion defined as:
1091 * uint16 ProtocolVersion;
1092 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001093 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1094 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1095 {
1096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1097 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1098 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001099 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1100 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001101 }
1102 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001103
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001104 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001105 * Random random;
1106 * ...
1107 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001108 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001109 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001110 if( !is_hrr )
1111 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001112 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001113 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1114 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1115 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1116 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001117 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118
Jerry Yu4a173382021-10-11 21:45:31 +08001119 /* ...
1120 * opaque legacy_session_id_echo<0..32>;
1121 * ...
1122 */
1123 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 {
XiaokangQian52da5582022-01-26 09:49:29 +00001125 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001126 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 }
1128
Jerry Yu4a173382021-10-11 21:45:31 +08001129 /* ...
1130 * CipherSuite cipher_suite;
1131 * ...
1132 * with CipherSuite defined as:
1133 * uint8 CipherSuite[2];
1134 */
1135 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001136 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1137 p += 2;
1138
Jerry Yu4a173382021-10-11 21:45:31 +08001139
XiaokangQian355e09a2022-01-20 11:14:50 +00001140 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001141 /*
1142 * Check whether this ciphersuite is supported and offered.
1143 * Via the force_ciphersuite version we may have instructed the client
1144 * to use a different ciphersuite.
1145 */
Jerry Yu4a173382021-10-11 21:45:31 +08001146 if( ciphersuite_info == NULL ||
1147 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 {
XiaokangQian52da5582022-01-26 09:49:29 +00001149 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001151 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001152 * If we received an HRR before and that the proposed selected
1153 * ciphersuite in this server hello is not the same as the one
1154 * proposed in the HRR, we abort the handshake and send an
1155 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001156 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001157 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1158 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001159 {
XiaokangQian52da5582022-01-26 09:49:29 +00001160 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001161 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001162
XiaokangQian52da5582022-01-26 09:49:29 +00001163 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001164 {
1165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1166 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001167 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001168 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001169
Jerry Yu4a173382021-10-11 21:45:31 +08001170 /* Configure ciphersuites */
1171 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1172
XiaokangQian355e09a2022-01-20 11:14:50 +00001173 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001174 ssl->session_negotiate->ciphersuite = cipher_suite;
1175
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1177 cipher_suite, ciphersuite_info->name ) );
1178
1179#if defined(MBEDTLS_HAVE_TIME)
1180 ssl->session_negotiate->start = time( NULL );
1181#endif /* MBEDTLS_HAVE_TIME */
1182
Jerry Yu4a173382021-10-11 21:45:31 +08001183 /* ...
1184 * uint8 legacy_compression_method = 0;
1185 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001186 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001187 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001188 if( p[0] != 0 )
1189 {
Jerry Yub85277e2021-10-13 13:36:05 +08001190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001191 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001192 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 }
1194 p++;
1195
Jerry Yub85277e2021-10-13 13:36:05 +08001196 /* ...
1197 * Extension extensions<6..2^16-1>;
1198 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001199 * struct {
1200 * ExtensionType extension_type; (2 bytes)
1201 * opaque extension_data<0..2^16-1>;
1202 * } Extension;
1203 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001205 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001206 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001207
Jerry Yu4a173382021-10-11 21:45:31 +08001208 /* Check extensions do not go beyond the buffer of data. */
1209 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1210 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001211
Jerry Yu4a173382021-10-11 21:45:31 +08001212 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001213
Jerry Yu4a173382021-10-11 21:45:31 +08001214 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 {
1216 unsigned int extension_type;
1217 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001218 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001219
Jerry Yu4a173382021-10-11 21:45:31 +08001220 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1222 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1223 p += 4;
1224
Jerry Yu4a173382021-10-11 21:45:31 +08001225 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001226 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001227
1228 switch( extension_type )
1229 {
XiaokangQianb851da82022-01-14 04:03:11 +00001230 case MBEDTLS_TLS_EXT_COOKIE:
1231
XiaokangQiand9e068e2022-01-18 06:23:32 +00001232 if( !is_hrr )
1233 {
XiaokangQian52da5582022-01-26 09:49:29 +00001234 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001235 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001236 }
1237
XiaokangQian43550bd2022-01-21 04:32:58 +00001238 ret = ssl_tls13_parse_cookie_ext( ssl,
1239 p, extension_data_end );
1240 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001241 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001242 MBEDTLS_SSL_DEBUG_RET( 1,
1243 "ssl_tls13_parse_cookie_ext",
1244 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001245 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001246 }
XiaokangQianb851da82022-01-14 04:03:11 +00001247 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001248
Jerry Yue1b9c292021-09-10 10:08:31 +08001249 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001250 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001251 MBEDTLS_SSL_DEBUG_MSG( 3,
1252 ( "found supported_versions extension" ) );
1253
Jerry Yuc068b662021-10-11 22:30:19 +08001254 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001255 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001256 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001257 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001258 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001259 break;
1260
1261 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001264
XiaokangQian52da5582022-01-26 09:49:29 +00001265 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001266 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001267
1268#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1269 case MBEDTLS_TLS_EXT_KEY_SHARE:
1270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001271 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1272 {
XiaokangQian52da5582022-01-26 09:49:29 +00001273 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001274 goto cleanup;
1275 }
1276
XiaokangQian53f20b72022-01-18 10:47:33 +00001277 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001278 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001279 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001280 else
XiaokangQianb851da82022-01-14 04:03:11 +00001281 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001282 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001283 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001284 {
1285 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001286 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001287 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001288 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001289 }
1290 break;
1291#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1292
1293 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001294 MBEDTLS_SSL_DEBUG_MSG(
1295 3,
1296 ( "unknown extension found: %u ( ignoring )",
1297 extension_type ) );
1298
XiaokangQian52da5582022-01-26 09:49:29 +00001299 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001300 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001301 }
1302
1303 p += extension_data_len;
1304 }
1305
XiaokangQian78b1fa72022-01-19 06:56:30 +00001306 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001307 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001309 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001310 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001311 }
1312
XiaokangQiand59be772022-01-24 10:12:51 +00001313cleanup:
1314
XiaokangQian52da5582022-01-26 09:49:29 +00001315 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001316 {
1317 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1318 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1319 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1320 }
XiaokangQian52da5582022-01-26 09:49:29 +00001321 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001322 {
1323 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1324 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1325 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1326 }
1327 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001328}
1329
XiaokangQian355e09a2022-01-20 11:14:50 +00001330static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001331{
Jerry Yub85277e2021-10-13 13:36:05 +08001332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001333 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001334 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001335 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001336
Jerry Yub85277e2021-10-13 13:36:05 +08001337 /* Determine the key exchange mode:
1338 * 1) If both the pre_shared_key and key_share extensions were received
1339 * then the key exchange mode is PSK with EPHEMERAL.
1340 * 2) If only the pre_shared_key extension was received then the key
1341 * exchange mode is PSK-only.
1342 * 3) If only the key_share extension was received then the key
1343 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001344 */
Jerry Yub85277e2021-10-13 13:36:05 +08001345 switch( handshake->extensions_present &
1346 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001347 {
Jerry Yu745bb612021-10-13 22:01:04 +08001348 /* Only the pre_shared_key extension was received */
1349 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001350 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001351 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001352
Jerry Yu745bb612021-10-13 22:01:04 +08001353 /* Only the key_share extension was received */
1354 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001355 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001356 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001357
Jerry Yu745bb612021-10-13 22:01:04 +08001358 /* Both the pre_shared_key and key_share extensions were received */
1359 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001360 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001361 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001362
Jerry Yu745bb612021-10-13 22:01:04 +08001363 /* Neither pre_shared_key nor key_share extension was received */
1364 default:
1365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1366 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1367 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001368 }
1369
1370 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1371 *
1372 * TODO: We don't have to do this in case we offered 0-RTT and the
1373 * server accepted it. In this case, we could skip generating
1374 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001375 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001376 if( ret != 0 )
1377 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001379 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001380 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001381 }
1382
1383 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001384 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001385 if( ret != 0 )
1386 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001388 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001389 }
1390
1391 /* Next evolution in key schedule: Establish handshake secret and
1392 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001393 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001394 if( ret != 0 )
1395 {
Jerry Yuc068b662021-10-11 22:30:19 +08001396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1397 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001398 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001399 }
1400
Jerry Yub85277e2021-10-13 13:36:05 +08001401 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001402 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001403 {
1404 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1405 goto cleanup;
1406 }
Jerry Yu0b177842021-09-05 19:41:30 +08001407
1408 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1409 ssl->conf->endpoint,
1410 ssl->session_negotiate->ciphersuite,
1411 &traffic_keys,
1412 ssl );
1413 if( ret != 0 )
1414 {
1415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001416 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001417 }
1418
Jerry Yub85277e2021-10-13 13:36:05 +08001419 handshake->transform_handshake = transform_handshake;
1420 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001421
1422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1423 ssl->session_in = ssl->session_negotiate;
1424
1425 /*
1426 * State machine update
1427 */
1428 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1429
Jerry Yu4a173382021-10-11 21:45:31 +08001430cleanup:
1431
Jerry Yu0b177842021-09-05 19:41:30 +08001432 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001433 if( ret != 0 )
1434 {
Jerry Yub85277e2021-10-13 13:36:05 +08001435 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001436
Jerry Yu4a173382021-10-11 21:45:31 +08001437 MBEDTLS_SSL_PEND_FATAL_ALERT(
1438 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1439 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1440 }
1441 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001442}
1443
XiaokangQian355e09a2022-01-20 11:14:50 +00001444static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001445{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001446#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1448
XiaokangQian647719a2021-12-07 09:16:29 +00001449#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1450 /* If not offering early data, the client sends a dummy CCS record
1451 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001452 * its second ClientHello or before its encrypted handshake flight.
1453 */
XiaokangQian647719a2021-12-07 09:16:29 +00001454 mbedtls_ssl_handshake_set_state( ssl,
1455 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1456#else
1457 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1458#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1459
XiaokangQian78b1fa72022-01-19 06:56:30 +00001460 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001461
XiaokangQian78b1fa72022-01-19 06:56:30 +00001462 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001463 * We are going to re-generate a shared secret corresponding to the group
1464 * selected by the server, which is different from the group for which we
1465 * generated a shared secret in the first client hello.
1466 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001467 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001468 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001469 if( ret != 0 )
1470 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001471#else
1472 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001473#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001474
1475 return( 0 );
1476}
1477
Jerry Yue1b9c292021-09-10 10:08:31 +08001478/*
Jerry Yu4a173382021-10-11 21:45:31 +08001479 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001480 * Handler for MBEDTLS_SSL_SERVER_HELLO
1481 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001482static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001483{
Jerry Yu4a173382021-10-11 21:45:31 +08001484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001485 unsigned char *buf = NULL;
1486 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001487 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001488
1489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1490
1491 /* Coordination step
1492 * - Fetch record
1493 * - Make sure it's either a ServerHello or a HRR.
1494 * - Switch processing routine in case of HRR
1495 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001496 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1497 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1498
XiaokangQian16acd4b2022-01-14 07:35:47 +00001499 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001500 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001501 goto cleanup;
1502 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001503 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001504
XiaokangQianb851da82022-01-14 04:03:11 +00001505 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001506 buf + buf_len,
1507 is_hrr ) );
1508 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001509 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1510
XiaokangQianb851da82022-01-14 04:03:11 +00001511 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1512 MBEDTLS_SSL_HS_SERVER_HELLO,
1513 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001514
XiaokangQiand9e068e2022-01-18 06:23:32 +00001515 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001516 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001517 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001518 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001519
1520cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1522 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001523 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001524}
1525
1526/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001527 *
1528 * EncryptedExtensions message
1529 *
1530 * The EncryptedExtensions message contains any extensions which
1531 * should be protected, i.e., any which are not needed to establish
1532 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001533 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001534
1535/*
1536 * Overview
1537 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001538
1539/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001540static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001541
XiaokangQian97799ac2021-10-11 10:05:54 +00001542static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1543 const unsigned char *buf,
1544 const unsigned char *end );
1545static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001546
1547/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001548 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001550static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001551{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001552 int ret;
1553 unsigned char *buf;
1554 size_t buf_len;
1555
1556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1557
Xiaofei Bai746f9482021-11-12 08:53:56 +00001558 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001559 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560 &buf, &buf_len ) );
1561
1562 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001563 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001564 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001565
Xiaofei Bai746f9482021-11-12 08:53:56 +00001566 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001567 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001568
XiaokangQian97799ac2021-10-11 10:05:54 +00001569 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001570
1571cleanup:
1572
1573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1574 return( ret );
1575
1576}
1577
XiaokangQian08da26c2021-10-09 10:12:11 +00001578/* Parse EncryptedExtensions message
1579 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001580 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001581 * } EncryptedExtensions;
1582 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001583static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1584 const unsigned char *buf,
1585 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001586{
1587 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001588 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001589 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001590 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001591
XiaokangQian08da26c2021-10-09 10:12:11 +00001592 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001593 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001594 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001595
XiaokangQian97799ac2021-10-11 10:05:54 +00001596 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001597 extensions_end = p + extensions_len;
1598 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001599
XiaokangQian8db25ff2021-10-13 05:56:18 +00001600 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001601 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001602 unsigned int extension_type;
1603 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001604
XiaokangQian08da26c2021-10-09 10:12:11 +00001605 /*
1606 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001607 * ExtensionType extension_type; (2 bytes)
1608 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001609 * } Extension;
1610 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001611 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001612 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1613 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1614 p += 4;
1615
XiaokangQian8db25ff2021-10-13 05:56:18 +00001616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001617
XiaokangQian97799ac2021-10-11 10:05:54 +00001618 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001619 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001620 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001621 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001622 switch( extension_type )
1623 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001624
XiaokangQian08da26c2021-10-09 10:12:11 +00001625 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1627 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001628
XiaokangQian08da26c2021-10-09 10:12:11 +00001629 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001630 MBEDTLS_SSL_DEBUG_MSG(
1631 3, ( "unsupported extension found: %u ", extension_type) );
1632 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001633 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001634 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1635 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001636 }
1637
XiaokangQian08da26c2021-10-09 10:12:11 +00001638 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001639 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001640
XiaokangQian97799ac2021-10-11 10:05:54 +00001641 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001642 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001643 {
1644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001645 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001646 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001647 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001648 }
1649
1650 return( ret );
1651}
1652
XiaokangQian97799ac2021-10-11 10:05:54 +00001653static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001654{
Jerry Yua93ac112021-10-27 16:31:48 +08001655#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001656 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001657 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1658 else
Jerry Yud2674312021-10-29 10:08:19 +08001659 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001660#else
1661 ((void) ssl);
1662 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1663#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001664 return( 0 );
1665}
1666
Jerry Yua93ac112021-10-27 16:31:48 +08001667#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001668/*
Jerry Yud2674312021-10-29 10:08:19 +08001669 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1670 */
1671static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1672{
1673 int ret = mbedtls_ssl_read_record( ssl, 0 );
1674
1675 if( ret != 0 )
1676 {
1677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1678 return( ret );
1679 }
1680
1681 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1682 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1683 {
1684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1685 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1686 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1687 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1688 }
1689
1690 ssl->keep_current_message = 1;
1691 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1692
1693 return( 0 );
1694}
1695
1696/*
Jerry Yu687101b2021-09-14 16:03:56 +08001697 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1698 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001699static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001700{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001701 int ret;
1702
1703 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001704 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001705 return( ret );
1706
Jerry Yu687101b2021-09-14 16:03:56 +08001707 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1708 return( 0 );
1709}
1710
1711/*
1712 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1713 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001714static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001715{
Jerry Yu30b071c2021-09-12 20:16:03 +08001716 int ret;
1717
1718 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1719 if( ret != 0 )
1720 return( ret );
1721
Jerry Yu687101b2021-09-14 16:03:56 +08001722 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1723 return( 0 );
1724}
Jerry Yua93ac112021-10-27 16:31:48 +08001725#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001726
Jerry Yu687101b2021-09-14 16:03:56 +08001727/*
1728 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1729 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001730static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001731{
XiaokangQianac0385c2021-11-03 06:40:11 +00001732 int ret;
1733
XiaokangQianc5c39d52021-11-09 11:55:10 +00001734 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001735 if( ret != 0 )
1736 return( ret );
1737
Ronald Cron49ad6192021-11-24 16:25:31 +01001738#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1739 mbedtls_ssl_handshake_set_state(
1740 ssl,
1741 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1742#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001743 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001744#endif
1745
XiaokangQianac0385c2021-11-03 06:40:11 +00001746 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001747}
1748
1749/*
Ronald Crond4c64022021-12-06 09:06:46 +01001750 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1751 */
1752#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1753static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1754{
1755 int ret;
1756
1757 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1758 if( ret != 0 )
1759 return( ret );
1760
Ronald Crond4c64022021-12-06 09:06:46 +01001761 return( 0 );
1762}
1763#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1764
1765/*
Jerry Yu687101b2021-09-14 16:03:56 +08001766 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1767 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001768static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001769{
XiaokangQian0fa66432021-11-15 03:33:57 +00001770 int ret;
1771
Ronald Cron49ad6192021-11-24 16:25:31 +01001772 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1773
XiaokangQian0fa66432021-11-15 03:33:57 +00001774 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1775 if( ret != 0 )
1776 return( ret );
1777
1778 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1779 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001780}
1781
1782/*
1783 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1784 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001785static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001786{
Jerry Yu378254d2021-10-30 21:44:47 +08001787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001788 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001789 return( 0 );
1790}
1791
1792/*
1793 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1794 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001795static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001796{
Jerry Yu378254d2021-10-30 21:44:47 +08001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1798 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1799
1800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1801 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1802
1803 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1804
1805 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1806 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001807}
1808
Jerry Yu92c6b402021-08-27 16:59:09 +08001809int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001810{
Jerry Yu92c6b402021-08-27 16:59:09 +08001811 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001812
Jerry Yue3b34122021-09-28 17:53:35 +08001813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1814 mbedtls_ssl_states_str( ssl->state ),
1815 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001816
1817 switch( ssl->state )
1818 {
1819 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001820 * ssl->state is initialized as HELLO_REQUEST. It is the same
1821 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001822 */
1823 case MBEDTLS_SSL_HELLO_REQUEST:
1824 case MBEDTLS_SSL_CLIENT_HELLO:
1825 ret = ssl_tls13_write_client_hello( ssl );
1826 break;
1827
1828 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001829 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001830 break;
1831
1832 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001833 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001834 break;
1835
Jerry Yua93ac112021-10-27 16:31:48 +08001836#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001837 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1838 ret = ssl_tls13_process_certificate_request( ssl );
1839 break;
1840
Jerry Yu687101b2021-09-14 16:03:56 +08001841 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001842 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001843 break;
1844
1845 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001846 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001847 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001848#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001849
1850 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001851 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001852 break;
1853
Jerry Yu687101b2021-09-14 16:03:56 +08001854 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001855 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001856 break;
1857
1858 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001859 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001860 break;
1861
1862 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001863 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001864 break;
1865
Ronald Cron49ad6192021-11-24 16:25:31 +01001866 /*
1867 * Injection of dummy-CCS's for middlebox compatibility
1868 */
1869#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1870 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001871 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001872 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001873 break;
1874#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1875
Jerry Yu92c6b402021-08-27 16:59:09 +08001876 default:
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1879 }
1880
1881 return( ret );
1882}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001883
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001884#endif /* MBEDTLS_SSL_CLI_C */
1885
Ronald Cron6f135e12021-12-08 16:57:54 +01001886#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */