blob: 7c1f95e5c9770cff0569f9d05e720799a64b6b8a [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
Jerry Yub85277e2021-10-13 13:36:05 +0800103 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
104 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
114 return( 0 );
115}
116
lhuang0486cacac2022-01-21 07:34:27 -0800117#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800118static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
119 const unsigned char *buf, size_t len )
120{
121 size_t list_len, name_len;
122 const unsigned char *p = buf;
123 const unsigned char *end = buf + len;
124
125 /* If we didn't send it, the server shouldn't send it */
126 if( ssl->conf->alpn_list == NULL )
127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
128
129 /*
130 * opaque ProtocolName<1..2^8-1>;
131 *
132 * struct {
133 * ProtocolName protocol_name_list<2..2^16-1>
134 * } ProtocolNameList;
135 *
136 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
137 */
138
139 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
140 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
141
142 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
143 p += 2;
144 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
145
146 name_len = *p++;
147 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
148
149 /* Check that the server chosen protocol was in our list and save it */
150 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
151 {
152 if( name_len == strlen( *alpn ) &&
153 memcmp( buf + 3, *alpn, name_len ) == 0 )
154 {
155 ssl->alpn_chosen = *alpn;
156 return( 0 );
157 }
158 }
159
160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161}
162#endif /* MBEDTLS_SSL_ALPN */
163
XiaokangQian16acd4b2022-01-14 07:35:47 +0000164static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000165{
166 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100167
XiaokangQian647719a2021-12-07 09:16:29 +0000168 if( group_id == 0 )
169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
170
XiaokangQian355e09a2022-01-20 11:14:50 +0000171#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000172 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000173 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
176
177 /* Destroy generated private key. */
178 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
179 if( status != PSA_SUCCESS )
180 {
181 ret = psa_ssl_status_to_mbedtls( status );
182 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
183 return( ret );
184 }
185
186 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000187 return( 0 );
188 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000189 else
190#endif /* MBEDTLS_ECDH_C */
191 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000192 {
193 /* Do something */
194 }
195
196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
197}
198
199/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200 * Functions for writing key_share extension.
201 */
202#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800203static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800204 mbedtls_ssl_context *ssl,
205 uint16_t named_group,
206 unsigned char *buf,
207 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000208 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800209{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100210 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
211 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
212 psa_key_attributes_t key_attributes;
213 size_t own_pubkey_len;
214 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
215 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100219 /* Convert EC group to PSA key type. */
220 if( ( handshake->ecdh_psa_type =
221 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
222 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800223
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100224 if( ecdh_bits > 0xffff )
225 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
226 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100228 key_attributes = psa_key_attributes_init();
229 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
230 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
231 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
232 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100233
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100234 /* Generate ECDH private key. */
235 status = psa_generate_key( &key_attributes,
236 &handshake->ecdh_psa_privkey );
237 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100239 ret = psa_ssl_status_to_mbedtls( status );
240 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100242
Jerry Yu56fc07f2021-09-01 17:48:49 +0800243 }
244
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100245 /* Export the public part of the ECDH private key from PSA. */
246 status = psa_export_public_key( handshake->ecdh_psa_privkey,
247 buf, (size_t)( end - buf ),
248 &own_pubkey_len );
249 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100251 ret = psa_ssl_status_to_mbedtls( status );
252 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100254
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 }
256
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100257 if( own_pubkey_len > (size_t)( end - buf ) )
258 {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
260 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
261 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
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;
872 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
873 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 /*
1078 * Check whether this ciphersuite is supported and offered.
1079 * Via the force_ciphersuite version we may have instructed the client
1080 * to use a different ciphersuite.
1081 */
Jerry Yu4a173382021-10-11 21:45:31 +08001082 if( ciphersuite_info == NULL ||
1083 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
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->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1416 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1417
XiaokangQian16acd4b2022-01-14 07:35:47 +00001418 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001419 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001420 goto cleanup;
1421 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001422 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001423
Ronald Cron9f0fba32022-02-10 16:45:15 +01001424 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1425 {
1426 ret = 0;
1427 goto cleanup;
1428 }
1429
XiaokangQianb851da82022-01-14 04:03:11 +00001430 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001431 buf + buf_len,
1432 is_hrr ) );
1433 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001434 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1435
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001436 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1437 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001438
XiaokangQiand9e068e2022-01-18 06:23:32 +00001439 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001440 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001441 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001442 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001443
1444cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1446 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001447 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001448}
1449
1450/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451 *
1452 * EncryptedExtensions message
1453 *
1454 * The EncryptedExtensions message contains any extensions which
1455 * should be protected, i.e., any which are not needed to establish
1456 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001457 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001458
1459/*
1460 * Overview
1461 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001462
1463/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001464static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465
XiaokangQian97799ac2021-10-11 10:05:54 +00001466static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1467 const unsigned char *buf,
1468 const unsigned char *end );
1469static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470
1471/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001472 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001474static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001475{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001476 int ret;
1477 unsigned char *buf;
1478 size_t buf_len;
1479
1480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1481
Xiaofei Bai746f9482021-11-12 08:53:56 +00001482 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001483 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001484 &buf, &buf_len ) );
1485
1486 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001487 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001488 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001489
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001490 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1491 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492
XiaokangQian97799ac2021-10-11 10:05:54 +00001493 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001494
1495cleanup:
1496
1497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1498 return( ret );
1499
1500}
1501
XiaokangQian08da26c2021-10-09 10:12:11 +00001502/* Parse EncryptedExtensions message
1503 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001504 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001505 * } EncryptedExtensions;
1506 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001507static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1508 const unsigned char *buf,
1509 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001510{
1511 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001512 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001513 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001514 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001515
XiaokangQian08da26c2021-10-09 10:12:11 +00001516 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001517 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001518 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001519
XiaokangQian97799ac2021-10-11 10:05:54 +00001520 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001521 extensions_end = p + extensions_len;
1522 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001523
XiaokangQian8db25ff2021-10-13 05:56:18 +00001524 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001525 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001526 unsigned int extension_type;
1527 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001528
XiaokangQian08da26c2021-10-09 10:12:11 +00001529 /*
1530 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001531 * ExtensionType extension_type; (2 bytes)
1532 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001533 * } Extension;
1534 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001535 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001536 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1537 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1538 p += 4;
1539
XiaokangQian8db25ff2021-10-13 05:56:18 +00001540 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001541
XiaokangQian97799ac2021-10-11 10:05:54 +00001542 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001543 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001544 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001545 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001546 switch( extension_type )
1547 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001548
XiaokangQian08da26c2021-10-09 10:12:11 +00001549 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1551 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001552
lhuang0486cacac2022-01-21 07:34:27 -08001553#if defined(MBEDTLS_SSL_ALPN)
1554 case MBEDTLS_TLS_EXT_ALPN:
1555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1556
1557 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1558 {
1559 return( ret );
1560 }
1561
1562 break;
1563#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001564 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001565 MBEDTLS_SSL_DEBUG_MSG(
1566 3, ( "unsupported extension found: %u ", extension_type) );
1567 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001568 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001569 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1570 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001571 }
1572
XiaokangQian08da26c2021-10-09 10:12:11 +00001573 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001574 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001575
XiaokangQian97799ac2021-10-11 10:05:54 +00001576 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001577 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001578 {
1579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001580 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001581 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001582 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001583 }
1584
1585 return( ret );
1586}
1587
XiaokangQian97799ac2021-10-11 10:05:54 +00001588static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001589{
Jerry Yua93ac112021-10-27 16:31:48 +08001590#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001591 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001592 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1593 else
Jerry Yud2674312021-10-29 10:08:19 +08001594 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001595#else
1596 ((void) ssl);
1597 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1598#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001599 return( 0 );
1600}
1601
Jerry Yua93ac112021-10-27 16:31:48 +08001602#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001603/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001604 *
1605 * STATE HANDLING: CertificateRequest
1606 *
Jerry Yud2674312021-10-29 10:08:19 +08001607 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001608#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1609#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001610/* Coordination:
1611 * Deals with the ambiguity of not knowing if a CertificateRequest
1612 * will be sent. Returns a negative code on failure, or
1613 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1614 * - SSL_CERTIFICATE_REQUEST_SKIP
1615 * indicating if a Certificate Request is expected or not.
1616 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001617static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001618{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001620
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001621 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001622 {
1623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1624 return( SSL_CERTIFICATE_REQUEST_SKIP );
1625 }
1626
1627 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001628 {
1629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1630 return( ret );
1631 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001632 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001633
1634 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1635 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1636 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001637 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001638 }
1639
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640 return( SSL_CERTIFICATE_REQUEST_SKIP );
1641}
1642
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001643/*
1644 * ssl_tls13_parse_certificate_request()
1645 * Parse certificate request
1646 * struct {
1647 * opaque certificate_request_context<0..2^8-1>;
1648 * Extension extensions<2..2^16-1>;
1649 * } CertificateRequest;
1650 */
1651static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1652 const unsigned char *buf,
1653 const unsigned char *end )
1654{
1655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1656 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001657 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001658 size_t extensions_len = 0;
1659 const unsigned char *extensions_end;
1660 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001661
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001662 /* ...
1663 * opaque certificate_request_context<0..2^8-1>
1664 * ...
1665 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1667 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001668 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001670 if( certificate_request_context_len > 0 )
1671 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001672 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001673 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1674 p, certificate_request_context_len );
1675
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001676 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001677 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001678 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001679 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001680 {
1681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1682 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1683 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001684 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001685 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001686 p += certificate_request_context_len;
1687 }
1688
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001689 /* ...
1690 * Extension extensions<2..2^16-1>;
1691 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 */
1693 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1694 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1695 p += 2;
1696
1697 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1698 extensions_end = p + extensions_len;
1699
1700 while( p < extensions_end )
1701 {
1702 unsigned int extension_type;
1703 size_t extension_data_len;
1704
1705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1706 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1707 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1708 p += 4;
1709
1710 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1711
1712 switch( extension_type )
1713 {
1714 case MBEDTLS_TLS_EXT_SIG_ALG:
1715 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001716 ( "found signature algorithms extension" ) );
1717 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001718 p + extension_data_len );
1719 if( ret != 0 )
1720 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001721 if( ! sig_alg_ext_found )
1722 sig_alg_ext_found = 1;
1723 else
1724 {
1725 MBEDTLS_SSL_DEBUG_MSG( 3,
1726 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001727 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001728 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001729 break;
1730
1731 default:
1732 MBEDTLS_SSL_DEBUG_MSG(
1733 3,
1734 ( "unknown extension found: %u ( ignoring )",
1735 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001736 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001737 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001738 p += extension_data_len;
1739 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001740 /* Check that we consumed all the message. */
1741 if( p != end )
1742 {
1743 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001744 ( "CertificateRequest misaligned" ) );
1745 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001746 }
1747 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001748 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001749 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001750 MBEDTLS_SSL_DEBUG_MSG( 3,
1751 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001752 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001753 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001754
Jerry Yu7840f812022-01-29 10:26:51 +08001755 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001756 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001757
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001758decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001759 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1760 MBEDTLS_ERR_SSL_DECODE_ERROR );
1761 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001763
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001764/*
1765 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1766 */
1767static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001768{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001770
1771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1772
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001773 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1774
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001775 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1776 {
1777 unsigned char *buf;
1778 size_t buf_len;
1779
1780 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1781 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1782 &buf, &buf_len ) );
1783
1784 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1785 buf, buf + buf_len ) );
1786
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001787 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1788 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001789 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001790 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001791 {
1792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001793 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001794 }
1795 else
1796 {
1797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001798 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1799 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001800 }
1801
1802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001803 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001804
Jerry Yud2674312021-10-29 10:08:19 +08001805 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1806
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001807cleanup:
1808
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1810 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001811}
1812
1813/*
Jerry Yu687101b2021-09-14 16:03:56 +08001814 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1815 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001816static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001817{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001818 int ret;
1819
1820 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001821 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001822 return( ret );
1823
Jerry Yu687101b2021-09-14 16:03:56 +08001824 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1825 return( 0 );
1826}
1827
1828/*
1829 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1830 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001831static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001832{
Jerry Yu30b071c2021-09-12 20:16:03 +08001833 int ret;
1834
1835 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1836 if( ret != 0 )
1837 return( ret );
1838
Jerry Yu687101b2021-09-14 16:03:56 +08001839 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1840 return( 0 );
1841}
Jerry Yua93ac112021-10-27 16:31:48 +08001842#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001843
Jerry Yu687101b2021-09-14 16:03:56 +08001844/*
1845 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1846 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001847static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001848{
XiaokangQianac0385c2021-11-03 06:40:11 +00001849 int ret;
1850
XiaokangQianc5c39d52021-11-09 11:55:10 +00001851 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001852 if( ret != 0 )
1853 return( ret );
1854
Ronald Cron49ad6192021-11-24 16:25:31 +01001855#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1856 mbedtls_ssl_handshake_set_state(
1857 ssl,
1858 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1859#else
Jerry Yuca133a32022-02-15 14:22:05 +08001860 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001861#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001862
XiaokangQianac0385c2021-11-03 06:40:11 +00001863 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001864}
1865
1866/*
Jerry Yu566c7812022-01-26 15:41:22 +08001867 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1868 */
1869static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1870{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001871 int non_empty_certificate_msg = 0;
1872
Jerry Yu5cc35062022-01-28 16:16:08 +08001873 MBEDTLS_SSL_DEBUG_MSG( 1,
1874 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001875 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001876
Ronald Cron9df7c802022-03-08 18:38:54 +01001877#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001878 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001879 {
1880 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1881 if( ret != 0 )
1882 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001883
Ronald Cron7a94aca2022-03-09 07:44:27 +01001884 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1885 non_empty_certificate_msg = 1;
1886 }
1887 else
1888 {
1889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1890 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001891#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001892
Ronald Cron7a94aca2022-03-09 07:44:27 +01001893 if( non_empty_certificate_msg )
1894 {
1895 mbedtls_ssl_handshake_set_state( ssl,
1896 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1897 }
1898 else
1899 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1900
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001901 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001902}
1903
Ronald Cron9df7c802022-03-08 18:38:54 +01001904#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001905/*
1906 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1907 */
1908static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1909{
Ronald Crona8b38872022-03-09 07:59:25 +01001910 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1911
1912 if( ret == 0 )
1913 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1914
1915 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001916}
Jerry Yu90f152d2022-01-29 22:12:42 +08001917#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001918
1919/*
Jerry Yu687101b2021-09-14 16:03:56 +08001920 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1921 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001922static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001923{
XiaokangQian0fa66432021-11-15 03:33:57 +00001924 int ret;
1925
1926 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1927 if( ret != 0 )
1928 return( ret );
1929
1930 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1931 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001932}
1933
1934/*
1935 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1936 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001937static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001938{
Jerry Yu378254d2021-10-30 21:44:47 +08001939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001940 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001941 return( 0 );
1942}
1943
1944/*
1945 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1946 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001947static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001948{
Jerry Yu378254d2021-10-30 21:44:47 +08001949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1950 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1951
1952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1953 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1954
1955 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1956
1957 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1958 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001959}
1960
Jerry Yu92c6b402021-08-27 16:59:09 +08001961int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001962{
Jerry Yu92c6b402021-08-27 16:59:09 +08001963 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001964
Jerry Yu92c6b402021-08-27 16:59:09 +08001965 switch( ssl->state )
1966 {
1967 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001968 * ssl->state is initialized as HELLO_REQUEST. It is the same
1969 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001970 */
1971 case MBEDTLS_SSL_HELLO_REQUEST:
1972 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001973 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001974 break;
1975
1976 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001977 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001978 break;
1979
1980 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001981 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001982 break;
1983
Jerry Yua93ac112021-10-27 16:31:48 +08001984#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001985 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1986 ret = ssl_tls13_process_certificate_request( ssl );
1987 break;
1988
Jerry Yu687101b2021-09-14 16:03:56 +08001989 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001990 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001991 break;
1992
1993 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001994 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001995 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001996#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001997
1998 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001999 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002000 break;
2001
Jerry Yu566c7812022-01-26 15:41:22 +08002002 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2003 ret = ssl_tls13_write_client_certificate( ssl );
2004 break;
2005
Ronald Cron9df7c802022-03-08 18:38:54 +01002006#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002007 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2008 ret = ssl_tls13_write_client_certificate_verify( ssl );
2009 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002010#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002011
Jerry Yu687101b2021-09-14 16:03:56 +08002012 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002013 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002014 break;
2015
2016 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002017 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002018 break;
2019
2020 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002021 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002022 break;
2023
Ronald Cron49ad6192021-11-24 16:25:31 +01002024 /*
2025 * Injection of dummy-CCS's for middlebox compatibility
2026 */
2027#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002028 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002029 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2030 if( ret == 0 )
2031 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2032 break;
2033
2034 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2035 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2036 if( ret == 0 )
2037 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002038 break;
2039#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2040
Jerry Yu92c6b402021-08-27 16:59:09 +08002041 default:
2042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2044 }
2045
2046 return( ret );
2047}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002048
Jerry Yufb4b6472022-01-27 15:03:26 +08002049#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002050
Jerry Yufb4b6472022-01-27 15:03:26 +08002051