blob: 9f22e1dcc6d7e79af51eb437319ac1c1b0d9cd57 [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
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001063 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001064 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
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001071 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1072 buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001073
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001074 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
1075 buf_len,
1076 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001077
Ronald Cron3addfa42022-02-08 16:10:25 +01001078 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1079
Jerry Yu92c6b402021-08-27 16:59:09 +08001080cleanup:
1081
1082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1083 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001084}
1085
Jerry Yu687101b2021-09-14 16:03:56 +08001086/*
Jerry Yu4a173382021-10-11 21:45:31 +08001087 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001088 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001089/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001090 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1091 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001092 * to indicate which message is expected and to be parsed next.
1093 */
Jerry Yub85277e2021-10-13 13:36:05 +08001094#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1095#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1096static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1097 const unsigned char *buf,
1098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001099{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001100 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001101 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1102 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1103 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1104 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1105
1106 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1107 *
Jerry Yu4a173382021-10-11 21:45:31 +08001108 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001109 * special value of the SHA-256 of "HelloRetryRequest".
1110 *
1111 * struct {
1112 * ProtocolVersion legacy_version = 0x0303;
1113 * Random random;
1114 * opaque legacy_session_id_echo<0..32>;
1115 * CipherSuite cipher_suite;
1116 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001117 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 * } ServerHello;
1119 *
1120 */
Jerry Yub85277e2021-10-13 13:36:05 +08001121 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001122
1123 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 {
Jerry Yub85277e2021-10-13 13:36:05 +08001125 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 }
1127
Jerry Yub85277e2021-10-13 13:36:05 +08001128 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001129}
1130
Jerry Yu745bb612021-10-13 22:01:04 +08001131/* Fetch and preprocess
1132 * Returns a negative value on failure, and otherwise
1133 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1134 * - SSL_SERVER_HELLO_COORDINATE_HRR
1135 */
Jerry Yub85277e2021-10-13 13:36:05 +08001136static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1137 unsigned char **buf,
1138 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001139{
Jerry Yu4a173382021-10-11 21:45:31 +08001140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001141
XiaokangQian355e09a2022-01-20 11:14:50 +00001142 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1143 MBEDTLS_SSL_HS_SERVER_HELLO,
1144 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001145
Jerry Yub85277e2021-10-13 13:36:05 +08001146 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1147 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 {
Jerry Yu745bb612021-10-13 22:01:04 +08001149 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1151 break;
1152 case SSL_SERVER_HELLO_COORDINATE_HRR:
1153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001154 /* If a client receives a second
1155 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1156 * was itself in response to a HelloRetryRequest), it MUST abort the
1157 * handshake with an "unexpected_message" alert.
1158 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001159 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001160 {
1161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1162 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1163 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1164 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1165 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001166 /*
1167 * Clients must abort the handshake with an "illegal_parameter"
1168 * alert if the HelloRetryRequest would not result in any change
1169 * in the ClientHello.
1170 * In a PSK only key exchange that what we expect.
1171 */
1172 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1173 {
1174 MBEDTLS_SSL_DEBUG_MSG( 1,
1175 ( "Unexpected HRR in pure PSK key exchange." ) );
1176 MBEDTLS_SSL_PEND_FATAL_ALERT(
1177 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1178 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1179 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1180 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001181
XiaokangQiand9e068e2022-01-18 06:23:32 +00001182 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001183
Jerry Yu745bb612021-10-13 22:01:04 +08001184 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001185 }
1186
1187cleanup:
1188
1189 return( ret );
1190}
1191
Jerry Yu4a173382021-10-11 21:45:31 +08001192static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1193 const unsigned char **buf,
1194 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001195{
1196 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001197 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001198
Jerry Yude4fb2c2021-09-19 18:05:08 +08001199 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001200 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
Jerry Yu4a173382021-10-11 21:45:31 +08001202 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001203
1204 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001205 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1206 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001208 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1209 ssl->session_negotiate->id,
1210 ssl->session_negotiate->id_len );
1211 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001212 legacy_session_id_echo_len );
1213
1214 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1215 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1216
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1218 }
1219
Jerry Yu4a173382021-10-11 21:45:31 +08001220 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 *buf = p;
1222
1223 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001224 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001225 return( 0 );
1226}
1227
Jerry Yu4a173382021-10-11 21:45:31 +08001228static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1229 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001230{
Jerry Yu4a173382021-10-11 21:45:31 +08001231 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1232
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001234 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 {
Jerry Yu4a173382021-10-11 21:45:31 +08001236 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001237 {
1238 return( 1 );
1239 }
1240 }
1241 return( 0 );
1242}
1243
1244/* Parse ServerHello message and configure context
1245 *
1246 * struct {
1247 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1248 * Random random;
1249 * opaque legacy_session_id_echo<0..32>;
1250 * CipherSuite cipher_suite;
1251 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001252 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 * } ServerHello;
1254 */
Jerry Yuc068b662021-10-11 22:30:19 +08001255static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1256 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001257 const unsigned char *end,
1258 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001259{
Jerry Yub85277e2021-10-13 13:36:05 +08001260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001262 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001263 size_t extensions_len;
1264 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001265 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001266 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001267 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001268 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001269
1270 /*
1271 * Check there is space for minimal fields
1272 *
1273 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001274 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001275 * - legacy_session_id_echo ( 1 byte ), minimum size
1276 * - cipher_suite ( 2 bytes)
1277 * - legacy_compression_method ( 1 byte )
1278 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001279 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001280
1281 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001282 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1283
Jerry Yu4a173382021-10-11 21:45:31 +08001284 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001285 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001286 * ...
1287 * with ProtocolVersion defined as:
1288 * uint16 ProtocolVersion;
1289 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001290 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1291 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1292 {
1293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1294 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1295 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001296 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1297 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001298 }
1299 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001300
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001301 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001302 * Random random;
1303 * ...
1304 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001305 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001306 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001307 if( !is_hrr )
1308 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001309 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001310 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1311 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1312 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1313 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001314 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001315
Jerry Yu4a173382021-10-11 21:45:31 +08001316 /* ...
1317 * opaque legacy_session_id_echo<0..32>;
1318 * ...
1319 */
1320 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001321 {
XiaokangQian52da5582022-01-26 09:49:29 +00001322 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001323 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001324 }
1325
Jerry Yu4a173382021-10-11 21:45:31 +08001326 /* ...
1327 * CipherSuite cipher_suite;
1328 * ...
1329 * with CipherSuite defined as:
1330 * uint8 CipherSuite[2];
1331 */
1332 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001333 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1334 p += 2;
1335
Jerry Yu4a173382021-10-11 21:45:31 +08001336
XiaokangQian355e09a2022-01-20 11:14:50 +00001337 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001338 /*
1339 * Check whether this ciphersuite is supported and offered.
1340 * Via the force_ciphersuite version we may have instructed the client
1341 * to use a different ciphersuite.
1342 */
Jerry Yu4a173382021-10-11 21:45:31 +08001343 if( ciphersuite_info == NULL ||
1344 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 {
XiaokangQian52da5582022-01-26 09:49:29 +00001346 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001347 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001348 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001349 * If we received an HRR before and that the proposed selected
1350 * ciphersuite in this server hello is not the same as the one
1351 * proposed in the HRR, we abort the handshake and send an
1352 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001353 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001354 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1355 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001356 {
XiaokangQian52da5582022-01-26 09:49:29 +00001357 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001358 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001359
XiaokangQian52da5582022-01-26 09:49:29 +00001360 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001361 {
1362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1363 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001364 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001365 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001366
Jerry Yu4a173382021-10-11 21:45:31 +08001367 /* Configure ciphersuites */
1368 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1369
XiaokangQian355e09a2022-01-20 11:14:50 +00001370 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001371 ssl->session_negotiate->ciphersuite = cipher_suite;
1372
Jerry Yue1b9c292021-09-10 10:08:31 +08001373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1374 cipher_suite, ciphersuite_info->name ) );
1375
1376#if defined(MBEDTLS_HAVE_TIME)
1377 ssl->session_negotiate->start = time( NULL );
1378#endif /* MBEDTLS_HAVE_TIME */
1379
Jerry Yu4a173382021-10-11 21:45:31 +08001380 /* ...
1381 * uint8 legacy_compression_method = 0;
1382 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001383 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001384 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001385 if( p[0] != 0 )
1386 {
Jerry Yub85277e2021-10-13 13:36:05 +08001387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001388 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001389 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001390 }
1391 p++;
1392
Jerry Yub85277e2021-10-13 13:36:05 +08001393 /* ...
1394 * Extension extensions<6..2^16-1>;
1395 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001396 * struct {
1397 * ExtensionType extension_type; (2 bytes)
1398 * opaque extension_data<0..2^16-1>;
1399 * } Extension;
1400 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001401 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001402 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001403 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001404
Jerry Yu4a173382021-10-11 21:45:31 +08001405 /* Check extensions do not go beyond the buffer of data. */
1406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1407 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001408
Jerry Yu4a173382021-10-11 21:45:31 +08001409 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001410
Jerry Yu4a173382021-10-11 21:45:31 +08001411 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001412 {
1413 unsigned int extension_type;
1414 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001415 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001416
Jerry Yu4a173382021-10-11 21:45:31 +08001417 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001418 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1419 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1420 p += 4;
1421
Jerry Yu4a173382021-10-11 21:45:31 +08001422 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001423 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001424
1425 switch( extension_type )
1426 {
XiaokangQianb851da82022-01-14 04:03:11 +00001427 case MBEDTLS_TLS_EXT_COOKIE:
1428
XiaokangQiand9e068e2022-01-18 06:23:32 +00001429 if( !is_hrr )
1430 {
XiaokangQian52da5582022-01-26 09:49:29 +00001431 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001432 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001433 }
1434
XiaokangQian43550bd2022-01-21 04:32:58 +00001435 ret = ssl_tls13_parse_cookie_ext( ssl,
1436 p, extension_data_end );
1437 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001438 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001439 MBEDTLS_SSL_DEBUG_RET( 1,
1440 "ssl_tls13_parse_cookie_ext",
1441 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001442 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001443 }
XiaokangQianb851da82022-01-14 04:03:11 +00001444 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001445
Jerry Yue1b9c292021-09-10 10:08:31 +08001446 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001447 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001448 MBEDTLS_SSL_DEBUG_MSG( 3,
1449 ( "found supported_versions extension" ) );
1450
Jerry Yuc068b662021-10-11 22:30:19 +08001451 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001452 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001453 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001454 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001455 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 break;
1457
1458 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001461
XiaokangQian52da5582022-01-26 09:49:29 +00001462 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001463 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001464
1465#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1466 case MBEDTLS_TLS_EXT_KEY_SHARE:
1467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001468 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1469 {
XiaokangQian52da5582022-01-26 09:49:29 +00001470 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001471 goto cleanup;
1472 }
1473
XiaokangQian53f20b72022-01-18 10:47:33 +00001474 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001475 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001476 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001477 else
XiaokangQianb851da82022-01-14 04:03:11 +00001478 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001479 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001480 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001481 {
1482 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001483 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001484 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001485 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001486 }
1487 break;
1488#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1489
1490 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001491 MBEDTLS_SSL_DEBUG_MSG(
1492 3,
1493 ( "unknown extension found: %u ( ignoring )",
1494 extension_type ) );
1495
XiaokangQian52da5582022-01-26 09:49:29 +00001496 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001497 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001498 }
1499
1500 p += extension_data_len;
1501 }
1502
XiaokangQian78b1fa72022-01-19 06:56:30 +00001503 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001504 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001506 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001507 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001508 }
1509
XiaokangQiand59be772022-01-24 10:12:51 +00001510cleanup:
1511
XiaokangQian52da5582022-01-26 09:49:29 +00001512 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001513 {
1514 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1515 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1516 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1517 }
XiaokangQian52da5582022-01-26 09:49:29 +00001518 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001519 {
1520 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1521 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1522 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1523 }
1524 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001525}
1526
XiaokangQian355e09a2022-01-20 11:14:50 +00001527static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001528{
Jerry Yub85277e2021-10-13 13:36:05 +08001529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001530 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001531 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001532 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001533
Jerry Yub85277e2021-10-13 13:36:05 +08001534 /* Determine the key exchange mode:
1535 * 1) If both the pre_shared_key and key_share extensions were received
1536 * then the key exchange mode is PSK with EPHEMERAL.
1537 * 2) If only the pre_shared_key extension was received then the key
1538 * exchange mode is PSK-only.
1539 * 3) If only the key_share extension was received then the key
1540 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001541 */
Jerry Yub85277e2021-10-13 13:36:05 +08001542 switch( handshake->extensions_present &
1543 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001544 {
Jerry Yu745bb612021-10-13 22:01:04 +08001545 /* Only the pre_shared_key extension was received */
1546 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001547 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001548 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001549
Jerry Yu745bb612021-10-13 22:01:04 +08001550 /* Only the key_share extension was received */
1551 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001552 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001553 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001554
Jerry Yu745bb612021-10-13 22:01:04 +08001555 /* Both the pre_shared_key and key_share extensions were received */
1556 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001557 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001558 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001559
Jerry Yu745bb612021-10-13 22:01:04 +08001560 /* Neither pre_shared_key nor key_share extension was received */
1561 default:
1562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1563 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1564 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001565 }
1566
1567 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1568 *
1569 * TODO: We don't have to do this in case we offered 0-RTT and the
1570 * server accepted it. In this case, we could skip generating
1571 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001572 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001573 if( ret != 0 )
1574 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001576 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001577 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001578 }
1579
1580 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001581 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001582 if( ret != 0 )
1583 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001585 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001586 }
1587
1588 /* Next evolution in key schedule: Establish handshake secret and
1589 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001590 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001591 if( ret != 0 )
1592 {
Jerry Yuc068b662021-10-11 22:30:19 +08001593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1594 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001595 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001596 }
1597
Jerry Yub85277e2021-10-13 13:36:05 +08001598 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001599 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001600 {
1601 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1602 goto cleanup;
1603 }
Jerry Yu0b177842021-09-05 19:41:30 +08001604
1605 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1606 ssl->conf->endpoint,
1607 ssl->session_negotiate->ciphersuite,
1608 &traffic_keys,
1609 ssl );
1610 if( ret != 0 )
1611 {
1612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001613 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001614 }
1615
Jerry Yub85277e2021-10-13 13:36:05 +08001616 handshake->transform_handshake = transform_handshake;
1617 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001618
1619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1620 ssl->session_in = ssl->session_negotiate;
1621
1622 /*
1623 * State machine update
1624 */
1625 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1626
Jerry Yu4a173382021-10-11 21:45:31 +08001627cleanup:
1628
Jerry Yu0b177842021-09-05 19:41:30 +08001629 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001630 if( ret != 0 )
1631 {
Jerry Yub85277e2021-10-13 13:36:05 +08001632 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001633
Jerry Yu4a173382021-10-11 21:45:31 +08001634 MBEDTLS_SSL_PEND_FATAL_ALERT(
1635 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1636 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1637 }
1638 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001639}
1640
XiaokangQian355e09a2022-01-20 11:14:50 +00001641static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001642{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001643#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1645
XiaokangQian647719a2021-12-07 09:16:29 +00001646#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1647 /* If not offering early data, the client sends a dummy CCS record
1648 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001649 * its second ClientHello or before its encrypted handshake flight.
1650 */
XiaokangQian647719a2021-12-07 09:16:29 +00001651 mbedtls_ssl_handshake_set_state( ssl,
1652 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1653#else
1654 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1655#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1656
XiaokangQian78b1fa72022-01-19 06:56:30 +00001657 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001658
XiaokangQian78b1fa72022-01-19 06:56:30 +00001659 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001660 * We are going to re-generate a shared secret corresponding to the group
1661 * selected by the server, which is different from the group for which we
1662 * generated a shared secret in the first client hello.
1663 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001664 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001665 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001666 if( ret != 0 )
1667 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001668#else
1669 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001670#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001671
1672 return( 0 );
1673}
1674
Jerry Yue1b9c292021-09-10 10:08:31 +08001675/*
Jerry Yu4a173382021-10-11 21:45:31 +08001676 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001677 * Handler for MBEDTLS_SSL_SERVER_HELLO
1678 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001679static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001680{
Jerry Yu4a173382021-10-11 21:45:31 +08001681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001682 unsigned char *buf = NULL;
1683 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001684 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001685
1686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1687
1688 /* Coordination step
1689 * - Fetch record
1690 * - Make sure it's either a ServerHello or a HRR.
1691 * - Switch processing routine in case of HRR
1692 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001693 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1694 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1695
XiaokangQian16acd4b2022-01-14 07:35:47 +00001696 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001697 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001698 goto cleanup;
1699 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001700 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001701
XiaokangQianb851da82022-01-14 04:03:11 +00001702 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001703 buf + buf_len,
1704 is_hrr ) );
1705 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001706 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1707
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001708 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1709 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001710
XiaokangQiand9e068e2022-01-18 06:23:32 +00001711 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001712 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001713 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001714 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001715
1716cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1718 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001719 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001720}
1721
1722/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001723 *
1724 * EncryptedExtensions message
1725 *
1726 * The EncryptedExtensions message contains any extensions which
1727 * should be protected, i.e., any which are not needed to establish
1728 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001729 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001730
1731/*
1732 * Overview
1733 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001734
1735/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001736static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001737
XiaokangQian97799ac2021-10-11 10:05:54 +00001738static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1739 const unsigned char *buf,
1740 const unsigned char *end );
1741static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001742
1743/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001744 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001745 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001746static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001747{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001748 int ret;
1749 unsigned char *buf;
1750 size_t buf_len;
1751
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1753
Xiaofei Bai746f9482021-11-12 08:53:56 +00001754 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001755 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001756 &buf, &buf_len ) );
1757
1758 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001759 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001760 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001761
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001762 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1763 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764
XiaokangQian97799ac2021-10-11 10:05:54 +00001765 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001766
1767cleanup:
1768
1769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1770 return( ret );
1771
1772}
1773
XiaokangQian08da26c2021-10-09 10:12:11 +00001774/* Parse EncryptedExtensions message
1775 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001776 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001777 * } EncryptedExtensions;
1778 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001779static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1780 const unsigned char *buf,
1781 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001782{
1783 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001784 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001785 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001786 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001787
XiaokangQian08da26c2021-10-09 10:12:11 +00001788 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001789 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001790 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001791
XiaokangQian97799ac2021-10-11 10:05:54 +00001792 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001793 extensions_end = p + extensions_len;
1794 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001795
XiaokangQian8db25ff2021-10-13 05:56:18 +00001796 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001797 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001798 unsigned int extension_type;
1799 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001800
XiaokangQian08da26c2021-10-09 10:12:11 +00001801 /*
1802 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001803 * ExtensionType extension_type; (2 bytes)
1804 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001805 * } Extension;
1806 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001807 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001808 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1809 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1810 p += 4;
1811
XiaokangQian8db25ff2021-10-13 05:56:18 +00001812 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001813
XiaokangQian97799ac2021-10-11 10:05:54 +00001814 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001815 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001816 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001817 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001818 switch( extension_type )
1819 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001820
XiaokangQian08da26c2021-10-09 10:12:11 +00001821 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1823 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001824
lhuang0486cacac2022-01-21 07:34:27 -08001825#if defined(MBEDTLS_SSL_ALPN)
1826 case MBEDTLS_TLS_EXT_ALPN:
1827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1828
1829 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1830 {
1831 return( ret );
1832 }
1833
1834 break;
1835#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001836 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001837 MBEDTLS_SSL_DEBUG_MSG(
1838 3, ( "unsupported extension found: %u ", extension_type) );
1839 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001840 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001841 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1842 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001843 }
1844
XiaokangQian08da26c2021-10-09 10:12:11 +00001845 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001846 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001847
XiaokangQian97799ac2021-10-11 10:05:54 +00001848 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001849 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001850 {
1851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001852 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001853 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001854 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001855 }
1856
1857 return( ret );
1858}
1859
XiaokangQian97799ac2021-10-11 10:05:54 +00001860static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001861{
Jerry Yua93ac112021-10-27 16:31:48 +08001862#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001863 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001864 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1865 else
Jerry Yud2674312021-10-29 10:08:19 +08001866 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001867#else
1868 ((void) ssl);
1869 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1870#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001871 return( 0 );
1872}
1873
Jerry Yua93ac112021-10-27 16:31:48 +08001874#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001875/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001876 *
1877 * STATE HANDLING: CertificateRequest
1878 *
Jerry Yud2674312021-10-29 10:08:19 +08001879 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001880#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1881#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001882/* Coordination:
1883 * Deals with the ambiguity of not knowing if a CertificateRequest
1884 * will be sent. Returns a negative code on failure, or
1885 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1886 * - SSL_CERTIFICATE_REQUEST_SKIP
1887 * indicating if a Certificate Request is expected or not.
1888 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001889static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001890{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001892
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001893 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001894 {
1895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1896 return( SSL_CERTIFICATE_REQUEST_SKIP );
1897 }
1898
1899 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001900 {
1901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1902 return( ret );
1903 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001904 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001905
1906 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1907 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1908 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001909 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001910 }
1911
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001912 return( SSL_CERTIFICATE_REQUEST_SKIP );
1913}
1914
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001915/*
1916 * ssl_tls13_parse_certificate_request()
1917 * Parse certificate request
1918 * struct {
1919 * opaque certificate_request_context<0..2^8-1>;
1920 * Extension extensions<2..2^16-1>;
1921 * } CertificateRequest;
1922 */
1923static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1924 const unsigned char *buf,
1925 const unsigned char *end )
1926{
1927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1928 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001929 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001930 size_t extensions_len = 0;
1931 const unsigned char *extensions_end;
1932 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001933
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001934 /* ...
1935 * opaque certificate_request_context<0..2^8-1>
1936 * ...
1937 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001938 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1939 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001940 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001941
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001942 if( certificate_request_context_len > 0 )
1943 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001944 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001945 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1946 p, certificate_request_context_len );
1947
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001948 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001949 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001950 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001951 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001952 {
1953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1954 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1955 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001956 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001957 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001958 p += certificate_request_context_len;
1959 }
1960
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001961 /* ...
1962 * Extension extensions<2..2^16-1>;
1963 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001964 */
1965 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1966 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1967 p += 2;
1968
1969 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1970 extensions_end = p + extensions_len;
1971
1972 while( p < extensions_end )
1973 {
1974 unsigned int extension_type;
1975 size_t extension_data_len;
1976
1977 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1978 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1979 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1980 p += 4;
1981
1982 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1983
1984 switch( extension_type )
1985 {
1986 case MBEDTLS_TLS_EXT_SIG_ALG:
1987 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001988 ( "found signature algorithms extension" ) );
1989 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001990 p + extension_data_len );
1991 if( ret != 0 )
1992 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001993 if( ! sig_alg_ext_found )
1994 sig_alg_ext_found = 1;
1995 else
1996 {
1997 MBEDTLS_SSL_DEBUG_MSG( 3,
1998 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001999 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002000 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002001 break;
2002
2003 default:
2004 MBEDTLS_SSL_DEBUG_MSG(
2005 3,
2006 ( "unknown extension found: %u ( ignoring )",
2007 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002008 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002009 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002010 p += extension_data_len;
2011 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002012 /* Check that we consumed all the message. */
2013 if( p != end )
2014 {
2015 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002016 ( "CertificateRequest misaligned" ) );
2017 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002018 }
2019 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002020 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002021 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002022 MBEDTLS_SSL_DEBUG_MSG( 3,
2023 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002024 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002025 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002026
Jerry Yu7840f812022-01-29 10:26:51 +08002027 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002028 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002029
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002030decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002031 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2032 MBEDTLS_ERR_SSL_DECODE_ERROR );
2033 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002034}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002035
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002036/*
2037 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2038 */
2039static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002040{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002042
2043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2044
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2046
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002047 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2048 {
2049 unsigned char *buf;
2050 size_t buf_len;
2051
2052 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2053 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2054 &buf, &buf_len ) );
2055
2056 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2057 buf, buf + buf_len ) );
2058
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002059 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2060 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002061 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002062 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002063 {
2064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002065 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002066 }
2067 else
2068 {
2069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002070 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2071 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002072 }
2073
2074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002075 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002076
Jerry Yud2674312021-10-29 10:08:19 +08002077 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2078
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002079cleanup:
2080
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2082 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002083}
2084
2085/*
Jerry Yu687101b2021-09-14 16:03:56 +08002086 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2087 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002088static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002089{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002090 int ret;
2091
2092 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002093 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002094 return( ret );
2095
Jerry Yu687101b2021-09-14 16:03:56 +08002096 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2097 return( 0 );
2098}
2099
2100/*
2101 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2102 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002103static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002104{
Jerry Yu30b071c2021-09-12 20:16:03 +08002105 int ret;
2106
2107 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2108 if( ret != 0 )
2109 return( ret );
2110
Jerry Yu687101b2021-09-14 16:03:56 +08002111 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2112 return( 0 );
2113}
Jerry Yua93ac112021-10-27 16:31:48 +08002114#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002115
Jerry Yu687101b2021-09-14 16:03:56 +08002116/*
2117 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2118 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002119static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002120{
XiaokangQianac0385c2021-11-03 06:40:11 +00002121 int ret;
2122
XiaokangQianc5c39d52021-11-09 11:55:10 +00002123 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002124 if( ret != 0 )
2125 return( ret );
2126
Ronald Cron49ad6192021-11-24 16:25:31 +01002127#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2128 mbedtls_ssl_handshake_set_state(
2129 ssl,
2130 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2131#else
Jerry Yuca133a32022-02-15 14:22:05 +08002132 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002133#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002134
XiaokangQianac0385c2021-11-03 06:40:11 +00002135 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002136}
2137
2138/*
Jerry Yu566c7812022-01-26 15:41:22 +08002139 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2140 */
2141static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2142{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002143 int non_empty_certificate_msg = 0;
2144
Jerry Yu5cc35062022-01-28 16:16:08 +08002145 MBEDTLS_SSL_DEBUG_MSG( 1,
2146 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002147 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002148
Ronald Cron9df7c802022-03-08 18:38:54 +01002149#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002150 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002151 {
2152 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2153 if( ret != 0 )
2154 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002155
Ronald Cron7a94aca2022-03-09 07:44:27 +01002156 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2157 non_empty_certificate_msg = 1;
2158 }
2159 else
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2162 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002163#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002164
Ronald Cron7a94aca2022-03-09 07:44:27 +01002165 if( non_empty_certificate_msg )
2166 {
2167 mbedtls_ssl_handshake_set_state( ssl,
2168 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2169 }
2170 else
2171 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2172
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002173 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002174}
2175
Ronald Cron9df7c802022-03-08 18:38:54 +01002176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002177/*
2178 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2179 */
2180static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2181{
Ronald Crona8b38872022-03-09 07:59:25 +01002182 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2183
2184 if( ret == 0 )
2185 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2186
2187 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002188}
Jerry Yu90f152d2022-01-29 22:12:42 +08002189#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002190
2191/*
Jerry Yu687101b2021-09-14 16:03:56 +08002192 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2193 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002194static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002195{
XiaokangQian0fa66432021-11-15 03:33:57 +00002196 int ret;
2197
2198 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2199 if( ret != 0 )
2200 return( ret );
2201
2202 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2203 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002204}
2205
2206/*
2207 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2208 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002209static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002210{
Jerry Yu378254d2021-10-30 21:44:47 +08002211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002212 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002213 return( 0 );
2214}
2215
2216/*
2217 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2218 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002219static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002220{
Jerry Yu378254d2021-10-30 21:44:47 +08002221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2222 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2223
2224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2225 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2226
2227 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2228
2229 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2230 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002231}
2232
Jerry Yu92c6b402021-08-27 16:59:09 +08002233int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002234{
Jerry Yu92c6b402021-08-27 16:59:09 +08002235 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002236
Jerry Yue3b34122021-09-28 17:53:35 +08002237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2238 mbedtls_ssl_states_str( ssl->state ),
2239 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002240
2241 switch( ssl->state )
2242 {
2243 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002244 * ssl->state is initialized as HELLO_REQUEST. It is the same
2245 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002246 */
2247 case MBEDTLS_SSL_HELLO_REQUEST:
2248 case MBEDTLS_SSL_CLIENT_HELLO:
2249 ret = ssl_tls13_write_client_hello( ssl );
2250 break;
2251
2252 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002253 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002254 break;
2255
2256 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002257 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002258 break;
2259
Jerry Yua93ac112021-10-27 16:31:48 +08002260#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002261 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2262 ret = ssl_tls13_process_certificate_request( ssl );
2263 break;
2264
Jerry Yu687101b2021-09-14 16:03:56 +08002265 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002266 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002267 break;
2268
2269 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002270 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002271 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002272#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002273
2274 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002275 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002276 break;
2277
Jerry Yu566c7812022-01-26 15:41:22 +08002278 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2279 ret = ssl_tls13_write_client_certificate( ssl );
2280 break;
2281
Ronald Cron9df7c802022-03-08 18:38:54 +01002282#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002283 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2284 ret = ssl_tls13_write_client_certificate_verify( ssl );
2285 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002286#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002287
Jerry Yu687101b2021-09-14 16:03:56 +08002288 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002289 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002290 break;
2291
2292 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002293 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002294 break;
2295
2296 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002297 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002298 break;
2299
Ronald Cron49ad6192021-11-24 16:25:31 +01002300 /*
2301 * Injection of dummy-CCS's for middlebox compatibility
2302 */
2303#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002304 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002305 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2306 if( ret == 0 )
2307 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2308 break;
2309
2310 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2311 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2312 if( ret == 0 )
2313 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002314 break;
2315#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2316
Jerry Yu92c6b402021-08-27 16:59:09 +08002317 default:
2318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2320 }
2321
2322 return( ret );
2323}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002324
Jerry Yufb4b6472022-01-27 15:03:26 +08002325#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002326
Jerry Yufb4b6472022-01-27 15:03:26 +08002327