blob: cd1baa1be3f89d732c087ef762ac41b9088fb66f [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 Yu335aca92021-09-12 20:18:56 +0800922static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800923{
Jerry Yu92c6b402021-08-27 16:59:09 +0800924 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
925 return( 0 );
926}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800927
Jerry Yu92c6b402021-08-27 16:59:09 +0800928static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
929{
930 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800931
Jerry Yu92c6b402021-08-27 16:59:09 +0800932 if( ssl->conf->f_rng == NULL )
933 {
934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
935 return( MBEDTLS_ERR_SSL_NO_RNG );
936 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800937
Jerry Yu92c6b402021-08-27 16:59:09 +0800938 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
939 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800940 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800941 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800942 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800943 return( ret );
944 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800945
Ronald Cron49ad6192021-11-24 16:25:31 +0100946#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
947 /*
948 * Create a session identifier for the purpose of middlebox compatibility
949 * only if one has not been created already.
950 */
951 if( ssl->session_negotiate->id_len == 0 )
952 {
953 /* Creating a session id with 32 byte length */
954 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
955 ssl->session_negotiate->id, 32 ) ) != 0 )
956 {
957 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
958 return( ret );
959 }
960 ssl->session_negotiate->id_len = 32;
961 }
962#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
963
Jerry Yu6f13f642021-08-26 17:18:15 +0800964 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800965}
966
Jerry Yu92c6b402021-08-27 16:59:09 +0800967/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800968 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800969 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800970 */
971static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800972{
Jerry Yu92c6b402021-08-27 16:59:09 +0800973 int ret = 0;
974 unsigned char *buf;
975 size_t buf_len, msg_len;
976
977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
978
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800979 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800980
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800981 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
982 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
983 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800984
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800985 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800986 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800987 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800988
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800989 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
990 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800991 msg_len );
992 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800993
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800994 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
995 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
996 buf_len,
997 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800998
999cleanup:
1000
1001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1002 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001003}
1004
Jerry Yu687101b2021-09-14 16:03:56 +08001005/*
Jerry Yu4a173382021-10-11 21:45:31 +08001006 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001008/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001009 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1010 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001011 * to indicate which message is expected and to be parsed next.
1012 */
Jerry Yub85277e2021-10-13 13:36:05 +08001013#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1014#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1015static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1016 const unsigned char *buf,
1017 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001018{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001019 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1021 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1022 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1023 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1024
1025 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1026 *
Jerry Yu4a173382021-10-11 21:45:31 +08001027 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001028 * special value of the SHA-256 of "HelloRetryRequest".
1029 *
1030 * struct {
1031 * ProtocolVersion legacy_version = 0x0303;
1032 * Random random;
1033 * opaque legacy_session_id_echo<0..32>;
1034 * CipherSuite cipher_suite;
1035 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001036 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 * } ServerHello;
1038 *
1039 */
Jerry Yub85277e2021-10-13 13:36:05 +08001040 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001041
1042 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 {
Jerry Yub85277e2021-10-13 13:36:05 +08001044 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 }
1046
Jerry Yub85277e2021-10-13 13:36:05 +08001047 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001048}
1049
Jerry Yu745bb612021-10-13 22:01:04 +08001050/* Fetch and preprocess
1051 * Returns a negative value on failure, and otherwise
1052 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1053 * - SSL_SERVER_HELLO_COORDINATE_HRR
1054 */
Jerry Yub85277e2021-10-13 13:36:05 +08001055static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1056 unsigned char **buf,
1057 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001058{
Jerry Yu4a173382021-10-11 21:45:31 +08001059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001060
XiaokangQian355e09a2022-01-20 11:14:50 +00001061 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1062 MBEDTLS_SSL_HS_SERVER_HELLO,
1063 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001064
Jerry Yub85277e2021-10-13 13:36:05 +08001065 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1066 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 {
Jerry Yu745bb612021-10-13 22:01:04 +08001068 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1070 break;
1071 case SSL_SERVER_HELLO_COORDINATE_HRR:
1072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001073 /* If a client receives a second
1074 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1075 * was itself in response to a HelloRetryRequest), it MUST abort the
1076 * handshake with an "unexpected_message" alert.
1077 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001078 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001079 {
1080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1081 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1082 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1083 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1084 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001085 /*
1086 * Clients must abort the handshake with an "illegal_parameter"
1087 * alert if the HelloRetryRequest would not result in any change
1088 * in the ClientHello.
1089 * In a PSK only key exchange that what we expect.
1090 */
1091 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1092 {
1093 MBEDTLS_SSL_DEBUG_MSG( 1,
1094 ( "Unexpected HRR in pure PSK key exchange." ) );
1095 MBEDTLS_SSL_PEND_FATAL_ALERT(
1096 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1097 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1098 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1099 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001100
XiaokangQiand9e068e2022-01-18 06:23:32 +00001101 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001102
Jerry Yu745bb612021-10-13 22:01:04 +08001103 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001104 }
1105
1106cleanup:
1107
1108 return( ret );
1109}
1110
Jerry Yu4a173382021-10-11 21:45:31 +08001111static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1112 const unsigned char **buf,
1113 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001114{
1115 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001116 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001117
Jerry Yude4fb2c2021-09-19 18:05:08 +08001118 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001119 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001120
Jerry Yu4a173382021-10-11 21:45:31 +08001121 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001122
1123 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001124 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1125 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1128 ssl->session_negotiate->id,
1129 ssl->session_negotiate->id_len );
1130 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001131 legacy_session_id_echo_len );
1132
1133 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1134 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1135
Jerry Yue1b9c292021-09-10 10:08:31 +08001136 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1137 }
1138
Jerry Yu4a173382021-10-11 21:45:31 +08001139 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 *buf = p;
1141
1142 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001143 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 return( 0 );
1145}
1146
Jerry Yu4a173382021-10-11 21:45:31 +08001147static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1148 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001149{
Jerry Yu4a173382021-10-11 21:45:31 +08001150 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1151
Jerry Yue1b9c292021-09-10 10:08:31 +08001152 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001153 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 {
Jerry Yu4a173382021-10-11 21:45:31 +08001155 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 {
1157 return( 1 );
1158 }
1159 }
1160 return( 0 );
1161}
1162
1163/* Parse ServerHello message and configure context
1164 *
1165 * struct {
1166 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1167 * Random random;
1168 * opaque legacy_session_id_echo<0..32>;
1169 * CipherSuite cipher_suite;
1170 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001171 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001172 * } ServerHello;
1173 */
Jerry Yuc068b662021-10-11 22:30:19 +08001174static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1175 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001176 const unsigned char *end,
1177 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001178{
Jerry Yub85277e2021-10-13 13:36:05 +08001179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001181 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001182 size_t extensions_len;
1183 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001185 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001186 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001187 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001188
1189 /*
1190 * Check there is space for minimal fields
1191 *
1192 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001193 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001194 * - legacy_session_id_echo ( 1 byte ), minimum size
1195 * - cipher_suite ( 2 bytes)
1196 * - legacy_compression_method ( 1 byte )
1197 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001198 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001199
1200 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001201 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1202
Jerry Yu4a173382021-10-11 21:45:31 +08001203 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001204 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001205 * ...
1206 * with ProtocolVersion defined as:
1207 * uint16 ProtocolVersion;
1208 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001209 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1210 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1211 {
1212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1213 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1214 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001215 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1216 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 }
1218 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001219
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001220 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001221 * Random random;
1222 * ...
1223 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001224 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001225 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001226 if( !is_hrr )
1227 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001228 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001229 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1230 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1231 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1232 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001233 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001234
Jerry Yu4a173382021-10-11 21:45:31 +08001235 /* ...
1236 * opaque legacy_session_id_echo<0..32>;
1237 * ...
1238 */
1239 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001240 {
XiaokangQian52da5582022-01-26 09:49:29 +00001241 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001242 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001243 }
1244
Jerry Yu4a173382021-10-11 21:45:31 +08001245 /* ...
1246 * CipherSuite cipher_suite;
1247 * ...
1248 * with CipherSuite defined as:
1249 * uint8 CipherSuite[2];
1250 */
1251 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001252 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1253 p += 2;
1254
Jerry Yu4a173382021-10-11 21:45:31 +08001255
XiaokangQian355e09a2022-01-20 11:14:50 +00001256 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001257 /*
1258 * Check whether this ciphersuite is supported and offered.
1259 * Via the force_ciphersuite version we may have instructed the client
1260 * to use a different ciphersuite.
1261 */
Jerry Yu4a173382021-10-11 21:45:31 +08001262 if( ciphersuite_info == NULL ||
1263 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001264 {
XiaokangQian52da5582022-01-26 09:49:29 +00001265 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001266 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001267 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001268 * If we received an HRR before and that the proposed selected
1269 * ciphersuite in this server hello is not the same as the one
1270 * proposed in the HRR, we abort the handshake and send an
1271 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001272 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001273 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1274 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001275 {
XiaokangQian52da5582022-01-26 09:49:29 +00001276 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001277 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001278
XiaokangQian52da5582022-01-26 09:49:29 +00001279 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001280 {
1281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1282 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001283 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001284 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001285
Jerry Yu4a173382021-10-11 21:45:31 +08001286 /* Configure ciphersuites */
1287 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1288
XiaokangQian355e09a2022-01-20 11:14:50 +00001289 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001290 ssl->session_negotiate->ciphersuite = cipher_suite;
1291
Jerry Yue1b9c292021-09-10 10:08:31 +08001292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1293 cipher_suite, ciphersuite_info->name ) );
1294
1295#if defined(MBEDTLS_HAVE_TIME)
1296 ssl->session_negotiate->start = time( NULL );
1297#endif /* MBEDTLS_HAVE_TIME */
1298
Jerry Yu4a173382021-10-11 21:45:31 +08001299 /* ...
1300 * uint8 legacy_compression_method = 0;
1301 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001302 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001303 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001304 if( p[0] != 0 )
1305 {
Jerry Yub85277e2021-10-13 13:36:05 +08001306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001307 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001308 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001309 }
1310 p++;
1311
Jerry Yub85277e2021-10-13 13:36:05 +08001312 /* ...
1313 * Extension extensions<6..2^16-1>;
1314 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001315 * struct {
1316 * ExtensionType extension_type; (2 bytes)
1317 * opaque extension_data<0..2^16-1>;
1318 * } Extension;
1319 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001320 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001321 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001322 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001323
Jerry Yu4a173382021-10-11 21:45:31 +08001324 /* Check extensions do not go beyond the buffer of data. */
1325 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1326 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001327
Jerry Yu4a173382021-10-11 21:45:31 +08001328 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001329
Jerry Yu4a173382021-10-11 21:45:31 +08001330 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001331 {
1332 unsigned int extension_type;
1333 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001334 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001335
Jerry Yu4a173382021-10-11 21:45:31 +08001336 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001337 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1338 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1339 p += 4;
1340
Jerry Yu4a173382021-10-11 21:45:31 +08001341 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001342 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001343
1344 switch( extension_type )
1345 {
XiaokangQianb851da82022-01-14 04:03:11 +00001346 case MBEDTLS_TLS_EXT_COOKIE:
1347
XiaokangQiand9e068e2022-01-18 06:23:32 +00001348 if( !is_hrr )
1349 {
XiaokangQian52da5582022-01-26 09:49:29 +00001350 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001351 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001352 }
1353
XiaokangQian43550bd2022-01-21 04:32:58 +00001354 ret = ssl_tls13_parse_cookie_ext( ssl,
1355 p, extension_data_end );
1356 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001357 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001358 MBEDTLS_SSL_DEBUG_RET( 1,
1359 "ssl_tls13_parse_cookie_ext",
1360 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001361 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001362 }
XiaokangQianb851da82022-01-14 04:03:11 +00001363 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001364
Jerry Yue1b9c292021-09-10 10:08:31 +08001365 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001366 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001367 MBEDTLS_SSL_DEBUG_MSG( 3,
1368 ( "found supported_versions extension" ) );
1369
Jerry Yuc068b662021-10-11 22:30:19 +08001370 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001371 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001372 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001373 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001374 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001375 break;
1376
1377 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001380
XiaokangQian52da5582022-01-26 09:49:29 +00001381 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001382 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001383
1384#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1385 case MBEDTLS_TLS_EXT_KEY_SHARE:
1386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001387 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1388 {
XiaokangQian52da5582022-01-26 09:49:29 +00001389 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001390 goto cleanup;
1391 }
1392
XiaokangQian53f20b72022-01-18 10:47:33 +00001393 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001394 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001395 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001396 else
XiaokangQianb851da82022-01-14 04:03:11 +00001397 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001398 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001399 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001400 {
1401 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001402 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001403 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001404 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001405 }
1406 break;
1407#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1408
1409 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001410 MBEDTLS_SSL_DEBUG_MSG(
1411 3,
1412 ( "unknown extension found: %u ( ignoring )",
1413 extension_type ) );
1414
XiaokangQian52da5582022-01-26 09:49:29 +00001415 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001416 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001417 }
1418
1419 p += extension_data_len;
1420 }
1421
XiaokangQian78b1fa72022-01-19 06:56:30 +00001422 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001423 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001425 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001426 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001427 }
1428
XiaokangQiand59be772022-01-24 10:12:51 +00001429cleanup:
1430
XiaokangQian52da5582022-01-26 09:49:29 +00001431 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001432 {
1433 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1434 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1435 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1436 }
XiaokangQian52da5582022-01-26 09:49:29 +00001437 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001438 {
1439 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1440 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1441 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1442 }
1443 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001444}
1445
XiaokangQian355e09a2022-01-20 11:14:50 +00001446static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001447{
Jerry Yub85277e2021-10-13 13:36:05 +08001448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001449 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001450 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001451 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001452
Jerry Yub85277e2021-10-13 13:36:05 +08001453 /* Determine the key exchange mode:
1454 * 1) If both the pre_shared_key and key_share extensions were received
1455 * then the key exchange mode is PSK with EPHEMERAL.
1456 * 2) If only the pre_shared_key extension was received then the key
1457 * exchange mode is PSK-only.
1458 * 3) If only the key_share extension was received then the key
1459 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001460 */
Jerry Yub85277e2021-10-13 13:36:05 +08001461 switch( handshake->extensions_present &
1462 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001463 {
Jerry Yu745bb612021-10-13 22:01:04 +08001464 /* Only the pre_shared_key extension was received */
1465 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001466 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
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 /* Only the key_share extension was received */
1470 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001471 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_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 /* Both the pre_shared_key and key_share extensions were received */
1475 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001476 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001477 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001478
Jerry Yu745bb612021-10-13 22:01:04 +08001479 /* Neither pre_shared_key nor key_share extension was received */
1480 default:
1481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1482 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1483 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001484 }
1485
1486 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1487 *
1488 * TODO: We don't have to do this in case we offered 0-RTT and the
1489 * server accepted it. In this case, we could skip generating
1490 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001491 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001492 if( ret != 0 )
1493 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001495 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001496 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001497 }
1498
1499 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001500 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001501 if( ret != 0 )
1502 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001504 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001505 }
1506
1507 /* Next evolution in key schedule: Establish handshake secret and
1508 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001509 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001510 if( ret != 0 )
1511 {
Jerry Yuc068b662021-10-11 22:30:19 +08001512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1513 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001514 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001515 }
1516
Jerry Yub85277e2021-10-13 13:36:05 +08001517 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001518 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001519 {
1520 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1521 goto cleanup;
1522 }
Jerry Yu0b177842021-09-05 19:41:30 +08001523
1524 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1525 ssl->conf->endpoint,
1526 ssl->session_negotiate->ciphersuite,
1527 &traffic_keys,
1528 ssl );
1529 if( ret != 0 )
1530 {
1531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001532 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001533 }
1534
Jerry Yub85277e2021-10-13 13:36:05 +08001535 handshake->transform_handshake = transform_handshake;
1536 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001537
1538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1539 ssl->session_in = ssl->session_negotiate;
1540
1541 /*
1542 * State machine update
1543 */
1544 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1545
Jerry Yu4a173382021-10-11 21:45:31 +08001546cleanup:
1547
Jerry Yu0b177842021-09-05 19:41:30 +08001548 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001549 if( ret != 0 )
1550 {
Jerry Yub85277e2021-10-13 13:36:05 +08001551 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001552
Jerry Yu4a173382021-10-11 21:45:31 +08001553 MBEDTLS_SSL_PEND_FATAL_ALERT(
1554 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1555 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1556 }
1557 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001558}
1559
XiaokangQian355e09a2022-01-20 11:14:50 +00001560static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001561{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001562#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1564
XiaokangQian647719a2021-12-07 09:16:29 +00001565#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1566 /* If not offering early data, the client sends a dummy CCS record
1567 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001568 * its second ClientHello or before its encrypted handshake flight.
1569 */
XiaokangQian647719a2021-12-07 09:16:29 +00001570 mbedtls_ssl_handshake_set_state( ssl,
1571 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1572#else
1573 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1574#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1575
XiaokangQian78b1fa72022-01-19 06:56:30 +00001576 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001577
XiaokangQian78b1fa72022-01-19 06:56:30 +00001578 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001579 * We are going to re-generate a shared secret corresponding to the group
1580 * selected by the server, which is different from the group for which we
1581 * generated a shared secret in the first client hello.
1582 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001583 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001584 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001585 if( ret != 0 )
1586 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001587#else
1588 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001589#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001590
1591 return( 0 );
1592}
1593
Jerry Yue1b9c292021-09-10 10:08:31 +08001594/*
Jerry Yu4a173382021-10-11 21:45:31 +08001595 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001596 * Handler for MBEDTLS_SSL_SERVER_HELLO
1597 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001598static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001599{
Jerry Yu4a173382021-10-11 21:45:31 +08001600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001601 unsigned char *buf = NULL;
1602 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001603 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001604
1605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1606
1607 /* Coordination step
1608 * - Fetch record
1609 * - Make sure it's either a ServerHello or a HRR.
1610 * - Switch processing routine in case of HRR
1611 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001612 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1613 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1614
XiaokangQian16acd4b2022-01-14 07:35:47 +00001615 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001616 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001617 goto cleanup;
1618 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001619 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001620
XiaokangQianb851da82022-01-14 04:03:11 +00001621 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001622 buf + buf_len,
1623 is_hrr ) );
1624 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001625 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1626
XiaokangQianb851da82022-01-14 04:03:11 +00001627 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1628 MBEDTLS_SSL_HS_SERVER_HELLO,
1629 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001630
XiaokangQiand9e068e2022-01-18 06:23:32 +00001631 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001632 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001633 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001634 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001635
1636cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1638 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001639 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001640}
1641
1642/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001643 *
1644 * EncryptedExtensions message
1645 *
1646 * The EncryptedExtensions message contains any extensions which
1647 * should be protected, i.e., any which are not needed to establish
1648 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001649 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001650
1651/*
1652 * Overview
1653 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001654
1655/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001656static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001657
XiaokangQian97799ac2021-10-11 10:05:54 +00001658static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1659 const unsigned char *buf,
1660 const unsigned char *end );
1661static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001662
1663/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001664 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001665 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001666static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001667{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001668 int ret;
1669 unsigned char *buf;
1670 size_t buf_len;
1671
1672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1673
Xiaofei Bai746f9482021-11-12 08:53:56 +00001674 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001675 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001676 &buf, &buf_len ) );
1677
1678 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001679 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001680 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001681
Xiaofei Bai746f9482021-11-12 08:53:56 +00001682 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001683 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001684
XiaokangQian97799ac2021-10-11 10:05:54 +00001685 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001686
1687cleanup:
1688
1689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1690 return( ret );
1691
1692}
1693
XiaokangQian08da26c2021-10-09 10:12:11 +00001694/* Parse EncryptedExtensions message
1695 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001696 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001697 * } EncryptedExtensions;
1698 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001699static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1700 const unsigned char *buf,
1701 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001702{
1703 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001704 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001705 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001706 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001707
XiaokangQian08da26c2021-10-09 10:12:11 +00001708 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001709 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001710 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001711
XiaokangQian97799ac2021-10-11 10:05:54 +00001712 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001713 extensions_end = p + extensions_len;
1714 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001715
XiaokangQian8db25ff2021-10-13 05:56:18 +00001716 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001717 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001718 unsigned int extension_type;
1719 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001720
XiaokangQian08da26c2021-10-09 10:12:11 +00001721 /*
1722 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001723 * ExtensionType extension_type; (2 bytes)
1724 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001725 * } Extension;
1726 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001728 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1729 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1730 p += 4;
1731
XiaokangQian8db25ff2021-10-13 05:56:18 +00001732 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001733
XiaokangQian97799ac2021-10-11 10:05:54 +00001734 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001735 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001736 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001737 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001738 switch( extension_type )
1739 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001740
XiaokangQian08da26c2021-10-09 10:12:11 +00001741 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1743 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001744
lhuang0486cacac2022-01-21 07:34:27 -08001745#if defined(MBEDTLS_SSL_ALPN)
1746 case MBEDTLS_TLS_EXT_ALPN:
1747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1748
1749 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1750 {
1751 return( ret );
1752 }
1753
1754 break;
1755#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001756 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001757 MBEDTLS_SSL_DEBUG_MSG(
1758 3, ( "unsupported extension found: %u ", extension_type) );
1759 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001760 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001761 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1762 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001763 }
1764
XiaokangQian08da26c2021-10-09 10:12:11 +00001765 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001766 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001767
XiaokangQian97799ac2021-10-11 10:05:54 +00001768 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001769 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001770 {
1771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001772 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001773 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001774 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001775 }
1776
1777 return( ret );
1778}
1779
XiaokangQian97799ac2021-10-11 10:05:54 +00001780static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001781{
Jerry Yua93ac112021-10-27 16:31:48 +08001782#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001783 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001784 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1785 else
Jerry Yud2674312021-10-29 10:08:19 +08001786 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001787#else
1788 ((void) ssl);
1789 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1790#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001791 return( 0 );
1792}
1793
Jerry Yua93ac112021-10-27 16:31:48 +08001794#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001795/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001796 *
1797 * STATE HANDLING: CertificateRequest
1798 *
Jerry Yud2674312021-10-29 10:08:19 +08001799 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001800#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1801#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001802/* Coordination:
1803 * Deals with the ambiguity of not knowing if a CertificateRequest
1804 * will be sent. Returns a negative code on failure, or
1805 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1806 * - SSL_CERTIFICATE_REQUEST_SKIP
1807 * indicating if a Certificate Request is expected or not.
1808 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001809static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001810{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001812
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001813 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001814 {
1815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1816 return( SSL_CERTIFICATE_REQUEST_SKIP );
1817 }
1818
1819 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001820 {
1821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1822 return( ret );
1823 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001824 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001825
1826 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1827 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1828 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001829 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001830 }
1831
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001832 return( SSL_CERTIFICATE_REQUEST_SKIP );
1833}
1834
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001835/*
1836 * ssl_tls13_parse_certificate_request()
1837 * Parse certificate request
1838 * struct {
1839 * opaque certificate_request_context<0..2^8-1>;
1840 * Extension extensions<2..2^16-1>;
1841 * } CertificateRequest;
1842 */
1843static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1844 const unsigned char *buf,
1845 const unsigned char *end )
1846{
1847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1848 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001849 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001850 size_t extensions_len = 0;
1851 const unsigned char *extensions_end;
1852 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001853
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001854 /* ...
1855 * opaque certificate_request_context<0..2^8-1>
1856 * ...
1857 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001858 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1859 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001860 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001861
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001862 if( certificate_request_context_len > 0 )
1863 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001864 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001865 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1866 p, certificate_request_context_len );
1867
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001868 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001869 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001870 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001871 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001872 {
1873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1874 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1875 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001876 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001877 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001878 p += certificate_request_context_len;
1879 }
1880
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001881 /* ...
1882 * Extension extensions<2..2^16-1>;
1883 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001884 */
1885 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1886 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1887 p += 2;
1888
1889 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1890 extensions_end = p + extensions_len;
1891
1892 while( p < extensions_end )
1893 {
1894 unsigned int extension_type;
1895 size_t extension_data_len;
1896
1897 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1898 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1899 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1900 p += 4;
1901
1902 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1903
1904 switch( extension_type )
1905 {
1906 case MBEDTLS_TLS_EXT_SIG_ALG:
1907 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001908 ( "found signature algorithms extension" ) );
1909 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001910 p + extension_data_len );
1911 if( ret != 0 )
1912 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001913 if( ! sig_alg_ext_found )
1914 sig_alg_ext_found = 1;
1915 else
1916 {
1917 MBEDTLS_SSL_DEBUG_MSG( 3,
1918 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001919 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001920 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001921 break;
1922
1923 default:
1924 MBEDTLS_SSL_DEBUG_MSG(
1925 3,
1926 ( "unknown extension found: %u ( ignoring )",
1927 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001928 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001929 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001930 p += extension_data_len;
1931 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001932 /* Check that we consumed all the message. */
1933 if( p != end )
1934 {
1935 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001936 ( "CertificateRequest misaligned" ) );
1937 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001938 }
1939 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001940 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001941 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001942 MBEDTLS_SSL_DEBUG_MSG( 3,
1943 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001944 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001945 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001946
Jerry Yu7840f812022-01-29 10:26:51 +08001947 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001948 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001949
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001950decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001951 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1952 MBEDTLS_ERR_SSL_DECODE_ERROR );
1953 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001954}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001956/*
1957 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1958 */
1959static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001960{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001962
1963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1964
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001965 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1966
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001967 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1968 {
1969 unsigned char *buf;
1970 size_t buf_len;
1971
1972 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1973 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1974 &buf, &buf_len ) );
1975
1976 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1977 buf, buf + buf_len ) );
1978
1979 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1980 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1981 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001982 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001983 {
1984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001985 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001986 }
1987 else
1988 {
1989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001990 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1991 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001992 }
1993
1994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001995 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001996
Jerry Yud2674312021-10-29 10:08:19 +08001997 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1998
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001999cleanup:
2000
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2002 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002003}
2004
2005/*
Jerry Yu687101b2021-09-14 16:03:56 +08002006 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2007 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002008static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002009{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002010 int ret;
2011
2012 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002013 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002014 return( ret );
2015
Jerry Yu687101b2021-09-14 16:03:56 +08002016 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2017 return( 0 );
2018}
2019
2020/*
2021 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2022 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002023static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002024{
Jerry Yu30b071c2021-09-12 20:16:03 +08002025 int ret;
2026
2027 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2028 if( ret != 0 )
2029 return( ret );
2030
Jerry Yu687101b2021-09-14 16:03:56 +08002031 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2032 return( 0 );
2033}
Jerry Yua93ac112021-10-27 16:31:48 +08002034#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002035
Jerry Yu687101b2021-09-14 16:03:56 +08002036/*
2037 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2038 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002039static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002040{
XiaokangQianac0385c2021-11-03 06:40:11 +00002041 int ret;
2042
XiaokangQianc5c39d52021-11-09 11:55:10 +00002043 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002044 if( ret != 0 )
2045 return( ret );
2046
Ronald Cron49ad6192021-11-24 16:25:31 +01002047#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2048 mbedtls_ssl_handshake_set_state(
2049 ssl,
2050 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2051#else
Jerry Yu566c7812022-01-26 15:41:22 +08002052#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuca133a32022-02-15 14:22:05 +08002053 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
2054#else
2055 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Jerry Yu566c7812022-01-26 15:41:22 +08002056#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuca133a32022-02-15 14:22:05 +08002057
Jerry Yu566c7812022-01-26 15:41:22 +08002058#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002059
XiaokangQianac0385c2021-11-03 06:40:11 +00002060 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002061}
2062
2063/*
Ronald Crond4c64022021-12-06 09:06:46 +01002064 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
2065 */
2066#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2067static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
2068{
2069 int ret;
2070
2071 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2072 if( ret != 0 )
2073 return( ret );
2074
Ronald Crond4c64022021-12-06 09:06:46 +01002075 return( 0 );
2076}
2077#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2078
Jerry Yu90f152d2022-01-29 22:12:42 +08002079#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Crond4c64022021-12-06 09:06:46 +01002080/*
Jerry Yu566c7812022-01-26 15:41:22 +08002081 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2082 */
2083static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2084{
Jerry Yu5cc35062022-01-28 16:16:08 +08002085 MBEDTLS_SSL_DEBUG_MSG( 1,
2086 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002087 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002088
2089 return( mbedtls_ssl_tls13_write_certificate( ssl ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002090}
2091
2092/*
2093 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2094 */
2095static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2096{
Jerry Yu8511f122022-01-29 10:01:04 +08002097 return( mbedtls_ssl_tls13_write_certificate_verify( ssl ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002098}
Jerry Yu90f152d2022-01-29 22:12:42 +08002099#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002100
2101/*
Jerry Yu687101b2021-09-14 16:03:56 +08002102 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2103 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002104static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002105{
XiaokangQian0fa66432021-11-15 03:33:57 +00002106 int ret;
2107
Jerry Yu72637c72022-01-29 17:10:19 +08002108 if( !ssl->handshake->client_auth )
2109 {
2110 MBEDTLS_SSL_DEBUG_MSG( 1,
2111 ( "Switch to handshake traffic keys for outbound traffic" ) );
2112 mbedtls_ssl_set_outbound_transform( ssl,
2113 ssl->handshake->transform_handshake );
2114 }
XiaokangQian0fa66432021-11-15 03:33:57 +00002115 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2116 if( ret != 0 )
2117 return( ret );
2118
2119 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2120 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002121}
2122
2123/*
2124 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2125 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002126static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002127{
Jerry Yu378254d2021-10-30 21:44:47 +08002128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002129 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002130 return( 0 );
2131}
2132
2133/*
2134 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2135 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002136static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002137{
Jerry Yu378254d2021-10-30 21:44:47 +08002138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2139 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2140
2141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2142 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2143
2144 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2145
2146 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2147 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002148}
2149
Jerry Yu92c6b402021-08-27 16:59:09 +08002150int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002151{
Jerry Yu92c6b402021-08-27 16:59:09 +08002152 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002153
Jerry Yue3b34122021-09-28 17:53:35 +08002154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2155 mbedtls_ssl_states_str( ssl->state ),
2156 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002157
2158 switch( ssl->state )
2159 {
2160 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002161 * ssl->state is initialized as HELLO_REQUEST. It is the same
2162 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002163 */
2164 case MBEDTLS_SSL_HELLO_REQUEST:
2165 case MBEDTLS_SSL_CLIENT_HELLO:
2166 ret = ssl_tls13_write_client_hello( ssl );
2167 break;
2168
2169 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002170 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002171 break;
2172
2173 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002174 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002175 break;
2176
Jerry Yua93ac112021-10-27 16:31:48 +08002177#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002178 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2179 ret = ssl_tls13_process_certificate_request( ssl );
2180 break;
2181
Jerry Yu687101b2021-09-14 16:03:56 +08002182 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002183 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002184 break;
2185
2186 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002187 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002188 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002189#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002190
2191 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002192 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002193 break;
2194
Jerry Yu90f152d2022-01-29 22:12:42 +08002195#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002196 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2197 ret = ssl_tls13_write_client_certificate( ssl );
2198 break;
2199
2200 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2201 ret = ssl_tls13_write_client_certificate_verify( ssl );
2202 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002203#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002204
Jerry Yu687101b2021-09-14 16:03:56 +08002205 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002206 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002207 break;
2208
2209 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002210 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002211 break;
2212
2213 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002214 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002215 break;
2216
Ronald Cron49ad6192021-11-24 16:25:31 +01002217 /*
2218 * Injection of dummy-CCS's for middlebox compatibility
2219 */
2220#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2221 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002222 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01002223 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01002224 break;
2225#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2226
Jerry Yu92c6b402021-08-27 16:59:09 +08002227 default:
2228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2230 }
2231
2232 return( ret );
2233}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002234
Jerry Yufb4b6472022-01-27 15:03:26 +08002235#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002236
Jerry Yufb4b6472022-01-27 15:03:26 +08002237