blob: 332f51980750e8cbe7dd0cf1ad42229146c20a01 [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;
390
Jerry Yuc068b662021-10-11 22:30:19 +0800391 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800392 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800393 if( ret != 0 )
394 {
395 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800396
397 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
398 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
399 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800400 }
401
Jerry Yuc068b662021-10-11 22:30:19 +0800402 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800403 {
Jerry Yuc068b662021-10-11 22:30:19 +0800404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800405
Jerry Yu4a173382021-10-11 21:45:31 +0800406 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
407 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
408 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800409 }
410
411 return( 0 );
412}
Jerry Yu4a173382021-10-11 21:45:31 +0800413#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800414
XiaokangQiand59be772022-01-24 10:12:51 +0000415/*
416 * ssl_tls13_parse_hrr_key_share_ext()
417 * Parse key_share extension in Hello Retry Request
418 *
419 * struct {
420 * NamedGroup selected_group;
421 * } KeyShareHelloRetryRequest;
422 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000423static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000424 const unsigned char *buf,
425 const unsigned char *end )
426{
XiaokangQianb851da82022-01-14 04:03:11 +0000427 const mbedtls_ecp_curve_info *curve_info = NULL;
428 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000429 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000430 int found = 0;
431
432 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
433 if( group_list == NULL )
434 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
435
436 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
437
438 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000439 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000440 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000442
443 /* Upon receipt of this extension in a HelloRetryRequest, the client
444 * MUST first verify that the selected_group field corresponds to a
445 * group which was provided in the "supported_groups" extension in the
446 * original ClientHello.
447 * The supported_group was based on the info in ssl->conf->group_list.
448 *
449 * If the server provided a key share that was not sent in the ClientHello
450 * then the client MUST abort the handshake with an "illegal_parameter" alert.
451 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000452 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000453 {
454 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000455 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000456 continue;
457
458 /* We found a match */
459 found = 1;
460 break;
461 }
462
463 /* Client MUST verify that the selected_group field does not
464 * correspond to a group which was provided in the "key_share"
465 * extension in the original ClientHello. If the server sent an
466 * HRR message with a key share already provided in the
467 * ClientHello then the client MUST abort the handshake with
468 * an "illegal_parameter" alert.
469 */
XiaokangQiand59be772022-01-24 10:12:51 +0000470 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000471 {
472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
473 MBEDTLS_SSL_PEND_FATAL_ALERT(
474 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
475 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
476 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
477 }
478
479 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000480 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000481
482 return( 0 );
483}
484
Jerry Yue1b9c292021-09-10 10:08:31 +0800485/*
Jerry Yub85277e2021-10-13 13:36:05 +0800486 * ssl_tls13_parse_key_share_ext()
487 * Parse key_share extension in Server Hello
488 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800489 * struct {
490 * KeyShareEntry server_share;
491 * } KeyShareServerHello;
492 * struct {
493 * NamedGroup group;
494 * opaque key_exchange<1..2^16-1>;
495 * } KeyShareEntry;
496 */
Jerry Yu4a173382021-10-11 21:45:31 +0800497static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800498 const unsigned char *buf,
499 const unsigned char *end )
500{
Jerry Yub85277e2021-10-13 13:36:05 +0800501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800502 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800503 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800504
Jerry Yu4a173382021-10-11 21:45:31 +0800505 /* ...
506 * NamedGroup group; (2 bytes)
507 * ...
508 */
509 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
510 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800511 p += 2;
512
Jerry Yu4a173382021-10-11 21:45:31 +0800513 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800514 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800515 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800516 {
517 MBEDTLS_SSL_DEBUG_MSG( 1,
518 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800519 (unsigned) offered_group, (unsigned) group ) );
520 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
521 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
522 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800523 }
524
525#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800526 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800527 {
528 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800529 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800530 if( ret != 0 )
531 return( ret );
532 }
Jerry Yub85277e2021-10-13 13:36:05 +0800533 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800534#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800535 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800536 {
537 /* Do something */
538 }
539 else
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
541
542 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
543 return( ret );
544}
545
Jerry Yubc20bdd2021-08-24 15:59:48 +0800546#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
547
XiaokangQiand59be772022-01-24 10:12:51 +0000548/*
549 * ssl_tls13_parse_cookie_ext()
550 * Parse cookie extension in Hello Retry Request
551 *
552 * struct {
553 * opaque cookie<1..2^16-1>;
554 * } Cookie;
555 *
556 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
557 * extension to the client (this is an exception to the usual rule that
558 * the only extensions that may be sent are those that appear in the
559 * ClientHello). When sending the new ClientHello, the client MUST copy
560 * the contents of the extension received in the HelloRetryRequest into
561 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
562 * cookies in their initial ClientHello in subsequent connections.
563 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000564static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
565 const unsigned char *buf,
566 const unsigned char *end )
567{
568 size_t cookie_len;
569 const unsigned char *p = buf;
570 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
571
572 /* Retrieve length field of cookie */
573 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
574 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
575 p += 2;
576
577 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
578 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
579
580 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000581 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000582 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
583 if( handshake->verify_cookie == NULL )
584 {
585 MBEDTLS_SSL_DEBUG_MSG( 1,
586 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
587 cookie_len ) );
588 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
589 }
590
591 memcpy( handshake->verify_cookie, p, cookie_len );
592 handshake->verify_cookie_len = (unsigned char) cookie_len;
593
594 return( 0 );
595}
XiaokangQian43550bd2022-01-21 04:32:58 +0000596
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800597/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800598 * CipherSuite cipher_suites<2..2^16-2>;
599 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800600static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800601 mbedtls_ssl_context *ssl,
602 unsigned char *buf,
603 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000604 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800605{
Jerry Yufec982e2021-09-07 17:26:06 +0800606 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800607 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000608 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800609 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800610
Xiaofei Baid25fab62021-12-02 06:36:27 +0000611 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800612
613 /*
614 * Ciphersuite list
615 *
616 * This is a list of the symmetric cipher options supported by
617 * the client, specifically the record protection algorithm
618 * ( including secret key length ) and a hash to be used with
619 * HKDF, in descending order of client preference.
620 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800621 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800622
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800623 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800624 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
625 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800626
Jerry Yu0c63af62021-09-02 12:59:12 +0800627 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000628 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800629 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800630 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800631 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800632 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800633
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800634 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800635 if( ciphersuite_info == NULL )
636 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800637 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
638 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800639 continue;
640
641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800642 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800643 ciphersuite_info->name ) );
644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800646 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
647 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
648 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800649 }
650
Jerry Yu0c63af62021-09-02 12:59:12 +0800651 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000652 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800653 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800654 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800655 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
656 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800657
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800658 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000659 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800660
Jerry Yu6a643102021-08-31 14:40:36 +0800661 return( 0 );
662}
663
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800664/*
665 * Structure of ClientHello message:
666 *
667 * struct {
668 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
669 * Random random;
670 * opaque legacy_session_id<0..32>;
671 * CipherSuite cipher_suites<2..2^16-2>;
672 * opaque legacy_compression_methods<1..2^8-1>;
673 * Extension extensions<8..2^16-1>;
674 * } ClientHello;
675 */
Jerry Yu08906d02021-08-31 11:05:27 +0800676static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800677 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800678 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000679 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800680{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800681
Jerry Yubc20bdd2021-08-24 15:59:48 +0800682 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000683 unsigned char *p_extensions_len; /* Pointer to extensions length */
684 size_t output_len; /* Length of buffer used by function */
685 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686
Jerry Yubc20bdd2021-08-24 15:59:48 +0800687 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800688 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800689
Xiaofei Baid25fab62021-12-02 06:36:27 +0000690 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800691
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800692 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800693 ssl->major_ver = ssl->conf->min_major_ver;
694 ssl->minor_ver = ssl->conf->min_minor_ver;
695
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800696 /*
697 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800698 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800699 *
700 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800702 */
Jerry Yufec982e2021-09-07 17:26:06 +0800703 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800704 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800705 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800706
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800707 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800708 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
709 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800710 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800711 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
712 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800713
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800714 /*
715 * Write legacy_session_id
716 *
717 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
718 * which has been merged with pre-shared keys in this version. A client
719 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
720 * this field to that value. In compatibility mode, this field MUST be
721 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
722 * a new 32-byte value. This value need not be random but SHOULD be
723 * unpredictable to avoid implementations fixating on a specific value
724 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
725 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800726 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100727#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
728 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
729 *p++ = (unsigned char)ssl->session_negotiate->id_len;
730 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
731 p += ssl->session_negotiate->id_len;
732
733 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
734 ssl->session_negotiate->id_len );
735#else
Jerry Yubbe09522021-09-06 21:17:54 +0800736 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
737 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100738#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800739
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800740 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800741 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800742 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800743 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800744 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800745
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800746 /* Write legacy_compression_methods
747 *
748 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800749 * one byte set to zero, which corresponds to the 'null' compression
750 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800751 */
Jerry Yubbe09522021-09-06 21:17:54 +0800752 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
753 *p++ = 1;
754 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800755
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800756 /* Write extensions */
757
758 /* Keeping track of the included extensions */
759 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800760
761 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800762 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000763 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800764 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800765
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800766 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800767 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800768 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769 */
Jerry Yubbe09522021-09-06 21:17:54 +0800770 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800771 if( ret != 0 )
772 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800773 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800774
775#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800776
Jerry Yub925f212022-01-12 11:17:02 +0800777 /*
778 * Add the extensions related to (EC)DHE ephemeral key establishment only if
779 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800780 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800781 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
782 {
783 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
784 if( ret != 0 )
785 return( ret );
786 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800787
Jerry Yuf46b0162022-01-11 16:28:00 +0800788 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
789 if( ret != 0 )
790 return( ret );
791 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800792
Jerry Yuf017ee42022-01-12 15:49:48 +0800793 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800794 if( ret != 0 )
795 return( ret );
796 p += output_len;
797 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800798#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
799
Xiaofei Bai15a56812021-11-05 10:52:12 +0000800#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000801 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000802 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
803 if( ret != 0 )
804 return( ret );
805 p += output_len;
806#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
807
Jerry Yubc20bdd2021-08-24 15:59:48 +0800808 /* Add more extensions here */
809
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800810 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000811 extensions_len = p - p_extensions_len - 2;
812 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800814 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000815 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800816
Xiaofei Baid25fab62021-12-02 06:36:27 +0000817 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800818 return( 0 );
819}
820
Jerry Yu335aca92021-09-12 20:18:56 +0800821static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800822{
Jerry Yu92c6b402021-08-27 16:59:09 +0800823 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
824 return( 0 );
825}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800826
Jerry Yu92c6b402021-08-27 16:59:09 +0800827static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
828{
829 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800830
Jerry Yu92c6b402021-08-27 16:59:09 +0800831 if( ssl->conf->f_rng == NULL )
832 {
833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
834 return( MBEDTLS_ERR_SSL_NO_RNG );
835 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800836
Jerry Yu92c6b402021-08-27 16:59:09 +0800837 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
838 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800839 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800840 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800841 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800842 return( ret );
843 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800844
Ronald Cron49ad6192021-11-24 16:25:31 +0100845#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
846 /*
847 * Create a session identifier for the purpose of middlebox compatibility
848 * only if one has not been created already.
849 */
850 if( ssl->session_negotiate->id_len == 0 )
851 {
852 /* Creating a session id with 32 byte length */
853 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
854 ssl->session_negotiate->id, 32 ) ) != 0 )
855 {
856 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
857 return( ret );
858 }
859 ssl->session_negotiate->id_len = 32;
860 }
861#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
862
Jerry Yu6f13f642021-08-26 17:18:15 +0800863 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800864}
865
Jerry Yu92c6b402021-08-27 16:59:09 +0800866/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800867 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800868 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800869 */
870static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800871{
Jerry Yu92c6b402021-08-27 16:59:09 +0800872 int ret = 0;
873 unsigned char *buf;
874 size_t buf_len, msg_len;
875
876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
877
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800878 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800879
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800880 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
881 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
882 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800883
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800884 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800885 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800886 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800887
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800888 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
889 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800890 msg_len );
891 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800892
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800893 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
894 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
895 buf_len,
896 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800897
898cleanup:
899
900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
901 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800902}
903
Jerry Yu687101b2021-09-14 16:03:56 +0800904/*
Jerry Yu4a173382021-10-11 21:45:31 +0800905 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800907/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800908 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
909 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000910 * to indicate which message is expected and to be parsed next.
911 */
Jerry Yub85277e2021-10-13 13:36:05 +0800912#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
913#define SSL_SERVER_HELLO_COORDINATE_HRR 1
914static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
915 const unsigned char *buf,
916 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800917{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800918 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800919 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
920 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
921 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
922 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
923
924 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
925 *
Jerry Yu4a173382021-10-11 21:45:31 +0800926 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800927 * special value of the SHA-256 of "HelloRetryRequest".
928 *
929 * struct {
930 * ProtocolVersion legacy_version = 0x0303;
931 * Random random;
932 * opaque legacy_session_id_echo<0..32>;
933 * CipherSuite cipher_suite;
934 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800935 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800936 * } ServerHello;
937 *
938 */
Jerry Yub85277e2021-10-13 13:36:05 +0800939 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800940
941 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800942 {
Jerry Yub85277e2021-10-13 13:36:05 +0800943 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800944 }
945
Jerry Yub85277e2021-10-13 13:36:05 +0800946 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800947}
948
Jerry Yu745bb612021-10-13 22:01:04 +0800949/* Fetch and preprocess
950 * Returns a negative value on failure, and otherwise
951 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
952 * - SSL_SERVER_HELLO_COORDINATE_HRR
953 */
Jerry Yub85277e2021-10-13 13:36:05 +0800954static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
955 unsigned char **buf,
956 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800957{
Jerry Yu4a173382021-10-11 21:45:31 +0800958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800959
XiaokangQian355e09a2022-01-20 11:14:50 +0000960 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
961 MBEDTLS_SSL_HS_SERVER_HELLO,
962 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800963
Jerry Yub85277e2021-10-13 13:36:05 +0800964 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
965 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800966 {
Jerry Yu745bb612021-10-13 22:01:04 +0800967 case SSL_SERVER_HELLO_COORDINATE_HELLO:
968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
969 break;
970 case SSL_SERVER_HELLO_COORDINATE_HRR:
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000972 /* If a client receives a second
973 * HelloRetryRequest in the same connection (i.e., where the ClientHello
974 * was itself in response to a HelloRetryRequest), it MUST abort the
975 * handshake with an "unexpected_message" alert.
976 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000977 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000978 {
979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
980 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
981 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
982 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
983 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000984 /*
985 * Clients must abort the handshake with an "illegal_parameter"
986 * alert if the HelloRetryRequest would not result in any change
987 * in the ClientHello.
988 * In a PSK only key exchange that what we expect.
989 */
990 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
991 {
992 MBEDTLS_SSL_DEBUG_MSG( 1,
993 ( "Unexpected HRR in pure PSK key exchange." ) );
994 MBEDTLS_SSL_PEND_FATAL_ALERT(
995 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
996 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
997 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
998 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000999
XiaokangQiand9e068e2022-01-18 06:23:32 +00001000 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001001
Jerry Yu745bb612021-10-13 22:01:04 +08001002 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003 }
1004
1005cleanup:
1006
1007 return( ret );
1008}
1009
Jerry Yu4a173382021-10-11 21:45:31 +08001010static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1011 const unsigned char **buf,
1012 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001013{
1014 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001015 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001016
Jerry Yude4fb2c2021-09-19 18:05:08 +08001017 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001018 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001019
Jerry Yu4a173382021-10-11 21:45:31 +08001020 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001021
1022 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001023 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1024 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001025 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1027 ssl->session_negotiate->id,
1028 ssl->session_negotiate->id_len );
1029 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001030 legacy_session_id_echo_len );
1031
1032 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1033 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1034
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1036 }
1037
Jerry Yu4a173382021-10-11 21:45:31 +08001038 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001039 *buf = p;
1040
1041 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001042 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 return( 0 );
1044}
1045
Jerry Yu4a173382021-10-11 21:45:31 +08001046static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1047 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001048{
Jerry Yu4a173382021-10-11 21:45:31 +08001049 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1050
Jerry Yue1b9c292021-09-10 10:08:31 +08001051 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001052 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001053 {
Jerry Yu4a173382021-10-11 21:45:31 +08001054 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 {
1056 return( 1 );
1057 }
1058 }
1059 return( 0 );
1060}
1061
1062/* Parse ServerHello message and configure context
1063 *
1064 * struct {
1065 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1066 * Random random;
1067 * opaque legacy_session_id_echo<0..32>;
1068 * CipherSuite cipher_suite;
1069 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001070 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 * } ServerHello;
1072 */
Jerry Yuc068b662021-10-11 22:30:19 +08001073static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1074 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001075 const unsigned char *end,
1076 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001077{
Jerry Yub85277e2021-10-13 13:36:05 +08001078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001080 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001081 size_t extensions_len;
1082 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001083 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001084 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001085 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001086 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001087
1088 /*
1089 * Check there is space for minimal fields
1090 *
1091 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001092 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001093 * - legacy_session_id_echo ( 1 byte ), minimum size
1094 * - cipher_suite ( 2 bytes)
1095 * - legacy_compression_method ( 1 byte )
1096 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001097 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098
1099 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001100 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1101
Jerry Yu4a173382021-10-11 21:45:31 +08001102 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001103 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001104 * ...
1105 * with ProtocolVersion defined as:
1106 * uint16 ProtocolVersion;
1107 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001108 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1109 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1110 {
1111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1112 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1113 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001114 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1115 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001116 }
1117 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001119 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001120 * Random random;
1121 * ...
1122 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001123 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001124 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001125 if( !is_hrr )
1126 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001127 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001128 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1129 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1130 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1131 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001132 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001133
Jerry Yu4a173382021-10-11 21:45:31 +08001134 /* ...
1135 * opaque legacy_session_id_echo<0..32>;
1136 * ...
1137 */
1138 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001139 {
XiaokangQian52da5582022-01-26 09:49:29 +00001140 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001141 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001142 }
1143
Jerry Yu4a173382021-10-11 21:45:31 +08001144 /* ...
1145 * CipherSuite cipher_suite;
1146 * ...
1147 * with CipherSuite defined as:
1148 * uint8 CipherSuite[2];
1149 */
1150 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1152 p += 2;
1153
Jerry Yu4a173382021-10-11 21:45:31 +08001154
XiaokangQian355e09a2022-01-20 11:14:50 +00001155 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001156 /*
1157 * Check whether this ciphersuite is supported and offered.
1158 * Via the force_ciphersuite version we may have instructed the client
1159 * to use a different ciphersuite.
1160 */
Jerry Yu4a173382021-10-11 21:45:31 +08001161 if( ciphersuite_info == NULL ||
1162 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001163 {
XiaokangQian52da5582022-01-26 09:49:29 +00001164 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001166 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001167 * If we received an HRR before and that the proposed selected
1168 * ciphersuite in this server hello is not the same as the one
1169 * proposed in the HRR, we abort the handshake and send an
1170 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001171 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001172 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1173 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001174 {
XiaokangQian52da5582022-01-26 09:49:29 +00001175 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001176 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001177
XiaokangQian52da5582022-01-26 09:49:29 +00001178 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001179 {
1180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1181 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001182 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001183 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001184
Jerry Yu4a173382021-10-11 21:45:31 +08001185 /* Configure ciphersuites */
1186 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1187
XiaokangQian355e09a2022-01-20 11:14:50 +00001188 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 ssl->session_negotiate->ciphersuite = cipher_suite;
1190
Jerry Yue1b9c292021-09-10 10:08:31 +08001191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1192 cipher_suite, ciphersuite_info->name ) );
1193
1194#if defined(MBEDTLS_HAVE_TIME)
1195 ssl->session_negotiate->start = time( NULL );
1196#endif /* MBEDTLS_HAVE_TIME */
1197
Jerry Yu4a173382021-10-11 21:45:31 +08001198 /* ...
1199 * uint8 legacy_compression_method = 0;
1200 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001201 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001202 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001203 if( p[0] != 0 )
1204 {
Jerry Yub85277e2021-10-13 13:36:05 +08001205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001206 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001207 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001208 }
1209 p++;
1210
Jerry Yub85277e2021-10-13 13:36:05 +08001211 /* ...
1212 * Extension extensions<6..2^16-1>;
1213 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001214 * struct {
1215 * ExtensionType extension_type; (2 bytes)
1216 * opaque extension_data<0..2^16-1>;
1217 * } Extension;
1218 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001219 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001220 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001222
Jerry Yu4a173382021-10-11 21:45:31 +08001223 /* Check extensions do not go beyond the buffer of data. */
1224 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1225 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001226
Jerry Yu4a173382021-10-11 21:45:31 +08001227 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001228
Jerry Yu4a173382021-10-11 21:45:31 +08001229 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001230 {
1231 unsigned int extension_type;
1232 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001233 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001234
Jerry Yu4a173382021-10-11 21:45:31 +08001235 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001236 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1237 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1238 p += 4;
1239
Jerry Yu4a173382021-10-11 21:45:31 +08001240 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001241 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001242
1243 switch( extension_type )
1244 {
XiaokangQianb851da82022-01-14 04:03:11 +00001245 case MBEDTLS_TLS_EXT_COOKIE:
1246
XiaokangQiand9e068e2022-01-18 06:23:32 +00001247 if( !is_hrr )
1248 {
XiaokangQian52da5582022-01-26 09:49:29 +00001249 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001250 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001251 }
1252
XiaokangQian43550bd2022-01-21 04:32:58 +00001253 ret = ssl_tls13_parse_cookie_ext( ssl,
1254 p, extension_data_end );
1255 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001256 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001257 MBEDTLS_SSL_DEBUG_RET( 1,
1258 "ssl_tls13_parse_cookie_ext",
1259 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001260 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001261 }
XiaokangQianb851da82022-01-14 04:03:11 +00001262 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001263
Jerry Yue1b9c292021-09-10 10:08:31 +08001264 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001265 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001266 MBEDTLS_SSL_DEBUG_MSG( 3,
1267 ( "found supported_versions extension" ) );
1268
Jerry Yuc068b662021-10-11 22:30:19 +08001269 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001270 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001271 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001272 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001273 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001274 break;
1275
1276 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001279
XiaokangQian52da5582022-01-26 09:49:29 +00001280 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001281 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001282
1283#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1284 case MBEDTLS_TLS_EXT_KEY_SHARE:
1285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001286 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1287 {
XiaokangQian52da5582022-01-26 09:49:29 +00001288 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001289 goto cleanup;
1290 }
1291
XiaokangQian53f20b72022-01-18 10:47:33 +00001292 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001293 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001294 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001295 else
XiaokangQianb851da82022-01-14 04:03:11 +00001296 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001297 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001298 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001299 {
1300 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001301 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001302 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001303 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001304 }
1305 break;
1306#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1307
1308 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001309 MBEDTLS_SSL_DEBUG_MSG(
1310 3,
1311 ( "unknown extension found: %u ( ignoring )",
1312 extension_type ) );
1313
XiaokangQian52da5582022-01-26 09:49:29 +00001314 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001315 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001316 }
1317
1318 p += extension_data_len;
1319 }
1320
XiaokangQian78b1fa72022-01-19 06:56:30 +00001321 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001322 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001324 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001325 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001326 }
1327
XiaokangQiand59be772022-01-24 10:12:51 +00001328cleanup:
1329
XiaokangQian52da5582022-01-26 09:49:29 +00001330 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001331 {
1332 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1333 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1334 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1335 }
XiaokangQian52da5582022-01-26 09:49:29 +00001336 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001337 {
1338 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1339 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1340 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1341 }
1342 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001343}
1344
XiaokangQian355e09a2022-01-20 11:14:50 +00001345static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001346{
Jerry Yub85277e2021-10-13 13:36:05 +08001347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001348 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001349 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001350 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001351
Jerry Yub85277e2021-10-13 13:36:05 +08001352 /* Determine the key exchange mode:
1353 * 1) If both the pre_shared_key and key_share extensions were received
1354 * then the key exchange mode is PSK with EPHEMERAL.
1355 * 2) If only the pre_shared_key extension was received then the key
1356 * exchange mode is PSK-only.
1357 * 3) If only the key_share extension was received then the key
1358 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001359 */
Jerry Yub85277e2021-10-13 13:36:05 +08001360 switch( handshake->extensions_present &
1361 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001362 {
Jerry Yu745bb612021-10-13 22:01:04 +08001363 /* Only the pre_shared_key extension was received */
1364 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001365 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001366 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001367
Jerry Yu745bb612021-10-13 22:01:04 +08001368 /* Only the key_share extension was received */
1369 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001370 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001371 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001372
Jerry Yu745bb612021-10-13 22:01:04 +08001373 /* Both the pre_shared_key and key_share extensions were received */
1374 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001375 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001376 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001377
Jerry Yu745bb612021-10-13 22:01:04 +08001378 /* Neither pre_shared_key nor key_share extension was received */
1379 default:
1380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1381 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1382 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001383 }
1384
1385 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1386 *
1387 * TODO: We don't have to do this in case we offered 0-RTT and the
1388 * server accepted it. In this case, we could skip generating
1389 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001390 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001391 if( ret != 0 )
1392 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001394 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001395 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001396 }
1397
1398 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001399 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001400 if( ret != 0 )
1401 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001403 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001404 }
1405
1406 /* Next evolution in key schedule: Establish handshake secret and
1407 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001408 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001409 if( ret != 0 )
1410 {
Jerry Yuc068b662021-10-11 22:30:19 +08001411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1412 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001413 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001414 }
1415
Jerry Yub85277e2021-10-13 13:36:05 +08001416 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001417 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001418 {
1419 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1420 goto cleanup;
1421 }
Jerry Yu0b177842021-09-05 19:41:30 +08001422
1423 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1424 ssl->conf->endpoint,
1425 ssl->session_negotiate->ciphersuite,
1426 &traffic_keys,
1427 ssl );
1428 if( ret != 0 )
1429 {
1430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001431 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001432 }
1433
Jerry Yub85277e2021-10-13 13:36:05 +08001434 handshake->transform_handshake = transform_handshake;
1435 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001436
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1438 ssl->session_in = ssl->session_negotiate;
1439
1440 /*
1441 * State machine update
1442 */
1443 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1444
Jerry Yu4a173382021-10-11 21:45:31 +08001445cleanup:
1446
Jerry Yu0b177842021-09-05 19:41:30 +08001447 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001448 if( ret != 0 )
1449 {
Jerry Yub85277e2021-10-13 13:36:05 +08001450 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001451
Jerry Yu4a173382021-10-11 21:45:31 +08001452 MBEDTLS_SSL_PEND_FATAL_ALERT(
1453 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1454 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1455 }
1456 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001457}
1458
XiaokangQian355e09a2022-01-20 11:14:50 +00001459static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001460{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001461#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1463
XiaokangQian647719a2021-12-07 09:16:29 +00001464#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1465 /* If not offering early data, the client sends a dummy CCS record
1466 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001467 * its second ClientHello or before its encrypted handshake flight.
1468 */
XiaokangQian647719a2021-12-07 09:16:29 +00001469 mbedtls_ssl_handshake_set_state( ssl,
1470 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1471#else
1472 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1473#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1474
XiaokangQian78b1fa72022-01-19 06:56:30 +00001475 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001476
XiaokangQian78b1fa72022-01-19 06:56:30 +00001477 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001478 * We are going to re-generate a shared secret corresponding to the group
1479 * selected by the server, which is different from the group for which we
1480 * generated a shared secret in the first client hello.
1481 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001482 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001483 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001484 if( ret != 0 )
1485 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001486#else
1487 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001488#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001489
1490 return( 0 );
1491}
1492
Jerry Yue1b9c292021-09-10 10:08:31 +08001493/*
Jerry Yu4a173382021-10-11 21:45:31 +08001494 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001495 * Handler for MBEDTLS_SSL_SERVER_HELLO
1496 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001497static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001498{
Jerry Yu4a173382021-10-11 21:45:31 +08001499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001500 unsigned char *buf = NULL;
1501 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001502 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001503
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1505
1506 /* Coordination step
1507 * - Fetch record
1508 * - Make sure it's either a ServerHello or a HRR.
1509 * - Switch processing routine in case of HRR
1510 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001511 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1512 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1513
XiaokangQian16acd4b2022-01-14 07:35:47 +00001514 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001515 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001516 goto cleanup;
1517 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001518 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001519
XiaokangQianb851da82022-01-14 04:03:11 +00001520 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001521 buf + buf_len,
1522 is_hrr ) );
1523 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001524 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1525
XiaokangQianb851da82022-01-14 04:03:11 +00001526 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1527 MBEDTLS_SSL_HS_SERVER_HELLO,
1528 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001529
XiaokangQiand9e068e2022-01-18 06:23:32 +00001530 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001531 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001532 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001533 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001534
1535cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1537 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001538 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001539}
1540
1541/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542 *
1543 * EncryptedExtensions message
1544 *
1545 * The EncryptedExtensions message contains any extensions which
1546 * should be protected, i.e., any which are not needed to establish
1547 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001548 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549
1550/*
1551 * Overview
1552 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001553
1554/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001555static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001556
XiaokangQian97799ac2021-10-11 10:05:54 +00001557static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1558 const unsigned char *buf,
1559 const unsigned char *end );
1560static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001561
1562/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001563 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001564 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001565static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001566{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001567 int ret;
1568 unsigned char *buf;
1569 size_t buf_len;
1570
1571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1572
Xiaofei Bai746f9482021-11-12 08:53:56 +00001573 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001574 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001575 &buf, &buf_len ) );
1576
1577 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001578 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001579 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580
Xiaofei Bai746f9482021-11-12 08:53:56 +00001581 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001582 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001583
XiaokangQian97799ac2021-10-11 10:05:54 +00001584 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001585
1586cleanup:
1587
1588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1589 return( ret );
1590
1591}
1592
XiaokangQian08da26c2021-10-09 10:12:11 +00001593/* Parse EncryptedExtensions message
1594 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001595 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001596 * } EncryptedExtensions;
1597 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001598static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1599 const unsigned char *buf,
1600 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001601{
1602 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001603 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001604 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001605 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001606
XiaokangQian08da26c2021-10-09 10:12:11 +00001607 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001608 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001609 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001610
XiaokangQian97799ac2021-10-11 10:05:54 +00001611 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001612 extensions_end = p + extensions_len;
1613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001614
XiaokangQian8db25ff2021-10-13 05:56:18 +00001615 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001616 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001617 unsigned int extension_type;
1618 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001619
XiaokangQian08da26c2021-10-09 10:12:11 +00001620 /*
1621 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001622 * ExtensionType extension_type; (2 bytes)
1623 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001624 * } Extension;
1625 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001626 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001627 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1628 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1629 p += 4;
1630
XiaokangQian8db25ff2021-10-13 05:56:18 +00001631 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001632
XiaokangQian97799ac2021-10-11 10:05:54 +00001633 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001634 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001635 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001636 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001637 switch( extension_type )
1638 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001639
XiaokangQian08da26c2021-10-09 10:12:11 +00001640 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1642 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001643
XiaokangQian08da26c2021-10-09 10:12:11 +00001644 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001645 MBEDTLS_SSL_DEBUG_MSG(
1646 3, ( "unsupported extension found: %u ", extension_type) );
1647 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001648 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001649 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1650 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001651 }
1652
XiaokangQian08da26c2021-10-09 10:12:11 +00001653 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001654 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001655
XiaokangQian97799ac2021-10-11 10:05:54 +00001656 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001657 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001658 {
1659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001660 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001661 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001662 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001663 }
1664
1665 return( ret );
1666}
1667
XiaokangQian97799ac2021-10-11 10:05:54 +00001668static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001669{
Jerry Yua93ac112021-10-27 16:31:48 +08001670#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001671 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001672 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1673 else
Jerry Yud2674312021-10-29 10:08:19 +08001674 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001675#else
1676 ((void) ssl);
1677 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1678#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001679 return( 0 );
1680}
1681
Jerry Yua93ac112021-10-27 16:31:48 +08001682#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001683/*
Jerry Yud2674312021-10-29 10:08:19 +08001684 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1685 */
1686static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1687{
1688 int ret = mbedtls_ssl_read_record( ssl, 0 );
1689
1690 if( ret != 0 )
1691 {
1692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1693 return( ret );
1694 }
1695
1696 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1697 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1698 {
1699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1700 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1701 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1702 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1703 }
1704
1705 ssl->keep_current_message = 1;
1706 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1707
1708 return( 0 );
1709}
1710
1711/*
Jerry Yu687101b2021-09-14 16:03:56 +08001712 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1713 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001714static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001715{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001716 int ret;
1717
1718 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001719 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001720 return( ret );
1721
Jerry Yu687101b2021-09-14 16:03:56 +08001722 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1723 return( 0 );
1724}
1725
1726/*
1727 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1728 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001729static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001730{
Jerry Yu30b071c2021-09-12 20:16:03 +08001731 int ret;
1732
1733 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1734 if( ret != 0 )
1735 return( ret );
1736
Jerry Yu687101b2021-09-14 16:03:56 +08001737 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1738 return( 0 );
1739}
Jerry Yua93ac112021-10-27 16:31:48 +08001740#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001741
Jerry Yu687101b2021-09-14 16:03:56 +08001742/*
1743 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1744 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001745static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001746{
XiaokangQianac0385c2021-11-03 06:40:11 +00001747 int ret;
1748
XiaokangQianc5c39d52021-11-09 11:55:10 +00001749 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001750 if( ret != 0 )
1751 return( ret );
1752
Ronald Cron49ad6192021-11-24 16:25:31 +01001753#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1754 mbedtls_ssl_handshake_set_state(
1755 ssl,
1756 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1757#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001758 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001759#endif
1760
XiaokangQianac0385c2021-11-03 06:40:11 +00001761 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001762}
1763
1764/*
Ronald Crond4c64022021-12-06 09:06:46 +01001765 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1766 */
1767#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1768static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1769{
1770 int ret;
1771
1772 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1773 if( ret != 0 )
1774 return( ret );
1775
Ronald Crond4c64022021-12-06 09:06:46 +01001776 return( 0 );
1777}
1778#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1779
1780/*
Jerry Yu687101b2021-09-14 16:03:56 +08001781 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1782 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001783static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001784{
XiaokangQian0fa66432021-11-15 03:33:57 +00001785 int ret;
1786
Ronald Cron49ad6192021-11-24 16:25:31 +01001787 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1788
XiaokangQian0fa66432021-11-15 03:33:57 +00001789 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1790 if( ret != 0 )
1791 return( ret );
1792
1793 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1794 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001795}
1796
1797/*
1798 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1799 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001800static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001801{
Jerry Yu378254d2021-10-30 21:44:47 +08001802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001803 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001804 return( 0 );
1805}
1806
1807/*
1808 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1809 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001810static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001811{
Jerry Yu378254d2021-10-30 21:44:47 +08001812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1813 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1814
1815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1816 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1817
1818 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1819
1820 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1821 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001822}
1823
Jerry Yu92c6b402021-08-27 16:59:09 +08001824int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001825{
Jerry Yu92c6b402021-08-27 16:59:09 +08001826 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001827
Jerry Yue3b34122021-09-28 17:53:35 +08001828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1829 mbedtls_ssl_states_str( ssl->state ),
1830 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001831
1832 switch( ssl->state )
1833 {
1834 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001835 * ssl->state is initialized as HELLO_REQUEST. It is the same
1836 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001837 */
1838 case MBEDTLS_SSL_HELLO_REQUEST:
1839 case MBEDTLS_SSL_CLIENT_HELLO:
1840 ret = ssl_tls13_write_client_hello( ssl );
1841 break;
1842
1843 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001844 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001845 break;
1846
1847 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001848 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001849 break;
1850
Jerry Yua93ac112021-10-27 16:31:48 +08001851#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001852 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1853 ret = ssl_tls13_process_certificate_request( ssl );
1854 break;
1855
Jerry Yu687101b2021-09-14 16:03:56 +08001856 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001857 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001858 break;
1859
1860 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001861 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001862 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001863#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001864
1865 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001866 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001867 break;
1868
Jerry Yu687101b2021-09-14 16:03:56 +08001869 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001870 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001871 break;
1872
1873 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001874 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001875 break;
1876
1877 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001878 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001879 break;
1880
Ronald Cron49ad6192021-11-24 16:25:31 +01001881 /*
1882 * Injection of dummy-CCS's for middlebox compatibility
1883 */
1884#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1885 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001886 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01001887 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001888 break;
1889#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1890
Jerry Yu92c6b402021-08-27 16:59:09 +08001891 default:
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1894 }
1895
1896 return( ret );
1897}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001898
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001899#endif /* MBEDTLS_SSL_CLI_C */
1900
Ronald Cron6f135e12021-12-08 16:57:54 +01001901#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */