blob: 7d1c826eb1ab757ee131e10e20868cf7bc55815f [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"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010035#include "ssl_debug_helpers.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;
52
Xiaofei Baid25fab62021-12-02 06:36:27 +000053 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Jerry Yu159c5a02021-08-31 12:51:25 +080055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu388bd0d2021-09-15 18:41:02 +080057 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080058 * - extension_type (2 bytes)
59 * - extension_data_length (2 bytes)
60 * - versions_length (1 byte )
61 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080062 */
Jerry Yu92c6b402021-08-27 16:59:09 +080063 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
64
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080065 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080067
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080068 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080069 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080073 *p++ = 0x2;
74
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080076 *
Jerry Yu0c63af62021-09-02 12:59:12 +080077 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080080 */
Jerry Yueecfbf02021-08-30 18:32:07 +080081 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
82 ssl->conf->max_minor_ver,
83 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080086 ssl->conf->max_major_ver,
87 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080088
Xiaofei Baid25fab62021-12-02 06:36:27 +000089 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Jerry Yub85277e2021-10-13 13:36:05 +0800100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
101 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800102 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
111 return( 0 );
112}
113
lhuang0486cacac2022-01-21 07:34:27 -0800114#if defined(MBEDTLS_SSL_ALPN)
115/*
Ronald Cron60ff7942022-03-09 13:56:48 +0100116 * ssl_tls13_write_alpn_ext()
117 *
118 * Structure of the application_layer_protocol_negotiation extension in
119 * ClientHello:
lhuang0486cacac2022-01-21 07:34:27 -0800120 *
121 * opaque ProtocolName<1..2^8-1>;
122 *
123 * struct {
124 * ProtocolName protocol_name_list<2..2^16-1>
125 * } ProtocolNameList;
126 *
127 */
128static int ssl_tls13_write_alpn_ext( mbedtls_ssl_context *ssl,
Ronald Cron60ff7942022-03-09 13:56:48 +0100129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *out_len )
lhuang0486cacac2022-01-21 07:34:27 -0800132{
133 unsigned char *p = buf;
lhuang0486cacac2022-01-21 07:34:27 -0800134
Ronald Cron60ff7942022-03-09 13:56:48 +0100135 *out_len = 0;
lhuang0486cacac2022-01-21 07:34:27 -0800136
137 if( ssl->conf->alpn_list == NULL )
138 return( 0 );
139
140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
141
lhuang0486cacac2022-01-21 07:34:27 -0800142
Ronald Cron13d8ea12022-03-09 10:48:18 +0100143 /* Check we have enough space for the extension type (2 bytes), the
144 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
145 */
146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
lhuang0486cacac2022-01-21 07:34:27 -0800147 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
Ronald Cron13d8ea12022-03-09 10:48:18 +0100148 /* Skip writing extension and list length for now */
149 p += 6;
lhuang0486cacac2022-01-21 07:34:27 -0800150
151 /*
152 * opaque ProtocolName<1..2^8-1>;
153 *
154 * struct {
155 * ProtocolName protocol_name_list<2..2^16-1>
156 * } ProtocolNameList;
157 */
Ronald Cron60ff7942022-03-09 13:56:48 +0100158 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
lhuang0486cacac2022-01-21 07:34:27 -0800159 {
160 /*
161 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
162 * protocol names is less than 255.
163 */
Ronald Cron60ff7942022-03-09 13:56:48 +0100164 size_t protocol_name_len = strlen( *cur );
165
Ronald Cron13d8ea12022-03-09 10:48:18 +0100166 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
167 *p++ = (unsigned char)protocol_name_len;
168 memcpy( p, *cur, protocol_name_len );
169 p += protocol_name_len;
lhuang0486cacac2022-01-21 07:34:27 -0800170 }
171
Ronald Cron60ff7942022-03-09 13:56:48 +0100172 *out_len = p - buf;
lhuang0486cacac2022-01-21 07:34:27 -0800173
Ronald Cron60ff7942022-03-09 13:56:48 +0100174 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
175 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
lhuang0486cacac2022-01-21 07:34:27 -0800176
Ronald Cron60ff7942022-03-09 13:56:48 +0100177 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
178 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
lhuang0486cacac2022-01-21 07:34:27 -0800179
180 return( 0 );
181}
182
183static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
184 const unsigned char *buf, size_t len )
185{
186 size_t list_len, name_len;
187 const unsigned char *p = buf;
188 const unsigned char *end = buf + len;
189
190 /* If we didn't send it, the server shouldn't send it */
191 if( ssl->conf->alpn_list == NULL )
192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
193
194 /*
195 * opaque ProtocolName<1..2^8-1>;
196 *
197 * struct {
198 * ProtocolName protocol_name_list<2..2^16-1>
199 * } ProtocolNameList;
200 *
201 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
202 */
203
204 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
205 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
206
207 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
208 p += 2;
209 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
210
211 name_len = *p++;
212 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
213
214 /* Check that the server chosen protocol was in our list and save it */
215 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
216 {
217 if( name_len == strlen( *alpn ) &&
218 memcmp( buf + 3, *alpn, name_len ) == 0 )
219 {
220 ssl->alpn_chosen = *alpn;
221 return( 0 );
222 }
223 }
224
225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
226}
227#endif /* MBEDTLS_SSL_ALPN */
228
Jerry Yubc20bdd2021-08-24 15:59:48 +0800229#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
230
XiaokangQian16acd4b2022-01-14 07:35:47 +0000231static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000232{
233 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100234
XiaokangQian647719a2021-12-07 09:16:29 +0000235 if( group_id == 0 )
236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
237
XiaokangQian355e09a2022-01-20 11:14:50 +0000238#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000239 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000240 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
243
244 /* Destroy generated private key. */
245 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
246 if( status != PSA_SUCCESS )
247 {
248 ret = psa_ssl_status_to_mbedtls( status );
249 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
250 return( ret );
251 }
252
253 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000254 return( 0 );
255 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000256 else
257#endif /* MBEDTLS_ECDH_C */
258 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000259 {
260 /* Do something */
261 }
262
263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
264}
265
266/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267 * Functions for writing key_share extension.
268 */
269#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800270static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800271 mbedtls_ssl_context *ssl,
272 uint16_t named_group,
273 unsigned char *buf,
274 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000275 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800276{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100277 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
278 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
279 psa_key_attributes_t key_attributes;
280 size_t own_pubkey_len;
281 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
282 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100286 /* Convert EC group to PSA key type. */
287 if( ( handshake->ecdh_psa_type =
288 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
289 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800290
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100291 if( ecdh_bits > 0xffff )
292 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
293 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100295 key_attributes = psa_key_attributes_init();
296 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
297 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
298 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
299 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100300
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100301 /* Generate ECDH private key. */
302 status = psa_generate_key( &key_attributes,
303 &handshake->ecdh_psa_privkey );
304 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100306 ret = psa_ssl_status_to_mbedtls( status );
307 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100309
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 }
311
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100312 /* Export the public part of the ECDH private key from PSA. */
313 status = psa_export_public_key( handshake->ecdh_psa_privkey,
314 buf, (size_t)( end - buf ),
315 &own_pubkey_len );
316 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800317 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100318 ret = psa_ssl_status_to_mbedtls( status );
319 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100321
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 }
323
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100324 if( own_pubkey_len > (size_t)( end - buf ) )
325 {
326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
327 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
328 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100329
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100330 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100331
Jerry Yu75336352021-09-01 15:59:36 +0800332 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800333}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800334#endif /* MBEDTLS_ECDH_C */
335
Jerry Yub60e3cf2021-09-08 16:41:02 +0800336static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
337 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338{
339 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
340
Jerry Yu56fc07f2021-09-01 17:48:49 +0800341
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100343 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800344 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100345 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800346 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
347
Brett Warren14efd332021-10-06 09:32:11 +0100348 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800349 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000350 const mbedtls_ecp_curve_info *curve_info;
351 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
352 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100353 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354 {
Brett Warren14efd332021-10-06 09:32:11 +0100355 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356 return( 0 );
357 }
358 }
359#else
360 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800361 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362#endif /* MBEDTLS_ECDH_C */
363
364 /*
365 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800366 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800367 */
368
369 return( ret );
370}
371
372/*
373 * ssl_tls13_write_key_share_ext
374 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800375 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800376 *
377 * struct {
378 * NamedGroup group;
379 * opaque key_exchange<1..2^16-1>;
380 * } KeyShareEntry;
381 * struct {
382 * KeyShareEntry client_shares<0..2^16-1>;
383 * } KeyShareClientHello;
384 */
385static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
386 unsigned char *buf,
387 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000388 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800389{
390 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000391 unsigned char *client_shares; /* Start of client_shares */
392 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800393 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800394 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
395
Xiaofei Baid25fab62021-12-02 06:36:27 +0000396 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397
Jerry Yub60e3cf2021-09-08 16:41:02 +0800398 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800399 * - extension_type (2 bytes)
400 * - extension_data_length (2 bytes)
401 * - client_shares_length (2 bytes)
402 */
403 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
404 p += 6;
405
406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
407
408 /* HRR could already have requested something else. */
409 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800410 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
411 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800413 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414 &group_id ) );
415 }
416
417 /*
418 * Dispatch to type-specific key generation function.
419 *
420 * So far, we're only supporting ECDHE. With the introduction
421 * of PQC KEMs, we'll want to have multiple branches, one per
422 * type of KEM, and dispatch to the corresponding crypto. And
423 * only one key share entry is allowed.
424 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000425 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800426#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800427 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800428 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800429 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000430 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800431 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100432 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800433
434 /* Check there is space for header of KeyShareEntry
435 * - group (2 bytes)
436 * - key_exchange_length (2 bytes)
437 */
438 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
439 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100440 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800441 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800442 p += key_exchange_len;
443 if( ret != 0 )
444 return( ret );
445
446 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000447 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800448 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000449 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800450 }
451 else
452#endif /* MBEDTLS_ECDH_C */
453 if( 0 /* other KEMs? */ )
454 {
455 /* Do something */
456 }
457 else
458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
459
Jerry Yub60e3cf2021-09-08 16:41:02 +0800460 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000461 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800462 if( client_shares_len == 0)
463 {
464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
465 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800466 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800467 /* Write extension_type */
468 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
469 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800470 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800471 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800472 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800473
474 /* Update offered_group_id field */
475 ssl->handshake->offered_group_id = group_id;
476
477 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000478 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800479
Xiaofei Baid25fab62021-12-02 06:36:27 +0000480 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800481
482 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
483
484cleanup:
485
486 return( ret );
487}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800488
Jerry Yue1b9c292021-09-10 10:08:31 +0800489#if defined(MBEDTLS_ECDH_C)
490
Jerry Yuc068b662021-10-11 22:30:19 +0800491static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
492 const unsigned char *buf,
493 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800494{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100495 uint8_t *p = (uint8_t*)buf;
496 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800497
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100498 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
499 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
500 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800501
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100502 /* Check if key size is consistent with given buffer length. */
503 if ( peerkey_len > ( buf_len - 2 ) )
504 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800505
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100506 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100507 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100508 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800509
510 return( 0 );
511}
Jerry Yu4a173382021-10-11 21:45:31 +0800512#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800513
XiaokangQiand59be772022-01-24 10:12:51 +0000514/*
515 * ssl_tls13_parse_hrr_key_share_ext()
516 * Parse key_share extension in Hello Retry Request
517 *
518 * struct {
519 * NamedGroup selected_group;
520 * } KeyShareHelloRetryRequest;
521 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000522static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000523 const unsigned char *buf,
524 const unsigned char *end )
525{
XiaokangQianb851da82022-01-14 04:03:11 +0000526 const mbedtls_ecp_curve_info *curve_info = NULL;
527 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000528 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000529 int found = 0;
530
531 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
532 if( group_list == NULL )
533 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
534
535 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
536
537 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000538 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000539 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000541
542 /* Upon receipt of this extension in a HelloRetryRequest, the client
543 * MUST first verify that the selected_group field corresponds to a
544 * group which was provided in the "supported_groups" extension in the
545 * original ClientHello.
546 * The supported_group was based on the info in ssl->conf->group_list.
547 *
548 * If the server provided a key share that was not sent in the ClientHello
549 * then the client MUST abort the handshake with an "illegal_parameter" alert.
550 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000551 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000552 {
553 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000554 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000555 continue;
556
557 /* We found a match */
558 found = 1;
559 break;
560 }
561
562 /* Client MUST verify that the selected_group field does not
563 * correspond to a group which was provided in the "key_share"
564 * extension in the original ClientHello. If the server sent an
565 * HRR message with a key share already provided in the
566 * ClientHello then the client MUST abort the handshake with
567 * an "illegal_parameter" alert.
568 */
XiaokangQiand59be772022-01-24 10:12:51 +0000569 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000570 {
571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
572 MBEDTLS_SSL_PEND_FATAL_ALERT(
573 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
574 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
575 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
576 }
577
578 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000579 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000580
581 return( 0 );
582}
583
Jerry Yue1b9c292021-09-10 10:08:31 +0800584/*
Jerry Yub85277e2021-10-13 13:36:05 +0800585 * ssl_tls13_parse_key_share_ext()
586 * Parse key_share extension in Server Hello
587 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800588 * struct {
589 * KeyShareEntry server_share;
590 * } KeyShareServerHello;
591 * struct {
592 * NamedGroup group;
593 * opaque key_exchange<1..2^16-1>;
594 * } KeyShareEntry;
595 */
Jerry Yu4a173382021-10-11 21:45:31 +0800596static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800597 const unsigned char *buf,
598 const unsigned char *end )
599{
Jerry Yub85277e2021-10-13 13:36:05 +0800600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800601 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800602 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800603
Jerry Yu4a173382021-10-11 21:45:31 +0800604 /* ...
605 * NamedGroup group; (2 bytes)
606 * ...
607 */
608 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
609 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800610 p += 2;
611
Jerry Yu4a173382021-10-11 21:45:31 +0800612 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800613 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800614 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800615 {
616 MBEDTLS_SSL_DEBUG_MSG( 1,
617 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800618 (unsigned) offered_group, (unsigned) group ) );
619 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
620 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
621 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800622 }
623
624#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800625 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800626 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100627 const mbedtls_ecp_curve_info *curve_info =
628 mbedtls_ecp_curve_info_from_tls_id( group );
629 if( curve_info == NULL )
630 {
631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
633 }
634
635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
636
Jerry Yuc068b662021-10-11 22:30:19 +0800637 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800638 if( ret != 0 )
639 return( ret );
640 }
Jerry Yub85277e2021-10-13 13:36:05 +0800641 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800642#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800643 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800644 {
645 /* Do something */
646 }
647 else
648 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
649
650 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
651 return( ret );
652}
653
Jerry Yubc20bdd2021-08-24 15:59:48 +0800654#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
655
XiaokangQiand59be772022-01-24 10:12:51 +0000656/*
657 * ssl_tls13_parse_cookie_ext()
658 * Parse cookie extension in Hello Retry Request
659 *
660 * struct {
661 * opaque cookie<1..2^16-1>;
662 * } Cookie;
663 *
664 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
665 * extension to the client (this is an exception to the usual rule that
666 * the only extensions that may be sent are those that appear in the
667 * ClientHello). When sending the new ClientHello, the client MUST copy
668 * the contents of the extension received in the HelloRetryRequest into
669 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
670 * cookies in their initial ClientHello in subsequent connections.
671 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000672static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
673 const unsigned char *buf,
674 const unsigned char *end )
675{
XiaokangQian25c9c902022-02-08 10:49:53 +0000676 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000677 const unsigned char *p = buf;
678 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
679
680 /* Retrieve length field of cookie */
681 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
682 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
683 p += 2;
684
685 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
686 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
687
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000688 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000689 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000690 handshake->cookie = mbedtls_calloc( 1, cookie_len );
691 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000692 {
693 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000694 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000695 cookie_len ) );
696 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
697 }
698
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000699 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000700 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000701
702 return( 0 );
703}
XiaokangQian43550bd2022-01-21 04:32:58 +0000704
XiaokangQian0b64eed2022-01-27 10:36:51 +0000705static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000706 unsigned char *buf,
707 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000708 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000709{
710 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000711 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000712 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000713
XiaokangQianc02768a2022-02-10 07:31:25 +0000714 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000715 {
716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
717 return( 0 );
718 }
719
720 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000721 handshake->cookie,
722 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000723
XiaokangQianc02768a2022-02-10 07:31:25 +0000724 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000725
726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
727
XiaokangQian0b64eed2022-01-27 10:36:51 +0000728 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000729 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
730 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000731 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000732
733 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000734 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000735
XiaokangQianc02768a2022-02-10 07:31:25 +0000736 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000737
738 return( 0 );
739}
740
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800741/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800742 * CipherSuite cipher_suites<2..2^16-2>;
743 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800744static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800745 mbedtls_ssl_context *ssl,
746 unsigned char *buf,
747 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000748 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800749{
Jerry Yufec982e2021-09-07 17:26:06 +0800750 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800751 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000752 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800753 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800754
Xiaofei Baid25fab62021-12-02 06:36:27 +0000755 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800756
757 /*
758 * Ciphersuite list
759 *
760 * This is a list of the symmetric cipher options supported by
761 * the client, specifically the record protection algorithm
762 * ( including secret key length ) and a hash to be used with
763 * HKDF, in descending order of client preference.
764 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800765 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800766
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800767 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800768 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
769 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800770
Jerry Yu0c63af62021-09-02 12:59:12 +0800771 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000772 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800773 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800774 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800775 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800776 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800777
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800778 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800779 if( ciphersuite_info == NULL )
780 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800781 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
782 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800783 continue;
784
785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800786 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800787 ciphersuite_info->name ) );
788
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800789 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800790 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
791 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
792 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800793 }
794
Jerry Yu0c63af62021-09-02 12:59:12 +0800795 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000796 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800797 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800798 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800799 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
800 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800801
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800802 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000803 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800804
Jerry Yu6a643102021-08-31 14:40:36 +0800805 return( 0 );
806}
807
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800808/*
809 * Structure of ClientHello message:
810 *
811 * struct {
812 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
813 * Random random;
814 * opaque legacy_session_id<0..32>;
815 * CipherSuite cipher_suites<2..2^16-2>;
816 * opaque legacy_compression_methods<1..2^8-1>;
817 * Extension extensions<8..2^16-1>;
818 * } ClientHello;
819 */
Jerry Yu08906d02021-08-31 11:05:27 +0800820static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800821 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800822 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000823 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800824{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800825
Jerry Yubc20bdd2021-08-24 15:59:48 +0800826 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000827 unsigned char *p_extensions_len; /* Pointer to extensions length */
828 size_t output_len; /* Length of buffer used by function */
829 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800830
Jerry Yubc20bdd2021-08-24 15:59:48 +0800831 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800832 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800833
Xiaofei Baid25fab62021-12-02 06:36:27 +0000834 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800835
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800836 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800837 ssl->major_ver = ssl->conf->min_major_ver;
838 ssl->minor_ver = ssl->conf->min_minor_ver;
839
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800840 /*
841 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800842 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800843 *
844 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800845 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800846 */
Jerry Yufec982e2021-09-07 17:26:06 +0800847 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800848 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800849 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800850
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800851 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800852 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
853 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800854 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800855 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
856 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800857
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800858 /*
859 * Write legacy_session_id
860 *
861 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
862 * which has been merged with pre-shared keys in this version. A client
863 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
864 * this field to that value. In compatibility mode, this field MUST be
865 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
866 * a new 32-byte value. This value need not be random but SHOULD be
867 * unpredictable to avoid implementations fixating on a specific value
868 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
869 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800870 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100871#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
872 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
873 *p++ = (unsigned char)ssl->session_negotiate->id_len;
874 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
875 p += ssl->session_negotiate->id_len;
876
877 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
878 ssl->session_negotiate->id_len );
879#else
Jerry Yubbe09522021-09-06 21:17:54 +0800880 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
881 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100882#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800883
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800884 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800885 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800886 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800887 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800888 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800889
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800890 /* Write legacy_compression_methods
891 *
892 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800893 * one byte set to zero, which corresponds to the 'null' compression
894 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800895 */
Jerry Yubbe09522021-09-06 21:17:54 +0800896 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
897 *p++ = 1;
898 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800899
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800900 /* Write extensions */
901
902 /* Keeping track of the included extensions */
903 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800904
905 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800906 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000907 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800908 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800909
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800910 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800911 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800912 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800913 */
Jerry Yubbe09522021-09-06 21:17:54 +0800914 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800915 if( ret != 0 )
916 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800917 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800918
lhuang0486cacac2022-01-21 07:34:27 -0800919#if defined(MBEDTLS_SSL_ALPN)
Ronald Crona0855a62022-03-09 10:04:54 +0100920 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
lhuang0486cacac2022-01-21 07:34:27 -0800921 if( ret != 0 )
922 return( ret );
923 p += output_len;
924#endif /* MBEDTLS_SSL_ALPN */
925
XiaokangQian233397e2022-02-07 08:32:16 +0000926 /* Echo the cookie if the server provided one in its preceding
927 * HelloRetryRequest message.
928 */
XiaokangQian0b64eed2022-01-27 10:36:51 +0000929 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &output_len );
930 if( ret != 0 )
931 return( ret );
932 p += output_len;
933
Jerry Yubc20bdd2021-08-24 15:59:48 +0800934#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800935
Jerry Yub925f212022-01-12 11:17:02 +0800936 /*
937 * Add the extensions related to (EC)DHE ephemeral key establishment only if
938 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800939 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800940 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
941 {
942 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
943 if( ret != 0 )
944 return( ret );
945 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800946
Jerry Yuf46b0162022-01-11 16:28:00 +0800947 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
948 if( ret != 0 )
949 return( ret );
950 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800951
Jerry Yuf017ee42022-01-12 15:49:48 +0800952 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800953 if( ret != 0 )
954 return( ret );
955 p += output_len;
956 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800957#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
958
Xiaofei Bai15a56812021-11-05 10:52:12 +0000959#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000960 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000961 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
962 if( ret != 0 )
963 return( ret );
964 p += output_len;
965#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
966
Jerry Yubc20bdd2021-08-24 15:59:48 +0800967 /* Add more extensions here */
968
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800969 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000970 extensions_len = p - p_extensions_len - 2;
971 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800973 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000974 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800975
Xiaofei Baid25fab62021-12-02 06:36:27 +0000976 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800977 return( 0 );
978}
979
Jerry Yu92c6b402021-08-27 16:59:09 +0800980static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
981{
982 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800983
Jerry Yu92c6b402021-08-27 16:59:09 +0800984 if( ssl->conf->f_rng == NULL )
985 {
986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
987 return( MBEDTLS_ERR_SSL_NO_RNG );
988 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800989
Jerry Yu92c6b402021-08-27 16:59:09 +0800990 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
991 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800992 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800993 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800994 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800995 return( ret );
996 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800997
Ronald Cron49ad6192021-11-24 16:25:31 +0100998#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
999 /*
1000 * Create a session identifier for the purpose of middlebox compatibility
1001 * only if one has not been created already.
1002 */
1003 if( ssl->session_negotiate->id_len == 0 )
1004 {
1005 /* Creating a session id with 32 byte length */
1006 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1007 ssl->session_negotiate->id, 32 ) ) != 0 )
1008 {
1009 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
1010 return( ret );
1011 }
1012 ssl->session_negotiate->id_len = 32;
1013 }
1014#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1015
Jerry Yu6f13f642021-08-26 17:18:15 +08001016 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001017}
1018
Jerry Yu92c6b402021-08-27 16:59:09 +08001019/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001020 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001021 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001022 */
1023static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001024{
Jerry Yu92c6b402021-08-27 16:59:09 +08001025 int ret = 0;
1026 unsigned char *buf;
1027 size_t buf_len, msg_len;
1028
1029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1030
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001031 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001032
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001033 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1034 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1035 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001036
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001037 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001038 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001039 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001040
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001041 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1042 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001043 msg_len );
1044 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001045
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001046 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1047 buf_len,
1048 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001049
Ronald Cron3addfa42022-02-08 16:10:25 +01001050 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1051
Jerry Yu92c6b402021-08-27 16:59:09 +08001052cleanup:
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1055 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001056}
1057
Jerry Yu687101b2021-09-14 16:03:56 +08001058/*
Jerry Yu4a173382021-10-11 21:45:31 +08001059 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001061/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001062 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1063 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001064 * to indicate which message is expected and to be parsed next.
1065 */
Jerry Yub85277e2021-10-13 13:36:05 +08001066#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1067#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1068static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1069 const unsigned char *buf,
1070 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001071{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001072 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1074 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1075 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1076 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1077
1078 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1079 *
Jerry Yu4a173382021-10-11 21:45:31 +08001080 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 * special value of the SHA-256 of "HelloRetryRequest".
1082 *
1083 * struct {
1084 * ProtocolVersion legacy_version = 0x0303;
1085 * Random random;
1086 * opaque legacy_session_id_echo<0..32>;
1087 * CipherSuite cipher_suite;
1088 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001089 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001090 * } ServerHello;
1091 *
1092 */
Jerry Yub85277e2021-10-13 13:36:05 +08001093 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001094
1095 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001096 {
Jerry Yub85277e2021-10-13 13:36:05 +08001097 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098 }
1099
Jerry Yub85277e2021-10-13 13:36:05 +08001100 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001101}
1102
Jerry Yu745bb612021-10-13 22:01:04 +08001103/* Fetch and preprocess
1104 * Returns a negative value on failure, and otherwise
1105 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1106 * - SSL_SERVER_HELLO_COORDINATE_HRR
1107 */
Jerry Yub85277e2021-10-13 13:36:05 +08001108static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1109 unsigned char **buf,
1110 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001111{
Jerry Yu4a173382021-10-11 21:45:31 +08001112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001113
XiaokangQian355e09a2022-01-20 11:14:50 +00001114 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1115 MBEDTLS_SSL_HS_SERVER_HELLO,
1116 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001117
Jerry Yub85277e2021-10-13 13:36:05 +08001118 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1119 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 {
Jerry Yu745bb612021-10-13 22:01:04 +08001121 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1123 break;
1124 case SSL_SERVER_HELLO_COORDINATE_HRR:
1125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001126 /* If a client receives a second
1127 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1128 * was itself in response to a HelloRetryRequest), it MUST abort the
1129 * handshake with an "unexpected_message" alert.
1130 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001131 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001132 {
1133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1134 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1135 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1136 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1137 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001138 /*
1139 * Clients must abort the handshake with an "illegal_parameter"
1140 * alert if the HelloRetryRequest would not result in any change
1141 * in the ClientHello.
1142 * In a PSK only key exchange that what we expect.
1143 */
1144 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1145 {
1146 MBEDTLS_SSL_DEBUG_MSG( 1,
1147 ( "Unexpected HRR in pure PSK key exchange." ) );
1148 MBEDTLS_SSL_PEND_FATAL_ALERT(
1149 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1150 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1151 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1152 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001153
XiaokangQiand9e068e2022-01-18 06:23:32 +00001154 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001155
Jerry Yu745bb612021-10-13 22:01:04 +08001156 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001157 }
1158
1159cleanup:
1160
1161 return( ret );
1162}
1163
Jerry Yu4a173382021-10-11 21:45:31 +08001164static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1165 const unsigned char **buf,
1166 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001167{
1168 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001169 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001170
Jerry Yude4fb2c2021-09-19 18:05:08 +08001171 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001172 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001173
Jerry Yu4a173382021-10-11 21:45:31 +08001174 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001175
1176 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001177 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1178 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1181 ssl->session_negotiate->id,
1182 ssl->session_negotiate->id_len );
1183 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001184 legacy_session_id_echo_len );
1185
1186 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1187 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1188
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1190 }
1191
Jerry Yu4a173382021-10-11 21:45:31 +08001192 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 *buf = p;
1194
1195 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001196 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001197 return( 0 );
1198}
1199
Jerry Yu4a173382021-10-11 21:45:31 +08001200static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1201 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001202{
Jerry Yu4a173382021-10-11 21:45:31 +08001203 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1204
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001206 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 {
Jerry Yu4a173382021-10-11 21:45:31 +08001208 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 {
1210 return( 1 );
1211 }
1212 }
1213 return( 0 );
1214}
1215
1216/* Parse ServerHello message and configure context
1217 *
1218 * struct {
1219 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1220 * Random random;
1221 * opaque legacy_session_id_echo<0..32>;
1222 * CipherSuite cipher_suite;
1223 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001224 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001225 * } ServerHello;
1226 */
Jerry Yuc068b662021-10-11 22:30:19 +08001227static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1228 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001229 const unsigned char *end,
1230 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001231{
Jerry Yub85277e2021-10-13 13:36:05 +08001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001234 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001235 size_t extensions_len;
1236 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001237 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001238 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001239 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001240 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001241
1242 /*
1243 * Check there is space for minimal fields
1244 *
1245 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001246 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 * - legacy_session_id_echo ( 1 byte ), minimum size
1248 * - cipher_suite ( 2 bytes)
1249 * - legacy_compression_method ( 1 byte )
1250 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001251 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001252
1253 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001254 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1255
Jerry Yu4a173382021-10-11 21:45:31 +08001256 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001257 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001258 * ...
1259 * with ProtocolVersion defined as:
1260 * uint16 ProtocolVersion;
1261 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001262 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1263 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1264 {
1265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1266 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1267 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001268 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1269 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001270 }
1271 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001272
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001273 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001274 * Random random;
1275 * ...
1276 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001277 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001278 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001279 if( !is_hrr )
1280 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001281 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001282 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1283 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1284 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1285 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001286 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001287
Jerry Yu4a173382021-10-11 21:45:31 +08001288 /* ...
1289 * opaque legacy_session_id_echo<0..32>;
1290 * ...
1291 */
1292 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001293 {
XiaokangQian52da5582022-01-26 09:49:29 +00001294 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001295 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001296 }
1297
Jerry Yu4a173382021-10-11 21:45:31 +08001298 /* ...
1299 * CipherSuite cipher_suite;
1300 * ...
1301 * with CipherSuite defined as:
1302 * uint8 CipherSuite[2];
1303 */
1304 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001305 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1306 p += 2;
1307
Jerry Yu4a173382021-10-11 21:45:31 +08001308
XiaokangQian355e09a2022-01-20 11:14:50 +00001309 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001310 /*
1311 * Check whether this ciphersuite is supported and offered.
1312 * Via the force_ciphersuite version we may have instructed the client
1313 * to use a different ciphersuite.
1314 */
Jerry Yu4a173382021-10-11 21:45:31 +08001315 if( ciphersuite_info == NULL ||
1316 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001317 {
XiaokangQian52da5582022-01-26 09:49:29 +00001318 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001319 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001320 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001321 * If we received an HRR before and that the proposed selected
1322 * ciphersuite in this server hello is not the same as the one
1323 * proposed in the HRR, we abort the handshake and send an
1324 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001325 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001326 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1327 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001328 {
XiaokangQian52da5582022-01-26 09:49:29 +00001329 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001330 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001331
XiaokangQian52da5582022-01-26 09:49:29 +00001332 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001333 {
1334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1335 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001336 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001337 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001338
Jerry Yu4a173382021-10-11 21:45:31 +08001339 /* Configure ciphersuites */
1340 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1341
XiaokangQian355e09a2022-01-20 11:14:50 +00001342 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001343 ssl->session_negotiate->ciphersuite = cipher_suite;
1344
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1346 cipher_suite, ciphersuite_info->name ) );
1347
1348#if defined(MBEDTLS_HAVE_TIME)
1349 ssl->session_negotiate->start = time( NULL );
1350#endif /* MBEDTLS_HAVE_TIME */
1351
Jerry Yu4a173382021-10-11 21:45:31 +08001352 /* ...
1353 * uint8 legacy_compression_method = 0;
1354 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001355 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001356 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001357 if( p[0] != 0 )
1358 {
Jerry Yub85277e2021-10-13 13:36:05 +08001359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001360 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001361 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001362 }
1363 p++;
1364
Jerry Yub85277e2021-10-13 13:36:05 +08001365 /* ...
1366 * Extension extensions<6..2^16-1>;
1367 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001368 * struct {
1369 * ExtensionType extension_type; (2 bytes)
1370 * opaque extension_data<0..2^16-1>;
1371 * } Extension;
1372 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001373 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001374 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001375 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001376
Jerry Yu4a173382021-10-11 21:45:31 +08001377 /* Check extensions do not go beyond the buffer of data. */
1378 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1379 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001380
Jerry Yu4a173382021-10-11 21:45:31 +08001381 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001382
Jerry Yu4a173382021-10-11 21:45:31 +08001383 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001384 {
1385 unsigned int extension_type;
1386 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001387 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001388
Jerry Yu4a173382021-10-11 21:45:31 +08001389 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001390 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1391 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1392 p += 4;
1393
Jerry Yu4a173382021-10-11 21:45:31 +08001394 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001395 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001396
1397 switch( extension_type )
1398 {
XiaokangQianb851da82022-01-14 04:03:11 +00001399 case MBEDTLS_TLS_EXT_COOKIE:
1400
XiaokangQiand9e068e2022-01-18 06:23:32 +00001401 if( !is_hrr )
1402 {
XiaokangQian52da5582022-01-26 09:49:29 +00001403 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001404 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001405 }
1406
XiaokangQian43550bd2022-01-21 04:32:58 +00001407 ret = ssl_tls13_parse_cookie_ext( ssl,
1408 p, extension_data_end );
1409 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001410 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001411 MBEDTLS_SSL_DEBUG_RET( 1,
1412 "ssl_tls13_parse_cookie_ext",
1413 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001414 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001415 }
XiaokangQianb851da82022-01-14 04:03:11 +00001416 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001417
Jerry Yue1b9c292021-09-10 10:08:31 +08001418 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001420 MBEDTLS_SSL_DEBUG_MSG( 3,
1421 ( "found supported_versions extension" ) );
1422
Jerry Yuc068b662021-10-11 22:30:19 +08001423 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001424 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001425 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001426 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001427 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001428 break;
1429
1430 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001433
XiaokangQian52da5582022-01-26 09:49:29 +00001434 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001435 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001436
1437#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1438 case MBEDTLS_TLS_EXT_KEY_SHARE:
1439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001440 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1441 {
XiaokangQian52da5582022-01-26 09:49:29 +00001442 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001443 goto cleanup;
1444 }
1445
XiaokangQian53f20b72022-01-18 10:47:33 +00001446 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001447 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001448 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001449 else
XiaokangQianb851da82022-01-14 04:03:11 +00001450 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001451 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001452 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001453 {
1454 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001455 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001457 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001458 }
1459 break;
1460#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1461
1462 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001463 MBEDTLS_SSL_DEBUG_MSG(
1464 3,
1465 ( "unknown extension found: %u ( ignoring )",
1466 extension_type ) );
1467
XiaokangQian52da5582022-01-26 09:49:29 +00001468 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001469 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001470 }
1471
1472 p += extension_data_len;
1473 }
1474
XiaokangQian78b1fa72022-01-19 06:56:30 +00001475 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001476 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001478 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001479 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001480 }
1481
XiaokangQiand59be772022-01-24 10:12:51 +00001482cleanup:
1483
XiaokangQian52da5582022-01-26 09:49:29 +00001484 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001485 {
1486 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1487 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1488 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1489 }
XiaokangQian52da5582022-01-26 09:49:29 +00001490 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001491 {
1492 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1493 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1494 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1495 }
1496 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001497}
1498
XiaokangQian355e09a2022-01-20 11:14:50 +00001499static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001500{
Jerry Yub85277e2021-10-13 13:36:05 +08001501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001502 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001503 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001504 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001505
Jerry Yub85277e2021-10-13 13:36:05 +08001506 /* Determine the key exchange mode:
1507 * 1) If both the pre_shared_key and key_share extensions were received
1508 * then the key exchange mode is PSK with EPHEMERAL.
1509 * 2) If only the pre_shared_key extension was received then the key
1510 * exchange mode is PSK-only.
1511 * 3) If only the key_share extension was received then the key
1512 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001513 */
Jerry Yub85277e2021-10-13 13:36:05 +08001514 switch( handshake->extensions_present &
1515 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001516 {
Jerry Yu745bb612021-10-13 22:01:04 +08001517 /* Only the pre_shared_key extension was received */
1518 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001519 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001520 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001521
Jerry Yu745bb612021-10-13 22:01:04 +08001522 /* Only the key_share extension was received */
1523 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001524 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001525 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001526
Jerry Yu745bb612021-10-13 22:01:04 +08001527 /* Both the pre_shared_key and key_share extensions were received */
1528 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001529 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001530 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001531
Jerry Yu745bb612021-10-13 22:01:04 +08001532 /* Neither pre_shared_key nor key_share extension was received */
1533 default:
1534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1535 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1536 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001537 }
1538
1539 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1540 *
1541 * TODO: We don't have to do this in case we offered 0-RTT and the
1542 * server accepted it. In this case, we could skip generating
1543 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001544 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001545 if( ret != 0 )
1546 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001548 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001549 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001550 }
1551
1552 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001553 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001554 if( ret != 0 )
1555 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001557 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001558 }
1559
1560 /* Next evolution in key schedule: Establish handshake secret and
1561 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001562 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001563 if( ret != 0 )
1564 {
Jerry Yuc068b662021-10-11 22:30:19 +08001565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1566 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001567 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001568 }
1569
Jerry Yub85277e2021-10-13 13:36:05 +08001570 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001571 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001572 {
1573 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1574 goto cleanup;
1575 }
Jerry Yu0b177842021-09-05 19:41:30 +08001576
1577 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1578 ssl->conf->endpoint,
1579 ssl->session_negotiate->ciphersuite,
1580 &traffic_keys,
1581 ssl );
1582 if( ret != 0 )
1583 {
1584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001585 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001586 }
1587
Jerry Yub85277e2021-10-13 13:36:05 +08001588 handshake->transform_handshake = transform_handshake;
1589 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001590
1591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1592 ssl->session_in = ssl->session_negotiate;
1593
1594 /*
1595 * State machine update
1596 */
1597 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1598
Jerry Yu4a173382021-10-11 21:45:31 +08001599cleanup:
1600
Jerry Yu0b177842021-09-05 19:41:30 +08001601 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001602 if( ret != 0 )
1603 {
Jerry Yub85277e2021-10-13 13:36:05 +08001604 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001605
Jerry Yu4a173382021-10-11 21:45:31 +08001606 MBEDTLS_SSL_PEND_FATAL_ALERT(
1607 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1608 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1609 }
1610 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001611}
1612
XiaokangQian355e09a2022-01-20 11:14:50 +00001613static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001614{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001615#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1617
XiaokangQian647719a2021-12-07 09:16:29 +00001618#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1619 /* If not offering early data, the client sends a dummy CCS record
1620 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001621 * its second ClientHello or before its encrypted handshake flight.
1622 */
XiaokangQian647719a2021-12-07 09:16:29 +00001623 mbedtls_ssl_handshake_set_state( ssl,
1624 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1625#else
1626 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1627#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1628
XiaokangQian78b1fa72022-01-19 06:56:30 +00001629 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001630
XiaokangQian78b1fa72022-01-19 06:56:30 +00001631 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001632 * We are going to re-generate a shared secret corresponding to the group
1633 * selected by the server, which is different from the group for which we
1634 * generated a shared secret in the first client hello.
1635 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001636 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001637 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001638 if( ret != 0 )
1639 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001640#else
1641 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001642#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001643
1644 return( 0 );
1645}
1646
Jerry Yue1b9c292021-09-10 10:08:31 +08001647/*
Jerry Yu4a173382021-10-11 21:45:31 +08001648 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001649 * Handler for MBEDTLS_SSL_SERVER_HELLO
1650 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001651static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001652{
Jerry Yu4a173382021-10-11 21:45:31 +08001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001654 unsigned char *buf = NULL;
1655 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001656 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001657
1658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1659
1660 /* Coordination step
1661 * - Fetch record
1662 * - Make sure it's either a ServerHello or a HRR.
1663 * - Switch processing routine in case of HRR
1664 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001665 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1666 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1667
XiaokangQian16acd4b2022-01-14 07:35:47 +00001668 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001669 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001670 goto cleanup;
1671 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001672 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001673
XiaokangQianb851da82022-01-14 04:03:11 +00001674 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001675 buf + buf_len,
1676 is_hrr ) );
1677 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001678 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1679
XiaokangQianb851da82022-01-14 04:03:11 +00001680 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1681 MBEDTLS_SSL_HS_SERVER_HELLO,
1682 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001683
XiaokangQiand9e068e2022-01-18 06:23:32 +00001684 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001685 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001686 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001687 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001688
1689cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1691 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001692 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001693}
1694
1695/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001696 *
1697 * EncryptedExtensions message
1698 *
1699 * The EncryptedExtensions message contains any extensions which
1700 * should be protected, i.e., any which are not needed to establish
1701 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001702 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001703
1704/*
1705 * Overview
1706 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001707
1708/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001709static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001710
XiaokangQian97799ac2021-10-11 10:05:54 +00001711static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1712 const unsigned char *buf,
1713 const unsigned char *end );
1714static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001715
1716/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001717 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001718 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001719static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001720{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001721 int ret;
1722 unsigned char *buf;
1723 size_t buf_len;
1724
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1726
Xiaofei Bai746f9482021-11-12 08:53:56 +00001727 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001728 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001729 &buf, &buf_len ) );
1730
1731 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001732 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001733 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001734
Xiaofei Bai746f9482021-11-12 08:53:56 +00001735 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001736 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001737
XiaokangQian97799ac2021-10-11 10:05:54 +00001738 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001739
1740cleanup:
1741
1742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1743 return( ret );
1744
1745}
1746
XiaokangQian08da26c2021-10-09 10:12:11 +00001747/* Parse EncryptedExtensions message
1748 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001749 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001750 * } EncryptedExtensions;
1751 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001752static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1753 const unsigned char *buf,
1754 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001755{
1756 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001757 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001758 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001759 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001760
XiaokangQian08da26c2021-10-09 10:12:11 +00001761 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001762 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001763 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764
XiaokangQian97799ac2021-10-11 10:05:54 +00001765 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001766 extensions_end = p + extensions_len;
1767 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001768
XiaokangQian8db25ff2021-10-13 05:56:18 +00001769 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001770 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001771 unsigned int extension_type;
1772 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001773
XiaokangQian08da26c2021-10-09 10:12:11 +00001774 /*
1775 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001776 * ExtensionType extension_type; (2 bytes)
1777 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001778 * } Extension;
1779 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001780 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001781 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1782 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1783 p += 4;
1784
XiaokangQian8db25ff2021-10-13 05:56:18 +00001785 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001786
XiaokangQian97799ac2021-10-11 10:05:54 +00001787 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001788 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001789 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001790 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001791 switch( extension_type )
1792 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001793
XiaokangQian08da26c2021-10-09 10:12:11 +00001794 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1796 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001797
lhuang0486cacac2022-01-21 07:34:27 -08001798#if defined(MBEDTLS_SSL_ALPN)
1799 case MBEDTLS_TLS_EXT_ALPN:
1800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1801
1802 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1803 {
1804 return( ret );
1805 }
1806
1807 break;
1808#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001809 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001810 MBEDTLS_SSL_DEBUG_MSG(
1811 3, ( "unsupported extension found: %u ", extension_type) );
1812 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001813 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001814 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1815 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001816 }
1817
XiaokangQian08da26c2021-10-09 10:12:11 +00001818 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001819 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001820
XiaokangQian97799ac2021-10-11 10:05:54 +00001821 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001822 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001823 {
1824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001825 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001826 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001827 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001828 }
1829
1830 return( ret );
1831}
1832
XiaokangQian97799ac2021-10-11 10:05:54 +00001833static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001834{
Jerry Yua93ac112021-10-27 16:31:48 +08001835#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001836 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001837 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1838 else
Jerry Yud2674312021-10-29 10:08:19 +08001839 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001840#else
1841 ((void) ssl);
1842 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1843#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001844 return( 0 );
1845}
1846
Jerry Yua93ac112021-10-27 16:31:48 +08001847#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001848/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001849 *
1850 * STATE HANDLING: CertificateRequest
1851 *
Jerry Yud2674312021-10-29 10:08:19 +08001852 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001853#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1854#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001855/* Coordination:
1856 * Deals with the ambiguity of not knowing if a CertificateRequest
1857 * will be sent. Returns a negative code on failure, or
1858 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1859 * - SSL_CERTIFICATE_REQUEST_SKIP
1860 * indicating if a Certificate Request is expected or not.
1861 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001862static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001863{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001865
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001866 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001867 {
1868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1869 return( SSL_CERTIFICATE_REQUEST_SKIP );
1870 }
1871
1872 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001873 {
1874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1875 return( ret );
1876 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001877 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001878
1879 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1880 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1881 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001882 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001883 }
1884
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001885 return( SSL_CERTIFICATE_REQUEST_SKIP );
1886}
1887
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001888/*
1889 * ssl_tls13_parse_certificate_request()
1890 * Parse certificate request
1891 * struct {
1892 * opaque certificate_request_context<0..2^8-1>;
1893 * Extension extensions<2..2^16-1>;
1894 * } CertificateRequest;
1895 */
1896static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1897 const unsigned char *buf,
1898 const unsigned char *end )
1899{
1900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1901 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001902 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001903 size_t extensions_len = 0;
1904 const unsigned char *extensions_end;
1905 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001906
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001907 /* ...
1908 * opaque certificate_request_context<0..2^8-1>
1909 * ...
1910 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001911 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1912 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001913 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001914
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001915 if( certificate_request_context_len > 0 )
1916 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001917 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001918 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1919 p, certificate_request_context_len );
1920
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001921 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001922 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001923 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001924 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001925 {
1926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1927 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1928 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001929 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001930 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001931 p += certificate_request_context_len;
1932 }
1933
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001934 /* ...
1935 * Extension extensions<2..2^16-1>;
1936 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001937 */
1938 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1939 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1940 p += 2;
1941
1942 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1943 extensions_end = p + extensions_len;
1944
1945 while( p < extensions_end )
1946 {
1947 unsigned int extension_type;
1948 size_t extension_data_len;
1949
1950 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1951 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1952 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1953 p += 4;
1954
1955 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1956
1957 switch( extension_type )
1958 {
1959 case MBEDTLS_TLS_EXT_SIG_ALG:
1960 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001961 ( "found signature algorithms extension" ) );
1962 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001963 p + extension_data_len );
1964 if( ret != 0 )
1965 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001966 if( ! sig_alg_ext_found )
1967 sig_alg_ext_found = 1;
1968 else
1969 {
1970 MBEDTLS_SSL_DEBUG_MSG( 3,
1971 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001972 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001973 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001974 break;
1975
1976 default:
1977 MBEDTLS_SSL_DEBUG_MSG(
1978 3,
1979 ( "unknown extension found: %u ( ignoring )",
1980 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001981 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001982 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001983 p += extension_data_len;
1984 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001985 /* Check that we consumed all the message. */
1986 if( p != end )
1987 {
1988 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001989 ( "CertificateRequest misaligned" ) );
1990 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001991 }
1992 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001993 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001994 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001995 MBEDTLS_SSL_DEBUG_MSG( 3,
1996 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001997 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001998 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001999
Jerry Yu7840f812022-01-29 10:26:51 +08002000 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002001 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002002
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002003decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002004 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2005 MBEDTLS_ERR_SSL_DECODE_ERROR );
2006 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002007}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002008
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002009/*
2010 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2011 */
2012static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002013{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002015
2016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2017
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002018 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2019
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002020 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2021 {
2022 unsigned char *buf;
2023 size_t buf_len;
2024
2025 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2026 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2027 &buf, &buf_len ) );
2028
2029 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2030 buf, buf + buf_len ) );
2031
2032 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2033 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2034 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002035 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002036 {
2037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002038 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002039 }
2040 else
2041 {
2042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002043 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2044 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 }
2046
2047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002048 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002049
Jerry Yud2674312021-10-29 10:08:19 +08002050 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2051
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002052cleanup:
2053
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2055 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002056}
2057
2058/*
Jerry Yu687101b2021-09-14 16:03:56 +08002059 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2060 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002061static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002062{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002063 int ret;
2064
2065 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002066 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002067 return( ret );
2068
Jerry Yu687101b2021-09-14 16:03:56 +08002069 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2070 return( 0 );
2071}
2072
2073/*
2074 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2075 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002076static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002077{
Jerry Yu30b071c2021-09-12 20:16:03 +08002078 int ret;
2079
2080 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2081 if( ret != 0 )
2082 return( ret );
2083
Jerry Yu687101b2021-09-14 16:03:56 +08002084 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2085 return( 0 );
2086}
Jerry Yua93ac112021-10-27 16:31:48 +08002087#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002088
Jerry Yu687101b2021-09-14 16:03:56 +08002089/*
2090 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2091 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002092static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002093{
XiaokangQianac0385c2021-11-03 06:40:11 +00002094 int ret;
2095
XiaokangQianc5c39d52021-11-09 11:55:10 +00002096 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002097 if( ret != 0 )
2098 return( ret );
2099
Ronald Cron49ad6192021-11-24 16:25:31 +01002100#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2101 mbedtls_ssl_handshake_set_state(
2102 ssl,
2103 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2104#else
Jerry Yuca133a32022-02-15 14:22:05 +08002105 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002106#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002107
XiaokangQianac0385c2021-11-03 06:40:11 +00002108 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002109}
2110
2111/*
Jerry Yu566c7812022-01-26 15:41:22 +08002112 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2113 */
2114static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2115{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002116 int non_empty_certificate_msg = 0;
2117
Jerry Yu5cc35062022-01-28 16:16:08 +08002118 MBEDTLS_SSL_DEBUG_MSG( 1,
2119 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002120 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002121
Ronald Cron9df7c802022-03-08 18:38:54 +01002122#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002123 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002124 {
2125 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2126 if( ret != 0 )
2127 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002128
Ronald Cron7a94aca2022-03-09 07:44:27 +01002129 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2130 non_empty_certificate_msg = 1;
2131 }
2132 else
2133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2135 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002136#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002137
Ronald Cron7a94aca2022-03-09 07:44:27 +01002138 if( non_empty_certificate_msg )
2139 {
2140 mbedtls_ssl_handshake_set_state( ssl,
2141 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2142 }
2143 else
2144 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2145
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002146 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002147}
2148
Ronald Cron9df7c802022-03-08 18:38:54 +01002149#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002150/*
2151 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2152 */
2153static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2154{
Ronald Crona8b38872022-03-09 07:59:25 +01002155 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2156
2157 if( ret == 0 )
2158 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2159
2160 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002161}
Jerry Yu90f152d2022-01-29 22:12:42 +08002162#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002163
2164/*
Jerry Yu687101b2021-09-14 16:03:56 +08002165 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2166 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002167static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002168{
XiaokangQian0fa66432021-11-15 03:33:57 +00002169 int ret;
2170
2171 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2172 if( ret != 0 )
2173 return( ret );
2174
2175 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2176 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002177}
2178
2179/*
2180 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2181 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002182static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002183{
Jerry Yu378254d2021-10-30 21:44:47 +08002184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002185 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002186 return( 0 );
2187}
2188
2189/*
2190 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2191 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002192static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002193{
Jerry Yu378254d2021-10-30 21:44:47 +08002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2195 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2196
2197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2198 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2199
2200 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2201
2202 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2203 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002204}
2205
Jerry Yu92c6b402021-08-27 16:59:09 +08002206int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002207{
Jerry Yu92c6b402021-08-27 16:59:09 +08002208 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002209
Jerry Yue3b34122021-09-28 17:53:35 +08002210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2211 mbedtls_ssl_states_str( ssl->state ),
2212 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002213
2214 switch( ssl->state )
2215 {
2216 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002217 * ssl->state is initialized as HELLO_REQUEST. It is the same
2218 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002219 */
2220 case MBEDTLS_SSL_HELLO_REQUEST:
2221 case MBEDTLS_SSL_CLIENT_HELLO:
2222 ret = ssl_tls13_write_client_hello( ssl );
2223 break;
2224
2225 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002226 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002227 break;
2228
2229 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002230 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002231 break;
2232
Jerry Yua93ac112021-10-27 16:31:48 +08002233#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002234 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2235 ret = ssl_tls13_process_certificate_request( ssl );
2236 break;
2237
Jerry Yu687101b2021-09-14 16:03:56 +08002238 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002239 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002240 break;
2241
2242 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002243 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002244 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002245#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002246
2247 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002248 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002249 break;
2250
Jerry Yu566c7812022-01-26 15:41:22 +08002251 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2252 ret = ssl_tls13_write_client_certificate( ssl );
2253 break;
2254
Ronald Cron9df7c802022-03-08 18:38:54 +01002255#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002256 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2257 ret = ssl_tls13_write_client_certificate_verify( ssl );
2258 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002259#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002260
Jerry Yu687101b2021-09-14 16:03:56 +08002261 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002262 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002263 break;
2264
2265 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002266 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002267 break;
2268
2269 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002270 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002271 break;
2272
Ronald Cron49ad6192021-11-24 16:25:31 +01002273 /*
2274 * Injection of dummy-CCS's for middlebox compatibility
2275 */
2276#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002277 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002278 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2279 if( ret == 0 )
2280 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2281 break;
2282
2283 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2284 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2285 if( ret == 0 )
2286 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002287 break;
2288#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2289
Jerry Yu92c6b402021-08-27 16:59:09 +08002290 default:
2291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2293 }
2294
2295 return( ret );
2296}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002297
Jerry Yufb4b6472022-01-27 15:03:26 +08002298#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002299
Jerry Yufb4b6472022-01-27 15:03:26 +08002300