blob: b05d2f239a84001eda26aacb3ab2f31f04018791 [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 Cronda41b382022-03-30 09:57:11 +0200714/**
715 * \brief Detect if the ServerHello contains a supported_versions extension
716 * or not.
717 *
718 * \param[in] ssl SSL context
719 * \param[in] buf Buffer containing the ServerHello message
720 * \param[in] end End of the buffer containing the ServerHello message
721 *
722 * \return 0 if the ServerHello does not contain a supported_versions extension
723 * \return 1 if the ServerHello contains a supported_versions extension
724 * \return A negative value if an error occurred while parsing the ServerHello.
725 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100726static int ssl_tls13_is_supported_versions_ext_present(
727 mbedtls_ssl_context *ssl,
728 const unsigned char *buf,
729 const unsigned char *end )
730{
731 const unsigned char *p = buf;
732 size_t legacy_session_id_echo_len;
733 size_t extensions_len;
734 const unsigned char *extensions_end;
735
736 /*
737 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200738 * length:
739 * - legacy_version 2 bytes
740 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
741 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100742 */
743 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
744 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
745 legacy_session_id_echo_len = *p;
746
747 /*
748 * Jump to the extensions, jumping over:
749 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
750 * - cipher_suite 2 bytes
751 * - legacy_compression_method 1 byte
752 */
753 p += legacy_session_id_echo_len + 4;
754
755 /* Case of no extension */
756 if( p == end )
757 return( 0 );
758
759 /* ...
760 * Extension extensions<6..2^16-1>;
761 * ...
762 * struct {
763 * ExtensionType extension_type; (2 bytes)
764 * opaque extension_data<0..2^16-1>;
765 * } Extension;
766 */
767 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
768 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
769 p += 2;
770
771 /* Check extensions do not go beyond the buffer of data. */
772 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
773 extensions_end = p + extensions_len;
774
775 while( p < extensions_end )
776 {
777 unsigned int extension_type;
778 size_t extension_data_len;
779
780 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
781 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
782 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
783 p += 4;
784
785 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
786 return( 1 );
787
788 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
789 p += extension_data_len;
790 }
791
792 return( 0 );
793}
794
Jerry Yu7a186a02021-10-15 18:46:14 +0800795/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800796 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
797 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000798 * to indicate which message is expected and to be parsed next.
799 */
Jerry Yub85277e2021-10-13 13:36:05 +0800800#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
801#define SSL_SERVER_HELLO_COORDINATE_HRR 1
802static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
803 const unsigned char *buf,
804 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800805{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800806 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800807 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
808 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
809 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
810 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
811
812 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
813 *
Jerry Yu4a173382021-10-11 21:45:31 +0800814 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800815 * special value of the SHA-256 of "HelloRetryRequest".
816 *
817 * struct {
818 * ProtocolVersion legacy_version = 0x0303;
819 * Random random;
820 * opaque legacy_session_id_echo<0..32>;
821 * CipherSuite cipher_suite;
822 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800823 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800824 * } ServerHello;
825 *
826 */
Jerry Yub85277e2021-10-13 13:36:05 +0800827 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800828
829 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800830 {
Jerry Yub85277e2021-10-13 13:36:05 +0800831 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 }
833
Jerry Yub85277e2021-10-13 13:36:05 +0800834 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800835}
836
Jerry Yu745bb612021-10-13 22:01:04 +0800837/* Fetch and preprocess
838 * Returns a negative value on failure, and otherwise
839 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100840 * - SSL_SERVER_HELLO_COORDINATE_HRR or
841 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800842 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100843#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800844static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
845 unsigned char **buf,
846 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800847{
Jerry Yu4a173382021-10-11 21:45:31 +0800848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800849
XiaokangQian355e09a2022-01-20 11:14:50 +0000850 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
851 MBEDTLS_SSL_HS_SERVER_HELLO,
852 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800853
Ronald Cron9f0fba32022-02-10 16:45:15 +0100854 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
855 ssl, *buf, *buf + *buf_len ) );
856 if( ret == 0 )
857 {
858 /* If the supported versions extension is not present but we were
859 * expecting it, abort the handshake. Otherwise, switch to TLS 1.2
860 * handshake.
861 */
Glenn Strausscd78df62022-04-07 19:07:11 -0400862 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100863 {
864 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
865 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
866 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
867 }
868
869 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400870 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100871 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
872 *buf, *buf_len );
873
874 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
875 {
876 ret = ssl_tls13_reset_key_share( ssl );
877 if( ret != 0 )
878 return( ret );
879 }
880
881 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
882 }
883
Jerry Yub85277e2021-10-13 13:36:05 +0800884 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
885 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800886 {
Jerry Yu745bb612021-10-13 22:01:04 +0800887 case SSL_SERVER_HELLO_COORDINATE_HELLO:
888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
889 break;
890 case SSL_SERVER_HELLO_COORDINATE_HRR:
891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000892 /* If a client receives a second
893 * HelloRetryRequest in the same connection (i.e., where the ClientHello
894 * was itself in response to a HelloRetryRequest), it MUST abort the
895 * handshake with an "unexpected_message" alert.
896 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000897 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000898 {
899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
900 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
901 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
902 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
903 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000904 /*
905 * Clients must abort the handshake with an "illegal_parameter"
906 * alert if the HelloRetryRequest would not result in any change
907 * in the ClientHello.
908 * In a PSK only key exchange that what we expect.
909 */
910 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
911 {
912 MBEDTLS_SSL_DEBUG_MSG( 1,
913 ( "Unexpected HRR in pure PSK key exchange." ) );
914 MBEDTLS_SSL_PEND_FATAL_ALERT(
915 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
916 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
917 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
918 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000919
XiaokangQiand9e068e2022-01-18 06:23:32 +0000920 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000921
Jerry Yu745bb612021-10-13 22:01:04 +0800922 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800923 }
924
925cleanup:
926
927 return( ret );
928}
929
Jerry Yu4a173382021-10-11 21:45:31 +0800930static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
931 const unsigned char **buf,
932 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800933{
934 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800935 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800936
Jerry Yude4fb2c2021-09-19 18:05:08 +0800937 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800938 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800939
Jerry Yu4a173382021-10-11 21:45:31 +0800940 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800941
942 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800943 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
944 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800945 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800946 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
947 ssl->session_negotiate->id,
948 ssl->session_negotiate->id_len );
949 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800950 legacy_session_id_echo_len );
951
952 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
953 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
954
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
956 }
957
Jerry Yu4a173382021-10-11 21:45:31 +0800958 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 *buf = p;
960
961 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800962 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800963 return( 0 );
964}
965
Jerry Yu4a173382021-10-11 21:45:31 +0800966static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
967 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800968{
Jerry Yu4a173382021-10-11 21:45:31 +0800969 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
970
Jerry Yue1b9c292021-09-10 10:08:31 +0800971 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800972 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 {
Jerry Yu4a173382021-10-11 21:45:31 +0800974 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800975 {
976 return( 1 );
977 }
978 }
979 return( 0 );
980}
981
982/* Parse ServerHello message and configure context
983 *
984 * struct {
985 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
986 * Random random;
987 * opaque legacy_session_id_echo<0..32>;
988 * CipherSuite cipher_suite;
989 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800990 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800991 * } ServerHello;
992 */
Jerry Yuc068b662021-10-11 22:30:19 +0800993static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
994 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000995 const unsigned char *end,
996 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800997{
Jerry Yub85277e2021-10-13 13:36:05 +0800998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800999 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001000 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001001 size_t extensions_len;
1002 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001005 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001006
1007 /*
1008 * Check there is space for minimal fields
1009 *
1010 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001011 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001012 * - legacy_session_id_echo ( 1 byte ), minimum size
1013 * - cipher_suite ( 2 bytes)
1014 * - legacy_compression_method ( 1 byte )
1015 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001016 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001017
1018 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1020
Jerry Yu4a173382021-10-11 21:45:31 +08001021 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001022 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001023 * ...
1024 * with ProtocolVersion defined as:
1025 * uint16 ProtocolVersion;
1026 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001027 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1028 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001029 {
1030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1031 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1032 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001033 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1034 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 }
1036 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001037
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001038 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001039 * Random random;
1040 * ...
1041 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001042 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001043 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001044 if( !is_hrr )
1045 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001046 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001047 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1048 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1049 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1050 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001051 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001052
Jerry Yu4a173382021-10-11 21:45:31 +08001053 /* ...
1054 * opaque legacy_session_id_echo<0..32>;
1055 * ...
1056 */
1057 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 {
XiaokangQian52da5582022-01-26 09:49:29 +00001059 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001060 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001061 }
1062
Jerry Yu4a173382021-10-11 21:45:31 +08001063 /* ...
1064 * CipherSuite cipher_suite;
1065 * ...
1066 * with CipherSuite defined as:
1067 * uint8 CipherSuite[2];
1068 */
1069 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1071 p += 2;
1072
Jerry Yu4a173382021-10-11 21:45:31 +08001073
XiaokangQian355e09a2022-01-20 11:14:50 +00001074 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001075 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001076 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001077 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001078 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1079 ssl->tls_version,
1080 ssl->tls_version ) != 0 ) ||
Ronald Cronba120bb2022-03-30 22:09:48 +02001081 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001082 {
XiaokangQian52da5582022-01-26 09:49:29 +00001083 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001085 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001086 * If we received an HRR before and that the proposed selected
1087 * ciphersuite in this server hello is not the same as the one
1088 * proposed in the HRR, we abort the handshake and send an
1089 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001090 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001091 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1092 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001093 {
XiaokangQian52da5582022-01-26 09:49:29 +00001094 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001095 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001096
XiaokangQian52da5582022-01-26 09:49:29 +00001097 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001098 {
1099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1100 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001101 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001102 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001103
Jerry Yu4a173382021-10-11 21:45:31 +08001104 /* Configure ciphersuites */
1105 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1106
XiaokangQian355e09a2022-01-20 11:14:50 +00001107 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001108 ssl->session_negotiate->ciphersuite = cipher_suite;
1109
Jerry Yue1b9c292021-09-10 10:08:31 +08001110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1111 cipher_suite, ciphersuite_info->name ) );
1112
1113#if defined(MBEDTLS_HAVE_TIME)
1114 ssl->session_negotiate->start = time( NULL );
1115#endif /* MBEDTLS_HAVE_TIME */
1116
Jerry Yu4a173382021-10-11 21:45:31 +08001117 /* ...
1118 * uint8 legacy_compression_method = 0;
1119 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001121 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 if( p[0] != 0 )
1123 {
Jerry Yub85277e2021-10-13 13:36:05 +08001124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001125 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001126 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 }
1128 p++;
1129
Jerry Yub85277e2021-10-13 13:36:05 +08001130 /* ...
1131 * Extension extensions<6..2^16-1>;
1132 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001133 * struct {
1134 * ExtensionType extension_type; (2 bytes)
1135 * opaque extension_data<0..2^16-1>;
1136 * } Extension;
1137 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001138 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001139 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001141
Jerry Yu4a173382021-10-11 21:45:31 +08001142 /* Check extensions do not go beyond the buffer of data. */
1143 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1144 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001145
Jerry Yu4a173382021-10-11 21:45:31 +08001146 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001147
Jerry Yu4a173382021-10-11 21:45:31 +08001148 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 {
1150 unsigned int extension_type;
1151 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001152 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001153
Jerry Yu4a173382021-10-11 21:45:31 +08001154 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001155 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1156 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1157 p += 4;
1158
Jerry Yu4a173382021-10-11 21:45:31 +08001159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001160 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001161
1162 switch( extension_type )
1163 {
XiaokangQianb851da82022-01-14 04:03:11 +00001164 case MBEDTLS_TLS_EXT_COOKIE:
1165
XiaokangQiand9e068e2022-01-18 06:23:32 +00001166 if( !is_hrr )
1167 {
XiaokangQian52da5582022-01-26 09:49:29 +00001168 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001169 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001170 }
1171
XiaokangQian43550bd2022-01-21 04:32:58 +00001172 ret = ssl_tls13_parse_cookie_ext( ssl,
1173 p, extension_data_end );
1174 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001175 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001176 MBEDTLS_SSL_DEBUG_RET( 1,
1177 "ssl_tls13_parse_cookie_ext",
1178 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001180 }
XiaokangQianb851da82022-01-14 04:03:11 +00001181 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001182
Jerry Yue1b9c292021-09-10 10:08:31 +08001183 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001184 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001185 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001186 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001187 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001188 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 break;
1190
1191 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001194
XiaokangQian52da5582022-01-26 09:49:29 +00001195 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001196 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001197
Jerry Yue1b9c292021-09-10 10:08:31 +08001198 case MBEDTLS_TLS_EXT_KEY_SHARE:
1199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001200 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1201 {
XiaokangQian52da5582022-01-26 09:49:29 +00001202 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001203 goto cleanup;
1204 }
1205
XiaokangQian53f20b72022-01-18 10:47:33 +00001206 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001207 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001208 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001209 else
XiaokangQianb851da82022-01-14 04:03:11 +00001210 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001211 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001212 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001213 {
1214 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001215 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001216 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001217 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001218 }
1219 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001220
1221 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001222 MBEDTLS_SSL_DEBUG_MSG(
1223 3,
1224 ( "unknown extension found: %u ( ignoring )",
1225 extension_type ) );
1226
XiaokangQian52da5582022-01-26 09:49:29 +00001227 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001228 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001229 }
1230
1231 p += extension_data_len;
1232 }
1233
XiaokangQiand59be772022-01-24 10:12:51 +00001234cleanup:
1235
XiaokangQian52da5582022-01-26 09:49:29 +00001236 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001237 {
1238 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1239 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1240 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1241 }
XiaokangQian52da5582022-01-26 09:49:29 +00001242 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001243 {
1244 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1245 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1246 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1247 }
1248 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001249}
1250
XiaokangQian355e09a2022-01-20 11:14:50 +00001251static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001252{
Jerry Yub85277e2021-10-13 13:36:05 +08001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001254 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001255 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001256 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001257
Jerry Yub85277e2021-10-13 13:36:05 +08001258 /* Determine the key exchange mode:
1259 * 1) If both the pre_shared_key and key_share extensions were received
1260 * then the key exchange mode is PSK with EPHEMERAL.
1261 * 2) If only the pre_shared_key extension was received then the key
1262 * exchange mode is PSK-only.
1263 * 3) If only the key_share extension was received then the key
1264 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001265 */
Jerry Yub85277e2021-10-13 13:36:05 +08001266 switch( handshake->extensions_present &
1267 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001268 {
Jerry Yu745bb612021-10-13 22:01:04 +08001269 /* Only the pre_shared_key extension was received */
1270 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001271 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001272 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001273
Jerry Yu745bb612021-10-13 22:01:04 +08001274 /* Only the key_share extension was received */
1275 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001276 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001277 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001278
Jerry Yu745bb612021-10-13 22:01:04 +08001279 /* Both the pre_shared_key and key_share extensions were received */
1280 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001281 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001282 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001283
Jerry Yu745bb612021-10-13 22:01:04 +08001284 /* Neither pre_shared_key nor key_share extension was received */
1285 default:
1286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1287 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1288 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001289 }
1290
1291 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1292 *
1293 * TODO: We don't have to do this in case we offered 0-RTT and the
1294 * server accepted it. In this case, we could skip generating
1295 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001296 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001297 if( ret != 0 )
1298 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001300 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001301 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001302 }
1303
1304 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001305 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001306 if( ret != 0 )
1307 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001309 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001310 }
1311
1312 /* Next evolution in key schedule: Establish handshake secret and
1313 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001314 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001315 if( ret != 0 )
1316 {
Jerry Yuc068b662021-10-11 22:30:19 +08001317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1318 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001319 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001320 }
1321
Jerry Yub85277e2021-10-13 13:36:05 +08001322 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001323 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001324 {
1325 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1326 goto cleanup;
1327 }
Jerry Yu0b177842021-09-05 19:41:30 +08001328
1329 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1330 ssl->conf->endpoint,
1331 ssl->session_negotiate->ciphersuite,
1332 &traffic_keys,
1333 ssl );
1334 if( ret != 0 )
1335 {
1336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001337 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001338 }
1339
Jerry Yub85277e2021-10-13 13:36:05 +08001340 handshake->transform_handshake = transform_handshake;
1341 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001342
1343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1344 ssl->session_in = ssl->session_negotiate;
1345
1346 /*
1347 * State machine update
1348 */
1349 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1350
Jerry Yu4a173382021-10-11 21:45:31 +08001351cleanup:
1352
Jerry Yu0b177842021-09-05 19:41:30 +08001353 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001354 if( ret != 0 )
1355 {
Jerry Yub85277e2021-10-13 13:36:05 +08001356 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001357
Jerry Yu4a173382021-10-11 21:45:31 +08001358 MBEDTLS_SSL_PEND_FATAL_ALERT(
1359 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1360 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1361 }
1362 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001363}
1364
XiaokangQian355e09a2022-01-20 11:14:50 +00001365static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001366{
1367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1368
XiaokangQian647719a2021-12-07 09:16:29 +00001369#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1370 /* If not offering early data, the client sends a dummy CCS record
1371 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001372 * its second ClientHello or before its encrypted handshake flight.
1373 */
XiaokangQian647719a2021-12-07 09:16:29 +00001374 mbedtls_ssl_handshake_set_state( ssl,
1375 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1376#else
1377 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1378#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1379
XiaokangQian78b1fa72022-01-19 06:56:30 +00001380 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001381
XiaokangQian78b1fa72022-01-19 06:56:30 +00001382 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001383 * We are going to re-generate a shared secret corresponding to the group
1384 * selected by the server, which is different from the group for which we
1385 * generated a shared secret in the first client hello.
1386 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001387 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001388 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001389 if( ret != 0 )
1390 return( ret );
1391
1392 return( 0 );
1393}
1394
Jerry Yue1b9c292021-09-10 10:08:31 +08001395/*
Jerry Yu4a173382021-10-11 21:45:31 +08001396 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001397 * Handler for MBEDTLS_SSL_SERVER_HELLO
1398 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001399static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001400{
Jerry Yu4a173382021-10-11 21:45:31 +08001401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001402 unsigned char *buf = NULL;
1403 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001404 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001405
1406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1407
1408 /* Coordination step
1409 * - Fetch record
1410 * - Make sure it's either a ServerHello or a HRR.
1411 * - Switch processing routine in case of HRR
1412 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001413 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1414
XiaokangQian16acd4b2022-01-14 07:35:47 +00001415 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001416 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001417 goto cleanup;
1418 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001419 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001420
Ronald Cron9f0fba32022-02-10 16:45:15 +01001421 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1422 {
1423 ret = 0;
1424 goto cleanup;
1425 }
1426
XiaokangQianb851da82022-01-14 04:03:11 +00001427 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001428 buf + buf_len,
1429 is_hrr ) );
1430 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001431 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1432
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001433 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1434 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001435
XiaokangQiand9e068e2022-01-18 06:23:32 +00001436 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001437 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001438 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001439 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001440
1441cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1443 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001445}
1446
1447/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001448 *
1449 * EncryptedExtensions message
1450 *
1451 * The EncryptedExtensions message contains any extensions which
1452 * should be protected, i.e., any which are not needed to establish
1453 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001454 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001455
1456/*
1457 * Overview
1458 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001459
1460/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001461static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001462
XiaokangQian97799ac2021-10-11 10:05:54 +00001463static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1464 const unsigned char *buf,
1465 const unsigned char *end );
1466static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467
1468/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001469 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001471static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001472{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001473 int ret;
1474 unsigned char *buf;
1475 size_t buf_len;
1476
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1478
Xiaofei Bai746f9482021-11-12 08:53:56 +00001479 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001480 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001481 &buf, &buf_len ) );
1482
1483 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001484 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001485 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001486
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001487 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1488 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001489
XiaokangQian97799ac2021-10-11 10:05:54 +00001490 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001491
1492cleanup:
1493
1494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1495 return( ret );
1496
1497}
1498
XiaokangQian08da26c2021-10-09 10:12:11 +00001499/* Parse EncryptedExtensions message
1500 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001501 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001502 * } EncryptedExtensions;
1503 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001504static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1505 const unsigned char *buf,
1506 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001507{
1508 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001509 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001510 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001511 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001512
XiaokangQian08da26c2021-10-09 10:12:11 +00001513 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001514 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001515 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001516
XiaokangQian97799ac2021-10-11 10:05:54 +00001517 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001518 extensions_end = p + extensions_len;
1519 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001520
XiaokangQian8db25ff2021-10-13 05:56:18 +00001521 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001522 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001523 unsigned int extension_type;
1524 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001525
XiaokangQian08da26c2021-10-09 10:12:11 +00001526 /*
1527 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001528 * ExtensionType extension_type; (2 bytes)
1529 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001530 * } Extension;
1531 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001532 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001533 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1534 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1535 p += 4;
1536
XiaokangQian8db25ff2021-10-13 05:56:18 +00001537 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001538
XiaokangQian97799ac2021-10-11 10:05:54 +00001539 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001540 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001541 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001542 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001543 switch( extension_type )
1544 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001545
XiaokangQian08da26c2021-10-09 10:12:11 +00001546 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1548 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001549
lhuang0486cacac2022-01-21 07:34:27 -08001550#if defined(MBEDTLS_SSL_ALPN)
1551 case MBEDTLS_TLS_EXT_ALPN:
1552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1553
1554 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1555 {
1556 return( ret );
1557 }
1558
1559 break;
1560#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001561 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001562 MBEDTLS_SSL_DEBUG_MSG(
1563 3, ( "unsupported extension found: %u ", extension_type) );
1564 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001565 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001566 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1567 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001568 }
1569
XiaokangQian08da26c2021-10-09 10:12:11 +00001570 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001571 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001572
XiaokangQian97799ac2021-10-11 10:05:54 +00001573 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001574 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001575 {
1576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001577 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001578 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001579 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001580 }
1581
1582 return( ret );
1583}
1584
XiaokangQian97799ac2021-10-11 10:05:54 +00001585static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001586{
Jerry Yua93ac112021-10-27 16:31:48 +08001587#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001588 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001589 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1590 else
Jerry Yud2674312021-10-29 10:08:19 +08001591 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001592#else
1593 ((void) ssl);
1594 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1595#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001596 return( 0 );
1597}
1598
Jerry Yua93ac112021-10-27 16:31:48 +08001599#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001600/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001601 *
1602 * STATE HANDLING: CertificateRequest
1603 *
Jerry Yud2674312021-10-29 10:08:19 +08001604 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001605#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1606#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001607/* Coordination:
1608 * Deals with the ambiguity of not knowing if a CertificateRequest
1609 * will be sent. Returns a negative code on failure, or
1610 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1611 * - SSL_CERTIFICATE_REQUEST_SKIP
1612 * indicating if a Certificate Request is expected or not.
1613 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001614static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001615{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001617
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001618 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001619 {
1620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1621 return( SSL_CERTIFICATE_REQUEST_SKIP );
1622 }
1623
1624 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001625 {
1626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1627 return( ret );
1628 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001629 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001630
1631 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1632 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1633 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001634 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001635 }
1636
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001637 return( SSL_CERTIFICATE_REQUEST_SKIP );
1638}
1639
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640/*
1641 * ssl_tls13_parse_certificate_request()
1642 * Parse certificate request
1643 * struct {
1644 * opaque certificate_request_context<0..2^8-1>;
1645 * Extension extensions<2..2^16-1>;
1646 * } CertificateRequest;
1647 */
1648static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1649 const unsigned char *buf,
1650 const unsigned char *end )
1651{
1652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1653 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001655 size_t extensions_len = 0;
1656 const unsigned char *extensions_end;
1657 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001658
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001659 /* ...
1660 * opaque certificate_request_context<0..2^8-1>
1661 * ...
1662 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001663 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1664 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001665 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001667 if( certificate_request_context_len > 0 )
1668 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001669 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001670 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1671 p, certificate_request_context_len );
1672
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001673 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001674 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001675 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001676 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677 {
1678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1679 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1680 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001681 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001682 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001683 p += certificate_request_context_len;
1684 }
1685
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001686 /* ...
1687 * Extension extensions<2..2^16-1>;
1688 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001689 */
1690 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1691 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1692 p += 2;
1693
1694 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1695 extensions_end = p + extensions_len;
1696
1697 while( p < extensions_end )
1698 {
1699 unsigned int extension_type;
1700 size_t extension_data_len;
1701
1702 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1703 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1704 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1705 p += 4;
1706
1707 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1708
1709 switch( extension_type )
1710 {
1711 case MBEDTLS_TLS_EXT_SIG_ALG:
1712 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001713 ( "found signature algorithms extension" ) );
1714 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001715 p + extension_data_len );
1716 if( ret != 0 )
1717 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001718 if( ! sig_alg_ext_found )
1719 sig_alg_ext_found = 1;
1720 else
1721 {
1722 MBEDTLS_SSL_DEBUG_MSG( 3,
1723 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001724 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001725 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 break;
1727
1728 default:
1729 MBEDTLS_SSL_DEBUG_MSG(
1730 3,
1731 ( "unknown extension found: %u ( ignoring )",
1732 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001733 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001734 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001735 p += extension_data_len;
1736 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001737 /* Check that we consumed all the message. */
1738 if( p != end )
1739 {
1740 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001741 ( "CertificateRequest misaligned" ) );
1742 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001743 }
1744 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001745 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001746 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001747 MBEDTLS_SSL_DEBUG_MSG( 3,
1748 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001749 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001750 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001751
Jerry Yu7840f812022-01-29 10:26:51 +08001752 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001753 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001754
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001755decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001756 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1757 MBEDTLS_ERR_SSL_DECODE_ERROR );
1758 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001759}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001760
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001761/*
1762 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1763 */
1764static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001765{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001767
1768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1769
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001770 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1771
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001772 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1773 {
1774 unsigned char *buf;
1775 size_t buf_len;
1776
1777 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1778 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1779 &buf, &buf_len ) );
1780
1781 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1782 buf, buf + buf_len ) );
1783
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001784 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1785 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001786 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001787 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001788 {
1789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001790 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001791 }
1792 else
1793 {
1794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001795 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1796 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797 }
1798
1799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001800 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001801
Jerry Yud2674312021-10-29 10:08:19 +08001802 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1803
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001804cleanup:
1805
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1807 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001808}
1809
1810/*
Jerry Yu687101b2021-09-14 16:03:56 +08001811 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1812 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001813static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001814{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001815 int ret;
1816
1817 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001818 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001819 return( ret );
1820
Jerry Yu687101b2021-09-14 16:03:56 +08001821 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1822 return( 0 );
1823}
1824
1825/*
1826 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1827 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001828static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001829{
Jerry Yu30b071c2021-09-12 20:16:03 +08001830 int ret;
1831
1832 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1833 if( ret != 0 )
1834 return( ret );
1835
Jerry Yu687101b2021-09-14 16:03:56 +08001836 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1837 return( 0 );
1838}
Jerry Yua93ac112021-10-27 16:31:48 +08001839#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001840
Jerry Yu687101b2021-09-14 16:03:56 +08001841/*
1842 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1843 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001844static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001845{
XiaokangQianac0385c2021-11-03 06:40:11 +00001846 int ret;
1847
XiaokangQianc5c39d52021-11-09 11:55:10 +00001848 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001849 if( ret != 0 )
1850 return( ret );
1851
Ronald Cron49ad6192021-11-24 16:25:31 +01001852#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1853 mbedtls_ssl_handshake_set_state(
1854 ssl,
1855 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1856#else
Jerry Yuca133a32022-02-15 14:22:05 +08001857 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001858#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001859
XiaokangQianac0385c2021-11-03 06:40:11 +00001860 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001861}
1862
1863/*
Jerry Yu566c7812022-01-26 15:41:22 +08001864 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1865 */
1866static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1867{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001868 int non_empty_certificate_msg = 0;
1869
Jerry Yu5cc35062022-01-28 16:16:08 +08001870 MBEDTLS_SSL_DEBUG_MSG( 1,
1871 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001872 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001873
Ronald Cron9df7c802022-03-08 18:38:54 +01001874#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001875 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001876 {
1877 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1878 if( ret != 0 )
1879 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001880
Ronald Cron7a94aca2022-03-09 07:44:27 +01001881 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1882 non_empty_certificate_msg = 1;
1883 }
1884 else
1885 {
1886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1887 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001888#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001889
Ronald Cron7a94aca2022-03-09 07:44:27 +01001890 if( non_empty_certificate_msg )
1891 {
1892 mbedtls_ssl_handshake_set_state( ssl,
1893 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1894 }
1895 else
1896 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1897
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001898 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001899}
1900
Ronald Cron9df7c802022-03-08 18:38:54 +01001901#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001902/*
1903 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1904 */
1905static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1906{
Ronald Crona8b38872022-03-09 07:59:25 +01001907 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1908
1909 if( ret == 0 )
1910 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1911
1912 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001913}
Jerry Yu90f152d2022-01-29 22:12:42 +08001914#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001915
1916/*
Jerry Yu687101b2021-09-14 16:03:56 +08001917 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1918 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001919static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001920{
XiaokangQian0fa66432021-11-15 03:33:57 +00001921 int ret;
1922
1923 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1924 if( ret != 0 )
1925 return( ret );
1926
1927 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1928 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001929}
1930
1931/*
1932 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1933 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001934static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001935{
Jerry Yu378254d2021-10-30 21:44:47 +08001936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001937 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001938 return( 0 );
1939}
1940
1941/*
1942 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1943 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001944static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001945{
Jerry Yu378254d2021-10-30 21:44:47 +08001946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1947 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1948
1949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1950 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1951
1952 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1953
1954 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1955 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001956}
1957
Jerry Yu92c6b402021-08-27 16:59:09 +08001958int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001959{
Jerry Yu92c6b402021-08-27 16:59:09 +08001960 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001961
Jerry Yu92c6b402021-08-27 16:59:09 +08001962 switch( ssl->state )
1963 {
1964 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001965 * ssl->state is initialized as HELLO_REQUEST. It is the same
1966 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001967 */
1968 case MBEDTLS_SSL_HELLO_REQUEST:
1969 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001970 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001971 break;
1972
1973 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001974 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001975 break;
1976
1977 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001978 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001979 break;
1980
Jerry Yua93ac112021-10-27 16:31:48 +08001981#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001982 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1983 ret = ssl_tls13_process_certificate_request( ssl );
1984 break;
1985
Jerry Yu687101b2021-09-14 16:03:56 +08001986 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001987 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001988 break;
1989
1990 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001991 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001992 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001993#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001994
1995 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001996 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001997 break;
1998
Jerry Yu566c7812022-01-26 15:41:22 +08001999 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2000 ret = ssl_tls13_write_client_certificate( ssl );
2001 break;
2002
Ronald Cron9df7c802022-03-08 18:38:54 +01002003#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002004 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2005 ret = ssl_tls13_write_client_certificate_verify( ssl );
2006 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002007#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002008
Jerry Yu687101b2021-09-14 16:03:56 +08002009 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002010 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002011 break;
2012
2013 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002014 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002015 break;
2016
2017 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002018 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002019 break;
2020
Ronald Cron49ad6192021-11-24 16:25:31 +01002021 /*
2022 * Injection of dummy-CCS's for middlebox compatibility
2023 */
2024#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002025 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002026 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2027 if( ret == 0 )
2028 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2029 break;
2030
2031 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2032 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2033 if( ret == 0 )
2034 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002035 break;
2036#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2037
Jerry Yu92c6b402021-08-27 16:59:09 +08002038 default:
2039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2041 }
2042
2043 return( ret );
2044}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002045
Jerry Yufb4b6472022-01-27 15:03:26 +08002046#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002047
Jerry Yufb4b6472022-01-27 15:03:26 +08002048