blob: d7748b78ae5801b8a39dabd54598d0a9a1f52044 [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
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
Ronald Crondbe87f02022-02-10 14:35:27 +010052 unsigned char versions_len = ( ssl->handshake->min_minor_ver <=
53 MBEDTLS_SSL_MINOR_VERSION_3 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
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 )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Ronald Crondbe87f02022-02-10 14:35:27 +010078 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
79 MBEDTLS_SSL_MINOR_VERSION_4,
80 MBEDTLS_SSL_TRANSPORT_STREAM, p );
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Jerry Yu92c6b402021-08-27 16:59:09 +080083
Ronald Crondbe87f02022-02-10 14:35:27 +010084 if( ssl->handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_3 )
85 {
86 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
87 MBEDTLS_SSL_MINOR_VERSION_3,
88 MBEDTLS_SSL_TRANSPORT_STREAM, p + 2 );
89 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
90 }
91
92 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080093
94 return( 0 );
95}
Jerry Yubc20bdd2021-08-24 15:59:48 +080096
Jerry Yuc068b662021-10-11 22:30:19 +080097static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
98 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080099 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800100{
Jerry Yue1b9c292021-09-10 10:08:31 +0800101 ((void) ssl);
102
Ronald Cron98473382022-03-30 20:04:10 +0200103 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Jerry Yub85277e2021-10-13 13:36:05 +0800104 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
106 {
107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800108
109 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
111 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800112 }
113
Ronald Cron98473382022-03-30 20:04:10 +0200114 if( &buf[2] != end )
115 {
116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
117 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
118 MBEDTLS_ERR_SSL_DECODE_ERROR );
119 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
120 }
121
Jerry Yue1b9c292021-09-10 10:08:31 +0800122 return( 0 );
123}
124
lhuang0486cacac2022-01-21 07:34:27 -0800125#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
127 const unsigned char *buf, size_t len )
128{
129 size_t list_len, name_len;
130 const unsigned char *p = buf;
131 const unsigned char *end = buf + len;
132
133 /* If we didn't send it, the server shouldn't send it */
134 if( ssl->conf->alpn_list == NULL )
135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
136
137 /*
138 * opaque ProtocolName<1..2^8-1>;
139 *
140 * struct {
141 * ProtocolName protocol_name_list<2..2^16-1>
142 * } ProtocolNameList;
143 *
144 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
145 */
146
147 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
149
150 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
151 p += 2;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
153
154 name_len = *p++;
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
156
157 /* Check that the server chosen protocol was in our list and save it */
158 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
159 {
160 if( name_len == strlen( *alpn ) &&
161 memcmp( buf + 3, *alpn, name_len ) == 0 )
162 {
163 ssl->alpn_chosen = *alpn;
164 return( 0 );
165 }
166 }
167
168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
169}
170#endif /* MBEDTLS_SSL_ALPN */
171
XiaokangQian16acd4b2022-01-14 07:35:47 +0000172static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000173{
174 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100175
XiaokangQian647719a2021-12-07 09:16:29 +0000176 if( group_id == 0 )
177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
178
XiaokangQian355e09a2022-01-20 11:14:50 +0000179#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000180 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000181 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
184
185 /* Destroy generated private key. */
186 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
187 if( status != PSA_SUCCESS )
188 {
189 ret = psa_ssl_status_to_mbedtls( status );
190 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
191 return( ret );
192 }
193
194 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000195 return( 0 );
196 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000197 else
198#endif /* MBEDTLS_ECDH_C */
199 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000200 {
201 /* Do something */
202 }
203
204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
205}
206
207/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208 * Functions for writing key_share extension.
209 */
210#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800211static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800212 mbedtls_ssl_context *ssl,
213 uint16_t named_group,
214 unsigned char *buf,
215 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000216 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800217{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100218 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
219 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
220 psa_key_attributes_t key_attributes;
221 size_t own_pubkey_len;
222 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
223 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800226
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100227 /* Convert EC group to PSA key type. */
228 if( ( handshake->ecdh_psa_type =
229 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
230 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231
Neil Armstrong91477a72022-03-25 15:42:20 +0100232 ssl->handshake->ecdh_bits = ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100234 key_attributes = psa_key_attributes_init();
235 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
236 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
237 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
238 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100239
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100240 /* Generate ECDH private key. */
241 status = psa_generate_key( &key_attributes,
242 &handshake->ecdh_psa_privkey );
243 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800244 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100245 ret = psa_ssl_status_to_mbedtls( status );
246 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100248
Jerry Yu56fc07f2021-09-01 17:48:49 +0800249 }
250
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100251 /* Export the public part of the ECDH private key from PSA. */
252 status = psa_export_public_key( handshake->ecdh_psa_privkey,
253 buf, (size_t)( end - buf ),
254 &own_pubkey_len );
255 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800256 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100257 ret = psa_ssl_status_to_mbedtls( status );
258 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800259 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100260
Jerry Yu56fc07f2021-09-01 17:48:49 +0800261 }
262
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100263 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100264
Jerry Yu75336352021-09-01 15:59:36 +0800265 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800266}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267#endif /* MBEDTLS_ECDH_C */
268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
270 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271{
272 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
273
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100276 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100278 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
280
Brett Warren14efd332021-10-06 09:32:11 +0100281 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800282 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000283 const mbedtls_ecp_curve_info *curve_info;
284 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
285 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100286 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 {
Brett Warren14efd332021-10-06 09:32:11 +0100288 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 return( 0 );
290 }
291 }
292#else
293 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800294 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295#endif /* MBEDTLS_ECDH_C */
296
297 /*
298 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800299 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300 */
301
302 return( ret );
303}
304
305/*
306 * ssl_tls13_write_key_share_ext
307 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800308 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309 *
310 * struct {
311 * NamedGroup group;
312 * opaque key_exchange<1..2^16-1>;
313 * } KeyShareEntry;
314 * struct {
315 * KeyShareEntry client_shares<0..2^16-1>;
316 * } KeyShareClientHello;
317 */
318static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000321 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322{
323 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000324 unsigned char *client_shares; /* Start of client_shares */
325 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800326 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
328
Xiaofei Baid25fab62021-12-02 06:36:27 +0000329 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800332 * - extension_type (2 bytes)
333 * - extension_data_length (2 bytes)
334 * - client_shares_length (2 bytes)
335 */
336 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
337 p += 6;
338
339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
340
341 /* HRR could already have requested something else. */
342 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
344 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800346 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 &group_id ) );
348 }
349
350 /*
351 * Dispatch to type-specific key generation function.
352 *
353 * So far, we're only supporting ECDHE. With the introduction
354 * of PQC KEMs, we'll want to have multiple branches, one per
355 * type of KEM, and dispatch to the corresponding crypto. And
356 * only one key share entry is allowed.
357 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000358 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800360 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800362 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000363 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800364 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100365 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800366
367 /* Check there is space for header of KeyShareEntry
368 * - group (2 bytes)
369 * - key_exchange_length (2 bytes)
370 */
371 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
372 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100373 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800374 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800375 p += key_exchange_len;
376 if( ret != 0 )
377 return( ret );
378
379 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000380 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000382 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800383 }
384 else
385#endif /* MBEDTLS_ECDH_C */
386 if( 0 /* other KEMs? */ )
387 {
388 /* Do something */
389 }
390 else
391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
392
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000394 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800395 if( client_shares_len == 0)
396 {
397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800399 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800400 /* Write extension_type */
401 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
402 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800404 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800405 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800406
407 /* Update offered_group_id field */
408 ssl->handshake->offered_group_id = group_id;
409
410 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000411 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412
Xiaofei Baid25fab62021-12-02 06:36:27 +0000413 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414
415 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
416
417cleanup:
418
419 return( ret );
420}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800421
Jerry Yue1b9c292021-09-10 10:08:31 +0800422#if defined(MBEDTLS_ECDH_C)
423
Jerry Yuc068b662021-10-11 22:30:19 +0800424static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
425 const unsigned char *buf,
426 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800427{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100428 uint8_t *p = (uint8_t*)buf;
429 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800430
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100431 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
432 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
433 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800434
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100435 /* Check if key size is consistent with given buffer length. */
436 if ( peerkey_len > ( buf_len - 2 ) )
437 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800438
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100439 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100440 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100441 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800442
443 return( 0 );
444}
Jerry Yu4a173382021-10-11 21:45:31 +0800445#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800446
XiaokangQiand59be772022-01-24 10:12:51 +0000447/*
448 * ssl_tls13_parse_hrr_key_share_ext()
449 * Parse key_share extension in Hello Retry Request
450 *
451 * struct {
452 * NamedGroup selected_group;
453 * } KeyShareHelloRetryRequest;
454 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000455static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000456 const unsigned char *buf,
457 const unsigned char *end )
458{
XiaokangQianb851da82022-01-14 04:03:11 +0000459 const mbedtls_ecp_curve_info *curve_info = NULL;
460 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000461 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000462 int found = 0;
463
464 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
465 if( group_list == NULL )
466 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
467
468 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
469
470 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000472 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000474
475 /* Upon receipt of this extension in a HelloRetryRequest, the client
476 * MUST first verify that the selected_group field corresponds to a
477 * group which was provided in the "supported_groups" extension in the
478 * original ClientHello.
479 * The supported_group was based on the info in ssl->conf->group_list.
480 *
481 * If the server provided a key share that was not sent in the ClientHello
482 * then the client MUST abort the handshake with an "illegal_parameter" alert.
483 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000484 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000485 {
486 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000487 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000488 continue;
489
490 /* We found a match */
491 found = 1;
492 break;
493 }
494
495 /* Client MUST verify that the selected_group field does not
496 * correspond to a group which was provided in the "key_share"
497 * extension in the original ClientHello. If the server sent an
498 * HRR message with a key share already provided in the
499 * ClientHello then the client MUST abort the handshake with
500 * an "illegal_parameter" alert.
501 */
XiaokangQiand59be772022-01-24 10:12:51 +0000502 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000503 {
504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
505 MBEDTLS_SSL_PEND_FATAL_ALERT(
506 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
507 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
508 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
509 }
510
511 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000512 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000513
514 return( 0 );
515}
516
Jerry Yue1b9c292021-09-10 10:08:31 +0800517/*
Jerry Yub85277e2021-10-13 13:36:05 +0800518 * ssl_tls13_parse_key_share_ext()
519 * Parse key_share extension in Server Hello
520 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800521 * struct {
522 * KeyShareEntry server_share;
523 * } KeyShareServerHello;
524 * struct {
525 * NamedGroup group;
526 * opaque key_exchange<1..2^16-1>;
527 * } KeyShareEntry;
528 */
Jerry Yu4a173382021-10-11 21:45:31 +0800529static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800530 const unsigned char *buf,
531 const unsigned char *end )
532{
Jerry Yub85277e2021-10-13 13:36:05 +0800533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800534 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800535 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800536
Jerry Yu4a173382021-10-11 21:45:31 +0800537 /* ...
538 * NamedGroup group; (2 bytes)
539 * ...
540 */
541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
542 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800543 p += 2;
544
Jerry Yu4a173382021-10-11 21:45:31 +0800545 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800547 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800548 {
549 MBEDTLS_SSL_DEBUG_MSG( 1,
550 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800551 (unsigned) offered_group, (unsigned) group ) );
552 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
553 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
554 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800555 }
556
557#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800558 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800559 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100560 const mbedtls_ecp_curve_info *curve_info =
561 mbedtls_ecp_curve_info_from_tls_id( group );
562 if( curve_info == NULL )
563 {
564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
566 }
567
568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
569
Jerry Yuc068b662021-10-11 22:30:19 +0800570 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800571 if( ret != 0 )
572 return( ret );
573 }
Jerry Yub85277e2021-10-13 13:36:05 +0800574 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800575#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800576 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800577 {
578 /* Do something */
579 }
580 else
581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
582
583 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
584 return( ret );
585}
586
XiaokangQiand59be772022-01-24 10:12:51 +0000587/*
588 * ssl_tls13_parse_cookie_ext()
589 * Parse cookie extension in Hello Retry Request
590 *
591 * struct {
592 * opaque cookie<1..2^16-1>;
593 * } Cookie;
594 *
595 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
596 * extension to the client (this is an exception to the usual rule that
597 * the only extensions that may be sent are those that appear in the
598 * ClientHello). When sending the new ClientHello, the client MUST copy
599 * the contents of the extension received in the HelloRetryRequest into
600 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
601 * cookies in their initial ClientHello in subsequent connections.
602 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000603static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
604 const unsigned char *buf,
605 const unsigned char *end )
606{
XiaokangQian25c9c902022-02-08 10:49:53 +0000607 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000608 const unsigned char *p = buf;
609 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
610
611 /* Retrieve length field of cookie */
612 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
613 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
614 p += 2;
615
616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
617 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
618
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000619 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000620 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000621 handshake->cookie = mbedtls_calloc( 1, cookie_len );
622 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000623 {
624 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000625 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000626 cookie_len ) );
627 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
628 }
629
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000630 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000631 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000632
633 return( 0 );
634}
XiaokangQian43550bd2022-01-21 04:32:58 +0000635
XiaokangQian0b64eed2022-01-27 10:36:51 +0000636static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000637 unsigned char *buf,
638 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000639 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000640{
641 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000642 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000643 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000644
XiaokangQianc02768a2022-02-10 07:31:25 +0000645 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000646 {
647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
648 return( 0 );
649 }
650
651 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000652 handshake->cookie,
653 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000654
XiaokangQianc02768a2022-02-10 07:31:25 +0000655 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000656
657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
658
XiaokangQian0b64eed2022-01-27 10:36:51 +0000659 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000660 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
661 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000662 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000663
664 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000665 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000666
XiaokangQianc02768a2022-02-10 07:31:25 +0000667 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000668
669 return( 0 );
670}
671
Ronald Cron3d580bf2022-02-18 17:24:56 +0100672int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
673 unsigned char *buf,
674 unsigned char *end,
675 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100676{
677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
678 unsigned char *p = buf;
679 size_t ext_len;
680
681 *out_len = 0;
682
683 /* Write supported_versions extension
684 *
685 * Supported Versions Extension is mandatory with TLS 1.3.
686 */
687 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
688 if( ret != 0 )
689 return( ret );
690 p += ext_len;
691
692 /* Echo the cookie if the server provided one in its preceding
693 * HelloRetryRequest message.
694 */
695 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
696 if( ret != 0 )
697 return( ret );
698 p += ext_len;
699
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100700 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
701 {
702 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
703 if( ret != 0 )
704 return( ret );
705 p += ext_len;
706 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100707
708 *out_len = p - buf;
709
710 return( 0 );
711}
712
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800713/*
Jerry Yu4a173382021-10-11 21:45:31 +0800714 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800715 */
Ronald Cronda41b382022-03-30 09:57:11 +0200716/**
717 * \brief Detect if the ServerHello contains a supported_versions extension
718 * or not.
719 *
720 * \param[in] ssl SSL context
721 * \param[in] buf Buffer containing the ServerHello message
722 * \param[in] end End of the buffer containing the ServerHello message
723 *
724 * \return 0 if the ServerHello does not contain a supported_versions extension
725 * \return 1 if the ServerHello contains a supported_versions extension
726 * \return A negative value if an error occurred while parsing the ServerHello.
727 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100728static int ssl_tls13_is_supported_versions_ext_present(
729 mbedtls_ssl_context *ssl,
730 const unsigned char *buf,
731 const unsigned char *end )
732{
733 const unsigned char *p = buf;
734 size_t legacy_session_id_echo_len;
735 size_t extensions_len;
736 const unsigned char *extensions_end;
737
738 /*
739 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200740 * length:
741 * - legacy_version 2 bytes
742 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
743 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100744 */
745 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
746 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
747 legacy_session_id_echo_len = *p;
748
749 /*
750 * Jump to the extensions, jumping over:
751 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
752 * - cipher_suite 2 bytes
753 * - legacy_compression_method 1 byte
754 */
755 p += legacy_session_id_echo_len + 4;
756
757 /* Case of no extension */
758 if( p == end )
759 return( 0 );
760
761 /* ...
762 * Extension extensions<6..2^16-1>;
763 * ...
764 * struct {
765 * ExtensionType extension_type; (2 bytes)
766 * opaque extension_data<0..2^16-1>;
767 * } Extension;
768 */
769 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
770 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
771 p += 2;
772
773 /* Check extensions do not go beyond the buffer of data. */
774 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
775 extensions_end = p + extensions_len;
776
777 while( p < extensions_end )
778 {
779 unsigned int extension_type;
780 size_t extension_data_len;
781
782 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
783 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
784 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
785 p += 4;
786
787 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
788 return( 1 );
789
790 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
791 p += extension_data_len;
792 }
793
794 return( 0 );
795}
796
Jerry Yu7a186a02021-10-15 18:46:14 +0800797/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800798 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
799 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000800 * to indicate which message is expected and to be parsed next.
801 */
Jerry Yub85277e2021-10-13 13:36:05 +0800802#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
803#define SSL_SERVER_HELLO_COORDINATE_HRR 1
804static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
805 const unsigned char *buf,
806 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800807{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800808 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800809 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
810 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
811 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
812 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
813
814 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
815 *
Jerry Yu4a173382021-10-11 21:45:31 +0800816 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800817 * special value of the SHA-256 of "HelloRetryRequest".
818 *
819 * struct {
820 * ProtocolVersion legacy_version = 0x0303;
821 * Random random;
822 * opaque legacy_session_id_echo<0..32>;
823 * CipherSuite cipher_suite;
824 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800825 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800826 * } ServerHello;
827 *
828 */
Jerry Yub85277e2021-10-13 13:36:05 +0800829 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800830
831 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 {
Jerry Yub85277e2021-10-13 13:36:05 +0800833 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800834 }
835
Jerry Yub85277e2021-10-13 13:36:05 +0800836 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800837}
838
Jerry Yu745bb612021-10-13 22:01:04 +0800839/* Fetch and preprocess
840 * Returns a negative value on failure, and otherwise
841 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100842 * - SSL_SERVER_HELLO_COORDINATE_HRR or
843 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800844 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100845#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800846static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
847 unsigned char **buf,
848 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800849{
Jerry Yu4a173382021-10-11 21:45:31 +0800850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800851
XiaokangQian355e09a2022-01-20 11:14:50 +0000852 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
853 MBEDTLS_SSL_HS_SERVER_HELLO,
854 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800855
Ronald Cron9f0fba32022-02-10 16:45:15 +0100856 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
857 ssl, *buf, *buf + *buf_len ) );
858 if( ret == 0 )
859 {
860 /* If the supported versions extension is not present but we were
861 * expecting it, abort the handshake. Otherwise, switch to TLS 1.2
862 * handshake.
863 */
864 if( ssl->handshake->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 )
865 {
866 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
867 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
868 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
869 }
870
871 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400872 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100873 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
874 *buf, *buf_len );
875
876 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
877 {
878 ret = ssl_tls13_reset_key_share( ssl );
879 if( ret != 0 )
880 return( ret );
881 }
882
883 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
884 }
885
Jerry Yub85277e2021-10-13 13:36:05 +0800886 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
887 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 {
Jerry Yu745bb612021-10-13 22:01:04 +0800889 case SSL_SERVER_HELLO_COORDINATE_HELLO:
890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
891 break;
892 case SSL_SERVER_HELLO_COORDINATE_HRR:
893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000894 /* If a client receives a second
895 * HelloRetryRequest in the same connection (i.e., where the ClientHello
896 * was itself in response to a HelloRetryRequest), it MUST abort the
897 * handshake with an "unexpected_message" alert.
898 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000899 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000900 {
901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
902 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
903 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
904 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
905 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000906 /*
907 * Clients must abort the handshake with an "illegal_parameter"
908 * alert if the HelloRetryRequest would not result in any change
909 * in the ClientHello.
910 * In a PSK only key exchange that what we expect.
911 */
912 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
913 {
914 MBEDTLS_SSL_DEBUG_MSG( 1,
915 ( "Unexpected HRR in pure PSK key exchange." ) );
916 MBEDTLS_SSL_PEND_FATAL_ALERT(
917 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
918 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
919 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
920 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000921
XiaokangQiand9e068e2022-01-18 06:23:32 +0000922 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000923
Jerry Yu745bb612021-10-13 22:01:04 +0800924 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 }
926
927cleanup:
928
929 return( ret );
930}
931
Jerry Yu4a173382021-10-11 21:45:31 +0800932static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
933 const unsigned char **buf,
934 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800935{
936 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800937 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800938
Jerry Yude4fb2c2021-09-19 18:05:08 +0800939 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800940 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941
Jerry Yu4a173382021-10-11 21:45:31 +0800942 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800943
944 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800945 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
946 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800948 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
949 ssl->session_negotiate->id,
950 ssl->session_negotiate->id_len );
951 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800952 legacy_session_id_echo_len );
953
954 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
955 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
956
Jerry Yue1b9c292021-09-10 10:08:31 +0800957 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
958 }
959
Jerry Yu4a173382021-10-11 21:45:31 +0800960 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800961 *buf = p;
962
963 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800964 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 return( 0 );
966}
967
Jerry Yu4a173382021-10-11 21:45:31 +0800968static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
969 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800970{
Jerry Yu4a173382021-10-11 21:45:31 +0800971 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
972
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800974 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800975 {
Jerry Yu4a173382021-10-11 21:45:31 +0800976 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800977 {
978 return( 1 );
979 }
980 }
981 return( 0 );
982}
983
984/* Parse ServerHello message and configure context
985 *
986 * struct {
987 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
988 * Random random;
989 * opaque legacy_session_id_echo<0..32>;
990 * CipherSuite cipher_suite;
991 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800992 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 * } ServerHello;
994 */
Jerry Yuc068b662021-10-11 22:30:19 +0800995static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
996 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000997 const unsigned char *end,
998 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800999{
Jerry Yub85277e2021-10-13 13:36:05 +08001000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001001 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001002 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001003 size_t extensions_len;
1004 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001006 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001007 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001008
1009 /*
1010 * Check there is space for minimal fields
1011 *
1012 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001013 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 * - legacy_session_id_echo ( 1 byte ), minimum size
1015 * - cipher_suite ( 2 bytes)
1016 * - legacy_compression_method ( 1 byte )
1017 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001018 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001019
1020 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1022
Jerry Yu4a173382021-10-11 21:45:31 +08001023 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001024 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001025 * ...
1026 * with ProtocolVersion defined as:
1027 * uint16 ProtocolVersion;
1028 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001029 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1030 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1031 {
1032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1033 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1034 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001035 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1036 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 }
1038 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001039
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001040 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001041 * Random random;
1042 * ...
1043 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001044 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001045 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001046 if( !is_hrr )
1047 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001048 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001049 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1050 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1051 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1052 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001053 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001054
Jerry Yu4a173382021-10-11 21:45:31 +08001055 /* ...
1056 * opaque legacy_session_id_echo<0..32>;
1057 * ...
1058 */
1059 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 {
XiaokangQian52da5582022-01-26 09:49:29 +00001061 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001062 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001063 }
1064
Jerry Yu4a173382021-10-11 21:45:31 +08001065 /* ...
1066 * CipherSuite cipher_suite;
1067 * ...
1068 * with CipherSuite defined as:
1069 * uint8 CipherSuite[2];
1070 */
1071 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1073 p += 2;
1074
Jerry Yu4a173382021-10-11 21:45:31 +08001075
XiaokangQian355e09a2022-01-20 11:14:50 +00001076 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001077 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001078 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001079 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001080 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1081 ssl->tls_version,
1082 ssl->tls_version ) != 0 ) ||
Ronald Cronba120bb2022-03-30 22:09:48 +02001083 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 {
XiaokangQian52da5582022-01-26 09:49:29 +00001085 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001086 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001087 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001088 * If we received an HRR before and that the proposed selected
1089 * ciphersuite in this server hello is not the same as the one
1090 * proposed in the HRR, we abort the handshake and send an
1091 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001092 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001093 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1094 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001095 {
XiaokangQian52da5582022-01-26 09:49:29 +00001096 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001097 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001098
XiaokangQian52da5582022-01-26 09:49:29 +00001099 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001100 {
1101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1102 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001103 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001104 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001105
Jerry Yu4a173382021-10-11 21:45:31 +08001106 /* Configure ciphersuites */
1107 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1108
XiaokangQian355e09a2022-01-20 11:14:50 +00001109 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001110 ssl->session_negotiate->ciphersuite = cipher_suite;
1111
Jerry Yue1b9c292021-09-10 10:08:31 +08001112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1113 cipher_suite, ciphersuite_info->name ) );
1114
1115#if defined(MBEDTLS_HAVE_TIME)
1116 ssl->session_negotiate->start = time( NULL );
1117#endif /* MBEDTLS_HAVE_TIME */
1118
Jerry Yu4a173382021-10-11 21:45:31 +08001119 /* ...
1120 * uint8 legacy_compression_method = 0;
1121 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001123 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 if( p[0] != 0 )
1125 {
Jerry Yub85277e2021-10-13 13:36:05 +08001126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001127 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001128 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001129 }
1130 p++;
1131
Jerry Yub85277e2021-10-13 13:36:05 +08001132 /* ...
1133 * Extension extensions<6..2^16-1>;
1134 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001135 * struct {
1136 * ExtensionType extension_type; (2 bytes)
1137 * opaque extension_data<0..2^16-1>;
1138 * } Extension;
1139 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001140 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001141 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001142 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001143
Jerry Yu4a173382021-10-11 21:45:31 +08001144 /* Check extensions do not go beyond the buffer of data. */
1145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1146 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001147
Jerry Yu4a173382021-10-11 21:45:31 +08001148 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001149
Jerry Yu4a173382021-10-11 21:45:31 +08001150 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 {
1152 unsigned int extension_type;
1153 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001154 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001155
Jerry Yu4a173382021-10-11 21:45:31 +08001156 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001157 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1158 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1159 p += 4;
1160
Jerry Yu4a173382021-10-11 21:45:31 +08001161 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001162 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001163
1164 switch( extension_type )
1165 {
XiaokangQianb851da82022-01-14 04:03:11 +00001166 case MBEDTLS_TLS_EXT_COOKIE:
1167
XiaokangQiand9e068e2022-01-18 06:23:32 +00001168 if( !is_hrr )
1169 {
XiaokangQian52da5582022-01-26 09:49:29 +00001170 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001171 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001172 }
1173
XiaokangQian43550bd2022-01-21 04:32:58 +00001174 ret = ssl_tls13_parse_cookie_ext( ssl,
1175 p, extension_data_end );
1176 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001177 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001178 MBEDTLS_SSL_DEBUG_RET( 1,
1179 "ssl_tls13_parse_cookie_ext",
1180 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001181 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001182 }
XiaokangQianb851da82022-01-14 04:03:11 +00001183 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001184
Jerry Yue1b9c292021-09-10 10:08:31 +08001185 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001186 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001187 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001188 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001190 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001191 break;
1192
1193 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001196
XiaokangQian52da5582022-01-26 09:49:29 +00001197 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001198 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
Jerry Yue1b9c292021-09-10 10:08:31 +08001200 case MBEDTLS_TLS_EXT_KEY_SHARE:
1201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001202 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1203 {
XiaokangQian52da5582022-01-26 09:49:29 +00001204 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001205 goto cleanup;
1206 }
1207
XiaokangQian53f20b72022-01-18 10:47:33 +00001208 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001209 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001210 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001211 else
XiaokangQianb851da82022-01-14 04:03:11 +00001212 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001213 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001214 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 {
1216 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001217 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001218 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001219 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 }
1221 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001222
1223 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001224 MBEDTLS_SSL_DEBUG_MSG(
1225 3,
1226 ( "unknown extension found: %u ( ignoring )",
1227 extension_type ) );
1228
XiaokangQian52da5582022-01-26 09:49:29 +00001229 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001230 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001231 }
1232
1233 p += extension_data_len;
1234 }
1235
XiaokangQiand59be772022-01-24 10:12:51 +00001236cleanup:
1237
XiaokangQian52da5582022-01-26 09:49:29 +00001238 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001239 {
1240 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1241 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1242 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1243 }
XiaokangQian52da5582022-01-26 09:49:29 +00001244 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001245 {
1246 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1247 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1248 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1249 }
1250 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001251}
1252
XiaokangQian355e09a2022-01-20 11:14:50 +00001253static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001254{
Jerry Yub85277e2021-10-13 13:36:05 +08001255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001256 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001257 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001258 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001259
Jerry Yub85277e2021-10-13 13:36:05 +08001260 /* Determine the key exchange mode:
1261 * 1) If both the pre_shared_key and key_share extensions were received
1262 * then the key exchange mode is PSK with EPHEMERAL.
1263 * 2) If only the pre_shared_key extension was received then the key
1264 * exchange mode is PSK-only.
1265 * 3) If only the key_share extension was received then the key
1266 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001267 */
Jerry Yub85277e2021-10-13 13:36:05 +08001268 switch( handshake->extensions_present &
1269 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001270 {
Jerry Yu745bb612021-10-13 22:01:04 +08001271 /* Only the pre_shared_key extension was received */
1272 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001273 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001274 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001275
Jerry Yu745bb612021-10-13 22:01:04 +08001276 /* Only the key_share extension was received */
1277 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001278 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001279 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001280
Jerry Yu745bb612021-10-13 22:01:04 +08001281 /* Both the pre_shared_key and key_share extensions were received */
1282 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001283 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001284 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001285
Jerry Yu745bb612021-10-13 22:01:04 +08001286 /* Neither pre_shared_key nor key_share extension was received */
1287 default:
1288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1289 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1290 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001291 }
1292
1293 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1294 *
1295 * TODO: We don't have to do this in case we offered 0-RTT and the
1296 * server accepted it. In this case, we could skip generating
1297 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001298 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001299 if( ret != 0 )
1300 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001302 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001303 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001304 }
1305
1306 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001307 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001308 if( ret != 0 )
1309 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001311 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001312 }
1313
1314 /* Next evolution in key schedule: Establish handshake secret and
1315 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001316 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001317 if( ret != 0 )
1318 {
Jerry Yuc068b662021-10-11 22:30:19 +08001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1320 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001321 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001322 }
1323
Jerry Yub85277e2021-10-13 13:36:05 +08001324 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001325 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001326 {
1327 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1328 goto cleanup;
1329 }
Jerry Yu0b177842021-09-05 19:41:30 +08001330
1331 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1332 ssl->conf->endpoint,
1333 ssl->session_negotiate->ciphersuite,
1334 &traffic_keys,
1335 ssl );
1336 if( ret != 0 )
1337 {
1338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001339 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001340 }
1341
Jerry Yub85277e2021-10-13 13:36:05 +08001342 handshake->transform_handshake = transform_handshake;
1343 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001344
1345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1346 ssl->session_in = ssl->session_negotiate;
1347
1348 /*
1349 * State machine update
1350 */
1351 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1352
Jerry Yu4a173382021-10-11 21:45:31 +08001353cleanup:
1354
Jerry Yu0b177842021-09-05 19:41:30 +08001355 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001356 if( ret != 0 )
1357 {
Jerry Yub85277e2021-10-13 13:36:05 +08001358 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001359
Jerry Yu4a173382021-10-11 21:45:31 +08001360 MBEDTLS_SSL_PEND_FATAL_ALERT(
1361 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1362 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1363 }
1364 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001365}
1366
XiaokangQian355e09a2022-01-20 11:14:50 +00001367static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001368{
1369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1370
XiaokangQian647719a2021-12-07 09:16:29 +00001371#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1372 /* If not offering early data, the client sends a dummy CCS record
1373 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001374 * its second ClientHello or before its encrypted handshake flight.
1375 */
XiaokangQian647719a2021-12-07 09:16:29 +00001376 mbedtls_ssl_handshake_set_state( ssl,
1377 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1378#else
1379 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1380#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1381
XiaokangQian78b1fa72022-01-19 06:56:30 +00001382 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001383
XiaokangQian78b1fa72022-01-19 06:56:30 +00001384 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001385 * We are going to re-generate a shared secret corresponding to the group
1386 * selected by the server, which is different from the group for which we
1387 * generated a shared secret in the first client hello.
1388 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001389 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001390 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001391 if( ret != 0 )
1392 return( ret );
1393
1394 return( 0 );
1395}
1396
Jerry Yue1b9c292021-09-10 10:08:31 +08001397/*
Jerry Yu4a173382021-10-11 21:45:31 +08001398 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001399 * Handler for MBEDTLS_SSL_SERVER_HELLO
1400 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001401static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001402{
Jerry Yu4a173382021-10-11 21:45:31 +08001403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001404 unsigned char *buf = NULL;
1405 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001406 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001407
1408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1409
1410 /* Coordination step
1411 * - Fetch record
1412 * - Make sure it's either a ServerHello or a HRR.
1413 * - Switch processing routine in case of HRR
1414 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001415 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1416
XiaokangQian16acd4b2022-01-14 07:35:47 +00001417 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001418 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001419 goto cleanup;
1420 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001421 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001422
Ronald Cron9f0fba32022-02-10 16:45:15 +01001423 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1424 {
1425 ret = 0;
1426 goto cleanup;
1427 }
1428
XiaokangQianb851da82022-01-14 04:03:11 +00001429 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001430 buf + buf_len,
1431 is_hrr ) );
1432 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001433 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1434
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001435 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1436 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001437
XiaokangQiand9e068e2022-01-18 06:23:32 +00001438 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001439 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001440 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001441 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001442
1443cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1445 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001446 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001447}
1448
1449/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001450 *
1451 * EncryptedExtensions message
1452 *
1453 * The EncryptedExtensions message contains any extensions which
1454 * should be protected, i.e., any which are not needed to establish
1455 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001456 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001457
1458/*
1459 * Overview
1460 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001461
1462/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001463static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001464
XiaokangQian97799ac2021-10-11 10:05:54 +00001465static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1466 const unsigned char *buf,
1467 const unsigned char *end );
1468static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001469
1470/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001471 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001472 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001473static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001474{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001475 int ret;
1476 unsigned char *buf;
1477 size_t buf_len;
1478
1479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1480
Xiaofei Bai746f9482021-11-12 08:53:56 +00001481 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001482 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483 &buf, &buf_len ) );
1484
1485 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001486 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001487 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001488
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001489 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1490 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001491
XiaokangQian97799ac2021-10-11 10:05:54 +00001492 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001493
1494cleanup:
1495
1496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1497 return( ret );
1498
1499}
1500
XiaokangQian08da26c2021-10-09 10:12:11 +00001501/* Parse EncryptedExtensions message
1502 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001503 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001504 * } EncryptedExtensions;
1505 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001506static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1507 const unsigned char *buf,
1508 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001509{
1510 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001511 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001512 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001513 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001514
XiaokangQian08da26c2021-10-09 10:12:11 +00001515 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001516 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001517 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001518
XiaokangQian97799ac2021-10-11 10:05:54 +00001519 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001520 extensions_end = p + extensions_len;
1521 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001522
XiaokangQian8db25ff2021-10-13 05:56:18 +00001523 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001525 unsigned int extension_type;
1526 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001527
XiaokangQian08da26c2021-10-09 10:12:11 +00001528 /*
1529 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001530 * ExtensionType extension_type; (2 bytes)
1531 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001532 * } Extension;
1533 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001534 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001535 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1536 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1537 p += 4;
1538
XiaokangQian8db25ff2021-10-13 05:56:18 +00001539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001540
XiaokangQian97799ac2021-10-11 10:05:54 +00001541 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001543 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001544 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001545 switch( extension_type )
1546 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001547
XiaokangQian08da26c2021-10-09 10:12:11 +00001548 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1550 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001551
lhuang0486cacac2022-01-21 07:34:27 -08001552#if defined(MBEDTLS_SSL_ALPN)
1553 case MBEDTLS_TLS_EXT_ALPN:
1554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1555
1556 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1557 {
1558 return( ret );
1559 }
1560
1561 break;
1562#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001563 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001564 MBEDTLS_SSL_DEBUG_MSG(
1565 3, ( "unsupported extension found: %u ", extension_type) );
1566 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001567 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001568 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1569 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001570 }
1571
XiaokangQian08da26c2021-10-09 10:12:11 +00001572 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001573 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001574
XiaokangQian97799ac2021-10-11 10:05:54 +00001575 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001576 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001577 {
1578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001579 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001580 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001581 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001582 }
1583
1584 return( ret );
1585}
1586
XiaokangQian97799ac2021-10-11 10:05:54 +00001587static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001588{
Jerry Yua93ac112021-10-27 16:31:48 +08001589#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001590 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001591 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1592 else
Jerry Yud2674312021-10-29 10:08:19 +08001593 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001594#else
1595 ((void) ssl);
1596 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1597#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001598 return( 0 );
1599}
1600
Jerry Yua93ac112021-10-27 16:31:48 +08001601#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001602/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001603 *
1604 * STATE HANDLING: CertificateRequest
1605 *
Jerry Yud2674312021-10-29 10:08:19 +08001606 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001607#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1608#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001609/* Coordination:
1610 * Deals with the ambiguity of not knowing if a CertificateRequest
1611 * will be sent. Returns a negative code on failure, or
1612 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1613 * - SSL_CERTIFICATE_REQUEST_SKIP
1614 * indicating if a Certificate Request is expected or not.
1615 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001616static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001617{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001619
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001620 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001621 {
1622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1623 return( SSL_CERTIFICATE_REQUEST_SKIP );
1624 }
1625
1626 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001627 {
1628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1629 return( ret );
1630 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001631 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001632
1633 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1634 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1635 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001636 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001637 }
1638
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001639 return( SSL_CERTIFICATE_REQUEST_SKIP );
1640}
1641
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001642/*
1643 * ssl_tls13_parse_certificate_request()
1644 * Parse certificate request
1645 * struct {
1646 * opaque certificate_request_context<0..2^8-1>;
1647 * Extension extensions<2..2^16-1>;
1648 * } CertificateRequest;
1649 */
1650static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1651 const unsigned char *buf,
1652 const unsigned char *end )
1653{
1654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1655 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001656 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001657 size_t extensions_len = 0;
1658 const unsigned char *extensions_end;
1659 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001660
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001661 /* ...
1662 * opaque certificate_request_context<0..2^8-1>
1663 * ...
1664 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1666 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001667 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001668
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669 if( certificate_request_context_len > 0 )
1670 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001671 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1673 p, certificate_request_context_len );
1674
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001675 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001676 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001677 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001678 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679 {
1680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1681 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1682 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001683 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001684 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001685 p += certificate_request_context_len;
1686 }
1687
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001688 /* ...
1689 * Extension extensions<2..2^16-1>;
1690 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 */
1692 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1693 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1694 p += 2;
1695
1696 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1697 extensions_end = p + extensions_len;
1698
1699 while( p < extensions_end )
1700 {
1701 unsigned int extension_type;
1702 size_t extension_data_len;
1703
1704 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1705 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1706 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1707 p += 4;
1708
1709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1710
1711 switch( extension_type )
1712 {
1713 case MBEDTLS_TLS_EXT_SIG_ALG:
1714 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001715 ( "found signature algorithms extension" ) );
1716 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001717 p + extension_data_len );
1718 if( ret != 0 )
1719 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001720 if( ! sig_alg_ext_found )
1721 sig_alg_ext_found = 1;
1722 else
1723 {
1724 MBEDTLS_SSL_DEBUG_MSG( 3,
1725 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001726 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001727 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001728 break;
1729
1730 default:
1731 MBEDTLS_SSL_DEBUG_MSG(
1732 3,
1733 ( "unknown extension found: %u ( ignoring )",
1734 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001735 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001736 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001737 p += extension_data_len;
1738 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001739 /* Check that we consumed all the message. */
1740 if( p != end )
1741 {
1742 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001743 ( "CertificateRequest misaligned" ) );
1744 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001745 }
1746 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001747 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001748 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001749 MBEDTLS_SSL_DEBUG_MSG( 3,
1750 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001751 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001752 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001753
Jerry Yu7840f812022-01-29 10:26:51 +08001754 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001755 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001756
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001757decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001758 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1759 MBEDTLS_ERR_SSL_DECODE_ERROR );
1760 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001761}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001763/*
1764 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1765 */
1766static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001767{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001769
1770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1771
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001772 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1773
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001774 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1775 {
1776 unsigned char *buf;
1777 size_t buf_len;
1778
1779 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1780 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1781 &buf, &buf_len ) );
1782
1783 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1784 buf, buf + buf_len ) );
1785
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001786 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1787 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001788 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001789 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001792 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001793 }
1794 else
1795 {
1796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001797 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1798 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001799 }
1800
1801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001802 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001803
Jerry Yud2674312021-10-29 10:08:19 +08001804 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1805
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001806cleanup:
1807
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1809 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001810}
1811
1812/*
Jerry Yu687101b2021-09-14 16:03:56 +08001813 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1814 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001815static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001816{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001817 int ret;
1818
1819 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001820 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001821 return( ret );
1822
Jerry Yu687101b2021-09-14 16:03:56 +08001823 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1824 return( 0 );
1825}
1826
1827/*
1828 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1829 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001830static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001831{
Jerry Yu30b071c2021-09-12 20:16:03 +08001832 int ret;
1833
1834 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1835 if( ret != 0 )
1836 return( ret );
1837
Jerry Yu687101b2021-09-14 16:03:56 +08001838 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1839 return( 0 );
1840}
Jerry Yua93ac112021-10-27 16:31:48 +08001841#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001842
Jerry Yu687101b2021-09-14 16:03:56 +08001843/*
1844 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1845 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001846static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001847{
XiaokangQianac0385c2021-11-03 06:40:11 +00001848 int ret;
1849
XiaokangQianc5c39d52021-11-09 11:55:10 +00001850 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001851 if( ret != 0 )
1852 return( ret );
1853
Ronald Cron49ad6192021-11-24 16:25:31 +01001854#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1855 mbedtls_ssl_handshake_set_state(
1856 ssl,
1857 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1858#else
Jerry Yuca133a32022-02-15 14:22:05 +08001859 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001860#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001861
XiaokangQianac0385c2021-11-03 06:40:11 +00001862 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001863}
1864
1865/*
Jerry Yu566c7812022-01-26 15:41:22 +08001866 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1867 */
1868static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1869{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001870 int non_empty_certificate_msg = 0;
1871
Jerry Yu5cc35062022-01-28 16:16:08 +08001872 MBEDTLS_SSL_DEBUG_MSG( 1,
1873 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001874 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001875
Ronald Cron9df7c802022-03-08 18:38:54 +01001876#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001877 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001878 {
1879 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1880 if( ret != 0 )
1881 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001882
Ronald Cron7a94aca2022-03-09 07:44:27 +01001883 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1884 non_empty_certificate_msg = 1;
1885 }
1886 else
1887 {
1888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1889 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001890#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001891
Ronald Cron7a94aca2022-03-09 07:44:27 +01001892 if( non_empty_certificate_msg )
1893 {
1894 mbedtls_ssl_handshake_set_state( ssl,
1895 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1896 }
1897 else
1898 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1899
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001900 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001901}
1902
Ronald Cron9df7c802022-03-08 18:38:54 +01001903#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001904/*
1905 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1906 */
1907static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1908{
Ronald Crona8b38872022-03-09 07:59:25 +01001909 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1910
1911 if( ret == 0 )
1912 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1913
1914 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001915}
Jerry Yu90f152d2022-01-29 22:12:42 +08001916#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001917
1918/*
Jerry Yu687101b2021-09-14 16:03:56 +08001919 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1920 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001921static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001922{
XiaokangQian0fa66432021-11-15 03:33:57 +00001923 int ret;
1924
1925 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1926 if( ret != 0 )
1927 return( ret );
1928
1929 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1930 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001931}
1932
1933/*
1934 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1935 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001936static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001937{
Jerry Yu378254d2021-10-30 21:44:47 +08001938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001939 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001940 return( 0 );
1941}
1942
1943/*
1944 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1945 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001946static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001947{
Jerry Yu378254d2021-10-30 21:44:47 +08001948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1949 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1950
1951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1952 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1953
1954 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1955
1956 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1957 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001958}
1959
Jerry Yu92c6b402021-08-27 16:59:09 +08001960int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001961{
Jerry Yu92c6b402021-08-27 16:59:09 +08001962 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001963
Jerry Yu92c6b402021-08-27 16:59:09 +08001964 switch( ssl->state )
1965 {
1966 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001967 * ssl->state is initialized as HELLO_REQUEST. It is the same
1968 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001969 */
1970 case MBEDTLS_SSL_HELLO_REQUEST:
1971 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001972 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001973 break;
1974
1975 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001976 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001977 break;
1978
1979 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001980 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001981 break;
1982
Jerry Yua93ac112021-10-27 16:31:48 +08001983#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001984 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1985 ret = ssl_tls13_process_certificate_request( ssl );
1986 break;
1987
Jerry Yu687101b2021-09-14 16:03:56 +08001988 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001989 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001990 break;
1991
1992 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001993 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001994 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001995#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001996
1997 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001998 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001999 break;
2000
Jerry Yu566c7812022-01-26 15:41:22 +08002001 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2002 ret = ssl_tls13_write_client_certificate( ssl );
2003 break;
2004
Ronald Cron9df7c802022-03-08 18:38:54 +01002005#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002006 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2007 ret = ssl_tls13_write_client_certificate_verify( ssl );
2008 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002009#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002010
Jerry Yu687101b2021-09-14 16:03:56 +08002011 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002012 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002013 break;
2014
2015 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002016 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002017 break;
2018
2019 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002020 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002021 break;
2022
Ronald Cron49ad6192021-11-24 16:25:31 +01002023 /*
2024 * Injection of dummy-CCS's for middlebox compatibility
2025 */
2026#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002027 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002028 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2029 if( ret == 0 )
2030 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2031 break;
2032
2033 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2034 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2035 if( ret == 0 )
2036 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002037 break;
2038#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2039
Jerry Yu92c6b402021-08-27 16:59:09 +08002040 default:
2041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2043 }
2044
2045 return( ret );
2046}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002047
Jerry Yufb4b6472022-01-27 15:03:26 +08002048#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002049
Jerry Yufb4b6472022-01-27 15:03:26 +08002050