blob: d066c39ff36e01e4f08593cb363d945f2126537f [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
XiaokangQian0b64eed2022-01-27 10:36:51 +0000691static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
692 unsigned char* buf,
693 unsigned char* end,
694 size_t* olen )
695{
696 unsigned char *p = buf;
697
698 *olen = 0;
699
700 if( ssl->handshake->verify_cookie == NULL )
701 {
702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
703 return( 0 );
704 }
705
706 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
707 ssl->handshake->verify_cookie,
708 ssl->handshake->verify_cookie_len );
709
710 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
711 p += 2;
712 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->handshake->verify_cookie_len + 4 );
713
714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
715
716 /* Extension Type */
717 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
718
719 /* Extension Length */
720 MBEDTLS_PUT_UINT16_BE( ssl->handshake->verify_cookie_len + 2, p, 0 );
721
722 /* Cookie Length */
723 MBEDTLS_PUT_UINT16_BE( ssl->handshake->verify_cookie_len, p, 0 );
724
725 /* Cookie */
726 memcpy( p, ssl->handshake->verify_cookie, ssl->handshake->verify_cookie_len );
727
728 *olen = ssl->handshake->verify_cookie_len + 6;
729
730 return( 0 );
731}
732
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800733/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800734 * CipherSuite cipher_suites<2..2^16-2>;
735 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800736static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800737 mbedtls_ssl_context *ssl,
738 unsigned char *buf,
739 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000740 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800741{
Jerry Yufec982e2021-09-07 17:26:06 +0800742 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800743 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000744 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800745 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800746
Xiaofei Baid25fab62021-12-02 06:36:27 +0000747 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800748
749 /*
750 * Ciphersuite list
751 *
752 * This is a list of the symmetric cipher options supported by
753 * the client, specifically the record protection algorithm
754 * ( including secret key length ) and a hash to be used with
755 * HKDF, in descending order of client preference.
756 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800757 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800758
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800759 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800760 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
761 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800762
Jerry Yu0c63af62021-09-02 12:59:12 +0800763 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000764 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800765 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800766 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800767 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800768 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800769
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800770 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800771 if( ciphersuite_info == NULL )
772 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800773 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
774 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800775 continue;
776
777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800778 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800779 ciphersuite_info->name ) );
780
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800781 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800782 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
783 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
784 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800785 }
786
Jerry Yu0c63af62021-09-02 12:59:12 +0800787 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000788 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800789 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800790 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800791 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
792 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800793
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800794 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000795 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800796
Jerry Yu6a643102021-08-31 14:40:36 +0800797 return( 0 );
798}
799
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800800/*
801 * Structure of ClientHello message:
802 *
803 * struct {
804 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
805 * Random random;
806 * opaque legacy_session_id<0..32>;
807 * CipherSuite cipher_suites<2..2^16-2>;
808 * opaque legacy_compression_methods<1..2^8-1>;
809 * Extension extensions<8..2^16-1>;
810 * } ClientHello;
811 */
Jerry Yu08906d02021-08-31 11:05:27 +0800812static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800813 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800814 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000815 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800816{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800817
Jerry Yubc20bdd2021-08-24 15:59:48 +0800818 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000819 unsigned char *p_extensions_len; /* Pointer to extensions length */
820 size_t output_len; /* Length of buffer used by function */
821 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800822
Jerry Yubc20bdd2021-08-24 15:59:48 +0800823 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800824 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800825
Xiaofei Baid25fab62021-12-02 06:36:27 +0000826 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800827
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800828 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800829 ssl->major_ver = ssl->conf->min_major_ver;
830 ssl->minor_ver = ssl->conf->min_minor_ver;
831
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800832 /*
833 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800834 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800835 *
836 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800837 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800838 */
Jerry Yufec982e2021-09-07 17:26:06 +0800839 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800840 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800841 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800842
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800843 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800844 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
845 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800846 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800847 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
848 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800849
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800850 /*
851 * Write legacy_session_id
852 *
853 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
854 * which has been merged with pre-shared keys in this version. A client
855 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
856 * this field to that value. In compatibility mode, this field MUST be
857 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
858 * a new 32-byte value. This value need not be random but SHOULD be
859 * unpredictable to avoid implementations fixating on a specific value
860 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
861 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800862 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100863#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
864 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
865 *p++ = (unsigned char)ssl->session_negotiate->id_len;
866 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
867 p += ssl->session_negotiate->id_len;
868
869 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
870 ssl->session_negotiate->id_len );
871#else
Jerry Yubbe09522021-09-06 21:17:54 +0800872 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
873 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100874#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800875
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800876 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800877 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800878 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800879 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800880 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800881
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800882 /* Write legacy_compression_methods
883 *
884 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800885 * one byte set to zero, which corresponds to the 'null' compression
886 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800887 */
Jerry Yubbe09522021-09-06 21:17:54 +0800888 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
889 *p++ = 1;
890 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800891
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800892 /* Write extensions */
893
894 /* Keeping track of the included extensions */
895 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800896
897 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800898 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000899 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800900 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800901
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800902 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800903 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800904 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800905 */
Jerry Yubbe09522021-09-06 21:17:54 +0800906 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800907 if( ret != 0 )
908 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800909 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800910
lhuang0486cacac2022-01-21 07:34:27 -0800911#if defined(MBEDTLS_SSL_ALPN)
912 ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
913 if( ret != 0 )
914 return( ret );
915 p += output_len;
916#endif /* MBEDTLS_SSL_ALPN */
917
XiaokangQian0b64eed2022-01-27 10:36:51 +0000918 /* For TLS / DTLS 1.3 we need to support the use of cookies
919 * ( if the server provided them ) */
920 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &output_len );
921 if( ret != 0 )
922 return( ret );
923 p += output_len;
924
Jerry Yubc20bdd2021-08-24 15:59:48 +0800925#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800926
Jerry Yub925f212022-01-12 11:17:02 +0800927 /*
928 * Add the extensions related to (EC)DHE ephemeral key establishment only if
929 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800930 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800931 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
932 {
933 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
934 if( ret != 0 )
935 return( ret );
936 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800937
Jerry Yuf46b0162022-01-11 16:28:00 +0800938 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
939 if( ret != 0 )
940 return( ret );
941 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800942
Jerry Yuf017ee42022-01-12 15:49:48 +0800943 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800944 if( ret != 0 )
945 return( ret );
946 p += output_len;
947 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800948#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
949
Xiaofei Bai15a56812021-11-05 10:52:12 +0000950#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000951 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000952 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
953 if( ret != 0 )
954 return( ret );
955 p += output_len;
956#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
957
Jerry Yubc20bdd2021-08-24 15:59:48 +0800958 /* Add more extensions here */
959
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800960 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000961 extensions_len = p - p_extensions_len - 2;
962 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800964 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000965 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800966
Xiaofei Baid25fab62021-12-02 06:36:27 +0000967 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800968 return( 0 );
969}
970
Jerry Yu92c6b402021-08-27 16:59:09 +0800971static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
972{
973 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800974
Jerry Yu92c6b402021-08-27 16:59:09 +0800975 if( ssl->conf->f_rng == NULL )
976 {
977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
978 return( MBEDTLS_ERR_SSL_NO_RNG );
979 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800980
Jerry Yu92c6b402021-08-27 16:59:09 +0800981 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
982 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800983 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800984 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800985 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800986 return( ret );
987 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800988
Ronald Cron49ad6192021-11-24 16:25:31 +0100989#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
990 /*
991 * Create a session identifier for the purpose of middlebox compatibility
992 * only if one has not been created already.
993 */
994 if( ssl->session_negotiate->id_len == 0 )
995 {
996 /* Creating a session id with 32 byte length */
997 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
998 ssl->session_negotiate->id, 32 ) ) != 0 )
999 {
1000 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
1001 return( ret );
1002 }
1003 ssl->session_negotiate->id_len = 32;
1004 }
1005#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1006
Jerry Yu6f13f642021-08-26 17:18:15 +08001007 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001008}
1009
Jerry Yu92c6b402021-08-27 16:59:09 +08001010/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001011 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001012 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001013 */
1014static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001015{
Jerry Yu92c6b402021-08-27 16:59:09 +08001016 int ret = 0;
1017 unsigned char *buf;
1018 size_t buf_len, msg_len;
1019
1020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1021
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001022 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001023
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001024 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1025 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1026 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001027
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001028 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001029 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001030 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001031
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001032 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1033 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001034 msg_len );
1035 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001036
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001037 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1038 buf_len,
1039 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001040
Ronald Cron3addfa42022-02-08 16:10:25 +01001041 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1042
Jerry Yu92c6b402021-08-27 16:59:09 +08001043cleanup:
1044
1045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1046 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001047}
1048
Jerry Yu687101b2021-09-14 16:03:56 +08001049/*
Jerry Yu4a173382021-10-11 21:45:31 +08001050 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001051 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001052/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001053 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1054 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001055 * to indicate which message is expected and to be parsed next.
1056 */
Jerry Yub85277e2021-10-13 13:36:05 +08001057#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1058#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1059static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1060 const unsigned char *buf,
1061 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001062{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001063 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001064 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1065 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1066 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1067 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1068
1069 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1070 *
Jerry Yu4a173382021-10-11 21:45:31 +08001071 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 * special value of the SHA-256 of "HelloRetryRequest".
1073 *
1074 * struct {
1075 * ProtocolVersion legacy_version = 0x0303;
1076 * Random random;
1077 * opaque legacy_session_id_echo<0..32>;
1078 * CipherSuite cipher_suite;
1079 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001080 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 * } ServerHello;
1082 *
1083 */
Jerry Yub85277e2021-10-13 13:36:05 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001085
1086 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 {
Jerry Yub85277e2021-10-13 13:36:05 +08001088 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 }
1090
Jerry Yub85277e2021-10-13 13:36:05 +08001091 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001092}
1093
Jerry Yu745bb612021-10-13 22:01:04 +08001094/* Fetch and preprocess
1095 * Returns a negative value on failure, and otherwise
1096 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1097 * - SSL_SERVER_HELLO_COORDINATE_HRR
1098 */
Jerry Yub85277e2021-10-13 13:36:05 +08001099static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1100 unsigned char **buf,
1101 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001102{
Jerry Yu4a173382021-10-11 21:45:31 +08001103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
XiaokangQian355e09a2022-01-20 11:14:50 +00001105 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1106 MBEDTLS_SSL_HS_SERVER_HELLO,
1107 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001108
Jerry Yub85277e2021-10-13 13:36:05 +08001109 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1110 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001111 {
Jerry Yu745bb612021-10-13 22:01:04 +08001112 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1114 break;
1115 case SSL_SERVER_HELLO_COORDINATE_HRR:
1116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001117 /* If a client receives a second
1118 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1119 * was itself in response to a HelloRetryRequest), it MUST abort the
1120 * handshake with an "unexpected_message" alert.
1121 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001122 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001123 {
1124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1125 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1126 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1127 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1128 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001129 /*
1130 * Clients must abort the handshake with an "illegal_parameter"
1131 * alert if the HelloRetryRequest would not result in any change
1132 * in the ClientHello.
1133 * In a PSK only key exchange that what we expect.
1134 */
1135 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1,
1138 ( "Unexpected HRR in pure PSK key exchange." ) );
1139 MBEDTLS_SSL_PEND_FATAL_ALERT(
1140 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1141 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1142 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1143 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001144
XiaokangQiand9e068e2022-01-18 06:23:32 +00001145 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001146
Jerry Yu745bb612021-10-13 22:01:04 +08001147 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 }
1149
1150cleanup:
1151
1152 return( ret );
1153}
1154
Jerry Yu4a173382021-10-11 21:45:31 +08001155static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1156 const unsigned char **buf,
1157 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001158{
1159 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001160 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001161
Jerry Yude4fb2c2021-09-19 18:05:08 +08001162 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001163 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164
Jerry Yu4a173382021-10-11 21:45:31 +08001165 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001166
1167 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001168 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1169 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001170 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001171 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1172 ssl->session_negotiate->id,
1173 ssl->session_negotiate->id_len );
1174 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001175 legacy_session_id_echo_len );
1176
1177 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1178 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1179
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1181 }
1182
Jerry Yu4a173382021-10-11 21:45:31 +08001183 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 *buf = p;
1185
1186 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001187 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001188 return( 0 );
1189}
1190
Jerry Yu4a173382021-10-11 21:45:31 +08001191static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1192 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001193{
Jerry Yu4a173382021-10-11 21:45:31 +08001194 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1195
Jerry Yue1b9c292021-09-10 10:08:31 +08001196 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001197 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001198 {
Jerry Yu4a173382021-10-11 21:45:31 +08001199 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001200 {
1201 return( 1 );
1202 }
1203 }
1204 return( 0 );
1205}
1206
1207/* Parse ServerHello message and configure context
1208 *
1209 * struct {
1210 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1211 * Random random;
1212 * opaque legacy_session_id_echo<0..32>;
1213 * CipherSuite cipher_suite;
1214 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001215 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001216 * } ServerHello;
1217 */
Jerry Yuc068b662021-10-11 22:30:19 +08001218static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1219 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001220 const unsigned char *end,
1221 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001222{
Jerry Yub85277e2021-10-13 13:36:05 +08001223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001225 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001226 size_t extensions_len;
1227 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001228 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001229 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001230 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001231 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001232
1233 /*
1234 * Check there is space for minimal fields
1235 *
1236 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001237 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001238 * - legacy_session_id_echo ( 1 byte ), minimum size
1239 * - cipher_suite ( 2 bytes)
1240 * - legacy_compression_method ( 1 byte )
1241 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001242 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001243
1244 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001245 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1246
Jerry Yu4a173382021-10-11 21:45:31 +08001247 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001248 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001249 * ...
1250 * with ProtocolVersion defined as:
1251 * uint16 ProtocolVersion;
1252 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1254 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1255 {
1256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1257 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1258 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001259 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1260 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 }
1262 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001263
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001264 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001265 * Random random;
1266 * ...
1267 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001268 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001269 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001270 if( !is_hrr )
1271 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001272 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001273 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1274 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1275 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1276 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001277 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001278
Jerry Yu4a173382021-10-11 21:45:31 +08001279 /* ...
1280 * opaque legacy_session_id_echo<0..32>;
1281 * ...
1282 */
1283 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001284 {
XiaokangQian52da5582022-01-26 09:49:29 +00001285 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001286 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001287 }
1288
Jerry Yu4a173382021-10-11 21:45:31 +08001289 /* ...
1290 * CipherSuite cipher_suite;
1291 * ...
1292 * with CipherSuite defined as:
1293 * uint8 CipherSuite[2];
1294 */
1295 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001296 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1297 p += 2;
1298
Jerry Yu4a173382021-10-11 21:45:31 +08001299
XiaokangQian355e09a2022-01-20 11:14:50 +00001300 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001301 /*
1302 * Check whether this ciphersuite is supported and offered.
1303 * Via the force_ciphersuite version we may have instructed the client
1304 * to use a different ciphersuite.
1305 */
Jerry Yu4a173382021-10-11 21:45:31 +08001306 if( ciphersuite_info == NULL ||
1307 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001308 {
XiaokangQian52da5582022-01-26 09:49:29 +00001309 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001310 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001311 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001312 * If we received an HRR before and that the proposed selected
1313 * ciphersuite in this server hello is not the same as the one
1314 * proposed in the HRR, we abort the handshake and send an
1315 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001316 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001317 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1318 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001319 {
XiaokangQian52da5582022-01-26 09:49:29 +00001320 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001321 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001322
XiaokangQian52da5582022-01-26 09:49:29 +00001323 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001324 {
1325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1326 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001327 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001328 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001329
Jerry Yu4a173382021-10-11 21:45:31 +08001330 /* Configure ciphersuites */
1331 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1332
XiaokangQian355e09a2022-01-20 11:14:50 +00001333 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001334 ssl->session_negotiate->ciphersuite = cipher_suite;
1335
Jerry Yue1b9c292021-09-10 10:08:31 +08001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1337 cipher_suite, ciphersuite_info->name ) );
1338
1339#if defined(MBEDTLS_HAVE_TIME)
1340 ssl->session_negotiate->start = time( NULL );
1341#endif /* MBEDTLS_HAVE_TIME */
1342
Jerry Yu4a173382021-10-11 21:45:31 +08001343 /* ...
1344 * uint8 legacy_compression_method = 0;
1345 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001346 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001347 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001348 if( p[0] != 0 )
1349 {
Jerry Yub85277e2021-10-13 13:36:05 +08001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001351 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001352 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001353 }
1354 p++;
1355
Jerry Yub85277e2021-10-13 13:36:05 +08001356 /* ...
1357 * Extension extensions<6..2^16-1>;
1358 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001359 * struct {
1360 * ExtensionType extension_type; (2 bytes)
1361 * opaque extension_data<0..2^16-1>;
1362 * } Extension;
1363 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001364 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001365 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001366 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001367
Jerry Yu4a173382021-10-11 21:45:31 +08001368 /* Check extensions do not go beyond the buffer of data. */
1369 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1370 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001371
Jerry Yu4a173382021-10-11 21:45:31 +08001372 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001373
Jerry Yu4a173382021-10-11 21:45:31 +08001374 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001375 {
1376 unsigned int extension_type;
1377 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001378 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001379
Jerry Yu4a173382021-10-11 21:45:31 +08001380 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001381 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1382 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1383 p += 4;
1384
Jerry Yu4a173382021-10-11 21:45:31 +08001385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001386 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001387
1388 switch( extension_type )
1389 {
XiaokangQianb851da82022-01-14 04:03:11 +00001390 case MBEDTLS_TLS_EXT_COOKIE:
1391
XiaokangQiand9e068e2022-01-18 06:23:32 +00001392 if( !is_hrr )
1393 {
XiaokangQian52da5582022-01-26 09:49:29 +00001394 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001395 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001396 }
1397
XiaokangQian43550bd2022-01-21 04:32:58 +00001398 ret = ssl_tls13_parse_cookie_ext( ssl,
1399 p, extension_data_end );
1400 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001401 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001402 MBEDTLS_SSL_DEBUG_RET( 1,
1403 "ssl_tls13_parse_cookie_ext",
1404 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001405 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001406 }
XiaokangQianb851da82022-01-14 04:03:11 +00001407 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001408
Jerry Yue1b9c292021-09-10 10:08:31 +08001409 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001410 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001411 MBEDTLS_SSL_DEBUG_MSG( 3,
1412 ( "found supported_versions extension" ) );
1413
Jerry Yuc068b662021-10-11 22:30:19 +08001414 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001415 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001416 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001417 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001418 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001419 break;
1420
1421 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001424
XiaokangQian52da5582022-01-26 09:49:29 +00001425 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001426 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001427
1428#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1429 case MBEDTLS_TLS_EXT_KEY_SHARE:
1430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001431 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1432 {
XiaokangQian52da5582022-01-26 09:49:29 +00001433 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001434 goto cleanup;
1435 }
1436
XiaokangQian53f20b72022-01-18 10:47:33 +00001437 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001438 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001439 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001440 else
XiaokangQianb851da82022-01-14 04:03:11 +00001441 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001442 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001443 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 {
1445 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001446 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001447 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001448 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001449 }
1450 break;
1451#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1452
1453 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001454 MBEDTLS_SSL_DEBUG_MSG(
1455 3,
1456 ( "unknown extension found: %u ( ignoring )",
1457 extension_type ) );
1458
XiaokangQian52da5582022-01-26 09:49:29 +00001459 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001460 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001461 }
1462
1463 p += extension_data_len;
1464 }
1465
XiaokangQian78b1fa72022-01-19 06:56:30 +00001466 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001467 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001469 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001470 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001471 }
1472
XiaokangQiand59be772022-01-24 10:12:51 +00001473cleanup:
1474
XiaokangQian52da5582022-01-26 09:49:29 +00001475 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001476 {
1477 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1478 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1479 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1480 }
XiaokangQian52da5582022-01-26 09:49:29 +00001481 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001482 {
1483 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1484 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1485 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1486 }
1487 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001488}
1489
XiaokangQian355e09a2022-01-20 11:14:50 +00001490static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001491{
Jerry Yub85277e2021-10-13 13:36:05 +08001492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001493 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001494 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001495 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001496
Jerry Yub85277e2021-10-13 13:36:05 +08001497 /* Determine the key exchange mode:
1498 * 1) If both the pre_shared_key and key_share extensions were received
1499 * then the key exchange mode is PSK with EPHEMERAL.
1500 * 2) If only the pre_shared_key extension was received then the key
1501 * exchange mode is PSK-only.
1502 * 3) If only the key_share extension was received then the key
1503 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001504 */
Jerry Yub85277e2021-10-13 13:36:05 +08001505 switch( handshake->extensions_present &
1506 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001507 {
Jerry Yu745bb612021-10-13 22:01:04 +08001508 /* Only the pre_shared_key extension was received */
1509 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001510 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001511 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001512
Jerry Yu745bb612021-10-13 22:01:04 +08001513 /* Only the key_share extension was received */
1514 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001515 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001516 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001517
Jerry Yu745bb612021-10-13 22:01:04 +08001518 /* Both the pre_shared_key and key_share extensions were received */
1519 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001520 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001521 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001522
Jerry Yu745bb612021-10-13 22:01:04 +08001523 /* Neither pre_shared_key nor key_share extension was received */
1524 default:
1525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1526 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1527 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001528 }
1529
1530 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1531 *
1532 * TODO: We don't have to do this in case we offered 0-RTT and the
1533 * server accepted it. In this case, we could skip generating
1534 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001535 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001536 if( ret != 0 )
1537 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001539 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001540 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001541 }
1542
1543 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001544 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001545 if( ret != 0 )
1546 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001548 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001549 }
1550
1551 /* Next evolution in key schedule: Establish handshake secret and
1552 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001553 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001554 if( ret != 0 )
1555 {
Jerry Yuc068b662021-10-11 22:30:19 +08001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1557 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001558 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001559 }
1560
Jerry Yub85277e2021-10-13 13:36:05 +08001561 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001562 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001563 {
1564 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1565 goto cleanup;
1566 }
Jerry Yu0b177842021-09-05 19:41:30 +08001567
1568 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1569 ssl->conf->endpoint,
1570 ssl->session_negotiate->ciphersuite,
1571 &traffic_keys,
1572 ssl );
1573 if( ret != 0 )
1574 {
1575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001576 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001577 }
1578
Jerry Yub85277e2021-10-13 13:36:05 +08001579 handshake->transform_handshake = transform_handshake;
1580 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001581
1582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1583 ssl->session_in = ssl->session_negotiate;
1584
1585 /*
1586 * State machine update
1587 */
1588 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1589
Jerry Yu4a173382021-10-11 21:45:31 +08001590cleanup:
1591
Jerry Yu0b177842021-09-05 19:41:30 +08001592 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001593 if( ret != 0 )
1594 {
Jerry Yub85277e2021-10-13 13:36:05 +08001595 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001596
Jerry Yu4a173382021-10-11 21:45:31 +08001597 MBEDTLS_SSL_PEND_FATAL_ALERT(
1598 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1599 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1600 }
1601 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001602}
1603
XiaokangQian355e09a2022-01-20 11:14:50 +00001604static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001605{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001606#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1608
XiaokangQian647719a2021-12-07 09:16:29 +00001609#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1610 /* If not offering early data, the client sends a dummy CCS record
1611 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001612 * its second ClientHello or before its encrypted handshake flight.
1613 */
XiaokangQian647719a2021-12-07 09:16:29 +00001614 mbedtls_ssl_handshake_set_state( ssl,
1615 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1616#else
1617 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1618#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1619
XiaokangQian78b1fa72022-01-19 06:56:30 +00001620 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001621
XiaokangQian78b1fa72022-01-19 06:56:30 +00001622 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001623 * We are going to re-generate a shared secret corresponding to the group
1624 * selected by the server, which is different from the group for which we
1625 * generated a shared secret in the first client hello.
1626 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001627 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001628 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001629 if( ret != 0 )
1630 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001631#else
1632 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001633#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001634
1635 return( 0 );
1636}
1637
Jerry Yue1b9c292021-09-10 10:08:31 +08001638/*
Jerry Yu4a173382021-10-11 21:45:31 +08001639 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001640 * Handler for MBEDTLS_SSL_SERVER_HELLO
1641 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001642static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001643{
Jerry Yu4a173382021-10-11 21:45:31 +08001644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001645 unsigned char *buf = NULL;
1646 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001647 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001648
1649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1650
1651 /* Coordination step
1652 * - Fetch record
1653 * - Make sure it's either a ServerHello or a HRR.
1654 * - Switch processing routine in case of HRR
1655 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001656 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1657 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1658
XiaokangQian16acd4b2022-01-14 07:35:47 +00001659 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001660 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001661 goto cleanup;
1662 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001663 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001664
XiaokangQianb851da82022-01-14 04:03:11 +00001665 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001666 buf + buf_len,
1667 is_hrr ) );
1668 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001669 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1670
XiaokangQianb851da82022-01-14 04:03:11 +00001671 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1672 MBEDTLS_SSL_HS_SERVER_HELLO,
1673 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001674
XiaokangQiand9e068e2022-01-18 06:23:32 +00001675 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001676 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001677 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001678 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001679
1680cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1682 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001683 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001684}
1685
1686/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001687 *
1688 * EncryptedExtensions message
1689 *
1690 * The EncryptedExtensions message contains any extensions which
1691 * should be protected, i.e., any which are not needed to establish
1692 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001693 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001694
1695/*
1696 * Overview
1697 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001698
1699/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001700static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001701
XiaokangQian97799ac2021-10-11 10:05:54 +00001702static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1703 const unsigned char *buf,
1704 const unsigned char *end );
1705static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001706
1707/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001708 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001709 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001710static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001711{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001712 int ret;
1713 unsigned char *buf;
1714 size_t buf_len;
1715
1716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1717
Xiaofei Bai746f9482021-11-12 08:53:56 +00001718 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001719 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001720 &buf, &buf_len ) );
1721
1722 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001723 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001724 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001725
Xiaofei Bai746f9482021-11-12 08:53:56 +00001726 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001727 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001728
XiaokangQian97799ac2021-10-11 10:05:54 +00001729 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001730
1731cleanup:
1732
1733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1734 return( ret );
1735
1736}
1737
XiaokangQian08da26c2021-10-09 10:12:11 +00001738/* Parse EncryptedExtensions message
1739 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001740 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001741 * } EncryptedExtensions;
1742 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001743static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1744 const unsigned char *buf,
1745 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001746{
1747 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001748 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001749 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001750 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001751
XiaokangQian08da26c2021-10-09 10:12:11 +00001752 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001753 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001754 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001755
XiaokangQian97799ac2021-10-11 10:05:54 +00001756 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001757 extensions_end = p + extensions_len;
1758 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001759
XiaokangQian8db25ff2021-10-13 05:56:18 +00001760 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001761 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001762 unsigned int extension_type;
1763 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764
XiaokangQian08da26c2021-10-09 10:12:11 +00001765 /*
1766 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001767 * ExtensionType extension_type; (2 bytes)
1768 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001769 * } Extension;
1770 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001771 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001772 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1773 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1774 p += 4;
1775
XiaokangQian8db25ff2021-10-13 05:56:18 +00001776 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001777
XiaokangQian97799ac2021-10-11 10:05:54 +00001778 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001779 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001780 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001781 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001782 switch( extension_type )
1783 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784
XiaokangQian08da26c2021-10-09 10:12:11 +00001785 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1787 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001788
lhuang0486cacac2022-01-21 07:34:27 -08001789#if defined(MBEDTLS_SSL_ALPN)
1790 case MBEDTLS_TLS_EXT_ALPN:
1791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1792
1793 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1794 {
1795 return( ret );
1796 }
1797
1798 break;
1799#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001800 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001801 MBEDTLS_SSL_DEBUG_MSG(
1802 3, ( "unsupported extension found: %u ", extension_type) );
1803 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001804 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001805 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1806 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001807 }
1808
XiaokangQian08da26c2021-10-09 10:12:11 +00001809 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001810 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001811
XiaokangQian97799ac2021-10-11 10:05:54 +00001812 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001813 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001814 {
1815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001816 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001817 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001818 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001819 }
1820
1821 return( ret );
1822}
1823
XiaokangQian97799ac2021-10-11 10:05:54 +00001824static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001825{
Jerry Yua93ac112021-10-27 16:31:48 +08001826#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001827 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001828 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1829 else
Jerry Yud2674312021-10-29 10:08:19 +08001830 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001831#else
1832 ((void) ssl);
1833 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1834#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001835 return( 0 );
1836}
1837
Jerry Yua93ac112021-10-27 16:31:48 +08001838#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001839/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001840 *
1841 * STATE HANDLING: CertificateRequest
1842 *
Jerry Yud2674312021-10-29 10:08:19 +08001843 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001844#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1845#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001846/* Coordination:
1847 * Deals with the ambiguity of not knowing if a CertificateRequest
1848 * will be sent. Returns a negative code on failure, or
1849 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1850 * - SSL_CERTIFICATE_REQUEST_SKIP
1851 * indicating if a Certificate Request is expected or not.
1852 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001853static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001854{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001856
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001857 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001858 {
1859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1860 return( SSL_CERTIFICATE_REQUEST_SKIP );
1861 }
1862
1863 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001864 {
1865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1866 return( ret );
1867 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001868 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001869
1870 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1871 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1872 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001873 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001874 }
1875
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001876 return( SSL_CERTIFICATE_REQUEST_SKIP );
1877}
1878
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001879/*
1880 * ssl_tls13_parse_certificate_request()
1881 * Parse certificate request
1882 * struct {
1883 * opaque certificate_request_context<0..2^8-1>;
1884 * Extension extensions<2..2^16-1>;
1885 * } CertificateRequest;
1886 */
1887static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1888 const unsigned char *buf,
1889 const unsigned char *end )
1890{
1891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1892 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001893 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001894 size_t extensions_len = 0;
1895 const unsigned char *extensions_end;
1896 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001897
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001898 /* ...
1899 * opaque certificate_request_context<0..2^8-1>
1900 * ...
1901 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001902 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1903 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001904 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001905
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001906 if( certificate_request_context_len > 0 )
1907 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001908 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001909 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1910 p, certificate_request_context_len );
1911
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001912 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001913 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001914 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001915 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001916 {
1917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1918 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1919 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001920 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001921 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001922 p += certificate_request_context_len;
1923 }
1924
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001925 /* ...
1926 * Extension extensions<2..2^16-1>;
1927 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001928 */
1929 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1930 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1931 p += 2;
1932
1933 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1934 extensions_end = p + extensions_len;
1935
1936 while( p < extensions_end )
1937 {
1938 unsigned int extension_type;
1939 size_t extension_data_len;
1940
1941 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1942 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1943 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1944 p += 4;
1945
1946 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1947
1948 switch( extension_type )
1949 {
1950 case MBEDTLS_TLS_EXT_SIG_ALG:
1951 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001952 ( "found signature algorithms extension" ) );
1953 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001954 p + extension_data_len );
1955 if( ret != 0 )
1956 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001957 if( ! sig_alg_ext_found )
1958 sig_alg_ext_found = 1;
1959 else
1960 {
1961 MBEDTLS_SSL_DEBUG_MSG( 3,
1962 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001963 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001964 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001965 break;
1966
1967 default:
1968 MBEDTLS_SSL_DEBUG_MSG(
1969 3,
1970 ( "unknown extension found: %u ( ignoring )",
1971 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001972 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001973 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001974 p += extension_data_len;
1975 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001976 /* Check that we consumed all the message. */
1977 if( p != end )
1978 {
1979 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001980 ( "CertificateRequest misaligned" ) );
1981 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001982 }
1983 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001984 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001985 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001986 MBEDTLS_SSL_DEBUG_MSG( 3,
1987 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001988 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001989 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001990
Jerry Yu7840f812022-01-29 10:26:51 +08001991 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001992 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001993
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001994decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001995 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1996 MBEDTLS_ERR_SSL_DECODE_ERROR );
1997 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001998}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001999
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002000/*
2001 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2002 */
2003static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002004{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002006
2007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2008
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002009 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2010
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002011 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2012 {
2013 unsigned char *buf;
2014 size_t buf_len;
2015
2016 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2017 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2018 &buf, &buf_len ) );
2019
2020 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2021 buf, buf + buf_len ) );
2022
2023 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2024 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2025 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002026 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002029 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002030 }
2031 else
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002034 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2035 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002036 }
2037
2038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002039 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002040
Jerry Yud2674312021-10-29 10:08:19 +08002041 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2042
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002043cleanup:
2044
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2046 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002047}
2048
2049/*
Jerry Yu687101b2021-09-14 16:03:56 +08002050 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2051 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002052static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002053{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002054 int ret;
2055
2056 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002057 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002058 return( ret );
2059
Jerry Yu687101b2021-09-14 16:03:56 +08002060 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2061 return( 0 );
2062}
2063
2064/*
2065 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2066 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002067static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002068{
Jerry Yu30b071c2021-09-12 20:16:03 +08002069 int ret;
2070
2071 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2072 if( ret != 0 )
2073 return( ret );
2074
Jerry Yu687101b2021-09-14 16:03:56 +08002075 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2076 return( 0 );
2077}
Jerry Yua93ac112021-10-27 16:31:48 +08002078#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002079
Jerry Yu687101b2021-09-14 16:03:56 +08002080/*
2081 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2082 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002083static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002084{
XiaokangQianac0385c2021-11-03 06:40:11 +00002085 int ret;
2086
XiaokangQianc5c39d52021-11-09 11:55:10 +00002087 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002088 if( ret != 0 )
2089 return( ret );
2090
Ronald Cron49ad6192021-11-24 16:25:31 +01002091#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2092 mbedtls_ssl_handshake_set_state(
2093 ssl,
2094 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2095#else
Jerry Yuca133a32022-02-15 14:22:05 +08002096 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002097#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002098
XiaokangQianac0385c2021-11-03 06:40:11 +00002099 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002100}
2101
2102/*
Jerry Yu566c7812022-01-26 15:41:22 +08002103 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2104 */
2105static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2106{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002107 int non_empty_certificate_msg = 0;
2108
Jerry Yu5cc35062022-01-28 16:16:08 +08002109 MBEDTLS_SSL_DEBUG_MSG( 1,
2110 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002111 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002112
Ronald Cron9df7c802022-03-08 18:38:54 +01002113#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002114 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002115 {
2116 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2117 if( ret != 0 )
2118 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002119
Ronald Cron7a94aca2022-03-09 07:44:27 +01002120 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2121 non_empty_certificate_msg = 1;
2122 }
2123 else
2124 {
2125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2126 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002127#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002128
Ronald Cron7a94aca2022-03-09 07:44:27 +01002129 if( non_empty_certificate_msg )
2130 {
2131 mbedtls_ssl_handshake_set_state( ssl,
2132 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2133 }
2134 else
2135 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2136
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002137 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002138}
2139
Ronald Cron9df7c802022-03-08 18:38:54 +01002140#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002141/*
2142 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2143 */
2144static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2145{
Ronald Crona8b38872022-03-09 07:59:25 +01002146 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2147
2148 if( ret == 0 )
2149 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2150
2151 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002152}
Jerry Yu90f152d2022-01-29 22:12:42 +08002153#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002154
2155/*
Jerry Yu687101b2021-09-14 16:03:56 +08002156 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2157 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002158static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002159{
XiaokangQian0fa66432021-11-15 03:33:57 +00002160 int ret;
2161
2162 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2163 if( ret != 0 )
2164 return( ret );
2165
2166 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2167 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002168}
2169
2170/*
2171 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2172 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002173static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002174{
Jerry Yu378254d2021-10-30 21:44:47 +08002175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002176 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002177 return( 0 );
2178}
2179
2180/*
2181 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2182 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002183static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002184{
Jerry Yu378254d2021-10-30 21:44:47 +08002185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2186 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2187
2188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2189 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2190
2191 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2192
2193 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2194 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002195}
2196
Jerry Yu92c6b402021-08-27 16:59:09 +08002197int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002198{
Jerry Yu92c6b402021-08-27 16:59:09 +08002199 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002200
Jerry Yue3b34122021-09-28 17:53:35 +08002201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2202 mbedtls_ssl_states_str( ssl->state ),
2203 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002204
2205 switch( ssl->state )
2206 {
2207 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002208 * ssl->state is initialized as HELLO_REQUEST. It is the same
2209 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002210 */
2211 case MBEDTLS_SSL_HELLO_REQUEST:
2212 case MBEDTLS_SSL_CLIENT_HELLO:
2213 ret = ssl_tls13_write_client_hello( ssl );
2214 break;
2215
2216 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002217 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002218 break;
2219
2220 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002221 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002222 break;
2223
Jerry Yua93ac112021-10-27 16:31:48 +08002224#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002225 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2226 ret = ssl_tls13_process_certificate_request( ssl );
2227 break;
2228
Jerry Yu687101b2021-09-14 16:03:56 +08002229 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002230 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002231 break;
2232
2233 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002234 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002235 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002236#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002237
2238 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002239 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002240 break;
2241
Jerry Yu566c7812022-01-26 15:41:22 +08002242 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2243 ret = ssl_tls13_write_client_certificate( ssl );
2244 break;
2245
Ronald Cron9df7c802022-03-08 18:38:54 +01002246#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002247 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2248 ret = ssl_tls13_write_client_certificate_verify( ssl );
2249 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002250#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002251
Jerry Yu687101b2021-09-14 16:03:56 +08002252 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002253 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002254 break;
2255
2256 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002257 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002258 break;
2259
2260 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002261 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002262 break;
2263
Ronald Cron49ad6192021-11-24 16:25:31 +01002264 /*
2265 * Injection of dummy-CCS's for middlebox compatibility
2266 */
2267#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002268 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002269 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2270 if( ret == 0 )
2271 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2272 break;
2273
2274 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2275 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2276 if( ret == 0 )
2277 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002278 break;
2279#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2280
Jerry Yu92c6b402021-08-27 16:59:09 +08002281 default:
2282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2284 }
2285
2286 return( ret );
2287}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002288
Jerry Yufb4b6472022-01-27 15:03:26 +08002289#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002290
Jerry Yufb4b6472022-01-27 15:03:26 +08002291