blob: c0094c065bcce02ec813735f68abbaafab2b432b [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
Neil Armstrong91477a72022-03-25 15:42:20 +0100291 ssl->handshake->ecdh_bits = ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100293 key_attributes = psa_key_attributes_init();
294 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
295 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
296 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
297 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100298
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100299 /* Generate ECDH private key. */
300 status = psa_generate_key( &key_attributes,
301 &handshake->ecdh_psa_privkey );
302 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800303 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100304 ret = psa_ssl_status_to_mbedtls( status );
305 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100307
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 }
309
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100310 /* Export the public part of the ECDH private key from PSA. */
311 status = psa_export_public_key( handshake->ecdh_psa_privkey,
312 buf, (size_t)( end - buf ),
313 &own_pubkey_len );
314 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800315 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100316 ret = psa_ssl_status_to_mbedtls( status );
317 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800318 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100319
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320 }
321
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100322 if( own_pubkey_len > (size_t)( end - buf ) )
323 {
324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
325 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
326 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100327
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100328 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100329
Jerry Yu75336352021-09-01 15:59:36 +0800330 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800331}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800332#endif /* MBEDTLS_ECDH_C */
333
Jerry Yub60e3cf2021-09-08 16:41:02 +0800334static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
335 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800336{
337 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
338
Jerry Yu56fc07f2021-09-01 17:48:49 +0800339
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100341 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800342 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100343 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800344 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
345
Brett Warren14efd332021-10-06 09:32:11 +0100346 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000348 const mbedtls_ecp_curve_info *curve_info;
349 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
350 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100351 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352 {
Brett Warren14efd332021-10-06 09:32:11 +0100353 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354 return( 0 );
355 }
356 }
357#else
358 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800359 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800360#endif /* MBEDTLS_ECDH_C */
361
362 /*
363 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800364 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800365 */
366
367 return( ret );
368}
369
370/*
371 * ssl_tls13_write_key_share_ext
372 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800373 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800374 *
375 * struct {
376 * NamedGroup group;
377 * opaque key_exchange<1..2^16-1>;
378 * } KeyShareEntry;
379 * struct {
380 * KeyShareEntry client_shares<0..2^16-1>;
381 * } KeyShareClientHello;
382 */
383static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
384 unsigned char *buf,
385 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000386 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800387{
388 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000389 unsigned char *client_shares; /* Start of client_shares */
390 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800391 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800392 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
393
Xiaofei Baid25fab62021-12-02 06:36:27 +0000394 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800395
Jerry Yub60e3cf2021-09-08 16:41:02 +0800396 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397 * - extension_type (2 bytes)
398 * - extension_data_length (2 bytes)
399 * - client_shares_length (2 bytes)
400 */
401 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
402 p += 6;
403
404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
405
406 /* HRR could already have requested something else. */
407 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800408 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
409 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800411 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412 &group_id ) );
413 }
414
415 /*
416 * Dispatch to type-specific key generation function.
417 *
418 * So far, we're only supporting ECDHE. With the introduction
419 * of PQC KEMs, we'll want to have multiple branches, one per
420 * type of KEM, and dispatch to the corresponding crypto. And
421 * only one key share entry is allowed.
422 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000423 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800424#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800425 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800426 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800427 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000428 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800429 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100430 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800431
432 /* Check there is space for header of KeyShareEntry
433 * - group (2 bytes)
434 * - key_exchange_length (2 bytes)
435 */
436 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
437 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100438 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800439 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800440 p += key_exchange_len;
441 if( ret != 0 )
442 return( ret );
443
444 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000445 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800446 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000447 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800448 }
449 else
450#endif /* MBEDTLS_ECDH_C */
451 if( 0 /* other KEMs? */ )
452 {
453 /* Do something */
454 }
455 else
456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
457
Jerry Yub60e3cf2021-09-08 16:41:02 +0800458 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000459 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800460 if( client_shares_len == 0)
461 {
462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800464 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800465 /* Write extension_type */
466 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
467 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800468 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800469 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800470 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800471
472 /* Update offered_group_id field */
473 ssl->handshake->offered_group_id = group_id;
474
475 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000476 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800477
Xiaofei Baid25fab62021-12-02 06:36:27 +0000478 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800479
480 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
481
482cleanup:
483
484 return( ret );
485}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800486
Jerry Yue1b9c292021-09-10 10:08:31 +0800487#if defined(MBEDTLS_ECDH_C)
488
Jerry Yuc068b662021-10-11 22:30:19 +0800489static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
490 const unsigned char *buf,
491 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800492{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100493 uint8_t *p = (uint8_t*)buf;
494 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800495
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100496 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
497 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
498 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800499
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100500 /* Check if key size is consistent with given buffer length. */
501 if ( peerkey_len > ( buf_len - 2 ) )
502 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800503
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100504 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100505 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100506 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800507
508 return( 0 );
509}
Jerry Yu4a173382021-10-11 21:45:31 +0800510#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800511
XiaokangQiand59be772022-01-24 10:12:51 +0000512/*
513 * ssl_tls13_parse_hrr_key_share_ext()
514 * Parse key_share extension in Hello Retry Request
515 *
516 * struct {
517 * NamedGroup selected_group;
518 * } KeyShareHelloRetryRequest;
519 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000520static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000521 const unsigned char *buf,
522 const unsigned char *end )
523{
XiaokangQianb851da82022-01-14 04:03:11 +0000524 const mbedtls_ecp_curve_info *curve_info = NULL;
525 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000526 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000527 int found = 0;
528
529 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
530 if( group_list == NULL )
531 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
532
533 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
534
535 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000536 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000537 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000539
540 /* Upon receipt of this extension in a HelloRetryRequest, the client
541 * MUST first verify that the selected_group field corresponds to a
542 * group which was provided in the "supported_groups" extension in the
543 * original ClientHello.
544 * The supported_group was based on the info in ssl->conf->group_list.
545 *
546 * If the server provided a key share that was not sent in the ClientHello
547 * then the client MUST abort the handshake with an "illegal_parameter" alert.
548 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000549 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000550 {
551 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000552 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000553 continue;
554
555 /* We found a match */
556 found = 1;
557 break;
558 }
559
560 /* Client MUST verify that the selected_group field does not
561 * correspond to a group which was provided in the "key_share"
562 * extension in the original ClientHello. If the server sent an
563 * HRR message with a key share already provided in the
564 * ClientHello then the client MUST abort the handshake with
565 * an "illegal_parameter" alert.
566 */
XiaokangQiand59be772022-01-24 10:12:51 +0000567 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000568 {
569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
570 MBEDTLS_SSL_PEND_FATAL_ALERT(
571 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
572 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
573 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
574 }
575
576 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000577 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000578
579 return( 0 );
580}
581
Jerry Yue1b9c292021-09-10 10:08:31 +0800582/*
Jerry Yub85277e2021-10-13 13:36:05 +0800583 * ssl_tls13_parse_key_share_ext()
584 * Parse key_share extension in Server Hello
585 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800586 * struct {
587 * KeyShareEntry server_share;
588 * } KeyShareServerHello;
589 * struct {
590 * NamedGroup group;
591 * opaque key_exchange<1..2^16-1>;
592 * } KeyShareEntry;
593 */
Jerry Yu4a173382021-10-11 21:45:31 +0800594static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800595 const unsigned char *buf,
596 const unsigned char *end )
597{
Jerry Yub85277e2021-10-13 13:36:05 +0800598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800599 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800600 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800601
Jerry Yu4a173382021-10-11 21:45:31 +0800602 /* ...
603 * NamedGroup group; (2 bytes)
604 * ...
605 */
606 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
607 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800608 p += 2;
609
Jerry Yu4a173382021-10-11 21:45:31 +0800610 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800611 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800612 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800613 {
614 MBEDTLS_SSL_DEBUG_MSG( 1,
615 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800616 (unsigned) offered_group, (unsigned) group ) );
617 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
618 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
619 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800620 }
621
622#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800623 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800624 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100625 const mbedtls_ecp_curve_info *curve_info =
626 mbedtls_ecp_curve_info_from_tls_id( group );
627 if( curve_info == NULL )
628 {
629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
631 }
632
633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
634
Jerry Yuc068b662021-10-11 22:30:19 +0800635 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800636 if( ret != 0 )
637 return( ret );
638 }
Jerry Yub85277e2021-10-13 13:36:05 +0800639 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800640#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800641 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800642 {
643 /* Do something */
644 }
645 else
646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
647
648 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
649 return( ret );
650}
651
Jerry Yubc20bdd2021-08-24 15:59:48 +0800652#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
653
XiaokangQiand59be772022-01-24 10:12:51 +0000654/*
655 * ssl_tls13_parse_cookie_ext()
656 * Parse cookie extension in Hello Retry Request
657 *
658 * struct {
659 * opaque cookie<1..2^16-1>;
660 * } Cookie;
661 *
662 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
663 * extension to the client (this is an exception to the usual rule that
664 * the only extensions that may be sent are those that appear in the
665 * ClientHello). When sending the new ClientHello, the client MUST copy
666 * the contents of the extension received in the HelloRetryRequest into
667 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
668 * cookies in their initial ClientHello in subsequent connections.
669 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000670static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
671 const unsigned char *buf,
672 const unsigned char *end )
673{
XiaokangQian25c9c902022-02-08 10:49:53 +0000674 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000675 const unsigned char *p = buf;
676 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
677
678 /* Retrieve length field of cookie */
679 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
680 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
681 p += 2;
682
683 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
684 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
685
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000686 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000687 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000688 handshake->cookie = mbedtls_calloc( 1, cookie_len );
689 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000690 {
691 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000692 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000693 cookie_len ) );
694 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
695 }
696
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000697 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000698 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000699
700 return( 0 );
701}
XiaokangQian43550bd2022-01-21 04:32:58 +0000702
XiaokangQian0b64eed2022-01-27 10:36:51 +0000703static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000704 unsigned char *buf,
705 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000706 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000707{
708 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000709 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000710 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000711
XiaokangQianc02768a2022-02-10 07:31:25 +0000712 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000713 {
714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
715 return( 0 );
716 }
717
718 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000719 handshake->cookie,
720 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000721
XiaokangQianc02768a2022-02-10 07:31:25 +0000722 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000723
724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
725
XiaokangQian0b64eed2022-01-27 10:36:51 +0000726 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000727 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
728 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000729 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000730
731 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000732 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000733
XiaokangQianc02768a2022-02-10 07:31:25 +0000734 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000735
736 return( 0 );
737}
738
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800739/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800740 * CipherSuite cipher_suites<2..2^16-2>;
741 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800742static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800743 mbedtls_ssl_context *ssl,
744 unsigned char *buf,
745 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000746 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800747{
Jerry Yufec982e2021-09-07 17:26:06 +0800748 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800749 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000750 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800751 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800752
Xiaofei Baid25fab62021-12-02 06:36:27 +0000753 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800754
755 /*
756 * Ciphersuite list
757 *
758 * This is a list of the symmetric cipher options supported by
759 * the client, specifically the record protection algorithm
760 * ( including secret key length ) and a hash to be used with
761 * HKDF, in descending order of client preference.
762 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800763 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800764
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800765 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800766 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
767 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800768
Jerry Yu0c63af62021-09-02 12:59:12 +0800769 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000770 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800771 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800772 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800773 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800774 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800775
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800776 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800777 if( ciphersuite_info == NULL )
778 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800779 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
780 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800781 continue;
782
783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800784 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800785 ciphersuite_info->name ) );
786
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800787 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800788 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
789 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
790 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800791 }
792
Jerry Yu0c63af62021-09-02 12:59:12 +0800793 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000794 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800795 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800796 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800797 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
798 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800799
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800800 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000801 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800802
Jerry Yu6a643102021-08-31 14:40:36 +0800803 return( 0 );
804}
805
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100806static int ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
807 unsigned char *buf,
808 unsigned char *end,
809 size_t *out_len )
810{
811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
812 unsigned char *p = buf;
813 size_t ext_len;
814
815 *out_len = 0;
816
817 /* Write supported_versions extension
818 *
819 * Supported Versions Extension is mandatory with TLS 1.3.
820 */
821 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
822 if( ret != 0 )
823 return( ret );
824 p += ext_len;
825
826 /* Echo the cookie if the server provided one in its preceding
827 * HelloRetryRequest message.
828 */
829 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
830 if( ret != 0 )
831 return( ret );
832 p += ext_len;
833
834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
835 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
836 {
837 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
838 if( ret != 0 )
839 return( ret );
840 p += ext_len;
841 }
842#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
843
844 *out_len = p - buf;
845
846 return( 0 );
847}
848
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800849/*
850 * Structure of ClientHello message:
851 *
852 * struct {
853 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
854 * Random random;
855 * opaque legacy_session_id<0..32>;
856 * CipherSuite cipher_suites<2..2^16-2>;
857 * opaque legacy_compression_methods<1..2^8-1>;
858 * Extension extensions<8..2^16-1>;
859 * } ClientHello;
860 */
Jerry Yu08906d02021-08-31 11:05:27 +0800861static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800862 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800863 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000864 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800865{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800866
Jerry Yubc20bdd2021-08-24 15:59:48 +0800867 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000868 unsigned char *p_extensions_len; /* Pointer to extensions length */
869 size_t output_len; /* Length of buffer used by function */
870 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800871
Jerry Yubc20bdd2021-08-24 15:59:48 +0800872 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800873 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800874
Xiaofei Baid25fab62021-12-02 06:36:27 +0000875 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800876
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800877 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800878 ssl->major_ver = ssl->conf->min_major_ver;
879 ssl->minor_ver = ssl->conf->min_minor_ver;
880
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800881 /*
882 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800883 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800884 *
885 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800886 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800887 */
Jerry Yufec982e2021-09-07 17:26:06 +0800888 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800889 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800890 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800891
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800892 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800893 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
894 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800895 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800896 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
897 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800898
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800899 /*
900 * Write legacy_session_id
901 *
902 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
903 * which has been merged with pre-shared keys in this version. A client
904 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
905 * this field to that value. In compatibility mode, this field MUST be
906 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
907 * a new 32-byte value. This value need not be random but SHOULD be
908 * unpredictable to avoid implementations fixating on a specific value
909 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
910 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800911 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100912#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
913 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
914 *p++ = (unsigned char)ssl->session_negotiate->id_len;
915 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
916 p += ssl->session_negotiate->id_len;
917
918 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
919 ssl->session_negotiate->id_len );
920#else
Jerry Yubbe09522021-09-06 21:17:54 +0800921 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
922 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100923#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800924
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800925 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800926 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800927 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800928 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800929 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800930
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800931 /* Write legacy_compression_methods
932 *
933 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800934 * one byte set to zero, which corresponds to the 'null' compression
935 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800936 */
Jerry Yubbe09522021-09-06 21:17:54 +0800937 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
938 *p++ = 1;
939 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800940
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800941 /* Write extensions */
942
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100943#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800944 /* Keeping track of the included extensions */
945 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100946#endif
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 Cron7ffe7eb2022-03-09 15:26:31 +0100953#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100954 ret = ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800955 if( ret != 0 )
956 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800957 p += output_len;
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100958#endif
Jerry Yubc20bdd2021-08-24 15:59:48 +0800959
lhuang0486cacac2022-01-21 07:34:27 -0800960#if defined(MBEDTLS_SSL_ALPN)
Ronald Crona0855a62022-03-09 10:04:54 +0100961 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
lhuang0486cacac2022-01-21 07:34:27 -0800962 if( ret != 0 )
963 return( ret );
964 p += output_len;
965#endif /* MBEDTLS_SSL_ALPN */
966
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100967#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800968#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf46b0162022-01-11 16:28:00 +0800969 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
970 {
971 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
972 if( ret != 0 )
973 return( ret );
974 p += output_len;
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100975 }
Jerry Yu6a643102021-08-31 14:40:36 +0800976
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100977 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
978 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800979 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800980 if( ret != 0 )
981 return( ret );
982 p += output_len;
983 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800984#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron7ffe7eb2022-03-09 15:26:31 +0100985#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800986
Xiaofei Bai15a56812021-11-05 10:52:12 +0000987#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000988 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000989 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
990 if( ret != 0 )
991 return( ret );
992 p += output_len;
993#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
994
Jerry Yubc20bdd2021-08-24 15:59:48 +0800995 /* Add more extensions here */
996
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800997 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000998 extensions_len = p - p_extensions_len - 2;
999 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +08001001 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +00001002 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001003
Xiaofei Baid25fab62021-12-02 06:36:27 +00001004 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +08001005 return( 0 );
1006}
1007
Jerry Yu92c6b402021-08-27 16:59:09 +08001008static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
1009{
1010 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +08001011
Jerry Yu92c6b402021-08-27 16:59:09 +08001012 if( ssl->conf->f_rng == NULL )
1013 {
1014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
1015 return( MBEDTLS_ERR_SSL_NO_RNG );
1016 }
Jerry Yuef6b36b2021-08-24 16:29:02 +08001017
Jerry Yu92c6b402021-08-27 16:59:09 +08001018 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1019 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001020 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +08001021 {
Jerry Yu8c02bb42021-09-03 21:09:22 +08001022 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +08001023 return( ret );
1024 }
Jerry Yu6f13f642021-08-26 17:18:15 +08001025
Ronald Cron49ad6192021-11-24 16:25:31 +01001026#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1027 /*
1028 * Create a session identifier for the purpose of middlebox compatibility
1029 * only if one has not been created already.
1030 */
1031 if( ssl->session_negotiate->id_len == 0 )
1032 {
1033 /* Creating a session id with 32 byte length */
1034 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
1035 ssl->session_negotiate->id, 32 ) ) != 0 )
1036 {
1037 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
1038 return( ret );
1039 }
1040 ssl->session_negotiate->id_len = 32;
1041 }
1042#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1043
Jerry Yu6f13f642021-08-26 17:18:15 +08001044 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001045}
1046
Jerry Yu92c6b402021-08-27 16:59:09 +08001047/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001048 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001049 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001050 */
1051static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001052{
Jerry Yu92c6b402021-08-27 16:59:09 +08001053 int ret = 0;
1054 unsigned char *buf;
1055 size_t buf_len, msg_len;
1056
1057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1058
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001059 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001060
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001061 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001062 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1063 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001064
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001065 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001066 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001067 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001068
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001069 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1070 buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001071
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001072 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
1073 buf_len,
1074 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001075
Ronald Cron3addfa42022-02-08 16:10:25 +01001076 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1077
Jerry Yu92c6b402021-08-27 16:59:09 +08001078cleanup:
1079
1080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1081 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001082}
1083
Jerry Yu687101b2021-09-14 16:03:56 +08001084/*
Jerry Yu4a173382021-10-11 21:45:31 +08001085 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001086 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001087/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001088 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1089 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001090 * to indicate which message is expected and to be parsed next.
1091 */
Jerry Yub85277e2021-10-13 13:36:05 +08001092#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1093#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1094static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1095 const unsigned char *buf,
1096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001097{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001098 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1100 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1101 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1102 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1103
1104 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1105 *
Jerry Yu4a173382021-10-11 21:45:31 +08001106 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 * special value of the SHA-256 of "HelloRetryRequest".
1108 *
1109 * struct {
1110 * ProtocolVersion legacy_version = 0x0303;
1111 * Random random;
1112 * opaque legacy_session_id_echo<0..32>;
1113 * CipherSuite cipher_suite;
1114 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001115 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001116 * } ServerHello;
1117 *
1118 */
Jerry Yub85277e2021-10-13 13:36:05 +08001119 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001120
1121 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 {
Jerry Yub85277e2021-10-13 13:36:05 +08001123 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 }
1125
Jerry Yub85277e2021-10-13 13:36:05 +08001126 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001127}
1128
Jerry Yu745bb612021-10-13 22:01:04 +08001129/* Fetch and preprocess
1130 * Returns a negative value on failure, and otherwise
1131 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1132 * - SSL_SERVER_HELLO_COORDINATE_HRR
1133 */
Jerry Yub85277e2021-10-13 13:36:05 +08001134static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1135 unsigned char **buf,
1136 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001137{
Jerry Yu4a173382021-10-11 21:45:31 +08001138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001139
XiaokangQian355e09a2022-01-20 11:14:50 +00001140 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1141 MBEDTLS_SSL_HS_SERVER_HELLO,
1142 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001143
Jerry Yub85277e2021-10-13 13:36:05 +08001144 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1145 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 {
Jerry Yu745bb612021-10-13 22:01:04 +08001147 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1149 break;
1150 case SSL_SERVER_HELLO_COORDINATE_HRR:
1151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001152 /* If a client receives a second
1153 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1154 * was itself in response to a HelloRetryRequest), it MUST abort the
1155 * handshake with an "unexpected_message" alert.
1156 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001157 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001158 {
1159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1160 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1161 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1162 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1163 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001164 /*
1165 * Clients must abort the handshake with an "illegal_parameter"
1166 * alert if the HelloRetryRequest would not result in any change
1167 * in the ClientHello.
1168 * In a PSK only key exchange that what we expect.
1169 */
1170 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1171 {
1172 MBEDTLS_SSL_DEBUG_MSG( 1,
1173 ( "Unexpected HRR in pure PSK key exchange." ) );
1174 MBEDTLS_SSL_PEND_FATAL_ALERT(
1175 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1176 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1177 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1178 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001179
XiaokangQiand9e068e2022-01-18 06:23:32 +00001180 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001181
Jerry Yu745bb612021-10-13 22:01:04 +08001182 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001183 }
1184
1185cleanup:
1186
1187 return( ret );
1188}
1189
Jerry Yu4a173382021-10-11 21:45:31 +08001190static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1191 const unsigned char **buf,
1192 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001193{
1194 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001195 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001196
Jerry Yude4fb2c2021-09-19 18:05:08 +08001197 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001198 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
Jerry Yu4a173382021-10-11 21:45:31 +08001200 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
1202 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001203 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1204 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001206 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1207 ssl->session_negotiate->id,
1208 ssl->session_negotiate->id_len );
1209 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001210 legacy_session_id_echo_len );
1211
1212 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1213 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1214
Jerry Yue1b9c292021-09-10 10:08:31 +08001215 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1216 }
1217
Jerry Yu4a173382021-10-11 21:45:31 +08001218 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001219 *buf = p;
1220
1221 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001222 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 return( 0 );
1224}
1225
Jerry Yu4a173382021-10-11 21:45:31 +08001226static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1227 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001228{
Jerry Yu4a173382021-10-11 21:45:31 +08001229 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1230
Jerry Yue1b9c292021-09-10 10:08:31 +08001231 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001232 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 {
Jerry Yu4a173382021-10-11 21:45:31 +08001234 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 {
1236 return( 1 );
1237 }
1238 }
1239 return( 0 );
1240}
1241
1242/* Parse ServerHello message and configure context
1243 *
1244 * struct {
1245 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1246 * Random random;
1247 * opaque legacy_session_id_echo<0..32>;
1248 * CipherSuite cipher_suite;
1249 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001250 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001251 * } ServerHello;
1252 */
Jerry Yuc068b662021-10-11 22:30:19 +08001253static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1254 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001255 const unsigned char *end,
1256 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001257{
Jerry Yub85277e2021-10-13 13:36:05 +08001258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001259 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001260 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001261 size_t extensions_len;
1262 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001263 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001264 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001265 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001266 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001267
1268 /*
1269 * Check there is space for minimal fields
1270 *
1271 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001272 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001273 * - legacy_session_id_echo ( 1 byte ), minimum size
1274 * - cipher_suite ( 2 bytes)
1275 * - legacy_compression_method ( 1 byte )
1276 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001277 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001278
1279 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1281
Jerry Yu4a173382021-10-11 21:45:31 +08001282 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001283 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001284 * ...
1285 * with ProtocolVersion defined as:
1286 * uint16 ProtocolVersion;
1287 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001288 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1289 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1290 {
1291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1292 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1293 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001294 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1295 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001296 }
1297 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001298
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001299 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001300 * Random random;
1301 * ...
1302 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001303 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001304 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001305 if( !is_hrr )
1306 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001307 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001308 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1309 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1310 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1311 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001312 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001313
Jerry Yu4a173382021-10-11 21:45:31 +08001314 /* ...
1315 * opaque legacy_session_id_echo<0..32>;
1316 * ...
1317 */
1318 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001319 {
XiaokangQian52da5582022-01-26 09:49:29 +00001320 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001321 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001322 }
1323
Jerry Yu4a173382021-10-11 21:45:31 +08001324 /* ...
1325 * CipherSuite cipher_suite;
1326 * ...
1327 * with CipherSuite defined as:
1328 * uint8 CipherSuite[2];
1329 */
1330 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001331 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1332 p += 2;
1333
Jerry Yu4a173382021-10-11 21:45:31 +08001334
XiaokangQian355e09a2022-01-20 11:14:50 +00001335 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001336 /*
1337 * Check whether this ciphersuite is supported and offered.
1338 * Via the force_ciphersuite version we may have instructed the client
1339 * to use a different ciphersuite.
1340 */
Jerry Yu4a173382021-10-11 21:45:31 +08001341 if( ciphersuite_info == NULL ||
1342 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001343 {
XiaokangQian52da5582022-01-26 09:49:29 +00001344 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001346 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001347 * If we received an HRR before and that the proposed selected
1348 * ciphersuite in this server hello is not the same as the one
1349 * proposed in the HRR, we abort the handshake and send an
1350 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001351 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001352 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1353 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001354 {
XiaokangQian52da5582022-01-26 09:49:29 +00001355 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001356 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001357
XiaokangQian52da5582022-01-26 09:49:29 +00001358 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001359 {
1360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1361 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001362 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001363 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001364
Jerry Yu4a173382021-10-11 21:45:31 +08001365 /* Configure ciphersuites */
1366 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1367
XiaokangQian355e09a2022-01-20 11:14:50 +00001368 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001369 ssl->session_negotiate->ciphersuite = cipher_suite;
1370
Jerry Yue1b9c292021-09-10 10:08:31 +08001371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1372 cipher_suite, ciphersuite_info->name ) );
1373
1374#if defined(MBEDTLS_HAVE_TIME)
1375 ssl->session_negotiate->start = time( NULL );
1376#endif /* MBEDTLS_HAVE_TIME */
1377
Jerry Yu4a173382021-10-11 21:45:31 +08001378 /* ...
1379 * uint8 legacy_compression_method = 0;
1380 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001381 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001382 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001383 if( p[0] != 0 )
1384 {
Jerry Yub85277e2021-10-13 13:36:05 +08001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001386 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001387 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001388 }
1389 p++;
1390
Jerry Yub85277e2021-10-13 13:36:05 +08001391 /* ...
1392 * Extension extensions<6..2^16-1>;
1393 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001394 * struct {
1395 * ExtensionType extension_type; (2 bytes)
1396 * opaque extension_data<0..2^16-1>;
1397 * } Extension;
1398 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001399 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001400 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001401 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001402
Jerry Yu4a173382021-10-11 21:45:31 +08001403 /* Check extensions do not go beyond the buffer of data. */
1404 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1405 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001406
Jerry Yu4a173382021-10-11 21:45:31 +08001407 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001408
Jerry Yu4a173382021-10-11 21:45:31 +08001409 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001410 {
1411 unsigned int extension_type;
1412 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001413 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001414
Jerry Yu4a173382021-10-11 21:45:31 +08001415 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001416 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1417 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1418 p += 4;
1419
Jerry Yu4a173382021-10-11 21:45:31 +08001420 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001421 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001422
1423 switch( extension_type )
1424 {
XiaokangQianb851da82022-01-14 04:03:11 +00001425 case MBEDTLS_TLS_EXT_COOKIE:
1426
XiaokangQiand9e068e2022-01-18 06:23:32 +00001427 if( !is_hrr )
1428 {
XiaokangQian52da5582022-01-26 09:49:29 +00001429 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001430 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001431 }
1432
XiaokangQian43550bd2022-01-21 04:32:58 +00001433 ret = ssl_tls13_parse_cookie_ext( ssl,
1434 p, extension_data_end );
1435 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001436 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001437 MBEDTLS_SSL_DEBUG_RET( 1,
1438 "ssl_tls13_parse_cookie_ext",
1439 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001440 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001441 }
XiaokangQianb851da82022-01-14 04:03:11 +00001442 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001443
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001445 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001446 MBEDTLS_SSL_DEBUG_MSG( 3,
1447 ( "found supported_versions extension" ) );
1448
Jerry Yuc068b662021-10-11 22:30:19 +08001449 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001450 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001451 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001452 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001453 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001454 break;
1455
1456 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001459
XiaokangQian52da5582022-01-26 09:49:29 +00001460 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001461 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001462
1463#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1464 case MBEDTLS_TLS_EXT_KEY_SHARE:
1465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001466 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1467 {
XiaokangQian52da5582022-01-26 09:49:29 +00001468 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001469 goto cleanup;
1470 }
1471
XiaokangQian53f20b72022-01-18 10:47:33 +00001472 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001473 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001474 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001475 else
XiaokangQianb851da82022-01-14 04:03:11 +00001476 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001477 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001478 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001479 {
1480 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001481 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001482 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001483 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001484 }
1485 break;
1486#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1487
1488 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001489 MBEDTLS_SSL_DEBUG_MSG(
1490 3,
1491 ( "unknown extension found: %u ( ignoring )",
1492 extension_type ) );
1493
XiaokangQian52da5582022-01-26 09:49:29 +00001494 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001495 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001496 }
1497
1498 p += extension_data_len;
1499 }
1500
XiaokangQian78b1fa72022-01-19 06:56:30 +00001501 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001502 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001504 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001505 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001506 }
1507
XiaokangQiand59be772022-01-24 10:12:51 +00001508cleanup:
1509
XiaokangQian52da5582022-01-26 09:49:29 +00001510 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001511 {
1512 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1513 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1514 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1515 }
XiaokangQian52da5582022-01-26 09:49:29 +00001516 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001517 {
1518 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1519 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1520 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1521 }
1522 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001523}
1524
XiaokangQian355e09a2022-01-20 11:14:50 +00001525static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001526{
Jerry Yub85277e2021-10-13 13:36:05 +08001527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001528 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001529 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001530 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001531
Jerry Yub85277e2021-10-13 13:36:05 +08001532 /* Determine the key exchange mode:
1533 * 1) If both the pre_shared_key and key_share extensions were received
1534 * then the key exchange mode is PSK with EPHEMERAL.
1535 * 2) If only the pre_shared_key extension was received then the key
1536 * exchange mode is PSK-only.
1537 * 3) If only the key_share extension was received then the key
1538 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001539 */
Jerry Yub85277e2021-10-13 13:36:05 +08001540 switch( handshake->extensions_present &
1541 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001542 {
Jerry Yu745bb612021-10-13 22:01:04 +08001543 /* Only the pre_shared_key extension was received */
1544 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001545 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001546 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001547
Jerry Yu745bb612021-10-13 22:01:04 +08001548 /* Only the key_share extension was received */
1549 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001550 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001551 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001552
Jerry Yu745bb612021-10-13 22:01:04 +08001553 /* Both the pre_shared_key and key_share extensions were received */
1554 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001555 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001556 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001557
Jerry Yu745bb612021-10-13 22:01:04 +08001558 /* Neither pre_shared_key nor key_share extension was received */
1559 default:
1560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1561 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1562 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001563 }
1564
1565 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1566 *
1567 * TODO: We don't have to do this in case we offered 0-RTT and the
1568 * server accepted it. In this case, we could skip generating
1569 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001570 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001571 if( ret != 0 )
1572 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001574 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001575 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001576 }
1577
1578 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001579 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001580 if( ret != 0 )
1581 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001583 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001584 }
1585
1586 /* Next evolution in key schedule: Establish handshake secret and
1587 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001588 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001589 if( ret != 0 )
1590 {
Jerry Yuc068b662021-10-11 22:30:19 +08001591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1592 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001593 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001594 }
1595
Jerry Yub85277e2021-10-13 13:36:05 +08001596 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001597 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001598 {
1599 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1600 goto cleanup;
1601 }
Jerry Yu0b177842021-09-05 19:41:30 +08001602
1603 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1604 ssl->conf->endpoint,
1605 ssl->session_negotiate->ciphersuite,
1606 &traffic_keys,
1607 ssl );
1608 if( ret != 0 )
1609 {
1610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001611 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001612 }
1613
Jerry Yub85277e2021-10-13 13:36:05 +08001614 handshake->transform_handshake = transform_handshake;
1615 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001616
1617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1618 ssl->session_in = ssl->session_negotiate;
1619
1620 /*
1621 * State machine update
1622 */
1623 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1624
Jerry Yu4a173382021-10-11 21:45:31 +08001625cleanup:
1626
Jerry Yu0b177842021-09-05 19:41:30 +08001627 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001628 if( ret != 0 )
1629 {
Jerry Yub85277e2021-10-13 13:36:05 +08001630 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001631
Jerry Yu4a173382021-10-11 21:45:31 +08001632 MBEDTLS_SSL_PEND_FATAL_ALERT(
1633 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1634 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1635 }
1636 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001637}
1638
XiaokangQian355e09a2022-01-20 11:14:50 +00001639static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001640{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001641#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1643
XiaokangQian647719a2021-12-07 09:16:29 +00001644#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1645 /* If not offering early data, the client sends a dummy CCS record
1646 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001647 * its second ClientHello or before its encrypted handshake flight.
1648 */
XiaokangQian647719a2021-12-07 09:16:29 +00001649 mbedtls_ssl_handshake_set_state( ssl,
1650 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1651#else
1652 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1653#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1654
XiaokangQian78b1fa72022-01-19 06:56:30 +00001655 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001656
XiaokangQian78b1fa72022-01-19 06:56:30 +00001657 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001658 * We are going to re-generate a shared secret corresponding to the group
1659 * selected by the server, which is different from the group for which we
1660 * generated a shared secret in the first client hello.
1661 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001662 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001663 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001664 if( ret != 0 )
1665 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001666#else
1667 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001668#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001669
1670 return( 0 );
1671}
1672
Jerry Yue1b9c292021-09-10 10:08:31 +08001673/*
Jerry Yu4a173382021-10-11 21:45:31 +08001674 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001675 * Handler for MBEDTLS_SSL_SERVER_HELLO
1676 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001677static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001678{
Jerry Yu4a173382021-10-11 21:45:31 +08001679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001680 unsigned char *buf = NULL;
1681 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001682 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001683
1684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1685
1686 /* Coordination step
1687 * - Fetch record
1688 * - Make sure it's either a ServerHello or a HRR.
1689 * - Switch processing routine in case of HRR
1690 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001691 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1692 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1693
XiaokangQian16acd4b2022-01-14 07:35:47 +00001694 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001695 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001696 goto cleanup;
1697 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001698 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001699
XiaokangQianb851da82022-01-14 04:03:11 +00001700 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001701 buf + buf_len,
1702 is_hrr ) );
1703 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001704 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1705
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001706 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1707 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001708
XiaokangQiand9e068e2022-01-18 06:23:32 +00001709 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001710 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001711 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001712 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001713
1714cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1716 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001717 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001718}
1719
1720/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001721 *
1722 * EncryptedExtensions message
1723 *
1724 * The EncryptedExtensions message contains any extensions which
1725 * should be protected, i.e., any which are not needed to establish
1726 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001727 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001728
1729/*
1730 * Overview
1731 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001732
1733/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001734static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001735
XiaokangQian97799ac2021-10-11 10:05:54 +00001736static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1737 const unsigned char *buf,
1738 const unsigned char *end );
1739static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001740
1741/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001742 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001743 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001744static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001745{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001746 int ret;
1747 unsigned char *buf;
1748 size_t buf_len;
1749
1750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1751
Xiaofei Bai746f9482021-11-12 08:53:56 +00001752 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001753 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001754 &buf, &buf_len ) );
1755
1756 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001757 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001758 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001759
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001760 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1761 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001762
XiaokangQian97799ac2021-10-11 10:05:54 +00001763 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764
1765cleanup:
1766
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1768 return( ret );
1769
1770}
1771
XiaokangQian08da26c2021-10-09 10:12:11 +00001772/* Parse EncryptedExtensions message
1773 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001774 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001775 * } EncryptedExtensions;
1776 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001777static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1778 const unsigned char *buf,
1779 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001780{
1781 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001782 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001783 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001784 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001785
XiaokangQian08da26c2021-10-09 10:12:11 +00001786 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001787 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001788 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001789
XiaokangQian97799ac2021-10-11 10:05:54 +00001790 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001791 extensions_end = p + extensions_len;
1792 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001793
XiaokangQian8db25ff2021-10-13 05:56:18 +00001794 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001795 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001796 unsigned int extension_type;
1797 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001798
XiaokangQian08da26c2021-10-09 10:12:11 +00001799 /*
1800 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001801 * ExtensionType extension_type; (2 bytes)
1802 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001803 * } Extension;
1804 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001805 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001806 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1807 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1808 p += 4;
1809
XiaokangQian8db25ff2021-10-13 05:56:18 +00001810 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001811
XiaokangQian97799ac2021-10-11 10:05:54 +00001812 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001813 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001814 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001815 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001816 switch( extension_type )
1817 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001818
XiaokangQian08da26c2021-10-09 10:12:11 +00001819 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1821 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001822
lhuang0486cacac2022-01-21 07:34:27 -08001823#if defined(MBEDTLS_SSL_ALPN)
1824 case MBEDTLS_TLS_EXT_ALPN:
1825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1826
1827 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1828 {
1829 return( ret );
1830 }
1831
1832 break;
1833#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001834 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001835 MBEDTLS_SSL_DEBUG_MSG(
1836 3, ( "unsupported extension found: %u ", extension_type) );
1837 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001838 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001839 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1840 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001841 }
1842
XiaokangQian08da26c2021-10-09 10:12:11 +00001843 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001844 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001845
XiaokangQian97799ac2021-10-11 10:05:54 +00001846 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001847 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001848 {
1849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001850 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001851 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001852 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001853 }
1854
1855 return( ret );
1856}
1857
XiaokangQian97799ac2021-10-11 10:05:54 +00001858static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001859{
Jerry Yua93ac112021-10-27 16:31:48 +08001860#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001861 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001862 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1863 else
Jerry Yud2674312021-10-29 10:08:19 +08001864 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001865#else
1866 ((void) ssl);
1867 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1868#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001869 return( 0 );
1870}
1871
Jerry Yua93ac112021-10-27 16:31:48 +08001872#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001873/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001874 *
1875 * STATE HANDLING: CertificateRequest
1876 *
Jerry Yud2674312021-10-29 10:08:19 +08001877 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001878#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1879#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001880/* Coordination:
1881 * Deals with the ambiguity of not knowing if a CertificateRequest
1882 * will be sent. Returns a negative code on failure, or
1883 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1884 * - SSL_CERTIFICATE_REQUEST_SKIP
1885 * indicating if a Certificate Request is expected or not.
1886 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001887static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001888{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001890
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001891 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001892 {
1893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1894 return( SSL_CERTIFICATE_REQUEST_SKIP );
1895 }
1896
1897 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001898 {
1899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1900 return( ret );
1901 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001902 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001903
1904 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1905 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1906 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001907 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001908 }
1909
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001910 return( SSL_CERTIFICATE_REQUEST_SKIP );
1911}
1912
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001913/*
1914 * ssl_tls13_parse_certificate_request()
1915 * Parse certificate request
1916 * struct {
1917 * opaque certificate_request_context<0..2^8-1>;
1918 * Extension extensions<2..2^16-1>;
1919 * } CertificateRequest;
1920 */
1921static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1922 const unsigned char *buf,
1923 const unsigned char *end )
1924{
1925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1926 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001927 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001928 size_t extensions_len = 0;
1929 const unsigned char *extensions_end;
1930 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001931
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001932 /* ...
1933 * opaque certificate_request_context<0..2^8-1>
1934 * ...
1935 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001936 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1937 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001938 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001939
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001940 if( certificate_request_context_len > 0 )
1941 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001942 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001943 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1944 p, certificate_request_context_len );
1945
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001946 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001947 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001948 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001949 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001950 {
1951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1952 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1953 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001954 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001956 p += certificate_request_context_len;
1957 }
1958
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001959 /* ...
1960 * Extension extensions<2..2^16-1>;
1961 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001962 */
1963 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1964 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1965 p += 2;
1966
1967 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1968 extensions_end = p + extensions_len;
1969
1970 while( p < extensions_end )
1971 {
1972 unsigned int extension_type;
1973 size_t extension_data_len;
1974
1975 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1976 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1977 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1978 p += 4;
1979
1980 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1981
1982 switch( extension_type )
1983 {
1984 case MBEDTLS_TLS_EXT_SIG_ALG:
1985 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001986 ( "found signature algorithms extension" ) );
1987 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001988 p + extension_data_len );
1989 if( ret != 0 )
1990 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001991 if( ! sig_alg_ext_found )
1992 sig_alg_ext_found = 1;
1993 else
1994 {
1995 MBEDTLS_SSL_DEBUG_MSG( 3,
1996 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001997 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001998 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001999 break;
2000
2001 default:
2002 MBEDTLS_SSL_DEBUG_MSG(
2003 3,
2004 ( "unknown extension found: %u ( ignoring )",
2005 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002006 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002007 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002008 p += extension_data_len;
2009 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002010 /* Check that we consumed all the message. */
2011 if( p != end )
2012 {
2013 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002014 ( "CertificateRequest misaligned" ) );
2015 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002016 }
2017 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002018 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002019 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002020 MBEDTLS_SSL_DEBUG_MSG( 3,
2021 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002022 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002023 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002024
Jerry Yu7840f812022-01-29 10:26:51 +08002025 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002026 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002027
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002028decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002029 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2030 MBEDTLS_ERR_SSL_DECODE_ERROR );
2031 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002032}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002033
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002034/*
2035 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2036 */
2037static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002038{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002040
2041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2042
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002043 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2044
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2046 {
2047 unsigned char *buf;
2048 size_t buf_len;
2049
2050 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2051 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2052 &buf, &buf_len ) );
2053
2054 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2055 buf, buf + buf_len ) );
2056
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002057 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2058 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002059 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002060 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002061 {
2062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002063 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002064 }
2065 else
2066 {
2067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002068 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2069 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002070 }
2071
2072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002073 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002074
Jerry Yud2674312021-10-29 10:08:19 +08002075 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2076
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002077cleanup:
2078
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2080 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002081}
2082
2083/*
Jerry Yu687101b2021-09-14 16:03:56 +08002084 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2085 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002086static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002087{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002088 int ret;
2089
2090 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002091 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002092 return( ret );
2093
Jerry Yu687101b2021-09-14 16:03:56 +08002094 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2095 return( 0 );
2096}
2097
2098/*
2099 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2100 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002101static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002102{
Jerry Yu30b071c2021-09-12 20:16:03 +08002103 int ret;
2104
2105 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2106 if( ret != 0 )
2107 return( ret );
2108
Jerry Yu687101b2021-09-14 16:03:56 +08002109 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2110 return( 0 );
2111}
Jerry Yua93ac112021-10-27 16:31:48 +08002112#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002113
Jerry Yu687101b2021-09-14 16:03:56 +08002114/*
2115 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2116 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002117static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002118{
XiaokangQianac0385c2021-11-03 06:40:11 +00002119 int ret;
2120
XiaokangQianc5c39d52021-11-09 11:55:10 +00002121 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002122 if( ret != 0 )
2123 return( ret );
2124
Ronald Cron49ad6192021-11-24 16:25:31 +01002125#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2126 mbedtls_ssl_handshake_set_state(
2127 ssl,
2128 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2129#else
Jerry Yuca133a32022-02-15 14:22:05 +08002130 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002131#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002132
XiaokangQianac0385c2021-11-03 06:40:11 +00002133 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002134}
2135
2136/*
Jerry Yu566c7812022-01-26 15:41:22 +08002137 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2138 */
2139static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2140{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002141 int non_empty_certificate_msg = 0;
2142
Jerry Yu5cc35062022-01-28 16:16:08 +08002143 MBEDTLS_SSL_DEBUG_MSG( 1,
2144 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002145 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002146
Ronald Cron9df7c802022-03-08 18:38:54 +01002147#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002148 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002149 {
2150 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2151 if( ret != 0 )
2152 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002153
Ronald Cron7a94aca2022-03-09 07:44:27 +01002154 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2155 non_empty_certificate_msg = 1;
2156 }
2157 else
2158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2160 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002161#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002162
Ronald Cron7a94aca2022-03-09 07:44:27 +01002163 if( non_empty_certificate_msg )
2164 {
2165 mbedtls_ssl_handshake_set_state( ssl,
2166 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2167 }
2168 else
2169 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2170
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002171 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002172}
2173
Ronald Cron9df7c802022-03-08 18:38:54 +01002174#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002175/*
2176 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2177 */
2178static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2179{
Ronald Crona8b38872022-03-09 07:59:25 +01002180 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2181
2182 if( ret == 0 )
2183 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2184
2185 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002186}
Jerry Yu90f152d2022-01-29 22:12:42 +08002187#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002188
2189/*
Jerry Yu687101b2021-09-14 16:03:56 +08002190 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2191 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002192static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002193{
XiaokangQian0fa66432021-11-15 03:33:57 +00002194 int ret;
2195
2196 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2197 if( ret != 0 )
2198 return( ret );
2199
2200 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2201 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002202}
2203
2204/*
2205 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2206 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002207static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002208{
Jerry Yu378254d2021-10-30 21:44:47 +08002209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002210 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002211 return( 0 );
2212}
2213
2214/*
2215 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2216 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002217static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002218{
Jerry Yu378254d2021-10-30 21:44:47 +08002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2220 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2221
2222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2223 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2224
2225 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2226
2227 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2228 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002229}
2230
Jerry Yu92c6b402021-08-27 16:59:09 +08002231int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002232{
Jerry Yu92c6b402021-08-27 16:59:09 +08002233 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002234
Jerry Yue3b34122021-09-28 17:53:35 +08002235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2236 mbedtls_ssl_states_str( ssl->state ),
2237 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002238
2239 switch( ssl->state )
2240 {
2241 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002242 * ssl->state is initialized as HELLO_REQUEST. It is the same
2243 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002244 */
2245 case MBEDTLS_SSL_HELLO_REQUEST:
2246 case MBEDTLS_SSL_CLIENT_HELLO:
2247 ret = ssl_tls13_write_client_hello( ssl );
2248 break;
2249
2250 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002251 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002252 break;
2253
2254 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002255 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002256 break;
2257
Jerry Yua93ac112021-10-27 16:31:48 +08002258#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002259 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2260 ret = ssl_tls13_process_certificate_request( ssl );
2261 break;
2262
Jerry Yu687101b2021-09-14 16:03:56 +08002263 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002264 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002265 break;
2266
2267 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002268 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002269 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002270#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002271
2272 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002273 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002274 break;
2275
Jerry Yu566c7812022-01-26 15:41:22 +08002276 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2277 ret = ssl_tls13_write_client_certificate( ssl );
2278 break;
2279
Ronald Cron9df7c802022-03-08 18:38:54 +01002280#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002281 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2282 ret = ssl_tls13_write_client_certificate_verify( ssl );
2283 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002284#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002285
Jerry Yu687101b2021-09-14 16:03:56 +08002286 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002287 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002288 break;
2289
2290 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002291 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002292 break;
2293
2294 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002295 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002296 break;
2297
Ronald Cron49ad6192021-11-24 16:25:31 +01002298 /*
2299 * Injection of dummy-CCS's for middlebox compatibility
2300 */
2301#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002302 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002303 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2304 if( ret == 0 )
2305 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2306 break;
2307
2308 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2309 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2310 if( ret == 0 )
2311 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002312 break;
2313#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2314
Jerry Yu92c6b402021-08-27 16:59:09 +08002315 default:
2316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2318 }
2319
2320 return( ret );
2321}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002322
Jerry Yufb4b6472022-01-27 15:03:26 +08002323#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002324
Jerry Yufb4b6472022-01-27 15:03:26 +08002325