blob: 91791b83e39d8f7f5da48d6a503a27538317ea01 [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/*
116 * ssl_tls13_write_alpn_ext( ) structure:
117 *
118 * opaque ProtocolName<1..2^8-1>;
119 *
120 * struct {
121 * ProtocolName protocol_name_list<2..2^16-1>
122 * } ProtocolNameList;
123 *
124 */
125static int ssl_tls13_write_alpn_ext( mbedtls_ssl_context *ssl,
126 unsigned char *buf,
127 const unsigned char *end,
128 size_t *olen )
129{
130 unsigned char *p = buf;
131 size_t alpnlen = 0;
132 const char **cur;
133
134 *olen = 0;
135
136 if( ssl->conf->alpn_list == NULL )
137 return( 0 );
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
140
141 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
142 alpnlen += strlen( *cur ) + 1;
143
144 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
145
146 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
147 p += 2;
148
149 /*
150 * opaque ProtocolName<1..2^8-1>;
151 *
152 * struct {
153 * ProtocolName protocol_name_list<2..2^16-1>
154 * } ProtocolNameList;
155 */
156
157 /* Skip writing extension and list length for now */
158 p += 4;
159
160 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
161 {
162 /*
163 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
164 * protocol names is less than 255.
165 */
166 *p = (unsigned char)strlen( *cur );
167 memcpy( p + 1, *cur, *p );
168 p += 1 + *p;
169 }
170
171 *olen = p - buf;
172
173 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
174 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
175
176 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
177 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
178
179 return( 0 );
180}
181
182static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
183 const unsigned char *buf, size_t len )
184{
185 size_t list_len, name_len;
186 const unsigned char *p = buf;
187 const unsigned char *end = buf + len;
188
189 /* If we didn't send it, the server shouldn't send it */
190 if( ssl->conf->alpn_list == NULL )
191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
192
193 /*
194 * opaque ProtocolName<1..2^8-1>;
195 *
196 * struct {
197 * ProtocolName protocol_name_list<2..2^16-1>
198 * } ProtocolNameList;
199 *
200 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
201 */
202
203 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
205
206 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
207 p += 2;
208 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
209
210 name_len = *p++;
211 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
212
213 /* Check that the server chosen protocol was in our list and save it */
214 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
215 {
216 if( name_len == strlen( *alpn ) &&
217 memcmp( buf + 3, *alpn, name_len ) == 0 )
218 {
219 ssl->alpn_chosen = *alpn;
220 return( 0 );
221 }
222 }
223
224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225}
226#endif /* MBEDTLS_SSL_ALPN */
227
Jerry Yubc20bdd2021-08-24 15:59:48 +0800228#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
229
XiaokangQian16acd4b2022-01-14 07:35:47 +0000230static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000231{
232 uint16_t group_id = ssl->handshake->offered_group_id;
233 if( group_id == 0 )
234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
235
XiaokangQian355e09a2022-01-20 11:14:50 +0000236#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000237 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000238 {
239 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
240 return( 0 );
241 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000242 else
243#endif /* MBEDTLS_ECDH_C */
244 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000245 {
246 /* Do something */
247 }
248
249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
250}
251
252/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253 * Functions for writing key_share extension.
254 */
255#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800256static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800257 mbedtls_ssl_context *ssl,
258 uint16_t named_group,
259 unsigned char *buf,
260 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000261 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800262{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100263 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
264 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
265 psa_key_attributes_t key_attributes;
266 size_t own_pubkey_len;
267 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
268 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100272 /* Convert EC group to PSA key type. */
273 if( ( handshake->ecdh_psa_type =
274 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
275 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100277 if( ecdh_bits > 0xffff )
278 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
279 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100281 key_attributes = psa_key_attributes_init();
282 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
283 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
284 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
285 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100286
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100287 /* Generate ECDH private key. */
288 status = psa_generate_key( &key_attributes,
289 &handshake->ecdh_psa_privkey );
290 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100292 ret = psa_ssl_status_to_mbedtls( status );
293 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100295
Jerry Yu56fc07f2021-09-01 17:48:49 +0800296 }
297
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100298 /* Export the public part of the ECDH private key from PSA. */
299 status = psa_export_public_key( handshake->ecdh_psa_privkey,
300 buf, (size_t)( end - buf ),
301 &own_pubkey_len );
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_export_public_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 if( own_pubkey_len > (size_t)( end - buf ) )
311 {
312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
313 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
314 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100315
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100316 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100317
Jerry Yu75336352021-09-01 15:59:36 +0800318 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800319}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320#endif /* MBEDTLS_ECDH_C */
321
Jerry Yub60e3cf2021-09-08 16:41:02 +0800322static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
323 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324{
325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
326
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100329 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800330 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100331 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800332 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
333
Brett Warren14efd332021-10-06 09:32:11 +0100334 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800335 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000336 const mbedtls_ecp_curve_info *curve_info;
337 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
338 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100339 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340 {
Brett Warren14efd332021-10-06 09:32:11 +0100341 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 return( 0 );
343 }
344 }
345#else
346 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800347 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348#endif /* MBEDTLS_ECDH_C */
349
350 /*
351 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800352 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353 */
354
355 return( ret );
356}
357
358/*
359 * ssl_tls13_write_key_share_ext
360 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800361 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 *
363 * struct {
364 * NamedGroup group;
365 * opaque key_exchange<1..2^16-1>;
366 * } KeyShareEntry;
367 * struct {
368 * KeyShareEntry client_shares<0..2^16-1>;
369 * } KeyShareClientHello;
370 */
371static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
372 unsigned char *buf,
373 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000374 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800375{
376 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000377 unsigned char *client_shares; /* Start of client_shares */
378 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800379 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800380 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
381
Xiaofei Baid25fab62021-12-02 06:36:27 +0000382 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800383
Jerry Yub60e3cf2021-09-08 16:41:02 +0800384 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800385 * - extension_type (2 bytes)
386 * - extension_data_length (2 bytes)
387 * - client_shares_length (2 bytes)
388 */
389 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
390 p += 6;
391
392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
393
394 /* HRR could already have requested something else. */
395 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800396 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
397 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800398 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800399 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800400 &group_id ) );
401 }
402
403 /*
404 * Dispatch to type-specific key generation function.
405 *
406 * So far, we're only supporting ECDHE. With the introduction
407 * of PQC KEMs, we'll want to have multiple branches, one per
408 * type of KEM, and dispatch to the corresponding crypto. And
409 * only one key share entry is allowed.
410 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000411 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800413 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800414 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800415 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000416 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800417 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100418 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800419
420 /* Check there is space for header of KeyShareEntry
421 * - group (2 bytes)
422 * - key_exchange_length (2 bytes)
423 */
424 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
425 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100426 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800427 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800428 p += key_exchange_len;
429 if( ret != 0 )
430 return( ret );
431
432 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000433 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800434 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000435 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800436 }
437 else
438#endif /* MBEDTLS_ECDH_C */
439 if( 0 /* other KEMs? */ )
440 {
441 /* Do something */
442 }
443 else
444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
445
Jerry Yub60e3cf2021-09-08 16:41:02 +0800446 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000447 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800448 if( client_shares_len == 0)
449 {
450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800452 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800453 /* Write extension_type */
454 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
455 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800456 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800457 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800458 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800459
460 /* Update offered_group_id field */
461 ssl->handshake->offered_group_id = group_id;
462
463 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000464 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800465
Xiaofei Baid25fab62021-12-02 06:36:27 +0000466 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800467
468 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
469
470cleanup:
471
472 return( ret );
473}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800474
Jerry Yue1b9c292021-09-10 10:08:31 +0800475#if defined(MBEDTLS_ECDH_C)
476
Jerry Yuc068b662021-10-11 22:30:19 +0800477static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
478 const unsigned char *buf,
479 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800480{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100481 uint8_t *p = (uint8_t*)buf;
482 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800483
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100484 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
485 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
486 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800487
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100488 /* Check if key size is consistent with given buffer length. */
489 if ( peerkey_len > ( buf_len - 2 ) )
490 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800491
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100492 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100493 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100494 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800495
496 return( 0 );
497}
Jerry Yu4a173382021-10-11 21:45:31 +0800498#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800499
XiaokangQiand59be772022-01-24 10:12:51 +0000500/*
501 * ssl_tls13_parse_hrr_key_share_ext()
502 * Parse key_share extension in Hello Retry Request
503 *
504 * struct {
505 * NamedGroup selected_group;
506 * } KeyShareHelloRetryRequest;
507 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000508static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000509 const unsigned char *buf,
510 const unsigned char *end )
511{
XiaokangQianb851da82022-01-14 04:03:11 +0000512 const mbedtls_ecp_curve_info *curve_info = NULL;
513 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000514 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000515 int found = 0;
516
517 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
518 if( group_list == NULL )
519 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
520
521 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
522
523 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000524 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000525 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000527
528 /* Upon receipt of this extension in a HelloRetryRequest, the client
529 * MUST first verify that the selected_group field corresponds to a
530 * group which was provided in the "supported_groups" extension in the
531 * original ClientHello.
532 * The supported_group was based on the info in ssl->conf->group_list.
533 *
534 * If the server provided a key share that was not sent in the ClientHello
535 * then the client MUST abort the handshake with an "illegal_parameter" alert.
536 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000537 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000538 {
539 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000540 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000541 continue;
542
543 /* We found a match */
544 found = 1;
545 break;
546 }
547
548 /* Client MUST verify that the selected_group field does not
549 * correspond to a group which was provided in the "key_share"
550 * extension in the original ClientHello. If the server sent an
551 * HRR message with a key share already provided in the
552 * ClientHello then the client MUST abort the handshake with
553 * an "illegal_parameter" alert.
554 */
XiaokangQiand59be772022-01-24 10:12:51 +0000555 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000556 {
557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
558 MBEDTLS_SSL_PEND_FATAL_ALERT(
559 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
560 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
561 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
562 }
563
564 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000565 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000566
567 return( 0 );
568}
569
Jerry Yue1b9c292021-09-10 10:08:31 +0800570/*
Jerry Yub85277e2021-10-13 13:36:05 +0800571 * ssl_tls13_parse_key_share_ext()
572 * Parse key_share extension in Server Hello
573 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800574 * struct {
575 * KeyShareEntry server_share;
576 * } KeyShareServerHello;
577 * struct {
578 * NamedGroup group;
579 * opaque key_exchange<1..2^16-1>;
580 * } KeyShareEntry;
581 */
Jerry Yu4a173382021-10-11 21:45:31 +0800582static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800583 const unsigned char *buf,
584 const unsigned char *end )
585{
Jerry Yub85277e2021-10-13 13:36:05 +0800586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800587 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800588 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800589
Jerry Yu4a173382021-10-11 21:45:31 +0800590 /* ...
591 * NamedGroup group; (2 bytes)
592 * ...
593 */
594 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
595 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800596 p += 2;
597
Jerry Yu4a173382021-10-11 21:45:31 +0800598 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800599 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800600 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800601 {
602 MBEDTLS_SSL_DEBUG_MSG( 1,
603 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800604 (unsigned) offered_group, (unsigned) group ) );
605 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
606 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
607 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800608 }
609
610#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800611 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800612 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100613 const mbedtls_ecp_curve_info *curve_info =
614 mbedtls_ecp_curve_info_from_tls_id( group );
615 if( curve_info == NULL )
616 {
617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
619 }
620
621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
622
Jerry Yuc068b662021-10-11 22:30:19 +0800623 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800624 if( ret != 0 )
625 return( ret );
626 }
Jerry Yub85277e2021-10-13 13:36:05 +0800627 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800628#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800629 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800630 {
631 /* Do something */
632 }
633 else
634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
635
636 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
637 return( ret );
638}
639
Jerry Yubc20bdd2021-08-24 15:59:48 +0800640#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
641
XiaokangQiand59be772022-01-24 10:12:51 +0000642/*
643 * ssl_tls13_parse_cookie_ext()
644 * Parse cookie extension in Hello Retry Request
645 *
646 * struct {
647 * opaque cookie<1..2^16-1>;
648 * } Cookie;
649 *
650 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
651 * extension to the client (this is an exception to the usual rule that
652 * the only extensions that may be sent are those that appear in the
653 * ClientHello). When sending the new ClientHello, the client MUST copy
654 * the contents of the extension received in the HelloRetryRequest into
655 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
656 * cookies in their initial ClientHello in subsequent connections.
657 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000658static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
659 const unsigned char *buf,
660 const unsigned char *end )
661{
XiaokangQian25c9c902022-02-08 10:49:53 +0000662 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000663 const unsigned char *p = buf;
664 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
665
666 /* Retrieve length field of cookie */
667 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
668 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
669 p += 2;
670
671 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
672 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
673
674 mbedtls_free( handshake->verify_cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000675 handshake->hrr_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000676 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
677 if( handshake->verify_cookie == NULL )
678 {
679 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000680 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000681 cookie_len ) );
682 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
683 }
684
685 memcpy( handshake->verify_cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000686 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000687
688 return( 0 );
689}
XiaokangQian43550bd2022-01-21 04:32:58 +0000690
XiaokangQian0b64eed2022-01-27 10:36:51 +0000691static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000692 unsigned char *buf,
693 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000694 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000695{
696 unsigned char *p = buf;
697
XiaokangQian9deb90f2022-02-08 10:31:07 +0000698 *out_len = 0;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000699
700 if( ssl->handshake->verify_cookie == NULL )
701 {
702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
703 return( 0 );
704 }
705
706 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
707 ssl->handshake->verify_cookie,
XiaokangQian25c9c902022-02-08 10:49:53 +0000708 ssl->handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000709
XiaokangQian25c9c902022-02-08 10:49:53 +0000710 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000711
712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
713
XiaokangQian0b64eed2022-01-27 10:36:51 +0000714 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQian25c9c902022-02-08 10:49:53 +0000715 MBEDTLS_PUT_UINT16_BE( ssl->handshake->hrr_cookie_len + 2, p, 2 );
716 MBEDTLS_PUT_UINT16_BE( ssl->handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000717 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000718
719 /* Cookie */
XiaokangQian25c9c902022-02-08 10:49:53 +0000720 memcpy( p, ssl->handshake->verify_cookie, ssl->handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000721
XiaokangQian25c9c902022-02-08 10:49:53 +0000722 *out_len = ssl->handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000723
724 return( 0 );
725}
726
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800727/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800728 * CipherSuite cipher_suites<2..2^16-2>;
729 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800730static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800731 mbedtls_ssl_context *ssl,
732 unsigned char *buf,
733 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000734 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800735{
Jerry Yufec982e2021-09-07 17:26:06 +0800736 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800737 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000738 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800739 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800740
Xiaofei Baid25fab62021-12-02 06:36:27 +0000741 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800742
743 /*
744 * Ciphersuite list
745 *
746 * This is a list of the symmetric cipher options supported by
747 * the client, specifically the record protection algorithm
748 * ( including secret key length ) and a hash to be used with
749 * HKDF, in descending order of client preference.
750 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800751 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800752
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800753 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800754 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
755 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800756
Jerry Yu0c63af62021-09-02 12:59:12 +0800757 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000758 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800759 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800760 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800761 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800762 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800763
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800764 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800765 if( ciphersuite_info == NULL )
766 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800767 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
768 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800769 continue;
770
771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800772 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800773 ciphersuite_info->name ) );
774
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800775 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800776 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
777 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
778 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800779 }
780
Jerry Yu0c63af62021-09-02 12:59:12 +0800781 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000782 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800783 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800784 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800785 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
786 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800787
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800788 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000789 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800790
Jerry Yu6a643102021-08-31 14:40:36 +0800791 return( 0 );
792}
793
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800794/*
795 * Structure of ClientHello message:
796 *
797 * struct {
798 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
799 * Random random;
800 * opaque legacy_session_id<0..32>;
801 * CipherSuite cipher_suites<2..2^16-2>;
802 * opaque legacy_compression_methods<1..2^8-1>;
803 * Extension extensions<8..2^16-1>;
804 * } ClientHello;
805 */
Jerry Yu08906d02021-08-31 11:05:27 +0800806static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800807 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800808 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000809 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800810{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800811
Jerry Yubc20bdd2021-08-24 15:59:48 +0800812 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000813 unsigned char *p_extensions_len; /* Pointer to extensions length */
814 size_t output_len; /* Length of buffer used by function */
815 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800816
Jerry Yubc20bdd2021-08-24 15:59:48 +0800817 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800818 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800819
Xiaofei Baid25fab62021-12-02 06:36:27 +0000820 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800821
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800822 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800823 ssl->major_ver = ssl->conf->min_major_ver;
824 ssl->minor_ver = ssl->conf->min_minor_ver;
825
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800826 /*
827 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800828 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800829 *
830 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800831 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800832 */
Jerry Yufec982e2021-09-07 17:26:06 +0800833 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800834 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800835 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800836
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800837 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800838 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
839 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800840 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800841 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
842 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800843
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800844 /*
845 * Write legacy_session_id
846 *
847 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
848 * which has been merged with pre-shared keys in this version. A client
849 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
850 * this field to that value. In compatibility mode, this field MUST be
851 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
852 * a new 32-byte value. This value need not be random but SHOULD be
853 * unpredictable to avoid implementations fixating on a specific value
854 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
855 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800856 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100857#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
858 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
859 *p++ = (unsigned char)ssl->session_negotiate->id_len;
860 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
861 p += ssl->session_negotiate->id_len;
862
863 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
864 ssl->session_negotiate->id_len );
865#else
Jerry Yubbe09522021-09-06 21:17:54 +0800866 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
867 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100868#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800869
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800870 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800871 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800872 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800873 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800874 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800875
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800876 /* Write legacy_compression_methods
877 *
878 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800879 * one byte set to zero, which corresponds to the 'null' compression
880 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800881 */
Jerry Yubbe09522021-09-06 21:17:54 +0800882 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
883 *p++ = 1;
884 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800885
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800886 /* Write extensions */
887
888 /* Keeping track of the included extensions */
889 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800890
891 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800892 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000893 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800894 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800895
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800896 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800897 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800898 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800899 */
Jerry Yubbe09522021-09-06 21:17:54 +0800900 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800901 if( ret != 0 )
902 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800903 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800904
lhuang0486cacac2022-01-21 07:34:27 -0800905#if defined(MBEDTLS_SSL_ALPN)
906 ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
907 if( ret != 0 )
908 return( ret );
909 p += output_len;
910#endif /* MBEDTLS_SSL_ALPN */
911
XiaokangQian233397e2022-02-07 08:32:16 +0000912 /* Echo the cookie if the server provided one in its preceding
913 * HelloRetryRequest message.
914 */
XiaokangQian0b64eed2022-01-27 10:36:51 +0000915 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &output_len );
916 if( ret != 0 )
917 return( ret );
918 p += output_len;
919
Jerry Yubc20bdd2021-08-24 15:59:48 +0800920#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800921
Jerry Yub925f212022-01-12 11:17:02 +0800922 /*
923 * Add the extensions related to (EC)DHE ephemeral key establishment only if
924 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800925 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800926 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
927 {
928 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
929 if( ret != 0 )
930 return( ret );
931 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800932
Jerry Yuf46b0162022-01-11 16:28:00 +0800933 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
934 if( ret != 0 )
935 return( ret );
936 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800937
Jerry Yuf017ee42022-01-12 15:49:48 +0800938 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800939 if( ret != 0 )
940 return( ret );
941 p += output_len;
942 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800943#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
944
Xiaofei Bai15a56812021-11-05 10:52:12 +0000945#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000946 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000947 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
948 if( ret != 0 )
949 return( ret );
950 p += output_len;
951#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
952
Jerry Yubc20bdd2021-08-24 15:59:48 +0800953 /* Add more extensions here */
954
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800955 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000956 extensions_len = p - p_extensions_len - 2;
957 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800959 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000960 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800961
Xiaofei Baid25fab62021-12-02 06:36:27 +0000962 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800963 return( 0 );
964}
965
Jerry Yu92c6b402021-08-27 16:59:09 +0800966static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
967{
968 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800969
Jerry Yu92c6b402021-08-27 16:59:09 +0800970 if( ssl->conf->f_rng == NULL )
971 {
972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
973 return( MBEDTLS_ERR_SSL_NO_RNG );
974 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800975
Jerry Yu92c6b402021-08-27 16:59:09 +0800976 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
977 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800978 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800979 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800980 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800981 return( ret );
982 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800983
Ronald Cron49ad6192021-11-24 16:25:31 +0100984#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
985 /*
986 * Create a session identifier for the purpose of middlebox compatibility
987 * only if one has not been created already.
988 */
989 if( ssl->session_negotiate->id_len == 0 )
990 {
991 /* Creating a session id with 32 byte length */
992 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
993 ssl->session_negotiate->id, 32 ) ) != 0 )
994 {
995 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
996 return( ret );
997 }
998 ssl->session_negotiate->id_len = 32;
999 }
1000#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1001
Jerry Yu6f13f642021-08-26 17:18:15 +08001002 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001003}
1004
Jerry Yu92c6b402021-08-27 16:59:09 +08001005/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001006 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001007 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001008 */
1009static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001010{
Jerry Yu92c6b402021-08-27 16:59:09 +08001011 int ret = 0;
1012 unsigned char *buf;
1013 size_t buf_len, msg_len;
1014
1015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1016
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001017 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001018
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001019 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1020 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1021 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001022
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001023 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001024 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001025 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001026
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001027 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1028 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001029 msg_len );
1030 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001031
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001032 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1033 buf_len,
1034 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001035
Ronald Cron3addfa42022-02-08 16:10:25 +01001036 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1037
Jerry Yu92c6b402021-08-27 16:59:09 +08001038cleanup:
1039
1040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1041 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001042}
1043
Jerry Yu687101b2021-09-14 16:03:56 +08001044/*
Jerry Yu4a173382021-10-11 21:45:31 +08001045 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001047/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001048 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1049 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001050 * to indicate which message is expected and to be parsed next.
1051 */
Jerry Yub85277e2021-10-13 13:36:05 +08001052#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1053#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1054static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1055 const unsigned char *buf,
1056 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001057{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001058 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001059 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1060 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1061 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1062 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1063
1064 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1065 *
Jerry Yu4a173382021-10-11 21:45:31 +08001066 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 * special value of the SHA-256 of "HelloRetryRequest".
1068 *
1069 * struct {
1070 * ProtocolVersion legacy_version = 0x0303;
1071 * Random random;
1072 * opaque legacy_session_id_echo<0..32>;
1073 * CipherSuite cipher_suite;
1074 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001075 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001076 * } ServerHello;
1077 *
1078 */
Jerry Yub85277e2021-10-13 13:36:05 +08001079 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001080
1081 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001082 {
Jerry Yub85277e2021-10-13 13:36:05 +08001083 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 }
1085
Jerry Yub85277e2021-10-13 13:36:05 +08001086 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001087}
1088
Jerry Yu745bb612021-10-13 22:01:04 +08001089/* Fetch and preprocess
1090 * Returns a negative value on failure, and otherwise
1091 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1092 * - SSL_SERVER_HELLO_COORDINATE_HRR
1093 */
Jerry Yub85277e2021-10-13 13:36:05 +08001094static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1095 unsigned char **buf,
1096 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001097{
Jerry Yu4a173382021-10-11 21:45:31 +08001098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099
XiaokangQian355e09a2022-01-20 11:14:50 +00001100 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1101 MBEDTLS_SSL_HS_SERVER_HELLO,
1102 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001103
Jerry Yub85277e2021-10-13 13:36:05 +08001104 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1105 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001106 {
Jerry Yu745bb612021-10-13 22:01:04 +08001107 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1109 break;
1110 case SSL_SERVER_HELLO_COORDINATE_HRR:
1111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001112 /* If a client receives a second
1113 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1114 * was itself in response to a HelloRetryRequest), it MUST abort the
1115 * handshake with an "unexpected_message" alert.
1116 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001117 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001118 {
1119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1120 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1121 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1122 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1123 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001124 /*
1125 * Clients must abort the handshake with an "illegal_parameter"
1126 * alert if the HelloRetryRequest would not result in any change
1127 * in the ClientHello.
1128 * In a PSK only key exchange that what we expect.
1129 */
1130 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1131 {
1132 MBEDTLS_SSL_DEBUG_MSG( 1,
1133 ( "Unexpected HRR in pure PSK key exchange." ) );
1134 MBEDTLS_SSL_PEND_FATAL_ALERT(
1135 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1136 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1137 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1138 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001139
XiaokangQiand9e068e2022-01-18 06:23:32 +00001140 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001141
Jerry Yu745bb612021-10-13 22:01:04 +08001142 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001143 }
1144
1145cleanup:
1146
1147 return( ret );
1148}
1149
Jerry Yu4a173382021-10-11 21:45:31 +08001150static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1151 const unsigned char **buf,
1152 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001153{
1154 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001155 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156
Jerry Yude4fb2c2021-09-19 18:05:08 +08001157 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001158 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001159
Jerry Yu4a173382021-10-11 21:45:31 +08001160 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001161
1162 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001163 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1164 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001166 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1167 ssl->session_negotiate->id,
1168 ssl->session_negotiate->id_len );
1169 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001170 legacy_session_id_echo_len );
1171
1172 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1173 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1174
Jerry Yue1b9c292021-09-10 10:08:31 +08001175 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1176 }
1177
Jerry Yu4a173382021-10-11 21:45:31 +08001178 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 *buf = p;
1180
1181 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001182 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001183 return( 0 );
1184}
1185
Jerry Yu4a173382021-10-11 21:45:31 +08001186static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1187 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001188{
Jerry Yu4a173382021-10-11 21:45:31 +08001189 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1190
Jerry Yue1b9c292021-09-10 10:08:31 +08001191 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001192 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 {
Jerry Yu4a173382021-10-11 21:45:31 +08001194 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001195 {
1196 return( 1 );
1197 }
1198 }
1199 return( 0 );
1200}
1201
1202/* Parse ServerHello message and configure context
1203 *
1204 * struct {
1205 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1206 * Random random;
1207 * opaque legacy_session_id_echo<0..32>;
1208 * CipherSuite cipher_suite;
1209 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001210 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001211 * } ServerHello;
1212 */
Jerry Yuc068b662021-10-11 22:30:19 +08001213static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1214 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001215 const unsigned char *end,
1216 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001217{
Jerry Yub85277e2021-10-13 13:36:05 +08001218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001219 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001220 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001221 size_t extensions_len;
1222 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001223 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001224 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001225 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001226 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001227
1228 /*
1229 * Check there is space for minimal fields
1230 *
1231 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001232 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 * - legacy_session_id_echo ( 1 byte ), minimum size
1234 * - cipher_suite ( 2 bytes)
1235 * - legacy_compression_method ( 1 byte )
1236 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001237 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001238
1239 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001240 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1241
Jerry Yu4a173382021-10-11 21:45:31 +08001242 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001243 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001244 * ...
1245 * with ProtocolVersion defined as:
1246 * uint16 ProtocolVersion;
1247 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001248 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1249 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1250 {
1251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1252 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1253 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001254 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1255 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001256 }
1257 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001258
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001259 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001260 * Random random;
1261 * ...
1262 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001263 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001264 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001265 if( !is_hrr )
1266 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001267 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001268 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1269 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1270 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1271 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001272 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001273
Jerry Yu4a173382021-10-11 21:45:31 +08001274 /* ...
1275 * opaque legacy_session_id_echo<0..32>;
1276 * ...
1277 */
1278 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001279 {
XiaokangQian52da5582022-01-26 09:49:29 +00001280 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001281 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001282 }
1283
Jerry Yu4a173382021-10-11 21:45:31 +08001284 /* ...
1285 * CipherSuite cipher_suite;
1286 * ...
1287 * with CipherSuite defined as:
1288 * uint8 CipherSuite[2];
1289 */
1290 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001291 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1292 p += 2;
1293
Jerry Yu4a173382021-10-11 21:45:31 +08001294
XiaokangQian355e09a2022-01-20 11:14:50 +00001295 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001296 /*
1297 * Check whether this ciphersuite is supported and offered.
1298 * Via the force_ciphersuite version we may have instructed the client
1299 * to use a different ciphersuite.
1300 */
Jerry Yu4a173382021-10-11 21:45:31 +08001301 if( ciphersuite_info == NULL ||
1302 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001303 {
XiaokangQian52da5582022-01-26 09:49:29 +00001304 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001305 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001306 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001307 * If we received an HRR before and that the proposed selected
1308 * ciphersuite in this server hello is not the same as the one
1309 * proposed in the HRR, we abort the handshake and send an
1310 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001311 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001312 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1313 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001314 {
XiaokangQian52da5582022-01-26 09:49:29 +00001315 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001316 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001317
XiaokangQian52da5582022-01-26 09:49:29 +00001318 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001319 {
1320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1321 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001322 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001323 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001324
Jerry Yu4a173382021-10-11 21:45:31 +08001325 /* Configure ciphersuites */
1326 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1327
XiaokangQian355e09a2022-01-20 11:14:50 +00001328 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001329 ssl->session_negotiate->ciphersuite = cipher_suite;
1330
Jerry Yue1b9c292021-09-10 10:08:31 +08001331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1332 cipher_suite, ciphersuite_info->name ) );
1333
1334#if defined(MBEDTLS_HAVE_TIME)
1335 ssl->session_negotiate->start = time( NULL );
1336#endif /* MBEDTLS_HAVE_TIME */
1337
Jerry Yu4a173382021-10-11 21:45:31 +08001338 /* ...
1339 * uint8 legacy_compression_method = 0;
1340 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001341 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001342 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001343 if( p[0] != 0 )
1344 {
Jerry Yub85277e2021-10-13 13:36:05 +08001345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001346 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001347 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001348 }
1349 p++;
1350
Jerry Yub85277e2021-10-13 13:36:05 +08001351 /* ...
1352 * Extension extensions<6..2^16-1>;
1353 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001354 * struct {
1355 * ExtensionType extension_type; (2 bytes)
1356 * opaque extension_data<0..2^16-1>;
1357 * } Extension;
1358 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001359 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001360 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001361 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001362
Jerry Yu4a173382021-10-11 21:45:31 +08001363 /* Check extensions do not go beyond the buffer of data. */
1364 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1365 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001366
Jerry Yu4a173382021-10-11 21:45:31 +08001367 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001368
Jerry Yu4a173382021-10-11 21:45:31 +08001369 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001370 {
1371 unsigned int extension_type;
1372 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001373 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001374
Jerry Yu4a173382021-10-11 21:45:31 +08001375 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001376 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1377 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1378 p += 4;
1379
Jerry Yu4a173382021-10-11 21:45:31 +08001380 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001381 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001382
1383 switch( extension_type )
1384 {
XiaokangQianb851da82022-01-14 04:03:11 +00001385 case MBEDTLS_TLS_EXT_COOKIE:
1386
XiaokangQiand9e068e2022-01-18 06:23:32 +00001387 if( !is_hrr )
1388 {
XiaokangQian52da5582022-01-26 09:49:29 +00001389 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001390 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001391 }
1392
XiaokangQian43550bd2022-01-21 04:32:58 +00001393 ret = ssl_tls13_parse_cookie_ext( ssl,
1394 p, extension_data_end );
1395 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001396 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001397 MBEDTLS_SSL_DEBUG_RET( 1,
1398 "ssl_tls13_parse_cookie_ext",
1399 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001400 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001401 }
XiaokangQianb851da82022-01-14 04:03:11 +00001402 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001403
Jerry Yue1b9c292021-09-10 10:08:31 +08001404 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001405 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001406 MBEDTLS_SSL_DEBUG_MSG( 3,
1407 ( "found supported_versions extension" ) );
1408
Jerry Yuc068b662021-10-11 22:30:19 +08001409 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001410 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001411 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001412 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001413 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001414 break;
1415
1416 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001419
XiaokangQian52da5582022-01-26 09:49:29 +00001420 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001421 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001422
1423#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1424 case MBEDTLS_TLS_EXT_KEY_SHARE:
1425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001426 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1427 {
XiaokangQian52da5582022-01-26 09:49:29 +00001428 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001429 goto cleanup;
1430 }
1431
XiaokangQian53f20b72022-01-18 10:47:33 +00001432 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001433 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001434 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001435 else
XiaokangQianb851da82022-01-14 04:03:11 +00001436 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001437 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001438 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001439 {
1440 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001441 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001442 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001443 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 }
1445 break;
1446#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1447
1448 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001449 MBEDTLS_SSL_DEBUG_MSG(
1450 3,
1451 ( "unknown extension found: %u ( ignoring )",
1452 extension_type ) );
1453
XiaokangQian52da5582022-01-26 09:49:29 +00001454 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001455 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 }
1457
1458 p += extension_data_len;
1459 }
1460
XiaokangQian78b1fa72022-01-19 06:56:30 +00001461 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001462 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001464 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001465 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001466 }
1467
XiaokangQiand59be772022-01-24 10:12:51 +00001468cleanup:
1469
XiaokangQian52da5582022-01-26 09:49:29 +00001470 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001471 {
1472 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1473 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1474 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1475 }
XiaokangQian52da5582022-01-26 09:49:29 +00001476 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001477 {
1478 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1479 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1480 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1481 }
1482 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001483}
1484
XiaokangQian355e09a2022-01-20 11:14:50 +00001485static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001486{
Jerry Yub85277e2021-10-13 13:36:05 +08001487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001488 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001489 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001490 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001491
Jerry Yub85277e2021-10-13 13:36:05 +08001492 /* Determine the key exchange mode:
1493 * 1) If both the pre_shared_key and key_share extensions were received
1494 * then the key exchange mode is PSK with EPHEMERAL.
1495 * 2) If only the pre_shared_key extension was received then the key
1496 * exchange mode is PSK-only.
1497 * 3) If only the key_share extension was received then the key
1498 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001499 */
Jerry Yub85277e2021-10-13 13:36:05 +08001500 switch( handshake->extensions_present &
1501 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001502 {
Jerry Yu745bb612021-10-13 22:01:04 +08001503 /* Only the pre_shared_key extension was received */
1504 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001505 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001506 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001507
Jerry Yu745bb612021-10-13 22:01:04 +08001508 /* Only the key_share extension was received */
1509 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001510 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001511 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001512
Jerry Yu745bb612021-10-13 22:01:04 +08001513 /* Both the pre_shared_key and key_share extensions were received */
1514 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001515 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001516 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001517
Jerry Yu745bb612021-10-13 22:01:04 +08001518 /* Neither pre_shared_key nor key_share extension was received */
1519 default:
1520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1521 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1522 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001523 }
1524
1525 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1526 *
1527 * TODO: We don't have to do this in case we offered 0-RTT and the
1528 * server accepted it. In this case, we could skip generating
1529 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001530 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001531 if( ret != 0 )
1532 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001534 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001535 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001536 }
1537
1538 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001539 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001540 if( ret != 0 )
1541 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001543 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001544 }
1545
1546 /* Next evolution in key schedule: Establish handshake secret and
1547 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001548 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001549 if( ret != 0 )
1550 {
Jerry Yuc068b662021-10-11 22:30:19 +08001551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1552 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001553 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001554 }
1555
Jerry Yub85277e2021-10-13 13:36:05 +08001556 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001557 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001558 {
1559 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1560 goto cleanup;
1561 }
Jerry Yu0b177842021-09-05 19:41:30 +08001562
1563 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1564 ssl->conf->endpoint,
1565 ssl->session_negotiate->ciphersuite,
1566 &traffic_keys,
1567 ssl );
1568 if( ret != 0 )
1569 {
1570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001571 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001572 }
1573
Jerry Yub85277e2021-10-13 13:36:05 +08001574 handshake->transform_handshake = transform_handshake;
1575 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001576
1577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1578 ssl->session_in = ssl->session_negotiate;
1579
1580 /*
1581 * State machine update
1582 */
1583 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1584
Jerry Yu4a173382021-10-11 21:45:31 +08001585cleanup:
1586
Jerry Yu0b177842021-09-05 19:41:30 +08001587 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001588 if( ret != 0 )
1589 {
Jerry Yub85277e2021-10-13 13:36:05 +08001590 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001591
Jerry Yu4a173382021-10-11 21:45:31 +08001592 MBEDTLS_SSL_PEND_FATAL_ALERT(
1593 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1594 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1595 }
1596 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001597}
1598
XiaokangQian355e09a2022-01-20 11:14:50 +00001599static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001600{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001601#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1603
XiaokangQian647719a2021-12-07 09:16:29 +00001604#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1605 /* If not offering early data, the client sends a dummy CCS record
1606 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001607 * its second ClientHello or before its encrypted handshake flight.
1608 */
XiaokangQian647719a2021-12-07 09:16:29 +00001609 mbedtls_ssl_handshake_set_state( ssl,
1610 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1611#else
1612 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1613#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1614
XiaokangQian78b1fa72022-01-19 06:56:30 +00001615 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001616
XiaokangQian78b1fa72022-01-19 06:56:30 +00001617 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001618 * We are going to re-generate a shared secret corresponding to the group
1619 * selected by the server, which is different from the group for which we
1620 * generated a shared secret in the first client hello.
1621 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001622 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001623 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001624 if( ret != 0 )
1625 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001626#else
1627 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001628#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001629
1630 return( 0 );
1631}
1632
Jerry Yue1b9c292021-09-10 10:08:31 +08001633/*
Jerry Yu4a173382021-10-11 21:45:31 +08001634 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001635 * Handler for MBEDTLS_SSL_SERVER_HELLO
1636 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001637static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001638{
Jerry Yu4a173382021-10-11 21:45:31 +08001639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001640 unsigned char *buf = NULL;
1641 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001642 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001643
1644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1645
1646 /* Coordination step
1647 * - Fetch record
1648 * - Make sure it's either a ServerHello or a HRR.
1649 * - Switch processing routine in case of HRR
1650 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001651 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1652 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1653
XiaokangQian16acd4b2022-01-14 07:35:47 +00001654 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001655 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001656 goto cleanup;
1657 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001658 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001659
XiaokangQianb851da82022-01-14 04:03:11 +00001660 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001661 buf + buf_len,
1662 is_hrr ) );
1663 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001664 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1665
XiaokangQianb851da82022-01-14 04:03:11 +00001666 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1667 MBEDTLS_SSL_HS_SERVER_HELLO,
1668 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001669
XiaokangQiand9e068e2022-01-18 06:23:32 +00001670 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001671 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001672 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001673 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001674
1675cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1677 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001678 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001679}
1680
1681/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001682 *
1683 * EncryptedExtensions message
1684 *
1685 * The EncryptedExtensions message contains any extensions which
1686 * should be protected, i.e., any which are not needed to establish
1687 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001688 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001689
1690/*
1691 * Overview
1692 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001693
1694/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001695static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001696
XiaokangQian97799ac2021-10-11 10:05:54 +00001697static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1698 const unsigned char *buf,
1699 const unsigned char *end );
1700static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001701
1702/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001703 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001704 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001705static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001706{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001707 int ret;
1708 unsigned char *buf;
1709 size_t buf_len;
1710
1711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1712
Xiaofei Bai746f9482021-11-12 08:53:56 +00001713 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001714 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001715 &buf, &buf_len ) );
1716
1717 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001718 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001719 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001720
Xiaofei Bai746f9482021-11-12 08:53:56 +00001721 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001722 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001723
XiaokangQian97799ac2021-10-11 10:05:54 +00001724 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001725
1726cleanup:
1727
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1729 return( ret );
1730
1731}
1732
XiaokangQian08da26c2021-10-09 10:12:11 +00001733/* Parse EncryptedExtensions message
1734 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001735 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001736 * } EncryptedExtensions;
1737 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001738static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1739 const unsigned char *buf,
1740 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001741{
1742 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001743 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001744 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001745 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001746
XiaokangQian08da26c2021-10-09 10:12:11 +00001747 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001748 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001749 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001750
XiaokangQian97799ac2021-10-11 10:05:54 +00001751 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001752 extensions_end = p + extensions_len;
1753 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001754
XiaokangQian8db25ff2021-10-13 05:56:18 +00001755 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001756 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001757 unsigned int extension_type;
1758 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001759
XiaokangQian08da26c2021-10-09 10:12:11 +00001760 /*
1761 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001762 * ExtensionType extension_type; (2 bytes)
1763 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001764 * } Extension;
1765 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001766 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001767 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1768 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1769 p += 4;
1770
XiaokangQian8db25ff2021-10-13 05:56:18 +00001771 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001772
XiaokangQian97799ac2021-10-11 10:05:54 +00001773 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001774 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001775 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001776 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001777 switch( extension_type )
1778 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001779
XiaokangQian08da26c2021-10-09 10:12:11 +00001780 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1782 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001783
lhuang0486cacac2022-01-21 07:34:27 -08001784#if defined(MBEDTLS_SSL_ALPN)
1785 case MBEDTLS_TLS_EXT_ALPN:
1786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1787
1788 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1789 {
1790 return( ret );
1791 }
1792
1793 break;
1794#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001795 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001796 MBEDTLS_SSL_DEBUG_MSG(
1797 3, ( "unsupported extension found: %u ", extension_type) );
1798 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001799 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001800 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1801 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001802 }
1803
XiaokangQian08da26c2021-10-09 10:12:11 +00001804 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001805 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001806
XiaokangQian97799ac2021-10-11 10:05:54 +00001807 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001808 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001809 {
1810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001811 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001812 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001813 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001814 }
1815
1816 return( ret );
1817}
1818
XiaokangQian97799ac2021-10-11 10:05:54 +00001819static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001820{
Jerry Yua93ac112021-10-27 16:31:48 +08001821#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001822 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001823 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1824 else
Jerry Yud2674312021-10-29 10:08:19 +08001825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001826#else
1827 ((void) ssl);
1828 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1829#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001830 return( 0 );
1831}
1832
Jerry Yua93ac112021-10-27 16:31:48 +08001833#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001834/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001835 *
1836 * STATE HANDLING: CertificateRequest
1837 *
Jerry Yud2674312021-10-29 10:08:19 +08001838 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001839#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1840#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001841/* Coordination:
1842 * Deals with the ambiguity of not knowing if a CertificateRequest
1843 * will be sent. Returns a negative code on failure, or
1844 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1845 * - SSL_CERTIFICATE_REQUEST_SKIP
1846 * indicating if a Certificate Request is expected or not.
1847 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001848static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001849{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001851
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001852 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001853 {
1854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1855 return( SSL_CERTIFICATE_REQUEST_SKIP );
1856 }
1857
1858 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001859 {
1860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1861 return( ret );
1862 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001863 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001864
1865 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1866 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1867 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001868 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001869 }
1870
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001871 return( SSL_CERTIFICATE_REQUEST_SKIP );
1872}
1873
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001874/*
1875 * ssl_tls13_parse_certificate_request()
1876 * Parse certificate request
1877 * struct {
1878 * opaque certificate_request_context<0..2^8-1>;
1879 * Extension extensions<2..2^16-1>;
1880 * } CertificateRequest;
1881 */
1882static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1883 const unsigned char *buf,
1884 const unsigned char *end )
1885{
1886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1887 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001888 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001889 size_t extensions_len = 0;
1890 const unsigned char *extensions_end;
1891 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001892
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001893 /* ...
1894 * opaque certificate_request_context<0..2^8-1>
1895 * ...
1896 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001897 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1898 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001899 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001900
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001901 if( certificate_request_context_len > 0 )
1902 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001903 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001904 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1905 p, certificate_request_context_len );
1906
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001907 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001908 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001909 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001910 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001911 {
1912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1913 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1914 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001915 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001916 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001917 p += certificate_request_context_len;
1918 }
1919
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001920 /* ...
1921 * Extension extensions<2..2^16-1>;
1922 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001923 */
1924 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1925 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1926 p += 2;
1927
1928 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1929 extensions_end = p + extensions_len;
1930
1931 while( p < extensions_end )
1932 {
1933 unsigned int extension_type;
1934 size_t extension_data_len;
1935
1936 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1937 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1938 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1939 p += 4;
1940
1941 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1942
1943 switch( extension_type )
1944 {
1945 case MBEDTLS_TLS_EXT_SIG_ALG:
1946 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001947 ( "found signature algorithms extension" ) );
1948 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001949 p + extension_data_len );
1950 if( ret != 0 )
1951 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001952 if( ! sig_alg_ext_found )
1953 sig_alg_ext_found = 1;
1954 else
1955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 3,
1957 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001958 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001959 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001960 break;
1961
1962 default:
1963 MBEDTLS_SSL_DEBUG_MSG(
1964 3,
1965 ( "unknown extension found: %u ( ignoring )",
1966 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001967 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001968 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001969 p += extension_data_len;
1970 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001971 /* Check that we consumed all the message. */
1972 if( p != end )
1973 {
1974 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001975 ( "CertificateRequest misaligned" ) );
1976 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001977 }
1978 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001979 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001980 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001981 MBEDTLS_SSL_DEBUG_MSG( 3,
1982 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001983 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001984 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001985
Jerry Yu7840f812022-01-29 10:26:51 +08001986 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001988
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001989decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001990 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1991 MBEDTLS_ERR_SSL_DECODE_ERROR );
1992 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001993}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001994
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001995/*
1996 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1997 */
1998static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001999{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002001
2002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2003
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002004 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2005
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002006 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2007 {
2008 unsigned char *buf;
2009 size_t buf_len;
2010
2011 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2012 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2013 &buf, &buf_len ) );
2014
2015 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2016 buf, buf + buf_len ) );
2017
2018 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2019 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2020 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002021 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002022 {
2023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002024 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002025 }
2026 else
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002029 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2030 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002031 }
2032
2033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002034 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002035
Jerry Yud2674312021-10-29 10:08:19 +08002036 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2037
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002038cleanup:
2039
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2041 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002042}
2043
2044/*
Jerry Yu687101b2021-09-14 16:03:56 +08002045 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2046 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002047static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002048{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002049 int ret;
2050
2051 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002052 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002053 return( ret );
2054
Jerry Yu687101b2021-09-14 16:03:56 +08002055 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2056 return( 0 );
2057}
2058
2059/*
2060 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2061 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002062static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002063{
Jerry Yu30b071c2021-09-12 20:16:03 +08002064 int ret;
2065
2066 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2067 if( ret != 0 )
2068 return( ret );
2069
Jerry Yu687101b2021-09-14 16:03:56 +08002070 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2071 return( 0 );
2072}
Jerry Yua93ac112021-10-27 16:31:48 +08002073#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002074
Jerry Yu687101b2021-09-14 16:03:56 +08002075/*
2076 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2077 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002078static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002079{
XiaokangQianac0385c2021-11-03 06:40:11 +00002080 int ret;
2081
XiaokangQianc5c39d52021-11-09 11:55:10 +00002082 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002083 if( ret != 0 )
2084 return( ret );
2085
Ronald Cron49ad6192021-11-24 16:25:31 +01002086#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2087 mbedtls_ssl_handshake_set_state(
2088 ssl,
2089 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2090#else
Jerry Yuca133a32022-02-15 14:22:05 +08002091 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002092#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002093
XiaokangQianac0385c2021-11-03 06:40:11 +00002094 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002095}
2096
2097/*
Jerry Yu566c7812022-01-26 15:41:22 +08002098 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2099 */
2100static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2101{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002102 int non_empty_certificate_msg = 0;
2103
Jerry Yu5cc35062022-01-28 16:16:08 +08002104 MBEDTLS_SSL_DEBUG_MSG( 1,
2105 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002106 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002107
Ronald Cron9df7c802022-03-08 18:38:54 +01002108#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002109 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002110 {
2111 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2112 if( ret != 0 )
2113 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002114
Ronald Cron7a94aca2022-03-09 07:44:27 +01002115 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2116 non_empty_certificate_msg = 1;
2117 }
2118 else
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2121 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002122#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002123
Ronald Cron7a94aca2022-03-09 07:44:27 +01002124 if( non_empty_certificate_msg )
2125 {
2126 mbedtls_ssl_handshake_set_state( ssl,
2127 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2128 }
2129 else
2130 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2131
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002132 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002133}
2134
Ronald Cron9df7c802022-03-08 18:38:54 +01002135#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002136/*
2137 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2138 */
2139static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2140{
Ronald Crona8b38872022-03-09 07:59:25 +01002141 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2142
2143 if( ret == 0 )
2144 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2145
2146 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002147}
Jerry Yu90f152d2022-01-29 22:12:42 +08002148#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002149
2150/*
Jerry Yu687101b2021-09-14 16:03:56 +08002151 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2152 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002153static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002154{
XiaokangQian0fa66432021-11-15 03:33:57 +00002155 int ret;
2156
2157 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2158 if( ret != 0 )
2159 return( ret );
2160
2161 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2162 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002163}
2164
2165/*
2166 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2167 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002168static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002169{
Jerry Yu378254d2021-10-30 21:44:47 +08002170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002171 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002172 return( 0 );
2173}
2174
2175/*
2176 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2177 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002178static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002179{
Jerry Yu378254d2021-10-30 21:44:47 +08002180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2181 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2182
2183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2184 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2185
2186 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2187
2188 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2189 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002190}
2191
Jerry Yu92c6b402021-08-27 16:59:09 +08002192int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002193{
Jerry Yu92c6b402021-08-27 16:59:09 +08002194 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002195
Jerry Yue3b34122021-09-28 17:53:35 +08002196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2197 mbedtls_ssl_states_str( ssl->state ),
2198 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002199
2200 switch( ssl->state )
2201 {
2202 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002203 * ssl->state is initialized as HELLO_REQUEST. It is the same
2204 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002205 */
2206 case MBEDTLS_SSL_HELLO_REQUEST:
2207 case MBEDTLS_SSL_CLIENT_HELLO:
2208 ret = ssl_tls13_write_client_hello( ssl );
2209 break;
2210
2211 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002212 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002213 break;
2214
2215 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002216 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002217 break;
2218
Jerry Yua93ac112021-10-27 16:31:48 +08002219#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002220 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2221 ret = ssl_tls13_process_certificate_request( ssl );
2222 break;
2223
Jerry Yu687101b2021-09-14 16:03:56 +08002224 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002225 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002226 break;
2227
2228 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002229 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002230 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002231#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002232
2233 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002234 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002235 break;
2236
Jerry Yu566c7812022-01-26 15:41:22 +08002237 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2238 ret = ssl_tls13_write_client_certificate( ssl );
2239 break;
2240
Ronald Cron9df7c802022-03-08 18:38:54 +01002241#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002242 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2243 ret = ssl_tls13_write_client_certificate_verify( ssl );
2244 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002245#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002246
Jerry Yu687101b2021-09-14 16:03:56 +08002247 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002248 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002249 break;
2250
2251 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002252 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002253 break;
2254
2255 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002256 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002257 break;
2258
Ronald Cron49ad6192021-11-24 16:25:31 +01002259 /*
2260 * Injection of dummy-CCS's for middlebox compatibility
2261 */
2262#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002263 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002264 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2265 if( ret == 0 )
2266 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2267 break;
2268
2269 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2270 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2271 if( ret == 0 )
2272 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002273 break;
2274#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2275
Jerry Yu92c6b402021-08-27 16:59:09 +08002276 default:
2277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2279 }
2280
2281 return( ret );
2282}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002283
Jerry Yufb4b6472022-01-27 15:03:26 +08002284#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002285
Jerry Yufb4b6472022-01-27 15:03:26 +08002286