blob: 64272590b87ff52608e860d134c0ae4b377ee021 [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;
Glenn Strausscd78df62022-04-07 19:07:11 -040052 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 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 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040078 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Glenn Strausscd78df62022-04-07 19:07:11 -040083 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010084 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040085 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
88 }
89
90 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuc068b662021-10-11 22:30:19 +080095static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
96 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080097 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080098{
Jerry Yue1b9c292021-09-10 10:08:31 +080099 ((void) ssl);
100
Ronald Cron98473382022-03-30 20:04:10 +0200101 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400102 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
103 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800106
107 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
109 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800110 }
111
Ronald Cron98473382022-03-30 20:04:10 +0200112 if( &buf[2] != end )
113 {
114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
115 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
116 MBEDTLS_ERR_SSL_DECODE_ERROR );
117 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
118 }
119
Jerry Yue1b9c292021-09-10 10:08:31 +0800120 return( 0 );
121}
122
lhuang0486cacac2022-01-21 07:34:27 -0800123#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800124static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
125 const unsigned char *buf, size_t len )
126{
127 size_t list_len, name_len;
128 const unsigned char *p = buf;
129 const unsigned char *end = buf + len;
130
131 /* If we didn't send it, the server shouldn't send it */
132 if( ssl->conf->alpn_list == NULL )
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134
135 /*
136 * opaque ProtocolName<1..2^8-1>;
137 *
138 * struct {
139 * ProtocolName protocol_name_list<2..2^16-1>
140 * } ProtocolNameList;
141 *
142 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
143 */
144
145 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
146 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
147
148 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
149 p += 2;
150 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
151
152 name_len = *p++;
153 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
154
155 /* Check that the server chosen protocol was in our list and save it */
156 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
157 {
158 if( name_len == strlen( *alpn ) &&
159 memcmp( buf + 3, *alpn, name_len ) == 0 )
160 {
161 ssl->alpn_chosen = *alpn;
162 return( 0 );
163 }
164 }
165
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167}
168#endif /* MBEDTLS_SSL_ALPN */
169
XiaokangQian16acd4b2022-01-14 07:35:47 +0000170static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000171{
172 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100173
XiaokangQian647719a2021-12-07 09:16:29 +0000174 if( group_id == 0 )
175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
176
XiaokangQian355e09a2022-01-20 11:14:50 +0000177#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000178 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000179 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
182
183 /* Destroy generated private key. */
184 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
185 if( status != PSA_SUCCESS )
186 {
187 ret = psa_ssl_status_to_mbedtls( status );
188 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
189 return( ret );
190 }
191
192 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000193 return( 0 );
194 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000195 else
196#endif /* MBEDTLS_ECDH_C */
197 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000198 {
199 /* Do something */
200 }
201
202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
203}
204
205/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800206 * Functions for writing key_share extension.
207 */
208#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800209static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800210 mbedtls_ssl_context *ssl,
211 uint16_t named_group,
212 unsigned char *buf,
213 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000214 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800215{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100216 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
217 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
218 psa_key_attributes_t key_attributes;
219 size_t own_pubkey_len;
220 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
221 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 /* Convert EC group to PSA key type. */
226 if( ( handshake->ecdh_psa_type =
227 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
228 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800229
Neil Armstrong91477a72022-03-25 15:42:20 +0100230 ssl->handshake->ecdh_bits = ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100232 key_attributes = psa_key_attributes_init();
233 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
234 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
235 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
236 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100237
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100238 /* Generate ECDH private key. */
239 status = psa_generate_key( &key_attributes,
240 &handshake->ecdh_psa_privkey );
241 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100243 ret = psa_ssl_status_to_mbedtls( status );
244 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100246
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 }
248
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100249 /* Export the public part of the ECDH private key from PSA. */
250 status = psa_export_public_key( handshake->ecdh_psa_privkey,
251 buf, (size_t)( end - buf ),
252 &own_pubkey_len );
253 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100255 ret = psa_ssl_status_to_mbedtls( status );
256 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800257 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100258
Jerry Yu56fc07f2021-09-01 17:48:49 +0800259 }
260
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100261 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
Jerry Yu75336352021-09-01 15:59:36 +0800263 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800264}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265#endif /* MBEDTLS_ECDH_C */
266
Jerry Yub60e3cf2021-09-08 16:41:02 +0800267static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
268 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269{
270 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
271
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100274 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800275 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100276 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
278
Brett Warren14efd332021-10-06 09:32:11 +0100279 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000281 const mbedtls_ecp_curve_info *curve_info;
282 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
283 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100284 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 {
Brett Warren14efd332021-10-06 09:32:11 +0100286 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 return( 0 );
288 }
289 }
290#else
291 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293#endif /* MBEDTLS_ECDH_C */
294
295 /*
296 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800297 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298 */
299
300 return( ret );
301}
302
303/*
304 * ssl_tls13_write_key_share_ext
305 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 *
308 * struct {
309 * NamedGroup group;
310 * opaque key_exchange<1..2^16-1>;
311 * } KeyShareEntry;
312 * struct {
313 * KeyShareEntry client_shares<0..2^16-1>;
314 * } KeyShareClientHello;
315 */
316static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
317 unsigned char *buf,
318 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000319 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320{
321 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000322 unsigned char *client_shares; /* Start of client_shares */
323 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
326
Xiaofei Baid25fab62021-12-02 06:36:27 +0000327 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328
Jerry Yub60e3cf2021-09-08 16:41:02 +0800329 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 * - extension_type (2 bytes)
331 * - extension_data_length (2 bytes)
332 * - client_shares_length (2 bytes)
333 */
334 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
335 p += 6;
336
337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
338
339 /* HRR could already have requested something else. */
340 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
342 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345 &group_id ) );
346 }
347
348 /*
349 * Dispatch to type-specific key generation function.
350 *
351 * So far, we're only supporting ECDHE. With the introduction
352 * of PQC KEMs, we'll want to have multiple branches, one per
353 * type of KEM, and dispatch to the corresponding crypto. And
354 * only one key share entry is allowed.
355 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000356 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800358 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800360 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000361 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100363 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800364
365 /* Check there is space for header of KeyShareEntry
366 * - group (2 bytes)
367 * - key_exchange_length (2 bytes)
368 */
369 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
370 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100371 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800372 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800373 p += key_exchange_len;
374 if( ret != 0 )
375 return( ret );
376
377 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000378 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800379 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000380 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 }
382 else
383#endif /* MBEDTLS_ECDH_C */
384 if( 0 /* other KEMs? */ )
385 {
386 /* Do something */
387 }
388 else
389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
390
Jerry Yub60e3cf2021-09-08 16:41:02 +0800391 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000392 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 if( client_shares_len == 0)
394 {
395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800397 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800398 /* Write extension_type */
399 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
400 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800401 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800402 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800404
405 /* Update offered_group_id field */
406 ssl->handshake->offered_group_id = group_id;
407
408 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000409 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410
Xiaofei Baid25fab62021-12-02 06:36:27 +0000411 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412
413 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
414
415cleanup:
416
417 return( ret );
418}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800419
Jerry Yue1b9c292021-09-10 10:08:31 +0800420#if defined(MBEDTLS_ECDH_C)
421
Jerry Yuc068b662021-10-11 22:30:19 +0800422static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
423 const unsigned char *buf,
424 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800425{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100426 uint8_t *p = (uint8_t*)buf;
427 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800428
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100429 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
430 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
431 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800432
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100433 /* Check if key size is consistent with given buffer length. */
434 if ( peerkey_len > ( buf_len - 2 ) )
435 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800436
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100437 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100438 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100439 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800440
441 return( 0 );
442}
Jerry Yu4a173382021-10-11 21:45:31 +0800443#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800444
XiaokangQiand59be772022-01-24 10:12:51 +0000445/*
446 * ssl_tls13_parse_hrr_key_share_ext()
447 * Parse key_share extension in Hello Retry Request
448 *
449 * struct {
450 * NamedGroup selected_group;
451 * } KeyShareHelloRetryRequest;
452 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000453static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000454 const unsigned char *buf,
455 const unsigned char *end )
456{
XiaokangQianb851da82022-01-14 04:03:11 +0000457 const mbedtls_ecp_curve_info *curve_info = NULL;
458 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000459 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000460 int found = 0;
461
462 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
463 if( group_list == NULL )
464 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
465
466 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
467
468 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000469 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000470 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000472
473 /* Upon receipt of this extension in a HelloRetryRequest, the client
474 * MUST first verify that the selected_group field corresponds to a
475 * group which was provided in the "supported_groups" extension in the
476 * original ClientHello.
477 * The supported_group was based on the info in ssl->conf->group_list.
478 *
479 * If the server provided a key share that was not sent in the ClientHello
480 * then the client MUST abort the handshake with an "illegal_parameter" alert.
481 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000482 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000483 {
484 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000485 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000486 continue;
487
488 /* We found a match */
489 found = 1;
490 break;
491 }
492
493 /* Client MUST verify that the selected_group field does not
494 * correspond to a group which was provided in the "key_share"
495 * extension in the original ClientHello. If the server sent an
496 * HRR message with a key share already provided in the
497 * ClientHello then the client MUST abort the handshake with
498 * an "illegal_parameter" alert.
499 */
XiaokangQiand59be772022-01-24 10:12:51 +0000500 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000501 {
502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
503 MBEDTLS_SSL_PEND_FATAL_ALERT(
504 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
505 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
506 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
507 }
508
509 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000510 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000511
512 return( 0 );
513}
514
Jerry Yue1b9c292021-09-10 10:08:31 +0800515/*
Jerry Yub85277e2021-10-13 13:36:05 +0800516 * ssl_tls13_parse_key_share_ext()
517 * Parse key_share extension in Server Hello
518 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800519 * struct {
520 * KeyShareEntry server_share;
521 * } KeyShareServerHello;
522 * struct {
523 * NamedGroup group;
524 * opaque key_exchange<1..2^16-1>;
525 * } KeyShareEntry;
526 */
Jerry Yu4a173382021-10-11 21:45:31 +0800527static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800528 const unsigned char *buf,
529 const unsigned char *end )
530{
Jerry Yub85277e2021-10-13 13:36:05 +0800531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800532 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800533 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800534
Jerry Yu4a173382021-10-11 21:45:31 +0800535 /* ...
536 * NamedGroup group; (2 bytes)
537 * ...
538 */
539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
540 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800541 p += 2;
542
Jerry Yu4a173382021-10-11 21:45:31 +0800543 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800544 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800545 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1,
548 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800549 (unsigned) offered_group, (unsigned) group ) );
550 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
551 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
552 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800553 }
554
555#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800556 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800557 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100558 const mbedtls_ecp_curve_info *curve_info =
559 mbedtls_ecp_curve_info_from_tls_id( group );
560 if( curve_info == NULL )
561 {
562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
564 }
565
566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
567
Jerry Yuc068b662021-10-11 22:30:19 +0800568 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800569 if( ret != 0 )
570 return( ret );
571 }
Jerry Yub85277e2021-10-13 13:36:05 +0800572 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800573#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800574 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800575 {
576 /* Do something */
577 }
578 else
579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
580
581 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
582 return( ret );
583}
584
XiaokangQiand59be772022-01-24 10:12:51 +0000585/*
586 * ssl_tls13_parse_cookie_ext()
587 * Parse cookie extension in Hello Retry Request
588 *
589 * struct {
590 * opaque cookie<1..2^16-1>;
591 * } Cookie;
592 *
593 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
594 * extension to the client (this is an exception to the usual rule that
595 * the only extensions that may be sent are those that appear in the
596 * ClientHello). When sending the new ClientHello, the client MUST copy
597 * the contents of the extension received in the HelloRetryRequest into
598 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
599 * cookies in their initial ClientHello in subsequent connections.
600 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000601static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
602 const unsigned char *buf,
603 const unsigned char *end )
604{
XiaokangQian25c9c902022-02-08 10:49:53 +0000605 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000606 const unsigned char *p = buf;
607 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
608
609 /* Retrieve length field of cookie */
610 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
611 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
612 p += 2;
613
614 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
615 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
616
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000617 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000618 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000619 handshake->cookie = mbedtls_calloc( 1, cookie_len );
620 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000621 {
622 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000623 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000624 cookie_len ) );
625 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
626 }
627
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000628 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000629 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000630
631 return( 0 );
632}
XiaokangQian43550bd2022-01-21 04:32:58 +0000633
XiaokangQian0b64eed2022-01-27 10:36:51 +0000634static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000635 unsigned char *buf,
636 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000637 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000638{
639 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000640 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000641 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000642
XiaokangQianc02768a2022-02-10 07:31:25 +0000643 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000644 {
645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
646 return( 0 );
647 }
648
649 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000650 handshake->cookie,
651 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000652
XiaokangQianc02768a2022-02-10 07:31:25 +0000653 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000654
655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
656
XiaokangQian0b64eed2022-01-27 10:36:51 +0000657 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000658 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
659 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000660 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000661
662 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000663 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000664
XiaokangQianc02768a2022-02-10 07:31:25 +0000665 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000666
667 return( 0 );
668}
669
Ronald Cron3d580bf2022-02-18 17:24:56 +0100670int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
671 unsigned char *buf,
672 unsigned char *end,
673 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100674{
675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
676 unsigned char *p = buf;
677 size_t ext_len;
678
679 *out_len = 0;
680
681 /* Write supported_versions extension
682 *
683 * Supported Versions Extension is mandatory with TLS 1.3.
684 */
685 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
686 if( ret != 0 )
687 return( ret );
688 p += ext_len;
689
690 /* Echo the cookie if the server provided one in its preceding
691 * HelloRetryRequest message.
692 */
693 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
694 if( ret != 0 )
695 return( ret );
696 p += ext_len;
697
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100698 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
699 {
700 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
701 if( ret != 0 )
702 return( ret );
703 p += ext_len;
704 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100705
706 *out_len = p - buf;
707
708 return( 0 );
709}
710
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800711/*
Jerry Yu4a173382021-10-11 21:45:31 +0800712 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800713 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200714
Ronald Cronda41b382022-03-30 09:57:11 +0200715/**
716 * \brief Detect if the ServerHello contains a supported_versions extension
717 * or not.
718 *
719 * \param[in] ssl SSL context
720 * \param[in] buf Buffer containing the ServerHello message
721 * \param[in] end End of the buffer containing the ServerHello message
722 *
723 * \return 0 if the ServerHello does not contain a supported_versions extension
724 * \return 1 if the ServerHello contains a supported_versions extension
725 * \return A negative value if an error occurred while parsing the ServerHello.
726 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100727static int ssl_tls13_is_supported_versions_ext_present(
728 mbedtls_ssl_context *ssl,
729 const unsigned char *buf,
730 const unsigned char *end )
731{
732 const unsigned char *p = buf;
733 size_t legacy_session_id_echo_len;
734 size_t extensions_len;
735 const unsigned char *extensions_end;
736
737 /*
738 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200739 * length:
740 * - legacy_version 2 bytes
741 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
742 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100743 */
744 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
745 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
746 legacy_session_id_echo_len = *p;
747
748 /*
749 * Jump to the extensions, jumping over:
750 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
751 * - cipher_suite 2 bytes
752 * - legacy_compression_method 1 byte
753 */
754 p += legacy_session_id_echo_len + 4;
755
756 /* Case of no extension */
757 if( p == end )
758 return( 0 );
759
760 /* ...
761 * Extension extensions<6..2^16-1>;
762 * ...
763 * struct {
764 * ExtensionType extension_type; (2 bytes)
765 * opaque extension_data<0..2^16-1>;
766 * } Extension;
767 */
768 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
769 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
770 p += 2;
771
772 /* Check extensions do not go beyond the buffer of data. */
773 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
774 extensions_end = p + extensions_len;
775
776 while( p < extensions_end )
777 {
778 unsigned int extension_type;
779 size_t extension_data_len;
780
781 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
782 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
783 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
784 p += 4;
785
786 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
787 return( 1 );
788
789 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
790 p += extension_data_len;
791 }
792
793 return( 0 );
794}
795
Jerry Yu7a186a02021-10-15 18:46:14 +0800796/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200797 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
798 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
799 * - 0 otherwise
800 */
801static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
802 const unsigned char *buf,
803 const unsigned char *end )
804{
805 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
806 static const unsigned char magic_downgrade_string[] =
807 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
808 const unsigned char *last_eight_bytes_of_random;
809 unsigned char last_byte_of_random;
810
811 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
812 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
813
814 if( memcmp( last_eight_bytes_of_random,
815 magic_downgrade_string,
816 sizeof( magic_downgrade_string ) ) == 0 )
817 {
818 last_byte_of_random = last_eight_bytes_of_random[7];
819 return( last_byte_of_random == 0 ||
820 last_byte_of_random == 1 );
821 }
822
823 return( 0 );
824}
825
826/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800827 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
828 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000829 * to indicate which message is expected and to be parsed next.
830 */
Jerry Yub85277e2021-10-13 13:36:05 +0800831#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
832#define SSL_SERVER_HELLO_COORDINATE_HRR 1
833static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
834 const unsigned char *buf,
835 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800836{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800837 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
839 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
840 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
841 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
842
843 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
844 *
Jerry Yu4a173382021-10-11 21:45:31 +0800845 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800846 * special value of the SHA-256 of "HelloRetryRequest".
847 *
848 * struct {
849 * ProtocolVersion legacy_version = 0x0303;
850 * Random random;
851 * opaque legacy_session_id_echo<0..32>;
852 * CipherSuite cipher_suite;
853 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800854 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800855 * } ServerHello;
856 *
857 */
Jerry Yub85277e2021-10-13 13:36:05 +0800858 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800859
860 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800861 {
Jerry Yub85277e2021-10-13 13:36:05 +0800862 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800863 }
864
Jerry Yub85277e2021-10-13 13:36:05 +0800865 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800866}
867
Jerry Yu745bb612021-10-13 22:01:04 +0800868/* Fetch and preprocess
869 * Returns a negative value on failure, and otherwise
870 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100871 * - SSL_SERVER_HELLO_COORDINATE_HRR or
872 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800873 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100874#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800875static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
876 unsigned char **buf,
877 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800878{
Jerry Yu4a173382021-10-11 21:45:31 +0800879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200880 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800881
XiaokangQian355e09a2022-01-20 11:14:50 +0000882 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
883 MBEDTLS_SSL_HS_SERVER_HELLO,
884 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200885 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800886
Ronald Cron9f0fba32022-02-10 16:45:15 +0100887 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200888 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100889 if( ret == 0 )
890 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200891 MBEDTLS_SSL_PROC_CHK_NEG(
892 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
893
894 /* If the server is negotiating TLS 1.2 or below and:
895 * . we did not propose TLS 1.2 or
896 * . the server responded it is TLS 1.3 capable but negotiating a lower
897 * version of the protocol and thus we are under downgrade attack
898 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100899 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200900 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100901 {
902 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
903 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
904 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
905 }
906
907 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400908 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100909 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
910 *buf, *buf_len );
911
912 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
913 {
914 ret = ssl_tls13_reset_key_share( ssl );
915 if( ret != 0 )
916 return( ret );
917 }
918
919 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
920 }
921
Ronald Cronfd6193c2022-04-05 11:04:20 +0200922 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800923 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800924 {
Jerry Yu745bb612021-10-13 22:01:04 +0800925 case SSL_SERVER_HELLO_COORDINATE_HELLO:
926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
927 break;
928 case SSL_SERVER_HELLO_COORDINATE_HRR:
929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000930 /* If a client receives a second
931 * HelloRetryRequest in the same connection (i.e., where the ClientHello
932 * was itself in response to a HelloRetryRequest), it MUST abort the
933 * handshake with an "unexpected_message" alert.
934 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000935 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000936 {
937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
938 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
939 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
940 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
941 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000942 /*
943 * Clients must abort the handshake with an "illegal_parameter"
944 * alert if the HelloRetryRequest would not result in any change
945 * in the ClientHello.
946 * In a PSK only key exchange that what we expect.
947 */
948 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
949 {
950 MBEDTLS_SSL_DEBUG_MSG( 1,
951 ( "Unexpected HRR in pure PSK key exchange." ) );
952 MBEDTLS_SSL_PEND_FATAL_ALERT(
953 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
954 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
955 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
956 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000957
XiaokangQiand9e068e2022-01-18 06:23:32 +0000958 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000959
Jerry Yu745bb612021-10-13 22:01:04 +0800960 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800961 }
962
963cleanup:
964
965 return( ret );
966}
967
Jerry Yu4a173382021-10-11 21:45:31 +0800968static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
969 const unsigned char **buf,
970 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800971{
972 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800973 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800974
Jerry Yude4fb2c2021-09-19 18:05:08 +0800975 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800976 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800977
Jerry Yu4a173382021-10-11 21:45:31 +0800978 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800979
980 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800981 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
982 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800983 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800984 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
985 ssl->session_negotiate->id,
986 ssl->session_negotiate->id_len );
987 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800988 legacy_session_id_echo_len );
989
990 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
991 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
992
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
994 }
995
Jerry Yu4a173382021-10-11 21:45:31 +0800996 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997 *buf = p;
998
999 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001000 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001001 return( 0 );
1002}
1003
Jerry Yu4a173382021-10-11 21:45:31 +08001004static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1005 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001006{
Jerry Yu4a173382021-10-11 21:45:31 +08001007 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1008
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001010 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001011 {
Jerry Yu4a173382021-10-11 21:45:31 +08001012 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001013 {
1014 return( 1 );
1015 }
1016 }
1017 return( 0 );
1018}
1019
1020/* Parse ServerHello message and configure context
1021 *
1022 * struct {
1023 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1024 * Random random;
1025 * opaque legacy_session_id_echo<0..32>;
1026 * CipherSuite cipher_suite;
1027 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001028 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001029 * } ServerHello;
1030 */
Jerry Yuc068b662021-10-11 22:30:19 +08001031static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1032 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001033 const unsigned char *end,
1034 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001035{
Jerry Yub85277e2021-10-13 13:36:05 +08001036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001038 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001039 size_t extensions_len;
1040 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001041 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001042 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001043 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001044
1045 /*
1046 * Check there is space for minimal fields
1047 *
1048 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001049 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001050 * - legacy_session_id_echo ( 1 byte ), minimum size
1051 * - cipher_suite ( 2 bytes)
1052 * - legacy_compression_method ( 1 byte )
1053 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001054 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001055
1056 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001057 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1058
Jerry Yu4a173382021-10-11 21:45:31 +08001059 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001060 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001061 * ...
1062 * with ProtocolVersion defined as:
1063 * uint16 ProtocolVersion;
1064 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001065 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1066 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 {
1068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1069 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1070 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001071 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1072 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 }
1074 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001075
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001076 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001077 * Random random;
1078 * ...
1079 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001080 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001081 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001082 if( !is_hrr )
1083 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001084 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001085 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1086 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1087 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1088 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001089 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001090
Jerry Yu4a173382021-10-11 21:45:31 +08001091 /* ...
1092 * opaque legacy_session_id_echo<0..32>;
1093 * ...
1094 */
1095 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001096 {
XiaokangQian52da5582022-01-26 09:49:29 +00001097 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001098 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 }
1100
Jerry Yu4a173382021-10-11 21:45:31 +08001101 /* ...
1102 * CipherSuite cipher_suite;
1103 * ...
1104 * with CipherSuite defined as:
1105 * uint8 CipherSuite[2];
1106 */
1107 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001108 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1109 p += 2;
1110
Jerry Yu4a173382021-10-11 21:45:31 +08001111
XiaokangQian355e09a2022-01-20 11:14:50 +00001112 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001113 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001114 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001115 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001116 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1117 ssl->tls_version,
1118 ssl->tls_version ) != 0 ) ||
Ronald Cronba120bb2022-03-30 22:09:48 +02001119 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 {
XiaokangQian52da5582022-01-26 09:49:29 +00001121 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001123 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001124 * If we received an HRR before and that the proposed selected
1125 * ciphersuite in this server hello is not the same as the one
1126 * proposed in the HRR, we abort the handshake and send an
1127 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001128 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001129 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1130 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001131 {
XiaokangQian52da5582022-01-26 09:49:29 +00001132 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001133 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001134
XiaokangQian52da5582022-01-26 09:49:29 +00001135 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1138 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001140 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001141
Jerry Yu4a173382021-10-11 21:45:31 +08001142 /* Configure ciphersuites */
1143 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1144
XiaokangQian355e09a2022-01-20 11:14:50 +00001145 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 ssl->session_negotiate->ciphersuite = cipher_suite;
1147
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1149 cipher_suite, ciphersuite_info->name ) );
1150
1151#if defined(MBEDTLS_HAVE_TIME)
1152 ssl->session_negotiate->start = time( NULL );
1153#endif /* MBEDTLS_HAVE_TIME */
1154
Jerry Yu4a173382021-10-11 21:45:31 +08001155 /* ...
1156 * uint8 legacy_compression_method = 0;
1157 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001158 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001160 if( p[0] != 0 )
1161 {
Jerry Yub85277e2021-10-13 13:36:05 +08001162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001163 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001164 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 }
1166 p++;
1167
Jerry Yub85277e2021-10-13 13:36:05 +08001168 /* ...
1169 * Extension extensions<6..2^16-1>;
1170 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001171 * struct {
1172 * ExtensionType extension_type; (2 bytes)
1173 * opaque extension_data<0..2^16-1>;
1174 * } Extension;
1175 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001176 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001177 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001178 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001179
Jerry Yu4a173382021-10-11 21:45:31 +08001180 /* Check extensions do not go beyond the buffer of data. */
1181 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1182 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001183
Jerry Yu4a173382021-10-11 21:45:31 +08001184 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001185
Jerry Yu4a173382021-10-11 21:45:31 +08001186 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001187 {
1188 unsigned int extension_type;
1189 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001190 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001191
Jerry Yu4a173382021-10-11 21:45:31 +08001192 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1194 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1195 p += 4;
1196
Jerry Yu4a173382021-10-11 21:45:31 +08001197 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001198 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
1200 switch( extension_type )
1201 {
XiaokangQianb851da82022-01-14 04:03:11 +00001202 case MBEDTLS_TLS_EXT_COOKIE:
1203
XiaokangQiand9e068e2022-01-18 06:23:32 +00001204 if( !is_hrr )
1205 {
XiaokangQian52da5582022-01-26 09:49:29 +00001206 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001207 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001208 }
1209
XiaokangQian43550bd2022-01-21 04:32:58 +00001210 ret = ssl_tls13_parse_cookie_ext( ssl,
1211 p, extension_data_end );
1212 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001213 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001214 MBEDTLS_SSL_DEBUG_RET( 1,
1215 "ssl_tls13_parse_cookie_ext",
1216 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001217 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001218 }
XiaokangQianb851da82022-01-14 04:03:11 +00001219 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001220
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001222 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001223 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001224 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001225 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001226 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001227 break;
1228
1229 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001232
XiaokangQian52da5582022-01-26 09:49:29 +00001233 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001234 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001235
Jerry Yue1b9c292021-09-10 10:08:31 +08001236 case MBEDTLS_TLS_EXT_KEY_SHARE:
1237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001238 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1239 {
XiaokangQian52da5582022-01-26 09:49:29 +00001240 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001241 goto cleanup;
1242 }
1243
XiaokangQian53f20b72022-01-18 10:47:33 +00001244 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001245 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001246 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001247 else
XiaokangQianb851da82022-01-14 04:03:11 +00001248 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001249 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001250 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001251 {
1252 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001253 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001254 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001255 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001256 }
1257 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001258
1259 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001260 MBEDTLS_SSL_DEBUG_MSG(
1261 3,
1262 ( "unknown extension found: %u ( ignoring )",
1263 extension_type ) );
1264
XiaokangQian52da5582022-01-26 09:49:29 +00001265 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001266 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001267 }
1268
1269 p += extension_data_len;
1270 }
1271
XiaokangQiand59be772022-01-24 10:12:51 +00001272cleanup:
1273
XiaokangQian52da5582022-01-26 09:49:29 +00001274 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001275 {
1276 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1277 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1278 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1279 }
XiaokangQian52da5582022-01-26 09:49:29 +00001280 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001281 {
1282 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1283 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1284 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1285 }
1286 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001287}
1288
XiaokangQian355e09a2022-01-20 11:14:50 +00001289static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001290{
Jerry Yub85277e2021-10-13 13:36:05 +08001291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001292 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001293 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001294 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001295
Jerry Yub85277e2021-10-13 13:36:05 +08001296 /* Determine the key exchange mode:
1297 * 1) If both the pre_shared_key and key_share extensions were received
1298 * then the key exchange mode is PSK with EPHEMERAL.
1299 * 2) If only the pre_shared_key extension was received then the key
1300 * exchange mode is PSK-only.
1301 * 3) If only the key_share extension was received then the key
1302 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001303 */
Jerry Yub85277e2021-10-13 13:36:05 +08001304 switch( handshake->extensions_present &
1305 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001306 {
Jerry Yu745bb612021-10-13 22:01:04 +08001307 /* Only the pre_shared_key extension was received */
1308 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001309 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001310 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001311
Jerry Yu745bb612021-10-13 22:01:04 +08001312 /* Only the key_share extension was received */
1313 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001314 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001315 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001316
Jerry Yu745bb612021-10-13 22:01:04 +08001317 /* Both the pre_shared_key and key_share extensions were received */
1318 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001319 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001320 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001321
Jerry Yu745bb612021-10-13 22:01:04 +08001322 /* Neither pre_shared_key nor key_share extension was received */
1323 default:
1324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1325 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1326 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001327 }
1328
1329 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1330 *
1331 * TODO: We don't have to do this in case we offered 0-RTT and the
1332 * server accepted it. In this case, we could skip generating
1333 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001334 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001335 if( ret != 0 )
1336 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001338 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001339 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001340 }
1341
1342 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001343 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001344 if( ret != 0 )
1345 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001347 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001348 }
1349
1350 /* Next evolution in key schedule: Establish handshake secret and
1351 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001352 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001353 if( ret != 0 )
1354 {
Jerry Yuc068b662021-10-11 22:30:19 +08001355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1356 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001357 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001358 }
1359
Jerry Yub85277e2021-10-13 13:36:05 +08001360 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001361 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001362 {
1363 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1364 goto cleanup;
1365 }
Jerry Yu0b177842021-09-05 19:41:30 +08001366
1367 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1368 ssl->conf->endpoint,
1369 ssl->session_negotiate->ciphersuite,
1370 &traffic_keys,
1371 ssl );
1372 if( ret != 0 )
1373 {
1374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001375 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001376 }
1377
Jerry Yub85277e2021-10-13 13:36:05 +08001378 handshake->transform_handshake = transform_handshake;
1379 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001380
1381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1382 ssl->session_in = ssl->session_negotiate;
1383
1384 /*
1385 * State machine update
1386 */
1387 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1388
Jerry Yu4a173382021-10-11 21:45:31 +08001389cleanup:
1390
Jerry Yu0b177842021-09-05 19:41:30 +08001391 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001392 if( ret != 0 )
1393 {
Jerry Yub85277e2021-10-13 13:36:05 +08001394 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001395
Jerry Yu4a173382021-10-11 21:45:31 +08001396 MBEDTLS_SSL_PEND_FATAL_ALERT(
1397 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1398 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1399 }
1400 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001401}
1402
XiaokangQian355e09a2022-01-20 11:14:50 +00001403static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001404{
1405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1406
XiaokangQian647719a2021-12-07 09:16:29 +00001407#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1408 /* If not offering early data, the client sends a dummy CCS record
1409 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001410 * its second ClientHello or before its encrypted handshake flight.
1411 */
XiaokangQian647719a2021-12-07 09:16:29 +00001412 mbedtls_ssl_handshake_set_state( ssl,
1413 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1414#else
1415 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1416#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1417
XiaokangQian78b1fa72022-01-19 06:56:30 +00001418 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001419
XiaokangQian78b1fa72022-01-19 06:56:30 +00001420 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001421 * We are going to re-generate a shared secret corresponding to the group
1422 * selected by the server, which is different from the group for which we
1423 * generated a shared secret in the first client hello.
1424 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001425 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001426 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001427 if( ret != 0 )
1428 return( ret );
1429
1430 return( 0 );
1431}
1432
Jerry Yue1b9c292021-09-10 10:08:31 +08001433/*
Jerry Yu4a173382021-10-11 21:45:31 +08001434 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001435 * Handler for MBEDTLS_SSL_SERVER_HELLO
1436 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001437static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001438{
Jerry Yu4a173382021-10-11 21:45:31 +08001439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001440 unsigned char *buf = NULL;
1441 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001442 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001443
1444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1445
1446 /* Coordination step
1447 * - Fetch record
1448 * - Make sure it's either a ServerHello or a HRR.
1449 * - Switch processing routine in case of HRR
1450 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001451 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1452
XiaokangQian16acd4b2022-01-14 07:35:47 +00001453 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001454 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001455 goto cleanup;
1456 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001457 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001458
Ronald Cron9f0fba32022-02-10 16:45:15 +01001459 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1460 {
1461 ret = 0;
1462 goto cleanup;
1463 }
1464
XiaokangQianb851da82022-01-14 04:03:11 +00001465 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001466 buf + buf_len,
1467 is_hrr ) );
1468 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001469 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1470
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001471 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1472 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001473
XiaokangQiand9e068e2022-01-18 06:23:32 +00001474 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001475 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001476 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001477 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001478
1479cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1481 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001482 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001483}
1484
1485/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001486 *
1487 * EncryptedExtensions message
1488 *
1489 * The EncryptedExtensions message contains any extensions which
1490 * should be protected, i.e., any which are not needed to establish
1491 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001492 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001493
1494/*
1495 * Overview
1496 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001497
1498/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001499static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001500
XiaokangQian97799ac2021-10-11 10:05:54 +00001501static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1502 const unsigned char *buf,
1503 const unsigned char *end );
1504static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001505
1506/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001507 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001508 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001509static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001510{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001511 int ret;
1512 unsigned char *buf;
1513 size_t buf_len;
1514
1515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1516
Xiaofei Bai746f9482021-11-12 08:53:56 +00001517 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001518 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001519 &buf, &buf_len ) );
1520
1521 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001522 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001523 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001525 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1526 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001527
XiaokangQian97799ac2021-10-11 10:05:54 +00001528 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001529
1530cleanup:
1531
1532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1533 return( ret );
1534
1535}
1536
XiaokangQian08da26c2021-10-09 10:12:11 +00001537/* Parse EncryptedExtensions message
1538 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001539 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001540 * } EncryptedExtensions;
1541 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001542static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1543 const unsigned char *buf,
1544 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001545{
1546 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001547 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001548 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001549 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001550
XiaokangQian08da26c2021-10-09 10:12:11 +00001551 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001552 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001553 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001554
XiaokangQian97799ac2021-10-11 10:05:54 +00001555 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001556 extensions_end = p + extensions_len;
1557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001558
XiaokangQian8db25ff2021-10-13 05:56:18 +00001559 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001560 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001561 unsigned int extension_type;
1562 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001563
XiaokangQian08da26c2021-10-09 10:12:11 +00001564 /*
1565 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001566 * ExtensionType extension_type; (2 bytes)
1567 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001568 * } Extension;
1569 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001570 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001571 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1572 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1573 p += 4;
1574
XiaokangQian8db25ff2021-10-13 05:56:18 +00001575 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001576
XiaokangQian97799ac2021-10-11 10:05:54 +00001577 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001578 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001579 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001581 switch( extension_type )
1582 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001583
XiaokangQian08da26c2021-10-09 10:12:11 +00001584 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1586 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001587
lhuang0486cacac2022-01-21 07:34:27 -08001588#if defined(MBEDTLS_SSL_ALPN)
1589 case MBEDTLS_TLS_EXT_ALPN:
1590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1591
1592 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1593 {
1594 return( ret );
1595 }
1596
1597 break;
1598#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001599 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001600 MBEDTLS_SSL_DEBUG_MSG(
1601 3, ( "unsupported extension found: %u ", extension_type) );
1602 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001603 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001604 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1605 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001606 }
1607
XiaokangQian08da26c2021-10-09 10:12:11 +00001608 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001609 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001610
XiaokangQian97799ac2021-10-11 10:05:54 +00001611 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001612 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001613 {
1614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001615 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001616 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001617 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001618 }
1619
1620 return( ret );
1621}
1622
XiaokangQian97799ac2021-10-11 10:05:54 +00001623static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001624{
Jerry Yua93ac112021-10-27 16:31:48 +08001625#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001626 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001627 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1628 else
Jerry Yud2674312021-10-29 10:08:19 +08001629 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001630#else
1631 ((void) ssl);
1632 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1633#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001634 return( 0 );
1635}
1636
Jerry Yua93ac112021-10-27 16:31:48 +08001637#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001638/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001639 *
1640 * STATE HANDLING: CertificateRequest
1641 *
Jerry Yud2674312021-10-29 10:08:19 +08001642 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001643#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1644#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001645/* Coordination:
1646 * Deals with the ambiguity of not knowing if a CertificateRequest
1647 * will be sent. Returns a negative code on failure, or
1648 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1649 * - SSL_CERTIFICATE_REQUEST_SKIP
1650 * indicating if a Certificate Request is expected or not.
1651 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001653{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001655
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001656 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001657 {
1658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1659 return( SSL_CERTIFICATE_REQUEST_SKIP );
1660 }
1661
1662 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001663 {
1664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1665 return( ret );
1666 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001667 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001668
1669 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1670 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1671 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001673 }
1674
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 return( SSL_CERTIFICATE_REQUEST_SKIP );
1676}
1677
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678/*
1679 * ssl_tls13_parse_certificate_request()
1680 * Parse certificate request
1681 * struct {
1682 * opaque certificate_request_context<0..2^8-1>;
1683 * Extension extensions<2..2^16-1>;
1684 * } CertificateRequest;
1685 */
1686static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1687 const unsigned char *buf,
1688 const unsigned char *end )
1689{
1690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1691 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001693 size_t extensions_len = 0;
1694 const unsigned char *extensions_end;
1695 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001696
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001697 /* ...
1698 * opaque certificate_request_context<0..2^8-1>
1699 * ...
1700 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1702 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001703 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001704
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001705 if( certificate_request_context_len > 0 )
1706 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001707 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001708 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1709 p, certificate_request_context_len );
1710
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001711 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001712 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001713 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001714 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001715 {
1716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1717 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1718 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001719 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001720 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001721 p += certificate_request_context_len;
1722 }
1723
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001724 /* ...
1725 * Extension extensions<2..2^16-1>;
1726 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727 */
1728 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1729 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1730 p += 2;
1731
1732 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1733 extensions_end = p + extensions_len;
1734
1735 while( p < extensions_end )
1736 {
1737 unsigned int extension_type;
1738 size_t extension_data_len;
1739
1740 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1741 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1742 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1743 p += 4;
1744
1745 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1746
1747 switch( extension_type )
1748 {
1749 case MBEDTLS_TLS_EXT_SIG_ALG:
1750 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001751 ( "found signature algorithms extension" ) );
1752 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001753 p + extension_data_len );
1754 if( ret != 0 )
1755 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001756 if( ! sig_alg_ext_found )
1757 sig_alg_ext_found = 1;
1758 else
1759 {
1760 MBEDTLS_SSL_DEBUG_MSG( 3,
1761 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001762 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001763 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764 break;
1765
1766 default:
1767 MBEDTLS_SSL_DEBUG_MSG(
1768 3,
1769 ( "unknown extension found: %u ( ignoring )",
1770 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001771 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001772 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001773 p += extension_data_len;
1774 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001775 /* Check that we consumed all the message. */
1776 if( p != end )
1777 {
1778 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001779 ( "CertificateRequest misaligned" ) );
1780 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001781 }
1782 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001783 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001784 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001785 MBEDTLS_SSL_DEBUG_MSG( 3,
1786 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001787 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001788 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001789
Jerry Yu7840f812022-01-29 10:26:51 +08001790 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001791 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001792
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001793decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001794 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1795 MBEDTLS_ERR_SSL_DECODE_ERROR );
1796 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001798
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001799/*
1800 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1801 */
1802static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001803{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001805
1806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1807
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001808 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1809
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001810 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1811 {
1812 unsigned char *buf;
1813 size_t buf_len;
1814
1815 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1816 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1817 &buf, &buf_len ) );
1818
1819 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1820 buf, buf + buf_len ) );
1821
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001822 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1823 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001824 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001825 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001826 {
1827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001828 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001829 }
1830 else
1831 {
1832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001833 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1834 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001835 }
1836
1837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001838 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001839
Jerry Yud2674312021-10-29 10:08:19 +08001840 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1841
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001842cleanup:
1843
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1845 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001846}
1847
1848/*
Jerry Yu687101b2021-09-14 16:03:56 +08001849 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1850 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001851static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001852{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001853 int ret;
1854
1855 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001856 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001857 return( ret );
1858
Jerry Yu687101b2021-09-14 16:03:56 +08001859 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1860 return( 0 );
1861}
1862
1863/*
1864 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1865 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001866static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001867{
Jerry Yu30b071c2021-09-12 20:16:03 +08001868 int ret;
1869
1870 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1871 if( ret != 0 )
1872 return( ret );
1873
Jerry Yu687101b2021-09-14 16:03:56 +08001874 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1875 return( 0 );
1876}
Jerry Yua93ac112021-10-27 16:31:48 +08001877#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001878
Jerry Yu687101b2021-09-14 16:03:56 +08001879/*
1880 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1881 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001882static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001883{
XiaokangQianac0385c2021-11-03 06:40:11 +00001884 int ret;
1885
XiaokangQianc5c39d52021-11-09 11:55:10 +00001886 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001887 if( ret != 0 )
1888 return( ret );
1889
Ronald Cron49ad6192021-11-24 16:25:31 +01001890#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1891 mbedtls_ssl_handshake_set_state(
1892 ssl,
1893 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1894#else
Jerry Yuca133a32022-02-15 14:22:05 +08001895 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001896#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001897
XiaokangQianac0385c2021-11-03 06:40:11 +00001898 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001899}
1900
1901/*
Jerry Yu566c7812022-01-26 15:41:22 +08001902 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1903 */
1904static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1905{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001906 int non_empty_certificate_msg = 0;
1907
Jerry Yu5cc35062022-01-28 16:16:08 +08001908 MBEDTLS_SSL_DEBUG_MSG( 1,
1909 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001910 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001911
Ronald Cron9df7c802022-03-08 18:38:54 +01001912#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001913 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001914 {
1915 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1916 if( ret != 0 )
1917 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001918
Ronald Cron7a94aca2022-03-09 07:44:27 +01001919 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1920 non_empty_certificate_msg = 1;
1921 }
1922 else
1923 {
1924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1925 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001926#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001927
Ronald Cron7a94aca2022-03-09 07:44:27 +01001928 if( non_empty_certificate_msg )
1929 {
1930 mbedtls_ssl_handshake_set_state( ssl,
1931 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1932 }
1933 else
1934 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1935
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001936 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001937}
1938
Ronald Cron9df7c802022-03-08 18:38:54 +01001939#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001940/*
1941 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1942 */
1943static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1944{
Ronald Crona8b38872022-03-09 07:59:25 +01001945 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1946
1947 if( ret == 0 )
1948 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1949
1950 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001951}
Jerry Yu90f152d2022-01-29 22:12:42 +08001952#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001953
1954/*
Jerry Yu687101b2021-09-14 16:03:56 +08001955 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1956 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001957static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001958{
XiaokangQian0fa66432021-11-15 03:33:57 +00001959 int ret;
1960
1961 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1962 if( ret != 0 )
1963 return( ret );
1964
1965 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1966 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001967}
1968
1969/*
1970 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1971 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001972static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001973{
Jerry Yu378254d2021-10-30 21:44:47 +08001974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001975 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001976 return( 0 );
1977}
1978
1979/*
1980 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1981 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001982static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001983{
Jerry Yu378254d2021-10-30 21:44:47 +08001984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1985 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1986
1987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1988 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1989
1990 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1991
1992 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1993 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001994}
1995
Jerry Yu92c6b402021-08-27 16:59:09 +08001996int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001997{
Jerry Yu92c6b402021-08-27 16:59:09 +08001998 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001999
Jerry Yu92c6b402021-08-27 16:59:09 +08002000 switch( ssl->state )
2001 {
2002 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002003 * ssl->state is initialized as HELLO_REQUEST. It is the same
2004 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002005 */
2006 case MBEDTLS_SSL_HELLO_REQUEST:
2007 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002008 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002009 break;
2010
2011 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002012 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002013 break;
2014
2015 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002016 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002017 break;
2018
Jerry Yua93ac112021-10-27 16:31:48 +08002019#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002020 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2021 ret = ssl_tls13_process_certificate_request( ssl );
2022 break;
2023
Jerry Yu687101b2021-09-14 16:03:56 +08002024 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002025 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002026 break;
2027
2028 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002029 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002030 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002031#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002032
2033 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002034 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002035 break;
2036
Jerry Yu566c7812022-01-26 15:41:22 +08002037 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2038 ret = ssl_tls13_write_client_certificate( ssl );
2039 break;
2040
Ronald Cron9df7c802022-03-08 18:38:54 +01002041#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002042 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2043 ret = ssl_tls13_write_client_certificate_verify( ssl );
2044 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002045#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002046
Jerry Yu687101b2021-09-14 16:03:56 +08002047 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002048 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002049 break;
2050
2051 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002052 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002053 break;
2054
2055 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002056 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002057 break;
2058
Ronald Cron49ad6192021-11-24 16:25:31 +01002059 /*
2060 * Injection of dummy-CCS's for middlebox compatibility
2061 */
2062#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002063 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002064 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2065 if( ret == 0 )
2066 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2067 break;
2068
2069 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2070 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2071 if( ret == 0 )
2072 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002073 break;
2074#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2075
Jerry Yu92c6b402021-08-27 16:59:09 +08002076 default:
2077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2079 }
2080
2081 return( ret );
2082}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002083
Jerry Yufb4b6472022-01-27 15:03:26 +08002084#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002085
Jerry Yufb4b6472022-01-27 15:03:26 +08002086