blob: 79ad9772d15ceb2b35289a83a6a2abb0dbb01c78 [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
945 /* Keeping track of the included extensions */
946 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800947
948 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800949 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000950 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800951 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800952
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100953 ret = ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800954 if( ret != 0 )
955 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800956 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800957
lhuang0486cacac2022-01-21 07:34:27 -0800958#if defined(MBEDTLS_SSL_ALPN)
Ronald Crona0855a62022-03-09 10:04:54 +0100959 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
lhuang0486cacac2022-01-21 07:34:27 -0800960 if( ret != 0 )
961 return( ret );
962 p += output_len;
963#endif /* MBEDTLS_SSL_ALPN */
964
Jerry Yubc20bdd2021-08-24 15:59:48 +0800965#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf46b0162022-01-11 16:28:00 +0800966 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
967 {
968 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
969 if( ret != 0 )
970 return( ret );
971 p += output_len;
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100972 }
Jerry Yu6a643102021-08-31 14:40:36 +0800973
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100974 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
975 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800976 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800977 if( ret != 0 )
978 return( ret );
979 p += output_len;
980 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800981#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
982
Xiaofei Bai15a56812021-11-05 10:52:12 +0000983#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000984 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000985 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
986 if( ret != 0 )
987 return( ret );
988 p += output_len;
989#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
990
Jerry Yubc20bdd2021-08-24 15:59:48 +0800991 /* Add more extensions here */
992
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800993 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000994 extensions_len = p - p_extensions_len - 2;
995 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800997 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000998 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800999
Xiaofei Baid25fab62021-12-02 06:36:27 +00001000 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +08001001 return( 0 );
1002}
1003
Jerry Yu92c6b402021-08-27 16:59:09 +08001004static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
1005{
1006 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +08001007
Jerry Yu92c6b402021-08-27 16:59:09 +08001008 if( ssl->conf->f_rng == NULL )
1009 {
1010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1011 return( MBEDTLS_ERR_SSL_NO_RNG );
1012 }
Jerry Yuef6b36b2021-08-24 16:29:02 +08001013
Jerry Yu92c6b402021-08-27 16:59:09 +08001014 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1015 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001016 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +08001017 {
Jerry Yu8c02bb42021-09-03 21:09:22 +08001018 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +08001019 return( ret );
1020 }
Jerry Yu6f13f642021-08-26 17:18:15 +08001021
Ronald Cron49ad6192021-11-24 16:25:31 +01001022#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1023 /*
1024 * Create a session identifier for the purpose of middlebox compatibility
1025 * only if one has not been created already.
1026 */
1027 if( ssl->session_negotiate->id_len == 0 )
1028 {
1029 /* Creating a session id with 32 byte length */
1030 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1031 ssl->session_negotiate->id, 32 ) ) != 0 )
1032 {
1033 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
1034 return( ret );
1035 }
1036 ssl->session_negotiate->id_len = 32;
1037 }
1038#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1039
Jerry Yu6f13f642021-08-26 17:18:15 +08001040 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001041}
1042
Jerry Yu92c6b402021-08-27 16:59:09 +08001043/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001044 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001045 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001046 */
1047static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001048{
Jerry Yu92c6b402021-08-27 16:59:09 +08001049 int ret = 0;
1050 unsigned char *buf;
1051 size_t buf_len, msg_len;
1052
1053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1054
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001055 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001056
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001057 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1058 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1059 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001060
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001061 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001062 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001063 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001064
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001065 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1066 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001067 msg_len );
1068 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001069
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001070 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1071 buf_len,
1072 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001073
Ronald Cron3addfa42022-02-08 16:10:25 +01001074 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1075
Jerry Yu92c6b402021-08-27 16:59:09 +08001076cleanup:
1077
1078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1079 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001080}
1081
Jerry Yu687101b2021-09-14 16:03:56 +08001082/*
Jerry Yu4a173382021-10-11 21:45:31 +08001083 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001085/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001086 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1087 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001088 * to indicate which message is expected and to be parsed next.
1089 */
Jerry Yub85277e2021-10-13 13:36:05 +08001090#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1091#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1092static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1093 const unsigned char *buf,
1094 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001095{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001096 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001097 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1098 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1099 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1100 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1101
1102 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1103 *
Jerry Yu4a173382021-10-11 21:45:31 +08001104 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001105 * special value of the SHA-256 of "HelloRetryRequest".
1106 *
1107 * struct {
1108 * ProtocolVersion legacy_version = 0x0303;
1109 * Random random;
1110 * opaque legacy_session_id_echo<0..32>;
1111 * CipherSuite cipher_suite;
1112 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001113 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001114 * } ServerHello;
1115 *
1116 */
Jerry Yub85277e2021-10-13 13:36:05 +08001117 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001118
1119 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001120 {
Jerry Yub85277e2021-10-13 13:36:05 +08001121 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 }
1123
Jerry Yub85277e2021-10-13 13:36:05 +08001124 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001125}
1126
Jerry Yu745bb612021-10-13 22:01:04 +08001127/* Fetch and preprocess
1128 * Returns a negative value on failure, and otherwise
1129 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1130 * - SSL_SERVER_HELLO_COORDINATE_HRR
1131 */
Jerry Yub85277e2021-10-13 13:36:05 +08001132static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1133 unsigned char **buf,
1134 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001135{
Jerry Yu4a173382021-10-11 21:45:31 +08001136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001137
XiaokangQian355e09a2022-01-20 11:14:50 +00001138 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1139 MBEDTLS_SSL_HS_SERVER_HELLO,
1140 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001141
Jerry Yub85277e2021-10-13 13:36:05 +08001142 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1143 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 {
Jerry Yu745bb612021-10-13 22:01:04 +08001145 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1147 break;
1148 case SSL_SERVER_HELLO_COORDINATE_HRR:
1149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001150 /* If a client receives a second
1151 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1152 * was itself in response to a HelloRetryRequest), it MUST abort the
1153 * handshake with an "unexpected_message" alert.
1154 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001155 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001156 {
1157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1158 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1159 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1160 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1161 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001162 /*
1163 * Clients must abort the handshake with an "illegal_parameter"
1164 * alert if the HelloRetryRequest would not result in any change
1165 * in the ClientHello.
1166 * In a PSK only key exchange that what we expect.
1167 */
1168 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1169 {
1170 MBEDTLS_SSL_DEBUG_MSG( 1,
1171 ( "Unexpected HRR in pure PSK key exchange." ) );
1172 MBEDTLS_SSL_PEND_FATAL_ALERT(
1173 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1174 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1175 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1176 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001177
XiaokangQiand9e068e2022-01-18 06:23:32 +00001178 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001179
Jerry Yu745bb612021-10-13 22:01:04 +08001180 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001181 }
1182
1183cleanup:
1184
1185 return( ret );
1186}
1187
Jerry Yu4a173382021-10-11 21:45:31 +08001188static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1189 const unsigned char **buf,
1190 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001191{
1192 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001193 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001194
Jerry Yude4fb2c2021-09-19 18:05:08 +08001195 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001196 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001197
Jerry Yu4a173382021-10-11 21:45:31 +08001198 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
1200 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001201 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1202 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001203 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001204 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1205 ssl->session_negotiate->id,
1206 ssl->session_negotiate->id_len );
1207 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001208 legacy_session_id_echo_len );
1209
1210 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1211 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1212
Jerry Yue1b9c292021-09-10 10:08:31 +08001213 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1214 }
1215
Jerry Yu4a173382021-10-11 21:45:31 +08001216 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 *buf = p;
1218
1219 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001220 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 return( 0 );
1222}
1223
Jerry Yu4a173382021-10-11 21:45:31 +08001224static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1225 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001226{
Jerry Yu4a173382021-10-11 21:45:31 +08001227 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1228
Jerry Yue1b9c292021-09-10 10:08:31 +08001229 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001230 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001231 {
Jerry Yu4a173382021-10-11 21:45:31 +08001232 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 {
1234 return( 1 );
1235 }
1236 }
1237 return( 0 );
1238}
1239
1240/* Parse ServerHello message and configure context
1241 *
1242 * struct {
1243 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1244 * Random random;
1245 * opaque legacy_session_id_echo<0..32>;
1246 * CipherSuite cipher_suite;
1247 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001248 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001249 * } ServerHello;
1250 */
Jerry Yuc068b662021-10-11 22:30:19 +08001251static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1252 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001253 const unsigned char *end,
1254 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001255{
Jerry Yub85277e2021-10-13 13:36:05 +08001256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001257 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001258 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001259 size_t extensions_len;
1260 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001262 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001263 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001264 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001265
1266 /*
1267 * Check there is space for minimal fields
1268 *
1269 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001270 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001271 * - legacy_session_id_echo ( 1 byte ), minimum size
1272 * - cipher_suite ( 2 bytes)
1273 * - legacy_compression_method ( 1 byte )
1274 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001275 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001276
1277 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001278 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1279
Jerry Yu4a173382021-10-11 21:45:31 +08001280 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001281 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001282 * ...
1283 * with ProtocolVersion defined as:
1284 * uint16 ProtocolVersion;
1285 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001286 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1287 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1288 {
1289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1290 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1291 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001292 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1293 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001294 }
1295 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001296
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001297 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001298 * Random random;
1299 * ...
1300 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001301 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001302 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001303 if( !is_hrr )
1304 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001305 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001306 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1307 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1308 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1309 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001310 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001311
Jerry Yu4a173382021-10-11 21:45:31 +08001312 /* ...
1313 * opaque legacy_session_id_echo<0..32>;
1314 * ...
1315 */
1316 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001317 {
XiaokangQian52da5582022-01-26 09:49:29 +00001318 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001319 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001320 }
1321
Jerry Yu4a173382021-10-11 21:45:31 +08001322 /* ...
1323 * CipherSuite cipher_suite;
1324 * ...
1325 * with CipherSuite defined as:
1326 * uint8 CipherSuite[2];
1327 */
1328 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001329 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1330 p += 2;
1331
Jerry Yu4a173382021-10-11 21:45:31 +08001332
XiaokangQian355e09a2022-01-20 11:14:50 +00001333 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001334 /*
1335 * Check whether this ciphersuite is supported and offered.
1336 * Via the force_ciphersuite version we may have instructed the client
1337 * to use a different ciphersuite.
1338 */
Jerry Yu4a173382021-10-11 21:45:31 +08001339 if( ciphersuite_info == NULL ||
1340 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001341 {
XiaokangQian52da5582022-01-26 09:49:29 +00001342 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001343 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001344 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001345 * If we received an HRR before and that the proposed selected
1346 * ciphersuite in this server hello is not the same as the one
1347 * proposed in the HRR, we abort the handshake and send an
1348 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001349 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001350 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1351 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001352 {
XiaokangQian52da5582022-01-26 09:49:29 +00001353 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001354 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001355
XiaokangQian52da5582022-01-26 09:49:29 +00001356 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001357 {
1358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1359 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001360 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001361 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001362
Jerry Yu4a173382021-10-11 21:45:31 +08001363 /* Configure ciphersuites */
1364 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1365
XiaokangQian355e09a2022-01-20 11:14:50 +00001366 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001367 ssl->session_negotiate->ciphersuite = cipher_suite;
1368
Jerry Yue1b9c292021-09-10 10:08:31 +08001369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1370 cipher_suite, ciphersuite_info->name ) );
1371
1372#if defined(MBEDTLS_HAVE_TIME)
1373 ssl->session_negotiate->start = time( NULL );
1374#endif /* MBEDTLS_HAVE_TIME */
1375
Jerry Yu4a173382021-10-11 21:45:31 +08001376 /* ...
1377 * uint8 legacy_compression_method = 0;
1378 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001379 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001380 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001381 if( p[0] != 0 )
1382 {
Jerry Yub85277e2021-10-13 13:36:05 +08001383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001384 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001385 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001386 }
1387 p++;
1388
Jerry Yub85277e2021-10-13 13:36:05 +08001389 /* ...
1390 * Extension extensions<6..2^16-1>;
1391 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001392 * struct {
1393 * ExtensionType extension_type; (2 bytes)
1394 * opaque extension_data<0..2^16-1>;
1395 * } Extension;
1396 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001397 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001398 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001399 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001400
Jerry Yu4a173382021-10-11 21:45:31 +08001401 /* Check extensions do not go beyond the buffer of data. */
1402 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1403 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001404
Jerry Yu4a173382021-10-11 21:45:31 +08001405 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001406
Jerry Yu4a173382021-10-11 21:45:31 +08001407 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001408 {
1409 unsigned int extension_type;
1410 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001411 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001412
Jerry Yu4a173382021-10-11 21:45:31 +08001413 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001414 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1415 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1416 p += 4;
1417
Jerry Yu4a173382021-10-11 21:45:31 +08001418 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001419 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001420
1421 switch( extension_type )
1422 {
XiaokangQianb851da82022-01-14 04:03:11 +00001423 case MBEDTLS_TLS_EXT_COOKIE:
1424
XiaokangQiand9e068e2022-01-18 06:23:32 +00001425 if( !is_hrr )
1426 {
XiaokangQian52da5582022-01-26 09:49:29 +00001427 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001428 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001429 }
1430
XiaokangQian43550bd2022-01-21 04:32:58 +00001431 ret = ssl_tls13_parse_cookie_ext( ssl,
1432 p, extension_data_end );
1433 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001434 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001435 MBEDTLS_SSL_DEBUG_RET( 1,
1436 "ssl_tls13_parse_cookie_ext",
1437 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001438 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001439 }
XiaokangQianb851da82022-01-14 04:03:11 +00001440 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001441
Jerry Yue1b9c292021-09-10 10:08:31 +08001442 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001443 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 MBEDTLS_SSL_DEBUG_MSG( 3,
1445 ( "found supported_versions extension" ) );
1446
Jerry Yuc068b662021-10-11 22:30:19 +08001447 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001448 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001449 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001450 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001451 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001452 break;
1453
1454 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001457
XiaokangQian52da5582022-01-26 09:49:29 +00001458 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001459 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001460
1461#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1462 case MBEDTLS_TLS_EXT_KEY_SHARE:
1463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001464 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1465 {
XiaokangQian52da5582022-01-26 09:49:29 +00001466 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001467 goto cleanup;
1468 }
1469
XiaokangQian53f20b72022-01-18 10:47:33 +00001470 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001471 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001472 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001473 else
XiaokangQianb851da82022-01-14 04:03:11 +00001474 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001475 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001476 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001477 {
1478 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001479 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001480 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001481 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001482 }
1483 break;
1484#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1485
1486 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001487 MBEDTLS_SSL_DEBUG_MSG(
1488 3,
1489 ( "unknown extension found: %u ( ignoring )",
1490 extension_type ) );
1491
XiaokangQian52da5582022-01-26 09:49:29 +00001492 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001493 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001494 }
1495
1496 p += extension_data_len;
1497 }
1498
XiaokangQian78b1fa72022-01-19 06:56:30 +00001499 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001500 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001502 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001503 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001504 }
1505
XiaokangQiand59be772022-01-24 10:12:51 +00001506cleanup:
1507
XiaokangQian52da5582022-01-26 09:49:29 +00001508 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001509 {
1510 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1511 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1512 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1513 }
XiaokangQian52da5582022-01-26 09:49:29 +00001514 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001515 {
1516 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1517 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1518 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1519 }
1520 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001521}
1522
XiaokangQian355e09a2022-01-20 11:14:50 +00001523static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001524{
Jerry Yub85277e2021-10-13 13:36:05 +08001525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001526 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001527 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001528 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001529
Jerry Yub85277e2021-10-13 13:36:05 +08001530 /* Determine the key exchange mode:
1531 * 1) If both the pre_shared_key and key_share extensions were received
1532 * then the key exchange mode is PSK with EPHEMERAL.
1533 * 2) If only the pre_shared_key extension was received then the key
1534 * exchange mode is PSK-only.
1535 * 3) If only the key_share extension was received then the key
1536 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001537 */
Jerry Yub85277e2021-10-13 13:36:05 +08001538 switch( handshake->extensions_present &
1539 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001540 {
Jerry Yu745bb612021-10-13 22:01:04 +08001541 /* Only the pre_shared_key extension was received */
1542 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001543 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001544 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001545
Jerry Yu745bb612021-10-13 22:01:04 +08001546 /* Only the key_share extension was received */
1547 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001548 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001549 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001550
Jerry Yu745bb612021-10-13 22:01:04 +08001551 /* Both the pre_shared_key and key_share extensions were received */
1552 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001553 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001554 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001555
Jerry Yu745bb612021-10-13 22:01:04 +08001556 /* Neither pre_shared_key nor key_share extension was received */
1557 default:
1558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1559 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1560 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001561 }
1562
1563 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1564 *
1565 * TODO: We don't have to do this in case we offered 0-RTT and the
1566 * server accepted it. In this case, we could skip generating
1567 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001568 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001569 if( ret != 0 )
1570 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001572 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001573 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001574 }
1575
1576 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001577 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001578 if( ret != 0 )
1579 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001581 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001582 }
1583
1584 /* Next evolution in key schedule: Establish handshake secret and
1585 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001586 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001587 if( ret != 0 )
1588 {
Jerry Yuc068b662021-10-11 22:30:19 +08001589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1590 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001591 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001592 }
1593
Jerry Yub85277e2021-10-13 13:36:05 +08001594 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001595 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001596 {
1597 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1598 goto cleanup;
1599 }
Jerry Yu0b177842021-09-05 19:41:30 +08001600
1601 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1602 ssl->conf->endpoint,
1603 ssl->session_negotiate->ciphersuite,
1604 &traffic_keys,
1605 ssl );
1606 if( ret != 0 )
1607 {
1608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001609 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001610 }
1611
Jerry Yub85277e2021-10-13 13:36:05 +08001612 handshake->transform_handshake = transform_handshake;
1613 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001614
1615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1616 ssl->session_in = ssl->session_negotiate;
1617
1618 /*
1619 * State machine update
1620 */
1621 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1622
Jerry Yu4a173382021-10-11 21:45:31 +08001623cleanup:
1624
Jerry Yu0b177842021-09-05 19:41:30 +08001625 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001626 if( ret != 0 )
1627 {
Jerry Yub85277e2021-10-13 13:36:05 +08001628 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001629
Jerry Yu4a173382021-10-11 21:45:31 +08001630 MBEDTLS_SSL_PEND_FATAL_ALERT(
1631 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1632 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1633 }
1634 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001635}
1636
XiaokangQian355e09a2022-01-20 11:14:50 +00001637static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001638{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001639#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1641
XiaokangQian647719a2021-12-07 09:16:29 +00001642#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1643 /* If not offering early data, the client sends a dummy CCS record
1644 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001645 * its second ClientHello or before its encrypted handshake flight.
1646 */
XiaokangQian647719a2021-12-07 09:16:29 +00001647 mbedtls_ssl_handshake_set_state( ssl,
1648 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1649#else
1650 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1651#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1652
XiaokangQian78b1fa72022-01-19 06:56:30 +00001653 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001654
XiaokangQian78b1fa72022-01-19 06:56:30 +00001655 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001656 * We are going to re-generate a shared secret corresponding to the group
1657 * selected by the server, which is different from the group for which we
1658 * generated a shared secret in the first client hello.
1659 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001660 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001661 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001662 if( ret != 0 )
1663 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001664#else
1665 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001666#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001667
1668 return( 0 );
1669}
1670
Jerry Yue1b9c292021-09-10 10:08:31 +08001671/*
Jerry Yu4a173382021-10-11 21:45:31 +08001672 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001673 * Handler for MBEDTLS_SSL_SERVER_HELLO
1674 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001675static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001676{
Jerry Yu4a173382021-10-11 21:45:31 +08001677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001678 unsigned char *buf = NULL;
1679 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001680 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001681
1682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1683
1684 /* Coordination step
1685 * - Fetch record
1686 * - Make sure it's either a ServerHello or a HRR.
1687 * - Switch processing routine in case of HRR
1688 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001689 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1690 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1691
XiaokangQian16acd4b2022-01-14 07:35:47 +00001692 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001693 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001694 goto cleanup;
1695 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001696 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001697
XiaokangQianb851da82022-01-14 04:03:11 +00001698 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001699 buf + buf_len,
1700 is_hrr ) );
1701 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001702 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1703
XiaokangQianb851da82022-01-14 04:03:11 +00001704 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1705 MBEDTLS_SSL_HS_SERVER_HELLO,
1706 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001707
XiaokangQiand9e068e2022-01-18 06:23:32 +00001708 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001709 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001710 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001711 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001712
1713cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1715 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001716 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001717}
1718
1719/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001720 *
1721 * EncryptedExtensions message
1722 *
1723 * The EncryptedExtensions message contains any extensions which
1724 * should be protected, i.e., any which are not needed to establish
1725 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001726 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001727
1728/*
1729 * Overview
1730 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001731
1732/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001733static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001734
XiaokangQian97799ac2021-10-11 10:05:54 +00001735static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1736 const unsigned char *buf,
1737 const unsigned char *end );
1738static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001739
1740/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001741 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001742 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001743static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001744{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001745 int ret;
1746 unsigned char *buf;
1747 size_t buf_len;
1748
1749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1750
Xiaofei Bai746f9482021-11-12 08:53:56 +00001751 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001752 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001753 &buf, &buf_len ) );
1754
1755 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001756 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001757 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001758
Xiaofei Bai746f9482021-11-12 08:53:56 +00001759 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001760 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001761
XiaokangQian97799ac2021-10-11 10:05:54 +00001762 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001763
1764cleanup:
1765
1766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1767 return( ret );
1768
1769}
1770
XiaokangQian08da26c2021-10-09 10:12:11 +00001771/* Parse EncryptedExtensions message
1772 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001773 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001774 * } EncryptedExtensions;
1775 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001776static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1777 const unsigned char *buf,
1778 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001779{
1780 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001781 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001782 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001783 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784
XiaokangQian08da26c2021-10-09 10:12:11 +00001785 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001786 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001787 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001788
XiaokangQian97799ac2021-10-11 10:05:54 +00001789 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001790 extensions_end = p + extensions_len;
1791 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001792
XiaokangQian8db25ff2021-10-13 05:56:18 +00001793 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001794 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001795 unsigned int extension_type;
1796 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001797
XiaokangQian08da26c2021-10-09 10:12:11 +00001798 /*
1799 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001800 * ExtensionType extension_type; (2 bytes)
1801 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001802 * } Extension;
1803 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001804 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001805 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1806 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1807 p += 4;
1808
XiaokangQian8db25ff2021-10-13 05:56:18 +00001809 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001810
XiaokangQian97799ac2021-10-11 10:05:54 +00001811 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001812 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001813 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001814 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001815 switch( extension_type )
1816 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001817
XiaokangQian08da26c2021-10-09 10:12:11 +00001818 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1820 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001821
lhuang0486cacac2022-01-21 07:34:27 -08001822#if defined(MBEDTLS_SSL_ALPN)
1823 case MBEDTLS_TLS_EXT_ALPN:
1824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1825
1826 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1827 {
1828 return( ret );
1829 }
1830
1831 break;
1832#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001833 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001834 MBEDTLS_SSL_DEBUG_MSG(
1835 3, ( "unsupported extension found: %u ", extension_type) );
1836 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001837 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001838 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1839 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001840 }
1841
XiaokangQian08da26c2021-10-09 10:12:11 +00001842 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001843 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001844
XiaokangQian97799ac2021-10-11 10:05:54 +00001845 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001846 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001847 {
1848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001849 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001850 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001851 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001852 }
1853
1854 return( ret );
1855}
1856
XiaokangQian97799ac2021-10-11 10:05:54 +00001857static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001858{
Jerry Yua93ac112021-10-27 16:31:48 +08001859#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001860 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001861 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1862 else
Jerry Yud2674312021-10-29 10:08:19 +08001863 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001864#else
1865 ((void) ssl);
1866 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1867#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001868 return( 0 );
1869}
1870
Jerry Yua93ac112021-10-27 16:31:48 +08001871#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001872/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001873 *
1874 * STATE HANDLING: CertificateRequest
1875 *
Jerry Yud2674312021-10-29 10:08:19 +08001876 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001877#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1878#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001879/* Coordination:
1880 * Deals with the ambiguity of not knowing if a CertificateRequest
1881 * will be sent. Returns a negative code on failure, or
1882 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1883 * - SSL_CERTIFICATE_REQUEST_SKIP
1884 * indicating if a Certificate Request is expected or not.
1885 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001886static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001887{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001889
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001890 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1893 return( SSL_CERTIFICATE_REQUEST_SKIP );
1894 }
1895
1896 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001897 {
1898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1899 return( ret );
1900 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001901 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001902
1903 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1904 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1905 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001906 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001907 }
1908
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001909 return( SSL_CERTIFICATE_REQUEST_SKIP );
1910}
1911
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001912/*
1913 * ssl_tls13_parse_certificate_request()
1914 * Parse certificate request
1915 * struct {
1916 * opaque certificate_request_context<0..2^8-1>;
1917 * Extension extensions<2..2^16-1>;
1918 * } CertificateRequest;
1919 */
1920static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1921 const unsigned char *buf,
1922 const unsigned char *end )
1923{
1924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1925 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001926 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001927 size_t extensions_len = 0;
1928 const unsigned char *extensions_end;
1929 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001930
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001931 /* ...
1932 * opaque certificate_request_context<0..2^8-1>
1933 * ...
1934 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001935 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1936 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001937 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001938
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001939 if( certificate_request_context_len > 0 )
1940 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001941 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001942 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1943 p, certificate_request_context_len );
1944
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001945 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001946 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001947 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001948 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001949 {
1950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1951 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1952 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001953 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001954 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955 p += certificate_request_context_len;
1956 }
1957
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001958 /* ...
1959 * Extension extensions<2..2^16-1>;
1960 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961 */
1962 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1963 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1964 p += 2;
1965
1966 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1967 extensions_end = p + extensions_len;
1968
1969 while( p < extensions_end )
1970 {
1971 unsigned int extension_type;
1972 size_t extension_data_len;
1973
1974 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1975 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1976 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1977 p += 4;
1978
1979 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1980
1981 switch( extension_type )
1982 {
1983 case MBEDTLS_TLS_EXT_SIG_ALG:
1984 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001985 ( "found signature algorithms extension" ) );
1986 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 p + extension_data_len );
1988 if( ret != 0 )
1989 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001990 if( ! sig_alg_ext_found )
1991 sig_alg_ext_found = 1;
1992 else
1993 {
1994 MBEDTLS_SSL_DEBUG_MSG( 3,
1995 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001996 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001997 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001998 break;
1999
2000 default:
2001 MBEDTLS_SSL_DEBUG_MSG(
2002 3,
2003 ( "unknown extension found: %u ( ignoring )",
2004 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002005 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002006 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002007 p += extension_data_len;
2008 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002009 /* Check that we consumed all the message. */
2010 if( p != end )
2011 {
2012 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002013 ( "CertificateRequest misaligned" ) );
2014 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002015 }
2016 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002017 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002018 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002019 MBEDTLS_SSL_DEBUG_MSG( 3,
2020 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002021 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002022 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002023
Jerry Yu7840f812022-01-29 10:26:51 +08002024 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002025 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002026
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002027decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002028 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2029 MBEDTLS_ERR_SSL_DECODE_ERROR );
2030 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002031}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002032
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002033/*
2034 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2035 */
2036static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002037{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002039
2040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2041
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002042 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2043
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002044 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2045 {
2046 unsigned char *buf;
2047 size_t buf_len;
2048
2049 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2050 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2051 &buf, &buf_len ) );
2052
2053 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2054 buf, buf + buf_len ) );
2055
2056 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2057 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2058 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002059 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002060 {
2061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002062 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002063 }
2064 else
2065 {
2066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002067 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2068 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002069 }
2070
2071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002072 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002073
Jerry Yud2674312021-10-29 10:08:19 +08002074 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2075
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002076cleanup:
2077
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2079 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002080}
2081
2082/*
Jerry Yu687101b2021-09-14 16:03:56 +08002083 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2084 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002085static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002086{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002087 int ret;
2088
2089 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002090 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002091 return( ret );
2092
Jerry Yu687101b2021-09-14 16:03:56 +08002093 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2094 return( 0 );
2095}
2096
2097/*
2098 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2099 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002100static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002101{
Jerry Yu30b071c2021-09-12 20:16:03 +08002102 int ret;
2103
2104 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2105 if( ret != 0 )
2106 return( ret );
2107
Jerry Yu687101b2021-09-14 16:03:56 +08002108 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2109 return( 0 );
2110}
Jerry Yua93ac112021-10-27 16:31:48 +08002111#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002112
Jerry Yu687101b2021-09-14 16:03:56 +08002113/*
2114 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2115 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002116static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002117{
XiaokangQianac0385c2021-11-03 06:40:11 +00002118 int ret;
2119
XiaokangQianc5c39d52021-11-09 11:55:10 +00002120 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002121 if( ret != 0 )
2122 return( ret );
2123
Ronald Cron49ad6192021-11-24 16:25:31 +01002124#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2125 mbedtls_ssl_handshake_set_state(
2126 ssl,
2127 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2128#else
Jerry Yuca133a32022-02-15 14:22:05 +08002129 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002130#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002131
XiaokangQianac0385c2021-11-03 06:40:11 +00002132 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002133}
2134
2135/*
Jerry Yu566c7812022-01-26 15:41:22 +08002136 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2137 */
2138static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2139{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002140 int non_empty_certificate_msg = 0;
2141
Jerry Yu5cc35062022-01-28 16:16:08 +08002142 MBEDTLS_SSL_DEBUG_MSG( 1,
2143 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002144 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002145
Ronald Cron9df7c802022-03-08 18:38:54 +01002146#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002147 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002148 {
2149 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2150 if( ret != 0 )
2151 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002152
Ronald Cron7a94aca2022-03-09 07:44:27 +01002153 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2154 non_empty_certificate_msg = 1;
2155 }
2156 else
2157 {
2158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2159 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002160#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002161
Ronald Cron7a94aca2022-03-09 07:44:27 +01002162 if( non_empty_certificate_msg )
2163 {
2164 mbedtls_ssl_handshake_set_state( ssl,
2165 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2166 }
2167 else
2168 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2169
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002170 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002171}
2172
Ronald Cron9df7c802022-03-08 18:38:54 +01002173#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002174/*
2175 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2176 */
2177static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2178{
Ronald Crona8b38872022-03-09 07:59:25 +01002179 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2180
2181 if( ret == 0 )
2182 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2183
2184 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002185}
Jerry Yu90f152d2022-01-29 22:12:42 +08002186#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002187
2188/*
Jerry Yu687101b2021-09-14 16:03:56 +08002189 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2190 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002191static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002192{
XiaokangQian0fa66432021-11-15 03:33:57 +00002193 int ret;
2194
2195 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2196 if( ret != 0 )
2197 return( ret );
2198
2199 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2200 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002201}
2202
2203/*
2204 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2205 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002206static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002207{
Jerry Yu378254d2021-10-30 21:44:47 +08002208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002209 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002210 return( 0 );
2211}
2212
2213/*
2214 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2215 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002216static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002217{
Jerry Yu378254d2021-10-30 21:44:47 +08002218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2219 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2220
2221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2222 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2223
2224 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2225
2226 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2227 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002228}
2229
Jerry Yu92c6b402021-08-27 16:59:09 +08002230int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002231{
Jerry Yu92c6b402021-08-27 16:59:09 +08002232 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002233
Jerry Yue3b34122021-09-28 17:53:35 +08002234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2235 mbedtls_ssl_states_str( ssl->state ),
2236 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002237
2238 switch( ssl->state )
2239 {
2240 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002241 * ssl->state is initialized as HELLO_REQUEST. It is the same
2242 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002243 */
2244 case MBEDTLS_SSL_HELLO_REQUEST:
2245 case MBEDTLS_SSL_CLIENT_HELLO:
2246 ret = ssl_tls13_write_client_hello( ssl );
2247 break;
2248
2249 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002250 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002251 break;
2252
2253 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002254 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002255 break;
2256
Jerry Yua93ac112021-10-27 16:31:48 +08002257#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002258 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2259 ret = ssl_tls13_process_certificate_request( ssl );
2260 break;
2261
Jerry Yu687101b2021-09-14 16:03:56 +08002262 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002263 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002264 break;
2265
2266 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002267 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002268 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002269#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002270
2271 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002272 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002273 break;
2274
Jerry Yu566c7812022-01-26 15:41:22 +08002275 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2276 ret = ssl_tls13_write_client_certificate( ssl );
2277 break;
2278
Ronald Cron9df7c802022-03-08 18:38:54 +01002279#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002280 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2281 ret = ssl_tls13_write_client_certificate_verify( ssl );
2282 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002283#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002284
Jerry Yu687101b2021-09-14 16:03:56 +08002285 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002286 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002287 break;
2288
2289 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002290 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002291 break;
2292
2293 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002294 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002295 break;
2296
Ronald Cron49ad6192021-11-24 16:25:31 +01002297 /*
2298 * Injection of dummy-CCS's for middlebox compatibility
2299 */
2300#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002301 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002302 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2303 if( ret == 0 )
2304 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2305 break;
2306
2307 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2308 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2309 if( ret == 0 )
2310 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002311 break;
2312#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2313
Jerry Yu92c6b402021-08-27 16:59:09 +08002314 default:
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2317 }
2318
2319 return( ret );
2320}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002321
Jerry Yufb4b6472022-01-27 15:03:26 +08002322#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002323
Jerry Yufb4b6472022-01-27 15:03:26 +08002324