blob: 7d3bdf24081d56560def1ea1be9702f65390ab8a [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
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100808static int ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
809 unsigned char *buf,
810 unsigned char *end,
811 size_t *out_len )
812{
813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
814 unsigned char *p = buf;
815 size_t ext_len;
816
817 *out_len = 0;
818
819 /* Write supported_versions extension
820 *
821 * Supported Versions Extension is mandatory with TLS 1.3.
822 */
823 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
824 if( ret != 0 )
825 return( ret );
826 p += ext_len;
827
828 /* Echo the cookie if the server provided one in its preceding
829 * HelloRetryRequest message.
830 */
831 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
832 if( ret != 0 )
833 return( ret );
834 p += ext_len;
835
836#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
837 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
838 {
839 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
840 if( ret != 0 )
841 return( ret );
842 p += ext_len;
843 }
844#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
845
846 *out_len = p - buf;
847
848 return( 0 );
849}
850
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800851/*
852 * Structure of ClientHello message:
853 *
854 * struct {
855 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
856 * Random random;
857 * opaque legacy_session_id<0..32>;
858 * CipherSuite cipher_suites<2..2^16-2>;
859 * opaque legacy_compression_methods<1..2^8-1>;
860 * Extension extensions<8..2^16-1>;
861 * } ClientHello;
862 */
Jerry Yu08906d02021-08-31 11:05:27 +0800863static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800864 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800865 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000866 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800867{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800868
Jerry Yubc20bdd2021-08-24 15:59:48 +0800869 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000870 unsigned char *p_extensions_len; /* Pointer to extensions length */
871 size_t output_len; /* Length of buffer used by function */
872 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800873
Jerry Yubc20bdd2021-08-24 15:59:48 +0800874 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800875 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800876
Xiaofei Baid25fab62021-12-02 06:36:27 +0000877 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800878
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800879 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800880 ssl->major_ver = ssl->conf->min_major_ver;
881 ssl->minor_ver = ssl->conf->min_minor_ver;
882
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800883 /*
884 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800885 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800886 *
887 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800888 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800889 */
Jerry Yufec982e2021-09-07 17:26:06 +0800890 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800891 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800892 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800893
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800894 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800895 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
896 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800897 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800898 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
899 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800900
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800901 /*
902 * Write legacy_session_id
903 *
904 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
905 * which has been merged with pre-shared keys in this version. A client
906 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
907 * this field to that value. In compatibility mode, this field MUST be
908 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
909 * a new 32-byte value. This value need not be random but SHOULD be
910 * unpredictable to avoid implementations fixating on a specific value
911 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
912 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800913 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100914#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
915 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
916 *p++ = (unsigned char)ssl->session_negotiate->id_len;
917 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
918 p += ssl->session_negotiate->id_len;
919
920 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
921 ssl->session_negotiate->id_len );
922#else
Jerry Yubbe09522021-09-06 21:17:54 +0800923 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
924 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100925#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800926
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800927 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800928 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800929 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800930 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800931 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800932
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800933 /* Write legacy_compression_methods
934 *
935 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800936 * one byte set to zero, which corresponds to the 'null' compression
937 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800938 */
Jerry Yubbe09522021-09-06 21:17:54 +0800939 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
940 *p++ = 1;
941 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800942
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800943 /* Write extensions */
944
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100945#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800946 /* Keeping track of the included extensions */
947 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100948#endif
Jerry Yubc20bdd2021-08-24 15:59:48 +0800949
950 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800951 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000952 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800953 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800954
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100955#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100956 ret = ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800957 if( ret != 0 )
958 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800959 p += output_len;
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100960#endif
Jerry Yubc20bdd2021-08-24 15:59:48 +0800961
lhuang0486cacac2022-01-21 07:34:27 -0800962#if defined(MBEDTLS_SSL_ALPN)
Ronald Crona0855a62022-03-09 10:04:54 +0100963 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
lhuang0486cacac2022-01-21 07:34:27 -0800964 if( ret != 0 )
965 return( ret );
966 p += output_len;
967#endif /* MBEDTLS_SSL_ALPN */
968
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100969#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800970#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf46b0162022-01-11 16:28:00 +0800971 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
972 {
973 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
974 if( ret != 0 )
975 return( ret );
976 p += output_len;
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100977 }
Jerry Yu6a643102021-08-31 14:40:36 +0800978
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100979 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
980 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800981 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800982 if( ret != 0 )
983 return( ret );
984 p += output_len;
985 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800986#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100987#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800988
Xiaofei Bai15a56812021-11-05 10:52:12 +0000989#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000990 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000991 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
992 if( ret != 0 )
993 return( ret );
994 p += output_len;
995#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
996
Jerry Yubc20bdd2021-08-24 15:59:48 +0800997 /* Add more extensions here */
998
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800999 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +00001000 extensions_len = p - p_extensions_len - 2;
1001 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +08001003 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +00001004 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001005
Xiaofei Baid25fab62021-12-02 06:36:27 +00001006 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +08001007 return( 0 );
1008}
1009
Jerry Yu92c6b402021-08-27 16:59:09 +08001010static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
1011{
1012 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +08001013
Jerry Yu92c6b402021-08-27 16:59:09 +08001014 if( ssl->conf->f_rng == NULL )
1015 {
1016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1017 return( MBEDTLS_ERR_SSL_NO_RNG );
1018 }
Jerry Yuef6b36b2021-08-24 16:29:02 +08001019
Jerry Yu92c6b402021-08-27 16:59:09 +08001020 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1021 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001022 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +08001023 {
Jerry Yu8c02bb42021-09-03 21:09:22 +08001024 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +08001025 return( ret );
1026 }
Jerry Yu6f13f642021-08-26 17:18:15 +08001027
Ronald Cron49ad6192021-11-24 16:25:31 +01001028#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1029 /*
1030 * Create a session identifier for the purpose of middlebox compatibility
1031 * only if one has not been created already.
1032 */
1033 if( ssl->session_negotiate->id_len == 0 )
1034 {
1035 /* Creating a session id with 32 byte length */
1036 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1037 ssl->session_negotiate->id, 32 ) ) != 0 )
1038 {
1039 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
1040 return( ret );
1041 }
1042 ssl->session_negotiate->id_len = 32;
1043 }
1044#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1045
Jerry Yu6f13f642021-08-26 17:18:15 +08001046 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001047}
1048
Jerry Yu92c6b402021-08-27 16:59:09 +08001049/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001050 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001051 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001052 */
1053static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001054{
Jerry Yu92c6b402021-08-27 16:59:09 +08001055 int ret = 0;
1056 unsigned char *buf;
1057 size_t buf_len, msg_len;
1058
1059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1060
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001061 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001062
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001063 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1064 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1065 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001066
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001067 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001068 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001069 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001070
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001071 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1072 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001073 msg_len );
1074 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001075
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001076 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1077 buf_len,
1078 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001079
Ronald Cron3addfa42022-02-08 16:10:25 +01001080 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1081
Jerry Yu92c6b402021-08-27 16:59:09 +08001082cleanup:
1083
1084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1085 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001086}
1087
Jerry Yu687101b2021-09-14 16:03:56 +08001088/*
Jerry Yu4a173382021-10-11 21:45:31 +08001089 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001090 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001091/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001092 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1093 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001094 * to indicate which message is expected and to be parsed next.
1095 */
Jerry Yub85277e2021-10-13 13:36:05 +08001096#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1097#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1098static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1099 const unsigned char *buf,
1100 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001101{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001102 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001103 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1104 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1105 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1106 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1107
1108 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1109 *
Jerry Yu4a173382021-10-11 21:45:31 +08001110 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001111 * special value of the SHA-256 of "HelloRetryRequest".
1112 *
1113 * struct {
1114 * ProtocolVersion legacy_version = 0x0303;
1115 * Random random;
1116 * opaque legacy_session_id_echo<0..32>;
1117 * CipherSuite cipher_suite;
1118 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001119 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 * } ServerHello;
1121 *
1122 */
Jerry Yub85277e2021-10-13 13:36:05 +08001123 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001124
1125 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 {
Jerry Yub85277e2021-10-13 13:36:05 +08001127 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001128 }
1129
Jerry Yub85277e2021-10-13 13:36:05 +08001130 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001131}
1132
Jerry Yu745bb612021-10-13 22:01:04 +08001133/* Fetch and preprocess
1134 * Returns a negative value on failure, and otherwise
1135 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1136 * - SSL_SERVER_HELLO_COORDINATE_HRR
1137 */
Jerry Yub85277e2021-10-13 13:36:05 +08001138static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1139 unsigned char **buf,
1140 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001141{
Jerry Yu4a173382021-10-11 21:45:31 +08001142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001143
XiaokangQian355e09a2022-01-20 11:14:50 +00001144 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1145 MBEDTLS_SSL_HS_SERVER_HELLO,
1146 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001147
Jerry Yub85277e2021-10-13 13:36:05 +08001148 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1149 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 {
Jerry Yu745bb612021-10-13 22:01:04 +08001151 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1153 break;
1154 case SSL_SERVER_HELLO_COORDINATE_HRR:
1155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001156 /* If a client receives a second
1157 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1158 * was itself in response to a HelloRetryRequest), it MUST abort the
1159 * handshake with an "unexpected_message" alert.
1160 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001161 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001162 {
1163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1164 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1165 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1166 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1167 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001168 /*
1169 * Clients must abort the handshake with an "illegal_parameter"
1170 * alert if the HelloRetryRequest would not result in any change
1171 * in the ClientHello.
1172 * In a PSK only key exchange that what we expect.
1173 */
1174 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1175 {
1176 MBEDTLS_SSL_DEBUG_MSG( 1,
1177 ( "Unexpected HRR in pure PSK key exchange." ) );
1178 MBEDTLS_SSL_PEND_FATAL_ALERT(
1179 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1180 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1181 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1182 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001183
XiaokangQiand9e068e2022-01-18 06:23:32 +00001184 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001185
Jerry Yu745bb612021-10-13 22:01:04 +08001186 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001187 }
1188
1189cleanup:
1190
1191 return( ret );
1192}
1193
Jerry Yu4a173382021-10-11 21:45:31 +08001194static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1195 const unsigned char **buf,
1196 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001197{
1198 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001199 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001200
Jerry Yude4fb2c2021-09-19 18:05:08 +08001201 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001202 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001203
Jerry Yu4a173382021-10-11 21:45:31 +08001204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001205
1206 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001207 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1208 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001210 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1211 ssl->session_negotiate->id,
1212 ssl->session_negotiate->id_len );
1213 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001214 legacy_session_id_echo_len );
1215
1216 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1217 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1218
Jerry Yue1b9c292021-09-10 10:08:31 +08001219 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1220 }
1221
Jerry Yu4a173382021-10-11 21:45:31 +08001222 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 *buf = p;
1224
1225 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001226 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001227 return( 0 );
1228}
1229
Jerry Yu4a173382021-10-11 21:45:31 +08001230static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1231 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001232{
Jerry Yu4a173382021-10-11 21:45:31 +08001233 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1234
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001236 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001237 {
Jerry Yu4a173382021-10-11 21:45:31 +08001238 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001239 {
1240 return( 1 );
1241 }
1242 }
1243 return( 0 );
1244}
1245
1246/* Parse ServerHello message and configure context
1247 *
1248 * struct {
1249 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1250 * Random random;
1251 * opaque legacy_session_id_echo<0..32>;
1252 * CipherSuite cipher_suite;
1253 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001254 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001255 * } ServerHello;
1256 */
Jerry Yuc068b662021-10-11 22:30:19 +08001257static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1258 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001259 const unsigned char *end,
1260 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001261{
Jerry Yub85277e2021-10-13 13:36:05 +08001262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001263 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001264 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001265 size_t extensions_len;
1266 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001267 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001268 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001269 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001270 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001271
1272 /*
1273 * Check there is space for minimal fields
1274 *
1275 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001276 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001277 * - legacy_session_id_echo ( 1 byte ), minimum size
1278 * - cipher_suite ( 2 bytes)
1279 * - legacy_compression_method ( 1 byte )
1280 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001281 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001282
1283 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001284 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1285
Jerry Yu4a173382021-10-11 21:45:31 +08001286 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001287 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001288 * ...
1289 * with ProtocolVersion defined as:
1290 * uint16 ProtocolVersion;
1291 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001292 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1293 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1294 {
1295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1296 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1297 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001298 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1299 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001300 }
1301 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001302
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001303 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001304 * Random random;
1305 * ...
1306 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001307 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001308 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001309 if( !is_hrr )
1310 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001311 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001312 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1313 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1314 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1315 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001316 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001317
Jerry Yu4a173382021-10-11 21:45:31 +08001318 /* ...
1319 * opaque legacy_session_id_echo<0..32>;
1320 * ...
1321 */
1322 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001323 {
XiaokangQian52da5582022-01-26 09:49:29 +00001324 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001325 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001326 }
1327
Jerry Yu4a173382021-10-11 21:45:31 +08001328 /* ...
1329 * CipherSuite cipher_suite;
1330 * ...
1331 * with CipherSuite defined as:
1332 * uint8 CipherSuite[2];
1333 */
1334 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001335 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1336 p += 2;
1337
Jerry Yu4a173382021-10-11 21:45:31 +08001338
XiaokangQian355e09a2022-01-20 11:14:50 +00001339 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001340 /*
1341 * Check whether this ciphersuite is supported and offered.
1342 * Via the force_ciphersuite version we may have instructed the client
1343 * to use a different ciphersuite.
1344 */
Jerry Yu4a173382021-10-11 21:45:31 +08001345 if( ciphersuite_info == NULL ||
1346 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001347 {
XiaokangQian52da5582022-01-26 09:49:29 +00001348 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001349 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001350 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001351 * If we received an HRR before and that the proposed selected
1352 * ciphersuite in this server hello is not the same as the one
1353 * proposed in the HRR, we abort the handshake and send an
1354 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001355 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001356 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1357 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001358 {
XiaokangQian52da5582022-01-26 09:49:29 +00001359 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001360 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001361
XiaokangQian52da5582022-01-26 09:49:29 +00001362 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001363 {
1364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1365 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001366 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001367 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001368
Jerry Yu4a173382021-10-11 21:45:31 +08001369 /* Configure ciphersuites */
1370 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1371
XiaokangQian355e09a2022-01-20 11:14:50 +00001372 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001373 ssl->session_negotiate->ciphersuite = cipher_suite;
1374
Jerry Yue1b9c292021-09-10 10:08:31 +08001375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1376 cipher_suite, ciphersuite_info->name ) );
1377
1378#if defined(MBEDTLS_HAVE_TIME)
1379 ssl->session_negotiate->start = time( NULL );
1380#endif /* MBEDTLS_HAVE_TIME */
1381
Jerry Yu4a173382021-10-11 21:45:31 +08001382 /* ...
1383 * uint8 legacy_compression_method = 0;
1384 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001385 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001386 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001387 if( p[0] != 0 )
1388 {
Jerry Yub85277e2021-10-13 13:36:05 +08001389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001390 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001391 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001392 }
1393 p++;
1394
Jerry Yub85277e2021-10-13 13:36:05 +08001395 /* ...
1396 * Extension extensions<6..2^16-1>;
1397 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001398 * struct {
1399 * ExtensionType extension_type; (2 bytes)
1400 * opaque extension_data<0..2^16-1>;
1401 * } Extension;
1402 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001403 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001404 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001405 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001406
Jerry Yu4a173382021-10-11 21:45:31 +08001407 /* Check extensions do not go beyond the buffer of data. */
1408 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1409 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001410
Jerry Yu4a173382021-10-11 21:45:31 +08001411 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001412
Jerry Yu4a173382021-10-11 21:45:31 +08001413 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001414 {
1415 unsigned int extension_type;
1416 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001417 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001418
Jerry Yu4a173382021-10-11 21:45:31 +08001419 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001420 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1421 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1422 p += 4;
1423
Jerry Yu4a173382021-10-11 21:45:31 +08001424 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001425 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001426
1427 switch( extension_type )
1428 {
XiaokangQianb851da82022-01-14 04:03:11 +00001429 case MBEDTLS_TLS_EXT_COOKIE:
1430
XiaokangQiand9e068e2022-01-18 06:23:32 +00001431 if( !is_hrr )
1432 {
XiaokangQian52da5582022-01-26 09:49:29 +00001433 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001434 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001435 }
1436
XiaokangQian43550bd2022-01-21 04:32:58 +00001437 ret = ssl_tls13_parse_cookie_ext( ssl,
1438 p, extension_data_end );
1439 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001440 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001441 MBEDTLS_SSL_DEBUG_RET( 1,
1442 "ssl_tls13_parse_cookie_ext",
1443 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001444 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001445 }
XiaokangQianb851da82022-01-14 04:03:11 +00001446 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001447
Jerry Yue1b9c292021-09-10 10:08:31 +08001448 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001449 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001450 MBEDTLS_SSL_DEBUG_MSG( 3,
1451 ( "found supported_versions extension" ) );
1452
Jerry Yuc068b662021-10-11 22:30:19 +08001453 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001454 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001455 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001457 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001458 break;
1459
1460 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001463
XiaokangQian52da5582022-01-26 09:49:29 +00001464 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001465 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001466
1467#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1468 case MBEDTLS_TLS_EXT_KEY_SHARE:
1469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001470 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1471 {
XiaokangQian52da5582022-01-26 09:49:29 +00001472 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001473 goto cleanup;
1474 }
1475
XiaokangQian53f20b72022-01-18 10:47:33 +00001476 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001477 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001478 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001479 else
XiaokangQianb851da82022-01-14 04:03:11 +00001480 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001481 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001482 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001483 {
1484 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001485 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001486 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001487 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001488 }
1489 break;
1490#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1491
1492 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001493 MBEDTLS_SSL_DEBUG_MSG(
1494 3,
1495 ( "unknown extension found: %u ( ignoring )",
1496 extension_type ) );
1497
XiaokangQian52da5582022-01-26 09:49:29 +00001498 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001499 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001500 }
1501
1502 p += extension_data_len;
1503 }
1504
XiaokangQian78b1fa72022-01-19 06:56:30 +00001505 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001506 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001508 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001509 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001510 }
1511
XiaokangQiand59be772022-01-24 10:12:51 +00001512cleanup:
1513
XiaokangQian52da5582022-01-26 09:49:29 +00001514 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001515 {
1516 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1517 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1518 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1519 }
XiaokangQian52da5582022-01-26 09:49:29 +00001520 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001521 {
1522 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1523 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1524 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1525 }
1526 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001527}
1528
XiaokangQian355e09a2022-01-20 11:14:50 +00001529static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001530{
Jerry Yub85277e2021-10-13 13:36:05 +08001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001532 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001533 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001534 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001535
Jerry Yub85277e2021-10-13 13:36:05 +08001536 /* Determine the key exchange mode:
1537 * 1) If both the pre_shared_key and key_share extensions were received
1538 * then the key exchange mode is PSK with EPHEMERAL.
1539 * 2) If only the pre_shared_key extension was received then the key
1540 * exchange mode is PSK-only.
1541 * 3) If only the key_share extension was received then the key
1542 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001543 */
Jerry Yub85277e2021-10-13 13:36:05 +08001544 switch( handshake->extensions_present &
1545 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001546 {
Jerry Yu745bb612021-10-13 22:01:04 +08001547 /* Only the pre_shared_key extension was received */
1548 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001549 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001550 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001551
Jerry Yu745bb612021-10-13 22:01:04 +08001552 /* Only the key_share extension was received */
1553 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001554 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001555 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001556
Jerry Yu745bb612021-10-13 22:01:04 +08001557 /* Both the pre_shared_key and key_share extensions were received */
1558 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001559 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001560 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001561
Jerry Yu745bb612021-10-13 22:01:04 +08001562 /* Neither pre_shared_key nor key_share extension was received */
1563 default:
1564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1565 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1566 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001567 }
1568
1569 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1570 *
1571 * TODO: We don't have to do this in case we offered 0-RTT and the
1572 * server accepted it. In this case, we could skip generating
1573 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001574 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001575 if( ret != 0 )
1576 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001578 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001579 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001580 }
1581
1582 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001583 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001584 if( ret != 0 )
1585 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001587 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001588 }
1589
1590 /* Next evolution in key schedule: Establish handshake secret and
1591 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001592 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001593 if( ret != 0 )
1594 {
Jerry Yuc068b662021-10-11 22:30:19 +08001595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1596 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001597 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001598 }
1599
Jerry Yub85277e2021-10-13 13:36:05 +08001600 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001601 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001602 {
1603 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1604 goto cleanup;
1605 }
Jerry Yu0b177842021-09-05 19:41:30 +08001606
1607 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1608 ssl->conf->endpoint,
1609 ssl->session_negotiate->ciphersuite,
1610 &traffic_keys,
1611 ssl );
1612 if( ret != 0 )
1613 {
1614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001615 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001616 }
1617
Jerry Yub85277e2021-10-13 13:36:05 +08001618 handshake->transform_handshake = transform_handshake;
1619 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001620
1621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1622 ssl->session_in = ssl->session_negotiate;
1623
1624 /*
1625 * State machine update
1626 */
1627 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1628
Jerry Yu4a173382021-10-11 21:45:31 +08001629cleanup:
1630
Jerry Yu0b177842021-09-05 19:41:30 +08001631 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001632 if( ret != 0 )
1633 {
Jerry Yub85277e2021-10-13 13:36:05 +08001634 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001635
Jerry Yu4a173382021-10-11 21:45:31 +08001636 MBEDTLS_SSL_PEND_FATAL_ALERT(
1637 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1638 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1639 }
1640 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001641}
1642
XiaokangQian355e09a2022-01-20 11:14:50 +00001643static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001644{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001645#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1647
XiaokangQian647719a2021-12-07 09:16:29 +00001648#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1649 /* If not offering early data, the client sends a dummy CCS record
1650 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001651 * its second ClientHello or before its encrypted handshake flight.
1652 */
XiaokangQian647719a2021-12-07 09:16:29 +00001653 mbedtls_ssl_handshake_set_state( ssl,
1654 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1655#else
1656 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1657#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1658
XiaokangQian78b1fa72022-01-19 06:56:30 +00001659 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001660
XiaokangQian78b1fa72022-01-19 06:56:30 +00001661 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001662 * We are going to re-generate a shared secret corresponding to the group
1663 * selected by the server, which is different from the group for which we
1664 * generated a shared secret in the first client hello.
1665 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001666 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001667 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001668 if( ret != 0 )
1669 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001670#else
1671 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001672#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001673
1674 return( 0 );
1675}
1676
Jerry Yue1b9c292021-09-10 10:08:31 +08001677/*
Jerry Yu4a173382021-10-11 21:45:31 +08001678 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001679 * Handler for MBEDTLS_SSL_SERVER_HELLO
1680 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001681static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001682{
Jerry Yu4a173382021-10-11 21:45:31 +08001683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001684 unsigned char *buf = NULL;
1685 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001686 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001687
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1689
1690 /* Coordination step
1691 * - Fetch record
1692 * - Make sure it's either a ServerHello or a HRR.
1693 * - Switch processing routine in case of HRR
1694 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001695 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1696 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1697
XiaokangQian16acd4b2022-01-14 07:35:47 +00001698 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001699 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001700 goto cleanup;
1701 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001702 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001703
XiaokangQianb851da82022-01-14 04:03:11 +00001704 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001705 buf + buf_len,
1706 is_hrr ) );
1707 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001708 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1709
XiaokangQianb851da82022-01-14 04:03:11 +00001710 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1711 MBEDTLS_SSL_HS_SERVER_HELLO,
1712 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001713
XiaokangQiand9e068e2022-01-18 06:23:32 +00001714 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001715 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001716 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001717 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001718
1719cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1721 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001722 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001723}
1724
1725/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001726 *
1727 * EncryptedExtensions message
1728 *
1729 * The EncryptedExtensions message contains any extensions which
1730 * should be protected, i.e., any which are not needed to establish
1731 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001732 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001733
1734/*
1735 * Overview
1736 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001737
1738/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001739static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001740
XiaokangQian97799ac2021-10-11 10:05:54 +00001741static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1742 const unsigned char *buf,
1743 const unsigned char *end );
1744static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001745
1746/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001747 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001748 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001749static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001750{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001751 int ret;
1752 unsigned char *buf;
1753 size_t buf_len;
1754
1755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1756
Xiaofei Bai746f9482021-11-12 08:53:56 +00001757 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001758 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001759 &buf, &buf_len ) );
1760
1761 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001762 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001763 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764
Xiaofei Bai746f9482021-11-12 08:53:56 +00001765 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001766 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001767
XiaokangQian97799ac2021-10-11 10:05:54 +00001768 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001769
1770cleanup:
1771
1772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1773 return( ret );
1774
1775}
1776
XiaokangQian08da26c2021-10-09 10:12:11 +00001777/* Parse EncryptedExtensions message
1778 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001779 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001780 * } EncryptedExtensions;
1781 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001782static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1783 const unsigned char *buf,
1784 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001785{
1786 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001787 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001788 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001789 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001790
XiaokangQian08da26c2021-10-09 10:12:11 +00001791 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001792 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001793 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001794
XiaokangQian97799ac2021-10-11 10:05:54 +00001795 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001796 extensions_end = p + extensions_len;
1797 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001798
XiaokangQian8db25ff2021-10-13 05:56:18 +00001799 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001800 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001801 unsigned int extension_type;
1802 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001803
XiaokangQian08da26c2021-10-09 10:12:11 +00001804 /*
1805 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001806 * ExtensionType extension_type; (2 bytes)
1807 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001808 * } Extension;
1809 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001810 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001811 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1812 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1813 p += 4;
1814
XiaokangQian8db25ff2021-10-13 05:56:18 +00001815 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001816
XiaokangQian97799ac2021-10-11 10:05:54 +00001817 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001818 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001819 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001820 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001821 switch( extension_type )
1822 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001823
XiaokangQian08da26c2021-10-09 10:12:11 +00001824 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1826 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001827
lhuang0486cacac2022-01-21 07:34:27 -08001828#if defined(MBEDTLS_SSL_ALPN)
1829 case MBEDTLS_TLS_EXT_ALPN:
1830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1831
1832 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1833 {
1834 return( ret );
1835 }
1836
1837 break;
1838#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001839 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001840 MBEDTLS_SSL_DEBUG_MSG(
1841 3, ( "unsupported extension found: %u ", extension_type) );
1842 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001843 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001844 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1845 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001846 }
1847
XiaokangQian08da26c2021-10-09 10:12:11 +00001848 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001849 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001850
XiaokangQian97799ac2021-10-11 10:05:54 +00001851 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001852 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001853 {
1854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001855 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001856 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001857 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001858 }
1859
1860 return( ret );
1861}
1862
XiaokangQian97799ac2021-10-11 10:05:54 +00001863static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001864{
Jerry Yua93ac112021-10-27 16:31:48 +08001865#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001866 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001867 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1868 else
Jerry Yud2674312021-10-29 10:08:19 +08001869 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001870#else
1871 ((void) ssl);
1872 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1873#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001874 return( 0 );
1875}
1876
Jerry Yua93ac112021-10-27 16:31:48 +08001877#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001878/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001879 *
1880 * STATE HANDLING: CertificateRequest
1881 *
Jerry Yud2674312021-10-29 10:08:19 +08001882 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001883#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1884#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001885/* Coordination:
1886 * Deals with the ambiguity of not knowing if a CertificateRequest
1887 * will be sent. Returns a negative code on failure, or
1888 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1889 * - SSL_CERTIFICATE_REQUEST_SKIP
1890 * indicating if a Certificate Request is expected or not.
1891 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001892static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001893{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001895
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001896 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001897 {
1898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1899 return( SSL_CERTIFICATE_REQUEST_SKIP );
1900 }
1901
1902 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001903 {
1904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1905 return( ret );
1906 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001907 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001908
1909 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1910 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1911 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001912 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001913 }
1914
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001915 return( SSL_CERTIFICATE_REQUEST_SKIP );
1916}
1917
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001918/*
1919 * ssl_tls13_parse_certificate_request()
1920 * Parse certificate request
1921 * struct {
1922 * opaque certificate_request_context<0..2^8-1>;
1923 * Extension extensions<2..2^16-1>;
1924 * } CertificateRequest;
1925 */
1926static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1927 const unsigned char *buf,
1928 const unsigned char *end )
1929{
1930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1931 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001932 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001933 size_t extensions_len = 0;
1934 const unsigned char *extensions_end;
1935 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001936
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001937 /* ...
1938 * opaque certificate_request_context<0..2^8-1>
1939 * ...
1940 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001941 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1942 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001943 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001944
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001945 if( certificate_request_context_len > 0 )
1946 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001947 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001948 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1949 p, certificate_request_context_len );
1950
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001951 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001952 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001953 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001954 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1957 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1958 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001959 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001960 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961 p += certificate_request_context_len;
1962 }
1963
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001964 /* ...
1965 * Extension extensions<2..2^16-1>;
1966 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001967 */
1968 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1969 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1970 p += 2;
1971
1972 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1973 extensions_end = p + extensions_len;
1974
1975 while( p < extensions_end )
1976 {
1977 unsigned int extension_type;
1978 size_t extension_data_len;
1979
1980 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1981 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1982 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1983 p += 4;
1984
1985 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1986
1987 switch( extension_type )
1988 {
1989 case MBEDTLS_TLS_EXT_SIG_ALG:
1990 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001991 ( "found signature algorithms extension" ) );
1992 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001993 p + extension_data_len );
1994 if( ret != 0 )
1995 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001996 if( ! sig_alg_ext_found )
1997 sig_alg_ext_found = 1;
1998 else
1999 {
2000 MBEDTLS_SSL_DEBUG_MSG( 3,
2001 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002002 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002003 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002004 break;
2005
2006 default:
2007 MBEDTLS_SSL_DEBUG_MSG(
2008 3,
2009 ( "unknown extension found: %u ( ignoring )",
2010 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002011 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002012 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002013 p += extension_data_len;
2014 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002015 /* Check that we consumed all the message. */
2016 if( p != end )
2017 {
2018 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002019 ( "CertificateRequest misaligned" ) );
2020 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002021 }
2022 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002023 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002024 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002025 MBEDTLS_SSL_DEBUG_MSG( 3,
2026 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002027 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002028 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002029
Jerry Yu7840f812022-01-29 10:26:51 +08002030 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002031 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002032
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002033decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002034 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2035 MBEDTLS_ERR_SSL_DECODE_ERROR );
2036 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002037}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002038
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002039/*
2040 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2041 */
2042static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002043{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045
2046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2047
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002048 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2049
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002050 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2051 {
2052 unsigned char *buf;
2053 size_t buf_len;
2054
2055 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2056 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2057 &buf, &buf_len ) );
2058
2059 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2060 buf, buf + buf_len ) );
2061
2062 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2063 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2064 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002065 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002066 {
2067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002068 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002069 }
2070 else
2071 {
2072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002073 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2074 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002075 }
2076
2077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002078 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002079
Jerry Yud2674312021-10-29 10:08:19 +08002080 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2081
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002082cleanup:
2083
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2085 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002086}
2087
2088/*
Jerry Yu687101b2021-09-14 16:03:56 +08002089 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2090 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002091static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002092{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002093 int ret;
2094
2095 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002096 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002097 return( ret );
2098
Jerry Yu687101b2021-09-14 16:03:56 +08002099 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2100 return( 0 );
2101}
2102
2103/*
2104 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2105 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002106static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002107{
Jerry Yu30b071c2021-09-12 20:16:03 +08002108 int ret;
2109
2110 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2111 if( ret != 0 )
2112 return( ret );
2113
Jerry Yu687101b2021-09-14 16:03:56 +08002114 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2115 return( 0 );
2116}
Jerry Yua93ac112021-10-27 16:31:48 +08002117#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002118
Jerry Yu687101b2021-09-14 16:03:56 +08002119/*
2120 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2121 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002122static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002123{
XiaokangQianac0385c2021-11-03 06:40:11 +00002124 int ret;
2125
XiaokangQianc5c39d52021-11-09 11:55:10 +00002126 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002127 if( ret != 0 )
2128 return( ret );
2129
Ronald Cron49ad6192021-11-24 16:25:31 +01002130#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2131 mbedtls_ssl_handshake_set_state(
2132 ssl,
2133 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2134#else
Jerry Yuca133a32022-02-15 14:22:05 +08002135 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002136#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002137
XiaokangQianac0385c2021-11-03 06:40:11 +00002138 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002139}
2140
2141/*
Jerry Yu566c7812022-01-26 15:41:22 +08002142 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2143 */
2144static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2145{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002146 int non_empty_certificate_msg = 0;
2147
Jerry Yu5cc35062022-01-28 16:16:08 +08002148 MBEDTLS_SSL_DEBUG_MSG( 1,
2149 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002150 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002151
Ronald Cron9df7c802022-03-08 18:38:54 +01002152#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002153 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002154 {
2155 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2156 if( ret != 0 )
2157 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002158
Ronald Cron7a94aca2022-03-09 07:44:27 +01002159 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2160 non_empty_certificate_msg = 1;
2161 }
2162 else
2163 {
2164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2165 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002166#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002167
Ronald Cron7a94aca2022-03-09 07:44:27 +01002168 if( non_empty_certificate_msg )
2169 {
2170 mbedtls_ssl_handshake_set_state( ssl,
2171 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2172 }
2173 else
2174 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2175
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002176 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002177}
2178
Ronald Cron9df7c802022-03-08 18:38:54 +01002179#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002180/*
2181 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2182 */
2183static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2184{
Ronald Crona8b38872022-03-09 07:59:25 +01002185 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2186
2187 if( ret == 0 )
2188 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2189
2190 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002191}
Jerry Yu90f152d2022-01-29 22:12:42 +08002192#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002193
2194/*
Jerry Yu687101b2021-09-14 16:03:56 +08002195 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2196 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002197static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002198{
XiaokangQian0fa66432021-11-15 03:33:57 +00002199 int ret;
2200
2201 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2202 if( ret != 0 )
2203 return( ret );
2204
2205 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2206 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002207}
2208
2209/*
2210 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2211 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002212static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002213{
Jerry Yu378254d2021-10-30 21:44:47 +08002214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002215 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002216 return( 0 );
2217}
2218
2219/*
2220 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2221 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002222static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002223{
Jerry Yu378254d2021-10-30 21:44:47 +08002224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2225 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2226
2227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2228 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2229
2230 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2231
2232 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2233 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002234}
2235
Jerry Yu92c6b402021-08-27 16:59:09 +08002236int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002237{
Jerry Yu92c6b402021-08-27 16:59:09 +08002238 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002239
Jerry Yue3b34122021-09-28 17:53:35 +08002240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2241 mbedtls_ssl_states_str( ssl->state ),
2242 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002243
2244 switch( ssl->state )
2245 {
2246 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002247 * ssl->state is initialized as HELLO_REQUEST. It is the same
2248 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002249 */
2250 case MBEDTLS_SSL_HELLO_REQUEST:
2251 case MBEDTLS_SSL_CLIENT_HELLO:
2252 ret = ssl_tls13_write_client_hello( ssl );
2253 break;
2254
2255 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002256 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002257 break;
2258
2259 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002260 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002261 break;
2262
Jerry Yua93ac112021-10-27 16:31:48 +08002263#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002264 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2265 ret = ssl_tls13_process_certificate_request( ssl );
2266 break;
2267
Jerry Yu687101b2021-09-14 16:03:56 +08002268 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002269 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002270 break;
2271
2272 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002273 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002274 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002275#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002276
2277 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002278 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002279 break;
2280
Jerry Yu566c7812022-01-26 15:41:22 +08002281 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2282 ret = ssl_tls13_write_client_certificate( ssl );
2283 break;
2284
Ronald Cron9df7c802022-03-08 18:38:54 +01002285#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002286 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2287 ret = ssl_tls13_write_client_certificate_verify( ssl );
2288 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002289#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002290
Jerry Yu687101b2021-09-14 16:03:56 +08002291 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002292 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002293 break;
2294
2295 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002296 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002297 break;
2298
2299 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002300 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002301 break;
2302
Ronald Cron49ad6192021-11-24 16:25:31 +01002303 /*
2304 * Injection of dummy-CCS's for middlebox compatibility
2305 */
2306#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002307 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002308 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2309 if( ret == 0 )
2310 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2311 break;
2312
2313 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2314 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2315 if( ret == 0 )
2316 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002317 break;
2318#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2319
Jerry Yu92c6b402021-08-27 16:59:09 +08002320 default:
2321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2323 }
2324
2325 return( ret );
2326}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002327
Jerry Yufb4b6472022-01-27 15:03:26 +08002328#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002329
Jerry Yufb4b6472022-01-27 15:03:26 +08002330