blob: f5c30b6015c172669dcba40fd2af9a888c53dd01 [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,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800146 unsigned char *buf,
147 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000148 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800149{
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100150 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
151 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
152 psa_key_attributes_t key_attributes;
153 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
154 size_t own_pubkey_len;
155 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800156
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800158
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100159 key_attributes = psa_key_attributes_init();
160 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
161 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
162 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
163 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800164
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100165 /* Generate ECDH private key. */
166 status = psa_generate_key( &key_attributes,
167 &handshake->ecdh_psa_privkey );
168 if( status != PSA_SUCCESS )
169 {
170 ret = psa_ssl_status_to_mbedtls( status );
171 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
172 return( ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800173
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100174 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800175
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100176 /* Export the public part of the ECDH private key from PSA. */
177 status = psa_export_public_key( handshake->ecdh_psa_privkey,
178 own_pubkey, sizeof( own_pubkey ),
179 &own_pubkey_len );
180 if( status != PSA_SUCCESS )
181 {
182 ret = psa_ssl_status_to_mbedtls( status );
183 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
184 return( ret );
185
186 }
187
188 if( own_pubkey_len > (size_t)( end - buf ) )
189 {
190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
191 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
192 }
193
194 *out_len = own_pubkey_len;
195
196 memcpy( buf, &own_pubkey, own_pubkey_len );
197
Jerry Yu75336352021-09-01 15:59:36 +0800198 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800199}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200#endif /* MBEDTLS_ECDH_C */
201
Jerry Yub60e3cf2021-09-08 16:41:02 +0800202static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
203 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800204{
205 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
206
Jerry Yu56fc07f2021-09-01 17:48:49 +0800207
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100209 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800210 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100211 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800212 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
213
Brett Warren14efd332021-10-06 09:32:11 +0100214 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000216 const mbedtls_ecp_curve_info *curve_info;
217 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
218 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100219 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Brett Warren14efd332021-10-06 09:32:11 +0100221 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222 return( 0 );
223 }
224 }
225#else
226 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800227 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228#endif /* MBEDTLS_ECDH_C */
229
230 /*
231 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800232 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233 */
234
235 return( ret );
236}
237
238/*
239 * ssl_tls13_write_key_share_ext
240 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800241 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242 *
243 * struct {
244 * NamedGroup group;
245 * opaque key_exchange<1..2^16-1>;
246 * } KeyShareEntry;
247 * struct {
248 * KeyShareEntry client_shares<0..2^16-1>;
249 * } KeyShareClientHello;
250 */
251static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
252 unsigned char *buf,
253 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000254 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255{
256 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000257 unsigned char *client_shares; /* Start of client_shares */
258 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800259 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
261
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800263
Jerry Yub60e3cf2021-09-08 16:41:02 +0800264 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 * - extension_type (2 bytes)
266 * - extension_data_length (2 bytes)
267 * - client_shares_length (2 bytes)
268 */
269 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
270 p += 6;
271
272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
273
274 /* HRR could already have requested something else. */
275 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800276 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
277 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800279 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280 &group_id ) );
281 }
282
283 /*
284 * Dispatch to type-specific key generation function.
285 *
286 * So far, we're only supporting ECDHE. With the introduction
287 * of PQC KEMs, we'll want to have multiple branches, one per
288 * type of KEM, and dispatch to the corresponding crypto. And
289 * only one key share entry is allowed.
290 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000291 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800293 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800295 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 /* Length of key_exchange */
298 size_t key_exchange_len;
299
300 /* Check there is space for header of KeyShareEntry
301 * - group (2 bytes)
302 * - key_exchange_length (2 bytes)
303 */
304 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
305 p += 4;
Przemyslaw Stekielb1b1f362022-02-10 10:19:46 +0100306 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800307 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 p += key_exchange_len;
309 if( ret != 0 )
310 return( ret );
311
312 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000313 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000315 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800316 }
317 else
318#endif /* MBEDTLS_ECDH_C */
319 if( 0 /* other KEMs? */ )
320 {
321 /* Do something */
322 }
323 else
324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
325
Jerry Yub60e3cf2021-09-08 16:41:02 +0800326 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000327 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800328 if( client_shares_len == 0)
329 {
330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800332 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800333 /* Write extension_type */
334 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
335 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800336 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800337 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800338 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800339
340 /* Update offered_group_id field */
341 ssl->handshake->offered_group_id = group_id;
342
343 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000344 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345
Xiaofei Baid25fab62021-12-02 06:36:27 +0000346 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347
348 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
349
350cleanup:
351
352 return( ret );
353}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800354
Jerry Yue1b9c292021-09-10 10:08:31 +0800355#if defined(MBEDTLS_ECDH_C)
356
Jerry Yuc068b662021-10-11 22:30:19 +0800357static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800358{
359 const mbedtls_ecp_curve_info *curve_info;
360 mbedtls_ecp_group_id grp_id;
361#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
362 grp_id = ssl->handshake->ecdh_ctx.grp.id;
363#else
364 grp_id = ssl->handshake->ecdh_ctx.grp_id;
365#endif
366
367 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
368 if( curve_info == NULL )
369 {
370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
372 }
373
374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
375
Jerry Yue1b9c292021-09-10 10:08:31 +0800376 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800377 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800378
379 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
380 MBEDTLS_DEBUG_ECDH_QP );
381
382 return( 0 );
383}
384
Jerry Yuc068b662021-10-11 22:30:19 +0800385static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
386 const unsigned char *buf,
387 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800388{
389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielf008ae72022-02-10 10:32:02 +0100390 uint8_t ecpoint_len;
391 uint8_t *p = (uint8_t*)buf;
392 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800393
Przemyslaw Stekielf008ae72022-02-10 10:32:02 +0100394 /*
395 * Put peer's ECDH public key in the format understood by PSA.
396 */
397
398 ecpoint_len = *p++;
399 if( ( buf_len - 1 ) < ecpoint_len )
400 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
401
402 if ( ( ret = mbedtls_psa_tls_ecpoint_to_psa_ec( p,
403 ecpoint_len, handshake->ecdh_psa_peerkey,
404 sizeof( handshake->ecdh_psa_peerkey ),
405 &handshake->ecdh_psa_peerkey_len ) ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800406 {
Przemyslaw Stekielf008ae72022-02-10 10:32:02 +0100407 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +0800408 }
409
410 return( 0 );
411}
Jerry Yu4a173382021-10-11 21:45:31 +0800412#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800413
XiaokangQiand59be772022-01-24 10:12:51 +0000414/*
415 * ssl_tls13_parse_hrr_key_share_ext()
416 * Parse key_share extension in Hello Retry Request
417 *
418 * struct {
419 * NamedGroup selected_group;
420 * } KeyShareHelloRetryRequest;
421 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000422static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000423 const unsigned char *buf,
424 const unsigned char *end )
425{
XiaokangQianb851da82022-01-14 04:03:11 +0000426 const mbedtls_ecp_curve_info *curve_info = NULL;
427 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000428 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000429 int found = 0;
430
431 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
432 if( group_list == NULL )
433 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
434
435 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
436
437 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000438 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000439 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000441
442 /* Upon receipt of this extension in a HelloRetryRequest, the client
443 * MUST first verify that the selected_group field corresponds to a
444 * group which was provided in the "supported_groups" extension in the
445 * original ClientHello.
446 * The supported_group was based on the info in ssl->conf->group_list.
447 *
448 * If the server provided a key share that was not sent in the ClientHello
449 * then the client MUST abort the handshake with an "illegal_parameter" alert.
450 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000451 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000452 {
453 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000454 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000455 continue;
456
457 /* We found a match */
458 found = 1;
459 break;
460 }
461
462 /* Client MUST verify that the selected_group field does not
463 * correspond to a group which was provided in the "key_share"
464 * extension in the original ClientHello. If the server sent an
465 * HRR message with a key share already provided in the
466 * ClientHello then the client MUST abort the handshake with
467 * an "illegal_parameter" alert.
468 */
XiaokangQiand59be772022-01-24 10:12:51 +0000469 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000470 {
471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
472 MBEDTLS_SSL_PEND_FATAL_ALERT(
473 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
474 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
475 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
476 }
477
478 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000479 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000480
481 return( 0 );
482}
483
Jerry Yue1b9c292021-09-10 10:08:31 +0800484/*
Jerry Yub85277e2021-10-13 13:36:05 +0800485 * ssl_tls13_parse_key_share_ext()
486 * Parse key_share extension in Server Hello
487 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800488 * struct {
489 * KeyShareEntry server_share;
490 * } KeyShareServerHello;
491 * struct {
492 * NamedGroup group;
493 * opaque key_exchange<1..2^16-1>;
494 * } KeyShareEntry;
495 */
Jerry Yu4a173382021-10-11 21:45:31 +0800496static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800497 const unsigned char *buf,
498 const unsigned char *end )
499{
Jerry Yub85277e2021-10-13 13:36:05 +0800500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800501 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800502 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800503
Jerry Yu4a173382021-10-11 21:45:31 +0800504 /* ...
505 * NamedGroup group; (2 bytes)
506 * ...
507 */
508 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
509 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800510 p += 2;
511
Jerry Yu4a173382021-10-11 21:45:31 +0800512 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800513 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800514 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800515 {
516 MBEDTLS_SSL_DEBUG_MSG( 1,
517 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800518 (unsigned) offered_group, (unsigned) group ) );
519 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
520 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
521 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800522 }
523
524#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800525 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800526 {
527 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800528 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800529 if( ret != 0 )
530 return( ret );
531 }
Jerry Yub85277e2021-10-13 13:36:05 +0800532 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800533#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800534 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800535 {
536 /* Do something */
537 }
538 else
539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
540
541 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
542 return( ret );
543}
544
Jerry Yubc20bdd2021-08-24 15:59:48 +0800545#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
546
XiaokangQiand59be772022-01-24 10:12:51 +0000547/*
548 * ssl_tls13_parse_cookie_ext()
549 * Parse cookie extension in Hello Retry Request
550 *
551 * struct {
552 * opaque cookie<1..2^16-1>;
553 * } Cookie;
554 *
555 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
556 * extension to the client (this is an exception to the usual rule that
557 * the only extensions that may be sent are those that appear in the
558 * ClientHello). When sending the new ClientHello, the client MUST copy
559 * the contents of the extension received in the HelloRetryRequest into
560 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
561 * cookies in their initial ClientHello in subsequent connections.
562 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000563static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
564 const unsigned char *buf,
565 const unsigned char *end )
566{
567 size_t cookie_len;
568 const unsigned char *p = buf;
569 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
570
571 /* Retrieve length field of cookie */
572 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
573 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
574 p += 2;
575
576 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
577 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
578
579 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000580 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000581 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
582 if( handshake->verify_cookie == NULL )
583 {
584 MBEDTLS_SSL_DEBUG_MSG( 1,
585 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
586 cookie_len ) );
587 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
588 }
589
590 memcpy( handshake->verify_cookie, p, cookie_len );
591 handshake->verify_cookie_len = (unsigned char) cookie_len;
592
593 return( 0 );
594}
XiaokangQian43550bd2022-01-21 04:32:58 +0000595
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800596/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800597 * CipherSuite cipher_suites<2..2^16-2>;
598 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800599static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800600 mbedtls_ssl_context *ssl,
601 unsigned char *buf,
602 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000603 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800604{
Jerry Yufec982e2021-09-07 17:26:06 +0800605 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800606 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000607 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800608 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800609
Xiaofei Baid25fab62021-12-02 06:36:27 +0000610 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800611
612 /*
613 * Ciphersuite list
614 *
615 * This is a list of the symmetric cipher options supported by
616 * the client, specifically the record protection algorithm
617 * ( including secret key length ) and a hash to be used with
618 * HKDF, in descending order of client preference.
619 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800620 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800621
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800622 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800623 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
624 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800625
Jerry Yu0c63af62021-09-02 12:59:12 +0800626 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000627 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800628 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800629 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800630 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800631 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800632
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800633 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800634 if( ciphersuite_info == NULL )
635 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800636 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
637 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800638 continue;
639
640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800641 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800642 ciphersuite_info->name ) );
643
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800644 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800645 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
646 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
647 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800648 }
649
Jerry Yu0c63af62021-09-02 12:59:12 +0800650 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000651 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800652 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800653 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800654 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
655 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800656
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800657 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000658 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800659
Jerry Yu6a643102021-08-31 14:40:36 +0800660 return( 0 );
661}
662
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800663/*
664 * Structure of ClientHello message:
665 *
666 * struct {
667 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
668 * Random random;
669 * opaque legacy_session_id<0..32>;
670 * CipherSuite cipher_suites<2..2^16-2>;
671 * opaque legacy_compression_methods<1..2^8-1>;
672 * Extension extensions<8..2^16-1>;
673 * } ClientHello;
674 */
Jerry Yu08906d02021-08-31 11:05:27 +0800675static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800676 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800677 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000678 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800679{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800680
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000682 unsigned char *p_extensions_len; /* Pointer to extensions length */
683 size_t output_len; /* Length of buffer used by function */
684 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800685
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800687 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800688
Xiaofei Baid25fab62021-12-02 06:36:27 +0000689 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800690
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800691 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800692 ssl->major_ver = ssl->conf->min_major_ver;
693 ssl->minor_ver = ssl->conf->min_minor_ver;
694
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800695 /*
696 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800697 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800698 *
699 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800700 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701 */
Jerry Yufec982e2021-09-07 17:26:06 +0800702 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800703 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800704 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800706 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800707 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
708 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800709 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800710 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
711 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800712
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800713 /*
714 * Write legacy_session_id
715 *
716 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
717 * which has been merged with pre-shared keys in this version. A client
718 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
719 * this field to that value. In compatibility mode, this field MUST be
720 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
721 * a new 32-byte value. This value need not be random but SHOULD be
722 * unpredictable to avoid implementations fixating on a specific value
723 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
724 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800725 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100726#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
727 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
728 *p++ = (unsigned char)ssl->session_negotiate->id_len;
729 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
730 p += ssl->session_negotiate->id_len;
731
732 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
733 ssl->session_negotiate->id_len );
734#else
Jerry Yubbe09522021-09-06 21:17:54 +0800735 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
736 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100737#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800738
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800739 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800740 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800741 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800742 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800743 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800744
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800745 /* Write legacy_compression_methods
746 *
747 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800748 * one byte set to zero, which corresponds to the 'null' compression
749 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750 */
Jerry Yubbe09522021-09-06 21:17:54 +0800751 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
752 *p++ = 1;
753 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800754
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800755 /* Write extensions */
756
757 /* Keeping track of the included extensions */
758 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800759
760 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800761 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000762 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800763 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800764
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800765 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800766 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800767 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800768 */
Jerry Yubbe09522021-09-06 21:17:54 +0800769 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800770 if( ret != 0 )
771 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800772 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800773
774#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800775
Jerry Yub925f212022-01-12 11:17:02 +0800776 /*
777 * Add the extensions related to (EC)DHE ephemeral key establishment only if
778 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800779 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800780 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
781 {
782 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
783 if( ret != 0 )
784 return( ret );
785 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800786
Jerry Yuf46b0162022-01-11 16:28:00 +0800787 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
788 if( ret != 0 )
789 return( ret );
790 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800791
Jerry Yuf017ee42022-01-12 15:49:48 +0800792 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800793 if( ret != 0 )
794 return( ret );
795 p += output_len;
796 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
798
Xiaofei Bai15a56812021-11-05 10:52:12 +0000799#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000800 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000801 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
802 if( ret != 0 )
803 return( ret );
804 p += output_len;
805#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
806
Jerry Yubc20bdd2021-08-24 15:59:48 +0800807 /* Add more extensions here */
808
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800809 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000810 extensions_len = p - p_extensions_len - 2;
811 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800813 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000814 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800815
Xiaofei Baid25fab62021-12-02 06:36:27 +0000816 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800817 return( 0 );
818}
819
Jerry Yu335aca92021-09-12 20:18:56 +0800820static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800821{
Jerry Yu92c6b402021-08-27 16:59:09 +0800822 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
823 return( 0 );
824}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800825
Jerry Yu92c6b402021-08-27 16:59:09 +0800826static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
827{
828 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800829
Jerry Yu92c6b402021-08-27 16:59:09 +0800830 if( ssl->conf->f_rng == NULL )
831 {
832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
833 return( MBEDTLS_ERR_SSL_NO_RNG );
834 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800835
Jerry Yu92c6b402021-08-27 16:59:09 +0800836 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
837 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800838 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800839 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800840 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800841 return( ret );
842 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800843
Ronald Cron49ad6192021-11-24 16:25:31 +0100844#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
845 /*
846 * Create a session identifier for the purpose of middlebox compatibility
847 * only if one has not been created already.
848 */
849 if( ssl->session_negotiate->id_len == 0 )
850 {
851 /* Creating a session id with 32 byte length */
852 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
853 ssl->session_negotiate->id, 32 ) ) != 0 )
854 {
855 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
856 return( ret );
857 }
858 ssl->session_negotiate->id_len = 32;
859 }
860#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
861
Jerry Yu6f13f642021-08-26 17:18:15 +0800862 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800863}
864
Jerry Yu92c6b402021-08-27 16:59:09 +0800865/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800866 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800867 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800868 */
869static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800870{
Jerry Yu92c6b402021-08-27 16:59:09 +0800871 int ret = 0;
872 unsigned char *buf;
873 size_t buf_len, msg_len;
874
875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
876
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800877 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800878
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800879 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
880 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
881 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800882
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800883 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800884 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800885 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800886
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800887 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
888 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800889 msg_len );
890 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800891
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800892 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
893 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
894 buf_len,
895 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800896
897cleanup:
898
899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
900 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800901}
902
Jerry Yu687101b2021-09-14 16:03:56 +0800903/*
Jerry Yu4a173382021-10-11 21:45:31 +0800904 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800905 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800906/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800907 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
908 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000909 * to indicate which message is expected and to be parsed next.
910 */
Jerry Yub85277e2021-10-13 13:36:05 +0800911#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
912#define SSL_SERVER_HELLO_COORDINATE_HRR 1
913static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
914 const unsigned char *buf,
915 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800916{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800917 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800918 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
919 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
920 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
921 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
922
923 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
924 *
Jerry Yu4a173382021-10-11 21:45:31 +0800925 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800926 * special value of the SHA-256 of "HelloRetryRequest".
927 *
928 * struct {
929 * ProtocolVersion legacy_version = 0x0303;
930 * Random random;
931 * opaque legacy_session_id_echo<0..32>;
932 * CipherSuite cipher_suite;
933 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800934 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800935 * } ServerHello;
936 *
937 */
Jerry Yub85277e2021-10-13 13:36:05 +0800938 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800939
940 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 {
Jerry Yub85277e2021-10-13 13:36:05 +0800942 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800943 }
944
Jerry Yub85277e2021-10-13 13:36:05 +0800945 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800946}
947
Jerry Yu745bb612021-10-13 22:01:04 +0800948/* Fetch and preprocess
949 * Returns a negative value on failure, and otherwise
950 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
951 * - SSL_SERVER_HELLO_COORDINATE_HRR
952 */
Jerry Yub85277e2021-10-13 13:36:05 +0800953static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
954 unsigned char **buf,
955 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800956{
Jerry Yu4a173382021-10-11 21:45:31 +0800957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800958
XiaokangQian355e09a2022-01-20 11:14:50 +0000959 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
960 MBEDTLS_SSL_HS_SERVER_HELLO,
961 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800962
Jerry Yub85277e2021-10-13 13:36:05 +0800963 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
964 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 {
Jerry Yu745bb612021-10-13 22:01:04 +0800966 case SSL_SERVER_HELLO_COORDINATE_HELLO:
967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
968 break;
969 case SSL_SERVER_HELLO_COORDINATE_HRR:
970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000971 /* If a client receives a second
972 * HelloRetryRequest in the same connection (i.e., where the ClientHello
973 * was itself in response to a HelloRetryRequest), it MUST abort the
974 * handshake with an "unexpected_message" alert.
975 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000976 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000977 {
978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
979 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
980 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
981 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
982 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000983 /*
984 * Clients must abort the handshake with an "illegal_parameter"
985 * alert if the HelloRetryRequest would not result in any change
986 * in the ClientHello.
987 * In a PSK only key exchange that what we expect.
988 */
989 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
990 {
991 MBEDTLS_SSL_DEBUG_MSG( 1,
992 ( "Unexpected HRR in pure PSK key exchange." ) );
993 MBEDTLS_SSL_PEND_FATAL_ALERT(
994 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
995 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
996 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
997 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000998
XiaokangQiand9e068e2022-01-18 06:23:32 +0000999 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001000
Jerry Yu745bb612021-10-13 22:01:04 +08001001 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001002 }
1003
1004cleanup:
1005
1006 return( ret );
1007}
1008
Jerry Yu4a173382021-10-11 21:45:31 +08001009static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1010 const unsigned char **buf,
1011 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001012{
1013 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001014 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001015
Jerry Yude4fb2c2021-09-19 18:05:08 +08001016 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001017 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001018
Jerry Yu4a173382021-10-11 21:45:31 +08001019 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001020
1021 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001022 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1023 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001024 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001025 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1026 ssl->session_negotiate->id,
1027 ssl->session_negotiate->id_len );
1028 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001029 legacy_session_id_echo_len );
1030
1031 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1032 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1033
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1035 }
1036
Jerry Yu4a173382021-10-11 21:45:31 +08001037 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 *buf = p;
1039
1040 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001041 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 return( 0 );
1043}
1044
Jerry Yu4a173382021-10-11 21:45:31 +08001045static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1046 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001047{
Jerry Yu4a173382021-10-11 21:45:31 +08001048 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1049
Jerry Yue1b9c292021-09-10 10:08:31 +08001050 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001051 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001052 {
Jerry Yu4a173382021-10-11 21:45:31 +08001053 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001054 {
1055 return( 1 );
1056 }
1057 }
1058 return( 0 );
1059}
1060
1061/* Parse ServerHello message and configure context
1062 *
1063 * struct {
1064 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1065 * Random random;
1066 * opaque legacy_session_id_echo<0..32>;
1067 * CipherSuite cipher_suite;
1068 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001069 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 * } ServerHello;
1071 */
Jerry Yuc068b662021-10-11 22:30:19 +08001072static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1073 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001074 const unsigned char *end,
1075 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001076{
Jerry Yub85277e2021-10-13 13:36:05 +08001077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001079 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001080 size_t extensions_len;
1081 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001082 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001084 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001085 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001086
1087 /*
1088 * Check there is space for minimal fields
1089 *
1090 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001091 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001092 * - legacy_session_id_echo ( 1 byte ), minimum size
1093 * - cipher_suite ( 2 bytes)
1094 * - legacy_compression_method ( 1 byte )
1095 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001096 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001097
1098 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1100
Jerry Yu4a173382021-10-11 21:45:31 +08001101 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001102 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001103 * ...
1104 * with ProtocolVersion defined as:
1105 * uint16 ProtocolVersion;
1106 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1108 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1109 {
1110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1111 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1112 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001113 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1114 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001115 }
1116 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001117
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001118 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001119 * Random random;
1120 * ...
1121 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001122 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001123 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001124 if( !is_hrr )
1125 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001126 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001127 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1128 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1129 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1130 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001131 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001132
Jerry Yu4a173382021-10-11 21:45:31 +08001133 /* ...
1134 * opaque legacy_session_id_echo<0..32>;
1135 * ...
1136 */
1137 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001138 {
XiaokangQian52da5582022-01-26 09:49:29 +00001139 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001140 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001141 }
1142
Jerry Yu4a173382021-10-11 21:45:31 +08001143 /* ...
1144 * CipherSuite cipher_suite;
1145 * ...
1146 * with CipherSuite defined as:
1147 * uint8 CipherSuite[2];
1148 */
1149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1151 p += 2;
1152
Jerry Yu4a173382021-10-11 21:45:31 +08001153
XiaokangQian355e09a2022-01-20 11:14:50 +00001154 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001155 /*
1156 * Check whether this ciphersuite is supported and offered.
1157 * Via the force_ciphersuite version we may have instructed the client
1158 * to use a different ciphersuite.
1159 */
Jerry Yu4a173382021-10-11 21:45:31 +08001160 if( ciphersuite_info == NULL ||
1161 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001162 {
XiaokangQian52da5582022-01-26 09:49:29 +00001163 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001165 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001166 * If we received an HRR before and that the proposed selected
1167 * ciphersuite in this server hello is not the same as the one
1168 * proposed in the HRR, we abort the handshake and send an
1169 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001170 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001171 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1172 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001173 {
XiaokangQian52da5582022-01-26 09:49:29 +00001174 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001175 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001176
XiaokangQian52da5582022-01-26 09:49:29 +00001177 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001178 {
1179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1180 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001181 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001182 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001183
Jerry Yu4a173382021-10-11 21:45:31 +08001184 /* Configure ciphersuites */
1185 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1186
XiaokangQian355e09a2022-01-20 11:14:50 +00001187 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001188 ssl->session_negotiate->ciphersuite = cipher_suite;
1189
Jerry Yue1b9c292021-09-10 10:08:31 +08001190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1191 cipher_suite, ciphersuite_info->name ) );
1192
1193#if defined(MBEDTLS_HAVE_TIME)
1194 ssl->session_negotiate->start = time( NULL );
1195#endif /* MBEDTLS_HAVE_TIME */
1196
Jerry Yu4a173382021-10-11 21:45:31 +08001197 /* ...
1198 * uint8 legacy_compression_method = 0;
1199 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001200 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001201 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001202 if( p[0] != 0 )
1203 {
Jerry Yub85277e2021-10-13 13:36:05 +08001204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001205 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001206 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 }
1208 p++;
1209
Jerry Yub85277e2021-10-13 13:36:05 +08001210 /* ...
1211 * Extension extensions<6..2^16-1>;
1212 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001213 * struct {
1214 * ExtensionType extension_type; (2 bytes)
1215 * opaque extension_data<0..2^16-1>;
1216 * } Extension;
1217 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001218 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001219 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001221
Jerry Yu4a173382021-10-11 21:45:31 +08001222 /* Check extensions do not go beyond the buffer of data. */
1223 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1224 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001225
Jerry Yu4a173382021-10-11 21:45:31 +08001226 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001227
Jerry Yu4a173382021-10-11 21:45:31 +08001228 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001229 {
1230 unsigned int extension_type;
1231 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001232 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001233
Jerry Yu4a173382021-10-11 21:45:31 +08001234 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1236 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1237 p += 4;
1238
Jerry Yu4a173382021-10-11 21:45:31 +08001239 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001240 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001241
1242 switch( extension_type )
1243 {
XiaokangQianb851da82022-01-14 04:03:11 +00001244 case MBEDTLS_TLS_EXT_COOKIE:
1245
XiaokangQiand9e068e2022-01-18 06:23:32 +00001246 if( !is_hrr )
1247 {
XiaokangQian52da5582022-01-26 09:49:29 +00001248 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001249 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001250 }
1251
XiaokangQian43550bd2022-01-21 04:32:58 +00001252 ret = ssl_tls13_parse_cookie_ext( ssl,
1253 p, extension_data_end );
1254 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001255 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001256 MBEDTLS_SSL_DEBUG_RET( 1,
1257 "ssl_tls13_parse_cookie_ext",
1258 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001259 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001260 }
XiaokangQianb851da82022-01-14 04:03:11 +00001261 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001262
Jerry Yue1b9c292021-09-10 10:08:31 +08001263 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001264 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001265 MBEDTLS_SSL_DEBUG_MSG( 3,
1266 ( "found supported_versions extension" ) );
1267
Jerry Yuc068b662021-10-11 22:30:19 +08001268 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001269 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001270 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001271 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001272 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001273 break;
1274
1275 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001278
XiaokangQian52da5582022-01-26 09:49:29 +00001279 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001280 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001281
1282#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1283 case MBEDTLS_TLS_EXT_KEY_SHARE:
1284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001285 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1286 {
XiaokangQian52da5582022-01-26 09:49:29 +00001287 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001288 goto cleanup;
1289 }
1290
XiaokangQian53f20b72022-01-18 10:47:33 +00001291 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001292 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001293 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001294 else
XiaokangQianb851da82022-01-14 04:03:11 +00001295 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001296 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001297 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001298 {
1299 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001300 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001301 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001302 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001303 }
1304 break;
1305#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1306
1307 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001308 MBEDTLS_SSL_DEBUG_MSG(
1309 3,
1310 ( "unknown extension found: %u ( ignoring )",
1311 extension_type ) );
1312
XiaokangQian52da5582022-01-26 09:49:29 +00001313 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001314 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001315 }
1316
1317 p += extension_data_len;
1318 }
1319
XiaokangQian78b1fa72022-01-19 06:56:30 +00001320 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001321 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001323 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001324 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001325 }
1326
XiaokangQiand59be772022-01-24 10:12:51 +00001327cleanup:
1328
XiaokangQian52da5582022-01-26 09:49:29 +00001329 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001330 {
1331 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1332 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1333 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1334 }
XiaokangQian52da5582022-01-26 09:49:29 +00001335 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001336 {
1337 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1338 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1339 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1340 }
1341 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001342}
1343
XiaokangQian355e09a2022-01-20 11:14:50 +00001344static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001345{
Jerry Yub85277e2021-10-13 13:36:05 +08001346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001347 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001348 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001349 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001350
Jerry Yub85277e2021-10-13 13:36:05 +08001351 /* Determine the key exchange mode:
1352 * 1) If both the pre_shared_key and key_share extensions were received
1353 * then the key exchange mode is PSK with EPHEMERAL.
1354 * 2) If only the pre_shared_key extension was received then the key
1355 * exchange mode is PSK-only.
1356 * 3) If only the key_share extension was received then the key
1357 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001358 */
Jerry Yub85277e2021-10-13 13:36:05 +08001359 switch( handshake->extensions_present &
1360 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001361 {
Jerry Yu745bb612021-10-13 22:01:04 +08001362 /* Only the pre_shared_key extension was received */
1363 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001364 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001365 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001366
Jerry Yu745bb612021-10-13 22:01:04 +08001367 /* Only the key_share extension was received */
1368 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001369 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001370 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001371
Jerry Yu745bb612021-10-13 22:01:04 +08001372 /* Both the pre_shared_key and key_share extensions were received */
1373 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001374 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001375 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001376
Jerry Yu745bb612021-10-13 22:01:04 +08001377 /* Neither pre_shared_key nor key_share extension was received */
1378 default:
1379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1380 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1381 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001382 }
1383
1384 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1385 *
1386 * TODO: We don't have to do this in case we offered 0-RTT and the
1387 * server accepted it. In this case, we could skip generating
1388 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001389 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001390 if( ret != 0 )
1391 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001393 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001394 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001395 }
1396
1397 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001398 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001399 if( ret != 0 )
1400 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001402 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001403 }
1404
1405 /* Next evolution in key schedule: Establish handshake secret and
1406 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001407 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001408 if( ret != 0 )
1409 {
Jerry Yuc068b662021-10-11 22:30:19 +08001410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1411 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001412 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001413 }
1414
Jerry Yub85277e2021-10-13 13:36:05 +08001415 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001416 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001417 {
1418 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1419 goto cleanup;
1420 }
Jerry Yu0b177842021-09-05 19:41:30 +08001421
1422 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1423 ssl->conf->endpoint,
1424 ssl->session_negotiate->ciphersuite,
1425 &traffic_keys,
1426 ssl );
1427 if( ret != 0 )
1428 {
1429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001430 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001431 }
1432
Jerry Yub85277e2021-10-13 13:36:05 +08001433 handshake->transform_handshake = transform_handshake;
1434 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001435
1436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1437 ssl->session_in = ssl->session_negotiate;
1438
1439 /*
1440 * State machine update
1441 */
1442 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1443
Jerry Yu4a173382021-10-11 21:45:31 +08001444cleanup:
1445
Jerry Yu0b177842021-09-05 19:41:30 +08001446 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001447 if( ret != 0 )
1448 {
Jerry Yub85277e2021-10-13 13:36:05 +08001449 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001450
Jerry Yu4a173382021-10-11 21:45:31 +08001451 MBEDTLS_SSL_PEND_FATAL_ALERT(
1452 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1453 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1454 }
1455 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001456}
1457
XiaokangQian355e09a2022-01-20 11:14:50 +00001458static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001459{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001460#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001461 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1462
XiaokangQian647719a2021-12-07 09:16:29 +00001463#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1464 /* If not offering early data, the client sends a dummy CCS record
1465 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001466 * its second ClientHello or before its encrypted handshake flight.
1467 */
XiaokangQian647719a2021-12-07 09:16:29 +00001468 mbedtls_ssl_handshake_set_state( ssl,
1469 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1470#else
1471 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1472#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1473
XiaokangQian78b1fa72022-01-19 06:56:30 +00001474 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001475
XiaokangQian78b1fa72022-01-19 06:56:30 +00001476 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001477 * We are going to re-generate a shared secret corresponding to the group
1478 * selected by the server, which is different from the group for which we
1479 * generated a shared secret in the first client hello.
1480 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001481 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001482 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001483 if( ret != 0 )
1484 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001485#else
1486 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001487#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001488
1489 return( 0 );
1490}
1491
Jerry Yue1b9c292021-09-10 10:08:31 +08001492/*
Jerry Yu4a173382021-10-11 21:45:31 +08001493 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001494 * Handler for MBEDTLS_SSL_SERVER_HELLO
1495 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001496static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001497{
Jerry Yu4a173382021-10-11 21:45:31 +08001498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001499 unsigned char *buf = NULL;
1500 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001501 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001502
1503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1504
1505 /* Coordination step
1506 * - Fetch record
1507 * - Make sure it's either a ServerHello or a HRR.
1508 * - Switch processing routine in case of HRR
1509 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001510 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1511 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1512
XiaokangQian16acd4b2022-01-14 07:35:47 +00001513 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001514 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001515 goto cleanup;
1516 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001517 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001518
XiaokangQianb851da82022-01-14 04:03:11 +00001519 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001520 buf + buf_len,
1521 is_hrr ) );
1522 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001523 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1524
XiaokangQianb851da82022-01-14 04:03:11 +00001525 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1526 MBEDTLS_SSL_HS_SERVER_HELLO,
1527 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001528
XiaokangQiand9e068e2022-01-18 06:23:32 +00001529 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001530 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001531 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001532 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001533
1534cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1536 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001537 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001538}
1539
1540/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001541 *
1542 * EncryptedExtensions message
1543 *
1544 * The EncryptedExtensions message contains any extensions which
1545 * should be protected, i.e., any which are not needed to establish
1546 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001547 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001548
1549/*
1550 * Overview
1551 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001552
1553/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001554static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001555
XiaokangQian97799ac2021-10-11 10:05:54 +00001556static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1557 const unsigned char *buf,
1558 const unsigned char *end );
1559static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560
1561/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001562 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001563 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001564static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001565{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001566 int ret;
1567 unsigned char *buf;
1568 size_t buf_len;
1569
1570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1571
Xiaofei Bai746f9482021-11-12 08:53:56 +00001572 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001573 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001574 &buf, &buf_len ) );
1575
1576 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001577 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001578 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001579
Xiaofei Bai746f9482021-11-12 08:53:56 +00001580 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001581 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001582
XiaokangQian97799ac2021-10-11 10:05:54 +00001583 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001584
1585cleanup:
1586
1587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1588 return( ret );
1589
1590}
1591
XiaokangQian08da26c2021-10-09 10:12:11 +00001592/* Parse EncryptedExtensions message
1593 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001594 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001595 * } EncryptedExtensions;
1596 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001597static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1598 const unsigned char *buf,
1599 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001600{
1601 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001602 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001603 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001604 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001605
XiaokangQian08da26c2021-10-09 10:12:11 +00001606 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001607 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001608 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001609
XiaokangQian97799ac2021-10-11 10:05:54 +00001610 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001611 extensions_end = p + extensions_len;
1612 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001613
XiaokangQian8db25ff2021-10-13 05:56:18 +00001614 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001615 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001616 unsigned int extension_type;
1617 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001618
XiaokangQian08da26c2021-10-09 10:12:11 +00001619 /*
1620 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001621 * ExtensionType extension_type; (2 bytes)
1622 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001623 * } Extension;
1624 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001625 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001626 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1627 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1628 p += 4;
1629
XiaokangQian8db25ff2021-10-13 05:56:18 +00001630 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001631
XiaokangQian97799ac2021-10-11 10:05:54 +00001632 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001633 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001634 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001635 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001636 switch( extension_type )
1637 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001638
XiaokangQian08da26c2021-10-09 10:12:11 +00001639 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1641 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001642
XiaokangQian08da26c2021-10-09 10:12:11 +00001643 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001644 MBEDTLS_SSL_DEBUG_MSG(
1645 3, ( "unsupported extension found: %u ", extension_type) );
1646 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001647 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001648 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1649 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001650 }
1651
XiaokangQian08da26c2021-10-09 10:12:11 +00001652 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001653 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001654
XiaokangQian97799ac2021-10-11 10:05:54 +00001655 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001656 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001657 {
1658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001659 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001660 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001661 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001662 }
1663
1664 return( ret );
1665}
1666
XiaokangQian97799ac2021-10-11 10:05:54 +00001667static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001668{
Jerry Yua93ac112021-10-27 16:31:48 +08001669#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001670 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001671 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1672 else
Jerry Yud2674312021-10-29 10:08:19 +08001673 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001674#else
1675 ((void) ssl);
1676 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1677#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001678 return( 0 );
1679}
1680
Jerry Yua93ac112021-10-27 16:31:48 +08001681#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001682/*
Jerry Yud2674312021-10-29 10:08:19 +08001683 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1684 */
1685static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1686{
1687 int ret = mbedtls_ssl_read_record( ssl, 0 );
1688
1689 if( ret != 0 )
1690 {
1691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1692 return( ret );
1693 }
1694
1695 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1696 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1697 {
1698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1699 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1700 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1701 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1702 }
1703
1704 ssl->keep_current_message = 1;
1705 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1706
1707 return( 0 );
1708}
1709
1710/*
Jerry Yu687101b2021-09-14 16:03:56 +08001711 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1712 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001713static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001714{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001715 int ret;
1716
1717 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001718 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001719 return( ret );
1720
Jerry Yu687101b2021-09-14 16:03:56 +08001721 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1722 return( 0 );
1723}
1724
1725/*
1726 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1727 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001728static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001729{
Jerry Yu30b071c2021-09-12 20:16:03 +08001730 int ret;
1731
1732 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1733 if( ret != 0 )
1734 return( ret );
1735
Jerry Yu687101b2021-09-14 16:03:56 +08001736 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1737 return( 0 );
1738}
Jerry Yua93ac112021-10-27 16:31:48 +08001739#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001740
Jerry Yu687101b2021-09-14 16:03:56 +08001741/*
1742 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1743 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001744static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001745{
XiaokangQianac0385c2021-11-03 06:40:11 +00001746 int ret;
1747
XiaokangQianc5c39d52021-11-09 11:55:10 +00001748 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001749 if( ret != 0 )
1750 return( ret );
1751
Ronald Cron49ad6192021-11-24 16:25:31 +01001752#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1753 mbedtls_ssl_handshake_set_state(
1754 ssl,
1755 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1756#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001757 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001758#endif
1759
XiaokangQianac0385c2021-11-03 06:40:11 +00001760 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001761}
1762
1763/*
Ronald Crond4c64022021-12-06 09:06:46 +01001764 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1765 */
1766#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1767static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1768{
1769 int ret;
1770
1771 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1772 if( ret != 0 )
1773 return( ret );
1774
Ronald Crond4c64022021-12-06 09:06:46 +01001775 return( 0 );
1776}
1777#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1778
1779/*
Jerry Yu687101b2021-09-14 16:03:56 +08001780 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1781 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001782static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001783{
XiaokangQian0fa66432021-11-15 03:33:57 +00001784 int ret;
1785
Ronald Cron49ad6192021-11-24 16:25:31 +01001786 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1787
XiaokangQian0fa66432021-11-15 03:33:57 +00001788 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1789 if( ret != 0 )
1790 return( ret );
1791
1792 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1793 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001794}
1795
1796/*
1797 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1798 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001799static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001800{
Jerry Yu378254d2021-10-30 21:44:47 +08001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001802 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001803 return( 0 );
1804}
1805
1806/*
1807 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1808 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001809static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001810{
Jerry Yu378254d2021-10-30 21:44:47 +08001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1812 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1813
1814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1815 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1816
1817 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1818
1819 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1820 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001821}
1822
Jerry Yu92c6b402021-08-27 16:59:09 +08001823int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001824{
Jerry Yu92c6b402021-08-27 16:59:09 +08001825 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001826
Jerry Yue3b34122021-09-28 17:53:35 +08001827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1828 mbedtls_ssl_states_str( ssl->state ),
1829 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001830
1831 switch( ssl->state )
1832 {
1833 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001834 * ssl->state is initialized as HELLO_REQUEST. It is the same
1835 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001836 */
1837 case MBEDTLS_SSL_HELLO_REQUEST:
1838 case MBEDTLS_SSL_CLIENT_HELLO:
1839 ret = ssl_tls13_write_client_hello( ssl );
1840 break;
1841
1842 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001843 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001844 break;
1845
1846 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001847 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001848 break;
1849
Jerry Yua93ac112021-10-27 16:31:48 +08001850#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001851 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1852 ret = ssl_tls13_process_certificate_request( ssl );
1853 break;
1854
Jerry Yu687101b2021-09-14 16:03:56 +08001855 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001856 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001857 break;
1858
1859 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001860 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001861 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001862#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001863
1864 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001865 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001866 break;
1867
Jerry Yu687101b2021-09-14 16:03:56 +08001868 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001869 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001870 break;
1871
1872 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001873 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001874 break;
1875
1876 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001877 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001878 break;
1879
Ronald Cron49ad6192021-11-24 16:25:31 +01001880 /*
1881 * Injection of dummy-CCS's for middlebox compatibility
1882 */
1883#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1884 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001885 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001886 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001887 break;
1888#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1889
Jerry Yu92c6b402021-08-27 16:59:09 +08001890 default:
1891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1893 }
1894
1895 return( ret );
1896}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001897
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001898#endif /* MBEDTLS_SSL_CLI_C */
1899
Ronald Cron6f135e12021-12-08 16:57:54 +01001900#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */