blob: 05b79415da74871fccca7efa516f10d276269a31 [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{
662 size_t cookie_len;
663 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 );
XiaokangQian34909742022-01-27 02:25:04 +0000675 handshake->verify_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,
680 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
681 cookie_len ) );
682 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
683 }
684
685 memcpy( handshake->verify_cookie, p, cookie_len );
686 handshake->verify_cookie_len = (unsigned char) cookie_len;
687
688 return( 0 );
689}
XiaokangQian43550bd2022-01-21 04:32:58 +0000690
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800691/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800692 * CipherSuite cipher_suites<2..2^16-2>;
693 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800694static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800695 mbedtls_ssl_context *ssl,
696 unsigned char *buf,
697 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000698 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800699{
Jerry Yufec982e2021-09-07 17:26:06 +0800700 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800701 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000702 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800703 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800704
Xiaofei Baid25fab62021-12-02 06:36:27 +0000705 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800706
707 /*
708 * Ciphersuite list
709 *
710 * This is a list of the symmetric cipher options supported by
711 * the client, specifically the record protection algorithm
712 * ( including secret key length ) and a hash to be used with
713 * HKDF, in descending order of client preference.
714 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800715 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800716
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800717 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800718 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
719 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800720
Jerry Yu0c63af62021-09-02 12:59:12 +0800721 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000722 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800723 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800724 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800725 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800726 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800727
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800728 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800729 if( ciphersuite_info == NULL )
730 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800731 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
732 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800733 continue;
734
735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800736 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800737 ciphersuite_info->name ) );
738
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800739 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800740 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
741 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
742 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800743 }
744
Jerry Yu0c63af62021-09-02 12:59:12 +0800745 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000746 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800747 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800748 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800749 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
750 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800751
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800752 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000753 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800754
Jerry Yu6a643102021-08-31 14:40:36 +0800755 return( 0 );
756}
757
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800758/*
759 * Structure of ClientHello message:
760 *
761 * struct {
762 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
763 * Random random;
764 * opaque legacy_session_id<0..32>;
765 * CipherSuite cipher_suites<2..2^16-2>;
766 * opaque legacy_compression_methods<1..2^8-1>;
767 * Extension extensions<8..2^16-1>;
768 * } ClientHello;
769 */
Jerry Yu08906d02021-08-31 11:05:27 +0800770static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800771 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800772 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000773 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800774{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800775
Jerry Yubc20bdd2021-08-24 15:59:48 +0800776 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000777 unsigned char *p_extensions_len; /* Pointer to extensions length */
778 size_t output_len; /* Length of buffer used by function */
779 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800780
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800782 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783
Xiaofei Baid25fab62021-12-02 06:36:27 +0000784 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800785
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800786 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800787 ssl->major_ver = ssl->conf->min_major_ver;
788 ssl->minor_ver = ssl->conf->min_minor_ver;
789
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800790 /*
791 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800792 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800793 *
794 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800795 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800796 */
Jerry Yufec982e2021-09-07 17:26:06 +0800797 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800798 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800799 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800800
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800801 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800802 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
803 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800804 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800805 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
806 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800807
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800808 /*
809 * Write legacy_session_id
810 *
811 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
812 * which has been merged with pre-shared keys in this version. A client
813 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
814 * this field to that value. In compatibility mode, this field MUST be
815 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
816 * a new 32-byte value. This value need not be random but SHOULD be
817 * unpredictable to avoid implementations fixating on a specific value
818 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
819 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800820 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100821#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
822 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
823 *p++ = (unsigned char)ssl->session_negotiate->id_len;
824 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
825 p += ssl->session_negotiate->id_len;
826
827 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
828 ssl->session_negotiate->id_len );
829#else
Jerry Yubbe09522021-09-06 21:17:54 +0800830 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
831 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100832#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800833
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800834 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800835 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800836 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800837 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800838 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800839
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800840 /* Write legacy_compression_methods
841 *
842 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800843 * one byte set to zero, which corresponds to the 'null' compression
844 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800845 */
Jerry Yubbe09522021-09-06 21:17:54 +0800846 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
847 *p++ = 1;
848 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800849
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800850 /* Write extensions */
851
852 /* Keeping track of the included extensions */
853 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800854
855 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800856 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000857 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800858 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800859
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800860 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800861 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800862 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800863 */
Jerry Yubbe09522021-09-06 21:17:54 +0800864 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800865 if( ret != 0 )
866 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800867 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800868
lhuang0486cacac2022-01-21 07:34:27 -0800869#if defined(MBEDTLS_SSL_ALPN)
870 ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
871 if( ret != 0 )
872 return( ret );
873 p += output_len;
874#endif /* MBEDTLS_SSL_ALPN */
875
Jerry Yubc20bdd2021-08-24 15:59:48 +0800876#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800877
Jerry Yub925f212022-01-12 11:17:02 +0800878 /*
879 * Add the extensions related to (EC)DHE ephemeral key establishment only if
880 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800881 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800882 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
883 {
884 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
885 if( ret != 0 )
886 return( ret );
887 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800888
Jerry Yuf46b0162022-01-11 16:28:00 +0800889 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
890 if( ret != 0 )
891 return( ret );
892 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800893
Jerry Yuf017ee42022-01-12 15:49:48 +0800894 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800895 if( ret != 0 )
896 return( ret );
897 p += output_len;
898 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800899#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
900
Xiaofei Bai15a56812021-11-05 10:52:12 +0000901#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000902 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000903 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
904 if( ret != 0 )
905 return( ret );
906 p += output_len;
907#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
908
Jerry Yubc20bdd2021-08-24 15:59:48 +0800909 /* Add more extensions here */
910
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800911 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000912 extensions_len = p - p_extensions_len - 2;
913 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800914 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800915 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000916 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800917
Xiaofei Baid25fab62021-12-02 06:36:27 +0000918 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800919 return( 0 );
920}
921
Jerry Yu92c6b402021-08-27 16:59:09 +0800922static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
923{
924 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800925
Jerry Yu92c6b402021-08-27 16:59:09 +0800926 if( ssl->conf->f_rng == NULL )
927 {
928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
929 return( MBEDTLS_ERR_SSL_NO_RNG );
930 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800931
Jerry Yu92c6b402021-08-27 16:59:09 +0800932 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
933 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800934 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800935 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800936 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800937 return( ret );
938 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800939
Ronald Cron49ad6192021-11-24 16:25:31 +0100940#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
941 /*
942 * Create a session identifier for the purpose of middlebox compatibility
943 * only if one has not been created already.
944 */
945 if( ssl->session_negotiate->id_len == 0 )
946 {
947 /* Creating a session id with 32 byte length */
948 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
949 ssl->session_negotiate->id, 32 ) ) != 0 )
950 {
951 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
952 return( ret );
953 }
954 ssl->session_negotiate->id_len = 32;
955 }
956#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
957
Jerry Yu6f13f642021-08-26 17:18:15 +0800958 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800959}
960
Jerry Yu92c6b402021-08-27 16:59:09 +0800961/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800962 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800963 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800964 */
965static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800966{
Jerry Yu92c6b402021-08-27 16:59:09 +0800967 int ret = 0;
968 unsigned char *buf;
969 size_t buf_len, msg_len;
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
972
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800973 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800974
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800975 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
976 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
977 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800978
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800979 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800980 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800981 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800982
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800983 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
984 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800985 msg_len );
986 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800987
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800988 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
989 buf_len,
990 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800991
Ronald Cron3addfa42022-02-08 16:10:25 +0100992 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
993
Jerry Yu92c6b402021-08-27 16:59:09 +0800994cleanup:
995
996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
997 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800998}
999
Jerry Yu687101b2021-09-14 16:03:56 +08001000/*
Jerry Yu4a173382021-10-11 21:45:31 +08001001 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001002 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001003/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001004 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1005 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001006 * to indicate which message is expected and to be parsed next.
1007 */
Jerry Yub85277e2021-10-13 13:36:05 +08001008#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1009#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1010static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1011 const unsigned char *buf,
1012 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001013{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001014 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001015 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1016 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1017 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1018 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1019
1020 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1021 *
Jerry Yu4a173382021-10-11 21:45:31 +08001022 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001023 * special value of the SHA-256 of "HelloRetryRequest".
1024 *
1025 * struct {
1026 * ProtocolVersion legacy_version = 0x0303;
1027 * Random random;
1028 * opaque legacy_session_id_echo<0..32>;
1029 * CipherSuite cipher_suite;
1030 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001031 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001032 * } ServerHello;
1033 *
1034 */
Jerry Yub85277e2021-10-13 13:36:05 +08001035 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001036
1037 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 {
Jerry Yub85277e2021-10-13 13:36:05 +08001039 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 }
1041
Jerry Yub85277e2021-10-13 13:36:05 +08001042 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001043}
1044
Jerry Yu745bb612021-10-13 22:01:04 +08001045/* Fetch and preprocess
1046 * Returns a negative value on failure, and otherwise
1047 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1048 * - SSL_SERVER_HELLO_COORDINATE_HRR
1049 */
Jerry Yub85277e2021-10-13 13:36:05 +08001050static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1051 unsigned char **buf,
1052 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001053{
Jerry Yu4a173382021-10-11 21:45:31 +08001054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001055
XiaokangQian355e09a2022-01-20 11:14:50 +00001056 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1057 MBEDTLS_SSL_HS_SERVER_HELLO,
1058 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001059
Jerry Yub85277e2021-10-13 13:36:05 +08001060 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1061 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001062 {
Jerry Yu745bb612021-10-13 22:01:04 +08001063 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1065 break;
1066 case SSL_SERVER_HELLO_COORDINATE_HRR:
1067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001068 /* If a client receives a second
1069 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1070 * was itself in response to a HelloRetryRequest), it MUST abort the
1071 * handshake with an "unexpected_message" alert.
1072 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001073 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001074 {
1075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1076 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1077 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1078 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1079 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001080 /*
1081 * Clients must abort the handshake with an "illegal_parameter"
1082 * alert if the HelloRetryRequest would not result in any change
1083 * in the ClientHello.
1084 * In a PSK only key exchange that what we expect.
1085 */
1086 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1087 {
1088 MBEDTLS_SSL_DEBUG_MSG( 1,
1089 ( "Unexpected HRR in pure PSK key exchange." ) );
1090 MBEDTLS_SSL_PEND_FATAL_ALERT(
1091 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1092 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1093 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1094 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001095
XiaokangQiand9e068e2022-01-18 06:23:32 +00001096 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001097
Jerry Yu745bb612021-10-13 22:01:04 +08001098 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099 }
1100
1101cleanup:
1102
1103 return( ret );
1104}
1105
Jerry Yu4a173382021-10-11 21:45:31 +08001106static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1107 const unsigned char **buf,
1108 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001109{
1110 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001111 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
Jerry Yude4fb2c2021-09-19 18:05:08 +08001113 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001114 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001115
Jerry Yu4a173382021-10-11 21:45:31 +08001116 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001117
1118 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001119 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1120 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1123 ssl->session_negotiate->id,
1124 ssl->session_negotiate->id_len );
1125 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001126 legacy_session_id_echo_len );
1127
1128 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1129 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1130
Jerry Yue1b9c292021-09-10 10:08:31 +08001131 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1132 }
1133
Jerry Yu4a173382021-10-11 21:45:31 +08001134 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001135 *buf = p;
1136
1137 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001138 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001139 return( 0 );
1140}
1141
Jerry Yu4a173382021-10-11 21:45:31 +08001142static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1143 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001144{
Jerry Yu4a173382021-10-11 21:45:31 +08001145 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1146
Jerry Yue1b9c292021-09-10 10:08:31 +08001147 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001148 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 {
Jerry Yu4a173382021-10-11 21:45:31 +08001150 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 {
1152 return( 1 );
1153 }
1154 }
1155 return( 0 );
1156}
1157
1158/* Parse ServerHello message and configure context
1159 *
1160 * struct {
1161 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1162 * Random random;
1163 * opaque legacy_session_id_echo<0..32>;
1164 * CipherSuite cipher_suite;
1165 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001166 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 * } ServerHello;
1168 */
Jerry Yuc068b662021-10-11 22:30:19 +08001169static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1170 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001171 const unsigned char *end,
1172 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001173{
Jerry Yub85277e2021-10-13 13:36:05 +08001174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001175 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001176 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001177 size_t extensions_len;
1178 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001180 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001181 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001182 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001183
1184 /*
1185 * Check there is space for minimal fields
1186 *
1187 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001188 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 * - legacy_session_id_echo ( 1 byte ), minimum size
1190 * - cipher_suite ( 2 bytes)
1191 * - legacy_compression_method ( 1 byte )
1192 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001193 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001194
1195 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001196 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1197
Jerry Yu4a173382021-10-11 21:45:31 +08001198 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001199 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001200 * ...
1201 * with ProtocolVersion defined as:
1202 * uint16 ProtocolVersion;
1203 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001204 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1205 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1206 {
1207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1208 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1209 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001210 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1211 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001212 }
1213 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001214
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001215 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001216 * Random random;
1217 * ...
1218 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001219 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001220 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001221 if( !is_hrr )
1222 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001223 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001224 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1225 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1226 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1227 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001228 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001229
Jerry Yu4a173382021-10-11 21:45:31 +08001230 /* ...
1231 * opaque legacy_session_id_echo<0..32>;
1232 * ...
1233 */
1234 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001235 {
XiaokangQian52da5582022-01-26 09:49:29 +00001236 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001237 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001238 }
1239
Jerry Yu4a173382021-10-11 21:45:31 +08001240 /* ...
1241 * CipherSuite cipher_suite;
1242 * ...
1243 * with CipherSuite defined as:
1244 * uint8 CipherSuite[2];
1245 */
1246 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1248 p += 2;
1249
Jerry Yu4a173382021-10-11 21:45:31 +08001250
XiaokangQian355e09a2022-01-20 11:14:50 +00001251 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001252 /*
1253 * Check whether this ciphersuite is supported and offered.
1254 * Via the force_ciphersuite version we may have instructed the client
1255 * to use a different ciphersuite.
1256 */
Jerry Yu4a173382021-10-11 21:45:31 +08001257 if( ciphersuite_info == NULL ||
1258 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001259 {
XiaokangQian52da5582022-01-26 09:49:29 +00001260 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001262 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001263 * If we received an HRR before and that the proposed selected
1264 * ciphersuite in this server hello is not the same as the one
1265 * proposed in the HRR, we abort the handshake and send an
1266 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001267 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001268 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1269 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001270 {
XiaokangQian52da5582022-01-26 09:49:29 +00001271 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001272 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001273
XiaokangQian52da5582022-01-26 09:49:29 +00001274 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001275 {
1276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1277 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001278 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001279 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001280
Jerry Yu4a173382021-10-11 21:45:31 +08001281 /* Configure ciphersuites */
1282 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1283
XiaokangQian355e09a2022-01-20 11:14:50 +00001284 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001285 ssl->session_negotiate->ciphersuite = cipher_suite;
1286
Jerry Yue1b9c292021-09-10 10:08:31 +08001287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1288 cipher_suite, ciphersuite_info->name ) );
1289
1290#if defined(MBEDTLS_HAVE_TIME)
1291 ssl->session_negotiate->start = time( NULL );
1292#endif /* MBEDTLS_HAVE_TIME */
1293
Jerry Yu4a173382021-10-11 21:45:31 +08001294 /* ...
1295 * uint8 legacy_compression_method = 0;
1296 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001297 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001298 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001299 if( p[0] != 0 )
1300 {
Jerry Yub85277e2021-10-13 13:36:05 +08001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001302 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001303 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001304 }
1305 p++;
1306
Jerry Yub85277e2021-10-13 13:36:05 +08001307 /* ...
1308 * Extension extensions<6..2^16-1>;
1309 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001310 * struct {
1311 * ExtensionType extension_type; (2 bytes)
1312 * opaque extension_data<0..2^16-1>;
1313 * } Extension;
1314 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001315 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001316 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001317 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001318
Jerry Yu4a173382021-10-11 21:45:31 +08001319 /* Check extensions do not go beyond the buffer of data. */
1320 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1321 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001322
Jerry Yu4a173382021-10-11 21:45:31 +08001323 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001324
Jerry Yu4a173382021-10-11 21:45:31 +08001325 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001326 {
1327 unsigned int extension_type;
1328 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001329 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001330
Jerry Yu4a173382021-10-11 21:45:31 +08001331 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1333 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1334 p += 4;
1335
Jerry Yu4a173382021-10-11 21:45:31 +08001336 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001337 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001338
1339 switch( extension_type )
1340 {
XiaokangQianb851da82022-01-14 04:03:11 +00001341 case MBEDTLS_TLS_EXT_COOKIE:
1342
XiaokangQiand9e068e2022-01-18 06:23:32 +00001343 if( !is_hrr )
1344 {
XiaokangQian52da5582022-01-26 09:49:29 +00001345 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001346 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001347 }
1348
XiaokangQian43550bd2022-01-21 04:32:58 +00001349 ret = ssl_tls13_parse_cookie_ext( ssl,
1350 p, extension_data_end );
1351 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001352 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001353 MBEDTLS_SSL_DEBUG_RET( 1,
1354 "ssl_tls13_parse_cookie_ext",
1355 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001356 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001357 }
XiaokangQianb851da82022-01-14 04:03:11 +00001358 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001359
Jerry Yue1b9c292021-09-10 10:08:31 +08001360 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001361 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001362 MBEDTLS_SSL_DEBUG_MSG( 3,
1363 ( "found supported_versions extension" ) );
1364
Jerry Yuc068b662021-10-11 22:30:19 +08001365 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001366 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001367 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001368 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001369 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001370 break;
1371
1372 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001375
XiaokangQian52da5582022-01-26 09:49:29 +00001376 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001377 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001378
1379#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1380 case MBEDTLS_TLS_EXT_KEY_SHARE:
1381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001382 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1383 {
XiaokangQian52da5582022-01-26 09:49:29 +00001384 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001385 goto cleanup;
1386 }
1387
XiaokangQian53f20b72022-01-18 10:47:33 +00001388 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001389 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001390 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001391 else
XiaokangQianb851da82022-01-14 04:03:11 +00001392 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001393 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001394 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001395 {
1396 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001397 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001398 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001399 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001400 }
1401 break;
1402#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1403
1404 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001405 MBEDTLS_SSL_DEBUG_MSG(
1406 3,
1407 ( "unknown extension found: %u ( ignoring )",
1408 extension_type ) );
1409
XiaokangQian52da5582022-01-26 09:49:29 +00001410 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001411 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001412 }
1413
1414 p += extension_data_len;
1415 }
1416
XiaokangQian78b1fa72022-01-19 06:56:30 +00001417 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001418 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001420 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001421 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001422 }
1423
XiaokangQiand59be772022-01-24 10:12:51 +00001424cleanup:
1425
XiaokangQian52da5582022-01-26 09:49:29 +00001426 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001427 {
1428 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1429 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1430 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1431 }
XiaokangQian52da5582022-01-26 09:49:29 +00001432 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001433 {
1434 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1435 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1436 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1437 }
1438 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001439}
1440
XiaokangQian355e09a2022-01-20 11:14:50 +00001441static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001442{
Jerry Yub85277e2021-10-13 13:36:05 +08001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001444 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001445 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001446 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001447
Jerry Yub85277e2021-10-13 13:36:05 +08001448 /* Determine the key exchange mode:
1449 * 1) If both the pre_shared_key and key_share extensions were received
1450 * then the key exchange mode is PSK with EPHEMERAL.
1451 * 2) If only the pre_shared_key extension was received then the key
1452 * exchange mode is PSK-only.
1453 * 3) If only the key_share extension was received then the key
1454 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001455 */
Jerry Yub85277e2021-10-13 13:36:05 +08001456 switch( handshake->extensions_present &
1457 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001458 {
Jerry Yu745bb612021-10-13 22:01:04 +08001459 /* Only the pre_shared_key extension was received */
1460 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001461 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001462 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001463
Jerry Yu745bb612021-10-13 22:01:04 +08001464 /* Only the key_share extension was received */
1465 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001466 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001467 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001468
Jerry Yu745bb612021-10-13 22:01:04 +08001469 /* Both the pre_shared_key and key_share extensions were received */
1470 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001471 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001472 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001473
Jerry Yu745bb612021-10-13 22:01:04 +08001474 /* Neither pre_shared_key nor key_share extension was received */
1475 default:
1476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1477 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1478 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001479 }
1480
1481 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1482 *
1483 * TODO: We don't have to do this in case we offered 0-RTT and the
1484 * server accepted it. In this case, we could skip generating
1485 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001486 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001487 if( ret != 0 )
1488 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001490 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001491 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001492 }
1493
1494 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001495 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001496 if( ret != 0 )
1497 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001499 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001500 }
1501
1502 /* Next evolution in key schedule: Establish handshake secret and
1503 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001504 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001505 if( ret != 0 )
1506 {
Jerry Yuc068b662021-10-11 22:30:19 +08001507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1508 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001509 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001510 }
1511
Jerry Yub85277e2021-10-13 13:36:05 +08001512 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001513 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001514 {
1515 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1516 goto cleanup;
1517 }
Jerry Yu0b177842021-09-05 19:41:30 +08001518
1519 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1520 ssl->conf->endpoint,
1521 ssl->session_negotiate->ciphersuite,
1522 &traffic_keys,
1523 ssl );
1524 if( ret != 0 )
1525 {
1526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001527 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001528 }
1529
Jerry Yub85277e2021-10-13 13:36:05 +08001530 handshake->transform_handshake = transform_handshake;
1531 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001532
1533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1534 ssl->session_in = ssl->session_negotiate;
1535
1536 /*
1537 * State machine update
1538 */
1539 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1540
Jerry Yu4a173382021-10-11 21:45:31 +08001541cleanup:
1542
Jerry Yu0b177842021-09-05 19:41:30 +08001543 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001544 if( ret != 0 )
1545 {
Jerry Yub85277e2021-10-13 13:36:05 +08001546 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001547
Jerry Yu4a173382021-10-11 21:45:31 +08001548 MBEDTLS_SSL_PEND_FATAL_ALERT(
1549 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1550 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1551 }
1552 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001553}
1554
XiaokangQian355e09a2022-01-20 11:14:50 +00001555static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001556{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001557#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1559
XiaokangQian647719a2021-12-07 09:16:29 +00001560#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1561 /* If not offering early data, the client sends a dummy CCS record
1562 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001563 * its second ClientHello or before its encrypted handshake flight.
1564 */
XiaokangQian647719a2021-12-07 09:16:29 +00001565 mbedtls_ssl_handshake_set_state( ssl,
1566 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1567#else
1568 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1569#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1570
XiaokangQian78b1fa72022-01-19 06:56:30 +00001571 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001572
XiaokangQian78b1fa72022-01-19 06:56:30 +00001573 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001574 * We are going to re-generate a shared secret corresponding to the group
1575 * selected by the server, which is different from the group for which we
1576 * generated a shared secret in the first client hello.
1577 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001578 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001579 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001580 if( ret != 0 )
1581 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001582#else
1583 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001584#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001585
1586 return( 0 );
1587}
1588
Jerry Yue1b9c292021-09-10 10:08:31 +08001589/*
Jerry Yu4a173382021-10-11 21:45:31 +08001590 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001591 * Handler for MBEDTLS_SSL_SERVER_HELLO
1592 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001593static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001594{
Jerry Yu4a173382021-10-11 21:45:31 +08001595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001596 unsigned char *buf = NULL;
1597 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001598 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001599
1600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1601
1602 /* Coordination step
1603 * - Fetch record
1604 * - Make sure it's either a ServerHello or a HRR.
1605 * - Switch processing routine in case of HRR
1606 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001607 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1608 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1609
XiaokangQian16acd4b2022-01-14 07:35:47 +00001610 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001611 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001612 goto cleanup;
1613 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001614 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001615
XiaokangQianb851da82022-01-14 04:03:11 +00001616 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001617 buf + buf_len,
1618 is_hrr ) );
1619 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001620 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1621
XiaokangQianb851da82022-01-14 04:03:11 +00001622 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1623 MBEDTLS_SSL_HS_SERVER_HELLO,
1624 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001625
XiaokangQiand9e068e2022-01-18 06:23:32 +00001626 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001627 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001628 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001629 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001630
1631cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1633 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001634 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001635}
1636
1637/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001638 *
1639 * EncryptedExtensions message
1640 *
1641 * The EncryptedExtensions message contains any extensions which
1642 * should be protected, i.e., any which are not needed to establish
1643 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001644 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001645
1646/*
1647 * Overview
1648 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001649
1650/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001651static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001652
XiaokangQian97799ac2021-10-11 10:05:54 +00001653static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1654 const unsigned char *buf,
1655 const unsigned char *end );
1656static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001657
1658/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001659 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001660 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001661static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001662{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001663 int ret;
1664 unsigned char *buf;
1665 size_t buf_len;
1666
1667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1668
Xiaofei Bai746f9482021-11-12 08:53:56 +00001669 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001670 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001671 &buf, &buf_len ) );
1672
1673 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001674 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001675 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001676
Xiaofei Bai746f9482021-11-12 08:53:56 +00001677 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001678 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001679
XiaokangQian97799ac2021-10-11 10:05:54 +00001680 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001681
1682cleanup:
1683
1684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1685 return( ret );
1686
1687}
1688
XiaokangQian08da26c2021-10-09 10:12:11 +00001689/* Parse EncryptedExtensions message
1690 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001691 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001692 * } EncryptedExtensions;
1693 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001694static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1695 const unsigned char *buf,
1696 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001697{
1698 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001699 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001700 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001701 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001702
XiaokangQian08da26c2021-10-09 10:12:11 +00001703 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001704 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001705 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001706
XiaokangQian97799ac2021-10-11 10:05:54 +00001707 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001708 extensions_end = p + extensions_len;
1709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001710
XiaokangQian8db25ff2021-10-13 05:56:18 +00001711 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001712 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001713 unsigned int extension_type;
1714 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001715
XiaokangQian08da26c2021-10-09 10:12:11 +00001716 /*
1717 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001718 * ExtensionType extension_type; (2 bytes)
1719 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001720 * } Extension;
1721 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001722 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001723 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1724 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1725 p += 4;
1726
XiaokangQian8db25ff2021-10-13 05:56:18 +00001727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001728
XiaokangQian97799ac2021-10-11 10:05:54 +00001729 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001730 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001731 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001732 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001733 switch( extension_type )
1734 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001735
XiaokangQian08da26c2021-10-09 10:12:11 +00001736 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1738 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001739
lhuang0486cacac2022-01-21 07:34:27 -08001740#if defined(MBEDTLS_SSL_ALPN)
1741 case MBEDTLS_TLS_EXT_ALPN:
1742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1743
1744 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1745 {
1746 return( ret );
1747 }
1748
1749 break;
1750#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001751 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001752 MBEDTLS_SSL_DEBUG_MSG(
1753 3, ( "unsupported extension found: %u ", extension_type) );
1754 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001755 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001756 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1757 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001758 }
1759
XiaokangQian08da26c2021-10-09 10:12:11 +00001760 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001761 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001762
XiaokangQian97799ac2021-10-11 10:05:54 +00001763 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001764 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001765 {
1766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001767 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001768 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001769 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001770 }
1771
1772 return( ret );
1773}
1774
XiaokangQian97799ac2021-10-11 10:05:54 +00001775static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001776{
Jerry Yua93ac112021-10-27 16:31:48 +08001777#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001778 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001779 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1780 else
Jerry Yud2674312021-10-29 10:08:19 +08001781 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001782#else
1783 ((void) ssl);
1784 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1785#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001786 return( 0 );
1787}
1788
Jerry Yua93ac112021-10-27 16:31:48 +08001789#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001790/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001791 *
1792 * STATE HANDLING: CertificateRequest
1793 *
Jerry Yud2674312021-10-29 10:08:19 +08001794 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001795#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1796#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797/* Coordination:
1798 * Deals with the ambiguity of not knowing if a CertificateRequest
1799 * will be sent. Returns a negative code on failure, or
1800 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1801 * - SSL_CERTIFICATE_REQUEST_SKIP
1802 * indicating if a Certificate Request is expected or not.
1803 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001804static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001805{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001807
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001808 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809 {
1810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1811 return( SSL_CERTIFICATE_REQUEST_SKIP );
1812 }
1813
1814 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001815 {
1816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1817 return( ret );
1818 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001819 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001820
1821 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1822 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1823 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001824 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001825 }
1826
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001827 return( SSL_CERTIFICATE_REQUEST_SKIP );
1828}
1829
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001830/*
1831 * ssl_tls13_parse_certificate_request()
1832 * Parse certificate request
1833 * struct {
1834 * opaque certificate_request_context<0..2^8-1>;
1835 * Extension extensions<2..2^16-1>;
1836 * } CertificateRequest;
1837 */
1838static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1839 const unsigned char *buf,
1840 const unsigned char *end )
1841{
1842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1843 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001844 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001845 size_t extensions_len = 0;
1846 const unsigned char *extensions_end;
1847 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001848
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001849 /* ...
1850 * opaque certificate_request_context<0..2^8-1>
1851 * ...
1852 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001853 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1854 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001855 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001856
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001857 if( certificate_request_context_len > 0 )
1858 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001859 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001860 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1861 p, certificate_request_context_len );
1862
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001863 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001864 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001865 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001866 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001867 {
1868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1869 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1870 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001871 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001872 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001873 p += certificate_request_context_len;
1874 }
1875
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001876 /* ...
1877 * Extension extensions<2..2^16-1>;
1878 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001879 */
1880 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1881 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1882 p += 2;
1883
1884 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1885 extensions_end = p + extensions_len;
1886
1887 while( p < extensions_end )
1888 {
1889 unsigned int extension_type;
1890 size_t extension_data_len;
1891
1892 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1893 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1894 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1895 p += 4;
1896
1897 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1898
1899 switch( extension_type )
1900 {
1901 case MBEDTLS_TLS_EXT_SIG_ALG:
1902 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001903 ( "found signature algorithms extension" ) );
1904 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001905 p + extension_data_len );
1906 if( ret != 0 )
1907 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001908 if( ! sig_alg_ext_found )
1909 sig_alg_ext_found = 1;
1910 else
1911 {
1912 MBEDTLS_SSL_DEBUG_MSG( 3,
1913 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001914 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001915 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001916 break;
1917
1918 default:
1919 MBEDTLS_SSL_DEBUG_MSG(
1920 3,
1921 ( "unknown extension found: %u ( ignoring )",
1922 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001923 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001924 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001925 p += extension_data_len;
1926 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001927 /* Check that we consumed all the message. */
1928 if( p != end )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001931 ( "CertificateRequest misaligned" ) );
1932 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001933 }
1934 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001935 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001936 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001937 MBEDTLS_SSL_DEBUG_MSG( 3,
1938 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001939 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001940 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001941
Jerry Yu7840f812022-01-29 10:26:51 +08001942 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001943 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001944
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001945decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001946 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1947 MBEDTLS_ERR_SSL_DECODE_ERROR );
1948 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001949}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001950
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001951/*
1952 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1953 */
1954static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001957
1958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1959
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001960 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1961
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001962 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1963 {
1964 unsigned char *buf;
1965 size_t buf_len;
1966
1967 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1968 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1969 &buf, &buf_len ) );
1970
1971 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1972 buf, buf + buf_len ) );
1973
1974 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1975 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1976 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001977 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001978 {
1979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001980 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001981 }
1982 else
1983 {
1984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001985 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1986 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 }
1988
1989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001990 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001991
Jerry Yud2674312021-10-29 10:08:19 +08001992 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1993
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001994cleanup:
1995
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1997 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001998}
1999
2000/*
Jerry Yu687101b2021-09-14 16:03:56 +08002001 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2002 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002003static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002004{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002005 int ret;
2006
2007 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002008 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002009 return( ret );
2010
Jerry Yu687101b2021-09-14 16:03:56 +08002011 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2012 return( 0 );
2013}
2014
2015/*
2016 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2017 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002018static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002019{
Jerry Yu30b071c2021-09-12 20:16:03 +08002020 int ret;
2021
2022 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2023 if( ret != 0 )
2024 return( ret );
2025
Jerry Yu687101b2021-09-14 16:03:56 +08002026 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2027 return( 0 );
2028}
Jerry Yua93ac112021-10-27 16:31:48 +08002029#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002030
Jerry Yu687101b2021-09-14 16:03:56 +08002031/*
2032 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2033 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002034static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002035{
XiaokangQianac0385c2021-11-03 06:40:11 +00002036 int ret;
2037
XiaokangQianc5c39d52021-11-09 11:55:10 +00002038 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002039 if( ret != 0 )
2040 return( ret );
2041
Ronald Cron49ad6192021-11-24 16:25:31 +01002042#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2043 mbedtls_ssl_handshake_set_state(
2044 ssl,
2045 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2046#else
Jerry Yuca133a32022-02-15 14:22:05 +08002047 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002048#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002049
XiaokangQianac0385c2021-11-03 06:40:11 +00002050 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002051}
2052
2053/*
Jerry Yu566c7812022-01-26 15:41:22 +08002054 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2055 */
2056static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2057{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002058 int non_empty_certificate_msg = 0;
2059
Jerry Yu5cc35062022-01-28 16:16:08 +08002060 MBEDTLS_SSL_DEBUG_MSG( 1,
2061 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002062 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002063
Ronald Cron9df7c802022-03-08 18:38:54 +01002064#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002065 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002066 {
2067 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2068 if( ret != 0 )
2069 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002070
Ronald Cron7a94aca2022-03-09 07:44:27 +01002071 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2072 non_empty_certificate_msg = 1;
2073 }
2074 else
2075 {
2076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2077 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002078#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002079
Ronald Cron7a94aca2022-03-09 07:44:27 +01002080 if( non_empty_certificate_msg )
2081 {
2082 mbedtls_ssl_handshake_set_state( ssl,
2083 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2084 }
2085 else
2086 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2087
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002088 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002089}
2090
Ronald Cron9df7c802022-03-08 18:38:54 +01002091#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002092/*
2093 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2094 */
2095static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2096{
Ronald Crona8b38872022-03-09 07:59:25 +01002097 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2098
2099 if( ret == 0 )
2100 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2101
2102 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002103}
Jerry Yu90f152d2022-01-29 22:12:42 +08002104#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002105
2106/*
Jerry Yu687101b2021-09-14 16:03:56 +08002107 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2108 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002109static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002110{
XiaokangQian0fa66432021-11-15 03:33:57 +00002111 int ret;
2112
2113 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2114 if( ret != 0 )
2115 return( ret );
2116
2117 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2118 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002119}
2120
2121/*
2122 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2123 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002124static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002125{
Jerry Yu378254d2021-10-30 21:44:47 +08002126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002127 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002128 return( 0 );
2129}
2130
2131/*
2132 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2133 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002134static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002135{
Jerry Yu378254d2021-10-30 21:44:47 +08002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2137 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2138
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2140 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2141
2142 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2143
2144 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2145 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002146}
2147
Jerry Yu92c6b402021-08-27 16:59:09 +08002148int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002149{
Jerry Yu92c6b402021-08-27 16:59:09 +08002150 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002151
Jerry Yue3b34122021-09-28 17:53:35 +08002152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2153 mbedtls_ssl_states_str( ssl->state ),
2154 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002155
2156 switch( ssl->state )
2157 {
2158 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002159 * ssl->state is initialized as HELLO_REQUEST. It is the same
2160 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002161 */
2162 case MBEDTLS_SSL_HELLO_REQUEST:
2163 case MBEDTLS_SSL_CLIENT_HELLO:
2164 ret = ssl_tls13_write_client_hello( ssl );
2165 break;
2166
2167 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002168 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002169 break;
2170
2171 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002172 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002173 break;
2174
Jerry Yua93ac112021-10-27 16:31:48 +08002175#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002176 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2177 ret = ssl_tls13_process_certificate_request( ssl );
2178 break;
2179
Jerry Yu687101b2021-09-14 16:03:56 +08002180 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002181 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002182 break;
2183
2184 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002185 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002186 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002187#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002188
2189 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002190 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002191 break;
2192
Jerry Yu566c7812022-01-26 15:41:22 +08002193 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2194 ret = ssl_tls13_write_client_certificate( ssl );
2195 break;
2196
Ronald Cron9df7c802022-03-08 18:38:54 +01002197#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002198 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2199 ret = ssl_tls13_write_client_certificate_verify( ssl );
2200 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002201#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002202
Jerry Yu687101b2021-09-14 16:03:56 +08002203 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002204 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002205 break;
2206
2207 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002208 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002209 break;
2210
2211 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002212 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002213 break;
2214
Ronald Cron49ad6192021-11-24 16:25:31 +01002215 /*
2216 * Injection of dummy-CCS's for middlebox compatibility
2217 */
2218#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002219 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002220 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2221 if( ret == 0 )
2222 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2223 break;
2224
2225 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2226 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2227 if( ret == 0 )
2228 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002229 break;
2230#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2231
Jerry Yu92c6b402021-08-27 16:59:09 +08002232 default:
2233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2235 }
2236
2237 return( ret );
2238}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002239
Jerry Yufb4b6472022-01-27 15:03:26 +08002240#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002241
Jerry Yufb4b6472022-01-27 15:03:26 +08002242