blob: 5c07bc058adcd0cc2ce43dfa8d4e47431af6beed [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
33#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Gilles Peskine923d5c92021-12-15 12:56:54 +010035#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
52
Xiaofei Baid25fab62021-12-02 06:36:27 +000053 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Jerry Yu159c5a02021-08-31 12:51:25 +080055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu388bd0d2021-09-15 18:41:02 +080057 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080058 * - extension_type (2 bytes)
59 * - extension_data_length (2 bytes)
60 * - versions_length (1 byte )
61 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080062 */
Jerry Yu92c6b402021-08-27 16:59:09 +080063 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
64
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080065 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080067
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080068 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080069 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080073 *p++ = 0x2;
74
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080076 *
Jerry Yu0c63af62021-09-02 12:59:12 +080077 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080078 *
Jerry Yu0c63af62021-09-02 12:59:12 +080079 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080080 */
Jerry Yueecfbf02021-08-30 18:32:07 +080081 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
82 ssl->conf->max_minor_ver,
83 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080086 ssl->conf->max_major_ver,
87 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080088
Xiaofei Baid25fab62021-12-02 06:36:27 +000089 *out_len = 7;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Jerry Yub85277e2021-10-13 13:36:05 +0800100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
101 if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
Jerry Yue1b9c292021-09-10 10:08:31 +0800102 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
111 return( 0 );
112}
113
lhuang0486cacac2022-01-21 07:34:27 -0800114#if defined(MBEDTLS_SSL_ALPN)
115/*
Ronald Cron60ff7942022-03-09 13:56:48 +0100116 * ssl_tls13_write_alpn_ext()
117 *
118 * Structure of the application_layer_protocol_negotiation extension in
119 * ClientHello:
lhuang0486cacac2022-01-21 07:34:27 -0800120 *
121 * opaque ProtocolName<1..2^8-1>;
122 *
123 * struct {
124 * ProtocolName protocol_name_list<2..2^16-1>
125 * } ProtocolNameList;
126 *
127 */
128static int ssl_tls13_write_alpn_ext( mbedtls_ssl_context *ssl,
Ronald Cron60ff7942022-03-09 13:56:48 +0100129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *out_len )
lhuang0486cacac2022-01-21 07:34:27 -0800132{
133 unsigned char *p = buf;
lhuang0486cacac2022-01-21 07:34:27 -0800134
Ronald Cron60ff7942022-03-09 13:56:48 +0100135 *out_len = 0;
lhuang0486cacac2022-01-21 07:34:27 -0800136
137 if( ssl->conf->alpn_list == NULL )
138 return( 0 );
139
140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
141
lhuang0486cacac2022-01-21 07:34:27 -0800142
Ronald Cron13d8ea12022-03-09 10:48:18 +0100143 /* Check we have enough space for the extension type (2 bytes), the
144 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
145 */
146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
lhuang0486cacac2022-01-21 07:34:27 -0800147 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
Ronald Cron13d8ea12022-03-09 10:48:18 +0100148 /* Skip writing extension and list length for now */
149 p += 6;
lhuang0486cacac2022-01-21 07:34:27 -0800150
151 /*
152 * opaque ProtocolName<1..2^8-1>;
153 *
154 * struct {
155 * ProtocolName protocol_name_list<2..2^16-1>
156 * } ProtocolNameList;
157 */
Ronald Cron60ff7942022-03-09 13:56:48 +0100158 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
lhuang0486cacac2022-01-21 07:34:27 -0800159 {
160 /*
161 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
162 * protocol names is less than 255.
163 */
Ronald Cron60ff7942022-03-09 13:56:48 +0100164 size_t protocol_name_len = strlen( *cur );
165
Ronald Cron13d8ea12022-03-09 10:48:18 +0100166 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
167 *p++ = (unsigned char)protocol_name_len;
168 memcpy( p, *cur, protocol_name_len );
169 p += protocol_name_len;
lhuang0486cacac2022-01-21 07:34:27 -0800170 }
171
Ronald Cron60ff7942022-03-09 13:56:48 +0100172 *out_len = p - buf;
lhuang0486cacac2022-01-21 07:34:27 -0800173
Ronald Cron60ff7942022-03-09 13:56:48 +0100174 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
175 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
lhuang0486cacac2022-01-21 07:34:27 -0800176
Ronald Cron60ff7942022-03-09 13:56:48 +0100177 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
178 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
lhuang0486cacac2022-01-21 07:34:27 -0800179
180 return( 0 );
181}
182
183static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
184 const unsigned char *buf, size_t len )
185{
186 size_t list_len, name_len;
187 const unsigned char *p = buf;
188 const unsigned char *end = buf + len;
189
190 /* If we didn't send it, the server shouldn't send it */
191 if( ssl->conf->alpn_list == NULL )
192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
193
194 /*
195 * opaque ProtocolName<1..2^8-1>;
196 *
197 * struct {
198 * ProtocolName protocol_name_list<2..2^16-1>
199 * } ProtocolNameList;
200 *
201 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
202 */
203
204 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
205 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
206
207 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
208 p += 2;
209 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
210
211 name_len = *p++;
212 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
213
214 /* Check that the server chosen protocol was in our list and save it */
215 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
216 {
217 if( name_len == strlen( *alpn ) &&
218 memcmp( buf + 3, *alpn, name_len ) == 0 )
219 {
220 ssl->alpn_chosen = *alpn;
221 return( 0 );
222 }
223 }
224
225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
226}
227#endif /* MBEDTLS_SSL_ALPN */
228
Jerry Yubc20bdd2021-08-24 15:59:48 +0800229#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
230
XiaokangQian16acd4b2022-01-14 07:35:47 +0000231static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000232{
233 uint16_t group_id = ssl->handshake->offered_group_id;
234 if( group_id == 0 )
235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
236
XiaokangQian355e09a2022-01-20 11:14:50 +0000237#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000238 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000239 {
240 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
241 return( 0 );
242 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000243 else
244#endif /* MBEDTLS_ECDH_C */
245 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000246 {
247 /* Do something */
248 }
249
250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
251}
252
253/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 * Functions for writing key_share extension.
255 */
256#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800257static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800258 mbedtls_ssl_context *ssl,
259 uint16_t named_group,
260 unsigned char *buf,
261 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800263{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100264 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266 psa_key_attributes_t key_attributes;
267 size_t own_pubkey_len;
268 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
269 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100273 /* Convert EC group to PSA key type. */
274 if( ( handshake->ecdh_psa_type =
275 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
276 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800277
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100278 if( ecdh_bits > 0xffff )
279 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
280 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800281
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100282 key_attributes = psa_key_attributes_init();
283 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
284 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
285 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
286 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100287
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100288 /* Generate ECDH private key. */
289 status = psa_generate_key( &key_attributes,
290 &handshake->ecdh_psa_privkey );
291 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100293 ret = psa_ssl_status_to_mbedtls( status );
294 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100296
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 }
298
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100299 /* Export the public part of the ECDH private key from PSA. */
300 status = psa_export_public_key( handshake->ecdh_psa_privkey,
301 buf, (size_t)( end - buf ),
302 &own_pubkey_len );
303 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100305 ret = psa_ssl_status_to_mbedtls( status );
306 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100308
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309 }
310
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100311 if( own_pubkey_len > (size_t)( end - buf ) )
312 {
313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No space in the buffer for ECDH public key." ) );
314 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
315 }
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100316
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100317 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100318
Jerry Yu75336352021-09-01 15:59:36 +0800319 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800320}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321#endif /* MBEDTLS_ECDH_C */
322
Jerry Yub60e3cf2021-09-08 16:41:02 +0800323static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
324 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325{
326 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
327
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100330 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800331 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100332 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800333 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
334
Brett Warren14efd332021-10-06 09:32:11 +0100335 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800336 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000337 const mbedtls_ecp_curve_info *curve_info;
338 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
339 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100340 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800341 {
Brett Warren14efd332021-10-06 09:32:11 +0100342 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 return( 0 );
344 }
345 }
346#else
347 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800348 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800349#endif /* MBEDTLS_ECDH_C */
350
351 /*
352 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800353 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354 */
355
356 return( ret );
357}
358
359/*
360 * ssl_tls13_write_key_share_ext
361 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800362 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800363 *
364 * struct {
365 * NamedGroup group;
366 * opaque key_exchange<1..2^16-1>;
367 * } KeyShareEntry;
368 * struct {
369 * KeyShareEntry client_shares<0..2^16-1>;
370 * } KeyShareClientHello;
371 */
372static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
373 unsigned char *buf,
374 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000375 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800376{
377 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000378 unsigned char *client_shares; /* Start of client_shares */
379 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800380 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
382
Xiaofei Baid25fab62021-12-02 06:36:27 +0000383 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800384
Jerry Yub60e3cf2021-09-08 16:41:02 +0800385 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800386 * - extension_type (2 bytes)
387 * - extension_data_length (2 bytes)
388 * - client_shares_length (2 bytes)
389 */
390 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
391 p += 6;
392
393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
394
395 /* HRR could already have requested something else. */
396 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800397 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
398 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800399 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800400 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800401 &group_id ) );
402 }
403
404 /*
405 * Dispatch to type-specific key generation function.
406 *
407 * So far, we're only supporting ECDHE. With the introduction
408 * of PQC KEMs, we'll want to have multiple branches, one per
409 * type of KEM, and dispatch to the corresponding crypto. And
410 * only one key share entry is allowed.
411 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000412 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800413#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800414 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800415 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800416 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000417 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800418 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100419 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800420
421 /* Check there is space for header of KeyShareEntry
422 * - group (2 bytes)
423 * - key_exchange_length (2 bytes)
424 */
425 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
426 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100427 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800428 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800429 p += key_exchange_len;
430 if( ret != 0 )
431 return( ret );
432
433 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000434 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800435 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000436 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800437 }
438 else
439#endif /* MBEDTLS_ECDH_C */
440 if( 0 /* other KEMs? */ )
441 {
442 /* Do something */
443 }
444 else
445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
446
Jerry Yub60e3cf2021-09-08 16:41:02 +0800447 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000448 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800449 if( client_shares_len == 0)
450 {
451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800453 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800454 /* Write extension_type */
455 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
456 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800457 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800458 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800459 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800460
461 /* Update offered_group_id field */
462 ssl->handshake->offered_group_id = group_id;
463
464 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000465 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800466
Xiaofei Baid25fab62021-12-02 06:36:27 +0000467 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800468
469 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
470
471cleanup:
472
473 return( ret );
474}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800475
Jerry Yue1b9c292021-09-10 10:08:31 +0800476#if defined(MBEDTLS_ECDH_C)
477
Jerry Yuc068b662021-10-11 22:30:19 +0800478static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
479 const unsigned char *buf,
480 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800481{
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100482 uint8_t *p = (uint8_t*)buf;
483 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800484
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100485 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
486 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
487 p += 2;
Jerry Yub85277e2021-10-13 13:36:05 +0800488
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100489 /* Check if key size is consistent with given buffer length. */
490 if ( peerkey_len > ( buf_len - 2 ) )
491 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800492
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100493 /* Store peer's ECDH public key. */
Przemyslaw Stekiel0f5ecef2022-02-14 17:10:05 +0100494 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100495 handshake->ecdh_psa_peerkey_len = peerkey_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800496
497 return( 0 );
498}
Jerry Yu4a173382021-10-11 21:45:31 +0800499#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800500
XiaokangQiand59be772022-01-24 10:12:51 +0000501/*
502 * ssl_tls13_parse_hrr_key_share_ext()
503 * Parse key_share extension in Hello Retry Request
504 *
505 * struct {
506 * NamedGroup selected_group;
507 * } KeyShareHelloRetryRequest;
508 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000509static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000510 const unsigned char *buf,
511 const unsigned char *end )
512{
XiaokangQianb851da82022-01-14 04:03:11 +0000513 const mbedtls_ecp_curve_info *curve_info = NULL;
514 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000515 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000516 int found = 0;
517
518 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
519 if( group_list == NULL )
520 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
521
522 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
523
524 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000525 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000526 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000528
529 /* Upon receipt of this extension in a HelloRetryRequest, the client
530 * MUST first verify that the selected_group field corresponds to a
531 * group which was provided in the "supported_groups" extension in the
532 * original ClientHello.
533 * The supported_group was based on the info in ssl->conf->group_list.
534 *
535 * If the server provided a key share that was not sent in the ClientHello
536 * then the client MUST abort the handshake with an "illegal_parameter" alert.
537 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000538 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000539 {
540 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000541 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000542 continue;
543
544 /* We found a match */
545 found = 1;
546 break;
547 }
548
549 /* Client MUST verify that the selected_group field does not
550 * correspond to a group which was provided in the "key_share"
551 * extension in the original ClientHello. If the server sent an
552 * HRR message with a key share already provided in the
553 * ClientHello then the client MUST abort the handshake with
554 * an "illegal_parameter" alert.
555 */
XiaokangQiand59be772022-01-24 10:12:51 +0000556 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000557 {
558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
559 MBEDTLS_SSL_PEND_FATAL_ALERT(
560 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
561 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
562 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
563 }
564
565 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000566 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000567
568 return( 0 );
569}
570
Jerry Yue1b9c292021-09-10 10:08:31 +0800571/*
Jerry Yub85277e2021-10-13 13:36:05 +0800572 * ssl_tls13_parse_key_share_ext()
573 * Parse key_share extension in Server Hello
574 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800575 * struct {
576 * KeyShareEntry server_share;
577 * } KeyShareServerHello;
578 * struct {
579 * NamedGroup group;
580 * opaque key_exchange<1..2^16-1>;
581 * } KeyShareEntry;
582 */
Jerry Yu4a173382021-10-11 21:45:31 +0800583static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800584 const unsigned char *buf,
585 const unsigned char *end )
586{
Jerry Yub85277e2021-10-13 13:36:05 +0800587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800588 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800589 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800590
Jerry Yu4a173382021-10-11 21:45:31 +0800591 /* ...
592 * NamedGroup group; (2 bytes)
593 * ...
594 */
595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
596 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800597 p += 2;
598
Jerry Yu4a173382021-10-11 21:45:31 +0800599 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800600 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800601 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800602 {
603 MBEDTLS_SSL_DEBUG_MSG( 1,
604 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800605 (unsigned) offered_group, (unsigned) group ) );
606 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
607 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
608 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800609 }
610
611#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800612 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800613 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100614 const mbedtls_ecp_curve_info *curve_info =
615 mbedtls_ecp_curve_info_from_tls_id( group );
616 if( curve_info == NULL )
617 {
618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
620 }
621
622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
623
Jerry Yuc068b662021-10-11 22:30:19 +0800624 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800625 if( ret != 0 )
626 return( ret );
627 }
Jerry Yub85277e2021-10-13 13:36:05 +0800628 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800629#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800630 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800631 {
632 /* Do something */
633 }
634 else
635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
636
637 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
638 return( ret );
639}
640
Jerry Yubc20bdd2021-08-24 15:59:48 +0800641#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
642
XiaokangQiand59be772022-01-24 10:12:51 +0000643/*
644 * ssl_tls13_parse_cookie_ext()
645 * Parse cookie extension in Hello Retry Request
646 *
647 * struct {
648 * opaque cookie<1..2^16-1>;
649 * } Cookie;
650 *
651 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
652 * extension to the client (this is an exception to the usual rule that
653 * the only extensions that may be sent are those that appear in the
654 * ClientHello). When sending the new ClientHello, the client MUST copy
655 * the contents of the extension received in the HelloRetryRequest into
656 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
657 * cookies in their initial ClientHello in subsequent connections.
658 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000659static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
660 const unsigned char *buf,
661 const unsigned char *end )
662{
XiaokangQian25c9c902022-02-08 10:49:53 +0000663 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000664 const unsigned char *p = buf;
665 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
666
667 /* Retrieve length field of cookie */
668 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
669 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
670 p += 2;
671
672 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
673 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
674
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000675 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000676 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000677 handshake->cookie = mbedtls_calloc( 1, cookie_len );
678 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000679 {
680 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000681 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000682 cookie_len ) );
683 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
684 }
685
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000686 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000687 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000688
689 return( 0 );
690}
XiaokangQian43550bd2022-01-21 04:32:58 +0000691
XiaokangQian0b64eed2022-01-27 10:36:51 +0000692static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000693 unsigned char *buf,
694 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000695 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000696{
697 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000698 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000699 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000700
XiaokangQianc02768a2022-02-10 07:31:25 +0000701 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000702 {
703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
704 return( 0 );
705 }
706
707 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000708 handshake->cookie,
709 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000710
XiaokangQianc02768a2022-02-10 07:31:25 +0000711 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000712
713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
714
XiaokangQian0b64eed2022-01-27 10:36:51 +0000715 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000716 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
717 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000718 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000719
720 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000721 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000722
XiaokangQianc02768a2022-02-10 07:31:25 +0000723 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000724
725 return( 0 );
726}
727
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800728/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800729 * CipherSuite cipher_suites<2..2^16-2>;
730 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800731static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800732 mbedtls_ssl_context *ssl,
733 unsigned char *buf,
734 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000735 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800736{
Jerry Yufec982e2021-09-07 17:26:06 +0800737 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800738 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000739 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800740 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800741
Xiaofei Baid25fab62021-12-02 06:36:27 +0000742 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800743
744 /*
745 * Ciphersuite list
746 *
747 * This is a list of the symmetric cipher options supported by
748 * the client, specifically the record protection algorithm
749 * ( including secret key length ) and a hash to be used with
750 * HKDF, in descending order of client preference.
751 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800752 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800753
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800754 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800755 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
756 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800757
Jerry Yu0c63af62021-09-02 12:59:12 +0800758 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000759 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800760 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800761 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800762 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800763 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800764
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800765 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800766 if( ciphersuite_info == NULL )
767 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800768 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
769 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800770 continue;
771
772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800773 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800774 ciphersuite_info->name ) );
775
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800776 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800777 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
778 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
779 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800780 }
781
Jerry Yu0c63af62021-09-02 12:59:12 +0800782 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000783 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800784 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800785 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800786 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
787 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800788
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800789 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000790 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800791
Jerry Yu6a643102021-08-31 14:40:36 +0800792 return( 0 );
793}
794
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800795/*
796 * Structure of ClientHello message:
797 *
798 * struct {
799 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
800 * Random random;
801 * opaque legacy_session_id<0..32>;
802 * CipherSuite cipher_suites<2..2^16-2>;
803 * opaque legacy_compression_methods<1..2^8-1>;
804 * Extension extensions<8..2^16-1>;
805 * } ClientHello;
806 */
Jerry Yu08906d02021-08-31 11:05:27 +0800807static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800808 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800809 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000810 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800811{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800812
Jerry Yubc20bdd2021-08-24 15:59:48 +0800813 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000814 unsigned char *p_extensions_len; /* Pointer to extensions length */
815 size_t output_len; /* Length of buffer used by function */
816 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800817
Jerry Yubc20bdd2021-08-24 15:59:48 +0800818 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800819 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800820
Xiaofei Baid25fab62021-12-02 06:36:27 +0000821 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800822
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800823 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824 ssl->major_ver = ssl->conf->min_major_ver;
825 ssl->minor_ver = ssl->conf->min_minor_ver;
826
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800827 /*
828 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800829 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800830 *
831 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800832 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800833 */
Jerry Yufec982e2021-09-07 17:26:06 +0800834 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800835 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800836 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800837
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800838 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800839 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
840 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800841 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800842 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
843 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800844
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800845 /*
846 * Write legacy_session_id
847 *
848 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
849 * which has been merged with pre-shared keys in this version. A client
850 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
851 * this field to that value. In compatibility mode, this field MUST be
852 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
853 * a new 32-byte value. This value need not be random but SHOULD be
854 * unpredictable to avoid implementations fixating on a specific value
855 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
856 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800857 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100858#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
859 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
860 *p++ = (unsigned char)ssl->session_negotiate->id_len;
861 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
862 p += ssl->session_negotiate->id_len;
863
864 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
865 ssl->session_negotiate->id_len );
866#else
Jerry Yubbe09522021-09-06 21:17:54 +0800867 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
868 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100869#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800870
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800871 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800872 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800873 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800874 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800875 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800876
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800877 /* Write legacy_compression_methods
878 *
879 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800880 * one byte set to zero, which corresponds to the 'null' compression
881 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800882 */
Jerry Yubbe09522021-09-06 21:17:54 +0800883 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
884 *p++ = 1;
885 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800886
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800887 /* Write extensions */
888
889 /* Keeping track of the included extensions */
890 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800891
892 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800893 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000894 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800895 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800896
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800897 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800898 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800899 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800900 */
Jerry Yubbe09522021-09-06 21:17:54 +0800901 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800902 if( ret != 0 )
903 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800904 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800905
lhuang0486cacac2022-01-21 07:34:27 -0800906#if defined(MBEDTLS_SSL_ALPN)
Ronald Crona0855a62022-03-09 10:04:54 +0100907 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
lhuang0486cacac2022-01-21 07:34:27 -0800908 if( ret != 0 )
909 return( ret );
910 p += output_len;
911#endif /* MBEDTLS_SSL_ALPN */
912
XiaokangQian233397e2022-02-07 08:32:16 +0000913 /* Echo the cookie if the server provided one in its preceding
914 * HelloRetryRequest message.
915 */
XiaokangQian0b64eed2022-01-27 10:36:51 +0000916 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &output_len );
917 if( ret != 0 )
918 return( ret );
919 p += output_len;
920
Jerry Yubc20bdd2021-08-24 15:59:48 +0800921#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800922
Jerry Yub925f212022-01-12 11:17:02 +0800923 /*
924 * Add the extensions related to (EC)DHE ephemeral key establishment only if
925 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800926 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800927 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
928 {
929 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
930 if( ret != 0 )
931 return( ret );
932 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800933
Jerry Yuf46b0162022-01-11 16:28:00 +0800934 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
935 if( ret != 0 )
936 return( ret );
937 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800938
Jerry Yuf017ee42022-01-12 15:49:48 +0800939 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800940 if( ret != 0 )
941 return( ret );
942 p += output_len;
943 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800944#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
945
Xiaofei Bai15a56812021-11-05 10:52:12 +0000946#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000947 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000948 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
949 if( ret != 0 )
950 return( ret );
951 p += output_len;
952#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
953
Jerry Yubc20bdd2021-08-24 15:59:48 +0800954 /* Add more extensions here */
955
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800956 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000957 extensions_len = p - p_extensions_len - 2;
958 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800960 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000961 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800962
Xiaofei Baid25fab62021-12-02 06:36:27 +0000963 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800964 return( 0 );
965}
966
Jerry Yu92c6b402021-08-27 16:59:09 +0800967static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
968{
969 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800970
Jerry Yu92c6b402021-08-27 16:59:09 +0800971 if( ssl->conf->f_rng == NULL )
972 {
973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
974 return( MBEDTLS_ERR_SSL_NO_RNG );
975 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800976
Jerry Yu92c6b402021-08-27 16:59:09 +0800977 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
978 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800979 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800980 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800981 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800982 return( ret );
983 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800984
Ronald Cron49ad6192021-11-24 16:25:31 +0100985#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
986 /*
987 * Create a session identifier for the purpose of middlebox compatibility
988 * only if one has not been created already.
989 */
990 if( ssl->session_negotiate->id_len == 0 )
991 {
992 /* Creating a session id with 32 byte length */
993 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
994 ssl->session_negotiate->id, 32 ) ) != 0 )
995 {
996 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
997 return( ret );
998 }
999 ssl->session_negotiate->id_len = 32;
1000 }
1001#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1002
Jerry Yu6f13f642021-08-26 17:18:15 +08001003 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +08001004}
1005
Jerry Yu92c6b402021-08-27 16:59:09 +08001006/*
Jerry Yu159c5a02021-08-31 12:51:25 +08001007 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001008 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +08001009 */
1010static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001011{
Jerry Yu92c6b402021-08-27 16:59:09 +08001012 int ret = 0;
1013 unsigned char *buf;
1014 size_t buf_len, msg_len;
1015
1016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1017
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001018 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001019
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001020 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
1021 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1022 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001023
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001024 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +08001025 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001026 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001027
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001028 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
1029 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +08001030 msg_len );
1031 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +08001032
Jerry Yu2c0fbf32021-09-02 13:53:46 +08001033 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1034 buf_len,
1035 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001036
Ronald Cron3addfa42022-02-08 16:10:25 +01001037 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1038
Jerry Yu92c6b402021-08-27 16:59:09 +08001039cleanup:
1040
1041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1042 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001043}
1044
Jerry Yu687101b2021-09-14 16:03:56 +08001045/*
Jerry Yu4a173382021-10-11 21:45:31 +08001046 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001047 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001048/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001049 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1050 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001051 * to indicate which message is expected and to be parsed next.
1052 */
Jerry Yub85277e2021-10-13 13:36:05 +08001053#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1054#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1055static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1056 const unsigned char *buf,
1057 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001058{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001059 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1061 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1062 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1063 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1064
1065 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1066 *
Jerry Yu4a173382021-10-11 21:45:31 +08001067 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 * special value of the SHA-256 of "HelloRetryRequest".
1069 *
1070 * struct {
1071 * ProtocolVersion legacy_version = 0x0303;
1072 * Random random;
1073 * opaque legacy_session_id_echo<0..32>;
1074 * CipherSuite cipher_suite;
1075 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001076 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001077 * } ServerHello;
1078 *
1079 */
Jerry Yub85277e2021-10-13 13:36:05 +08001080 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001081
1082 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001083 {
Jerry Yub85277e2021-10-13 13:36:05 +08001084 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 }
1086
Jerry Yub85277e2021-10-13 13:36:05 +08001087 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001088}
1089
Jerry Yu745bb612021-10-13 22:01:04 +08001090/* Fetch and preprocess
1091 * Returns a negative value on failure, and otherwise
1092 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1093 * - SSL_SERVER_HELLO_COORDINATE_HRR
1094 */
Jerry Yub85277e2021-10-13 13:36:05 +08001095static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1096 unsigned char **buf,
1097 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001098{
Jerry Yu4a173382021-10-11 21:45:31 +08001099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001100
XiaokangQian355e09a2022-01-20 11:14:50 +00001101 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1102 MBEDTLS_SSL_HS_SERVER_HELLO,
1103 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
Jerry Yub85277e2021-10-13 13:36:05 +08001105 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1106 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001107 {
Jerry Yu745bb612021-10-13 22:01:04 +08001108 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1110 break;
1111 case SSL_SERVER_HELLO_COORDINATE_HRR:
1112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001113 /* If a client receives a second
1114 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1115 * was itself in response to a HelloRetryRequest), it MUST abort the
1116 * handshake with an "unexpected_message" alert.
1117 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001118 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001119 {
1120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1121 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1122 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1123 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1124 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001125 /*
1126 * Clients must abort the handshake with an "illegal_parameter"
1127 * alert if the HelloRetryRequest would not result in any change
1128 * in the ClientHello.
1129 * In a PSK only key exchange that what we expect.
1130 */
1131 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1132 {
1133 MBEDTLS_SSL_DEBUG_MSG( 1,
1134 ( "Unexpected HRR in pure PSK key exchange." ) );
1135 MBEDTLS_SSL_PEND_FATAL_ALERT(
1136 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1137 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1138 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1139 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001140
XiaokangQiand9e068e2022-01-18 06:23:32 +00001141 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001142
Jerry Yu745bb612021-10-13 22:01:04 +08001143 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001144 }
1145
1146cleanup:
1147
1148 return( ret );
1149}
1150
Jerry Yu4a173382021-10-11 21:45:31 +08001151static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1152 const unsigned char **buf,
1153 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001154{
1155 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001156 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001157
Jerry Yude4fb2c2021-09-19 18:05:08 +08001158 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001159 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001160
Jerry Yu4a173382021-10-11 21:45:31 +08001161 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001162
1163 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001164 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1165 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001166 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1168 ssl->session_negotiate->id,
1169 ssl->session_negotiate->id_len );
1170 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001171 legacy_session_id_echo_len );
1172
1173 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1174 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1175
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1177 }
1178
Jerry Yu4a173382021-10-11 21:45:31 +08001179 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 *buf = p;
1181
1182 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001183 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 return( 0 );
1185}
1186
Jerry Yu4a173382021-10-11 21:45:31 +08001187static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1188 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001189{
Jerry Yu4a173382021-10-11 21:45:31 +08001190 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1191
Jerry Yue1b9c292021-09-10 10:08:31 +08001192 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001193 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001194 {
Jerry Yu4a173382021-10-11 21:45:31 +08001195 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001196 {
1197 return( 1 );
1198 }
1199 }
1200 return( 0 );
1201}
1202
1203/* Parse ServerHello message and configure context
1204 *
1205 * struct {
1206 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1207 * Random random;
1208 * opaque legacy_session_id_echo<0..32>;
1209 * CipherSuite cipher_suite;
1210 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001211 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001212 * } ServerHello;
1213 */
Jerry Yuc068b662021-10-11 22:30:19 +08001214static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1215 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001216 const unsigned char *end,
1217 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001218{
Jerry Yub85277e2021-10-13 13:36:05 +08001219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001221 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001222 size_t extensions_len;
1223 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001225 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001226 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001227 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001228
1229 /*
1230 * Check there is space for minimal fields
1231 *
1232 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001233 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001234 * - legacy_session_id_echo ( 1 byte ), minimum size
1235 * - cipher_suite ( 2 bytes)
1236 * - legacy_compression_method ( 1 byte )
1237 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001238 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001239
1240 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001241 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1242
Jerry Yu4a173382021-10-11 21:45:31 +08001243 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001244 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001245 * ...
1246 * with ProtocolVersion defined as:
1247 * uint16 ProtocolVersion;
1248 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001249 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1250 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1251 {
1252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1253 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1254 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001255 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1256 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001257 }
1258 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001259
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001260 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001261 * Random random;
1262 * ...
1263 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001264 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001265 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001266 if( !is_hrr )
1267 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001268 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001269 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1270 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1271 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1272 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001273 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001274
Jerry Yu4a173382021-10-11 21:45:31 +08001275 /* ...
1276 * opaque legacy_session_id_echo<0..32>;
1277 * ...
1278 */
1279 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 {
XiaokangQian52da5582022-01-26 09:49:29 +00001281 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001282 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001283 }
1284
Jerry Yu4a173382021-10-11 21:45:31 +08001285 /* ...
1286 * CipherSuite cipher_suite;
1287 * ...
1288 * with CipherSuite defined as:
1289 * uint8 CipherSuite[2];
1290 */
1291 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001292 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1293 p += 2;
1294
Jerry Yu4a173382021-10-11 21:45:31 +08001295
XiaokangQian355e09a2022-01-20 11:14:50 +00001296 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001297 /*
1298 * Check whether this ciphersuite is supported and offered.
1299 * Via the force_ciphersuite version we may have instructed the client
1300 * to use a different ciphersuite.
1301 */
Jerry Yu4a173382021-10-11 21:45:31 +08001302 if( ciphersuite_info == NULL ||
1303 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001304 {
XiaokangQian52da5582022-01-26 09:49:29 +00001305 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001306 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001307 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001308 * If we received an HRR before and that the proposed selected
1309 * ciphersuite in this server hello is not the same as the one
1310 * proposed in the HRR, we abort the handshake and send an
1311 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001312 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001313 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1314 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001315 {
XiaokangQian52da5582022-01-26 09:49:29 +00001316 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001317 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001318
XiaokangQian52da5582022-01-26 09:49:29 +00001319 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001320 {
1321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1322 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001323 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001324 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001325
Jerry Yu4a173382021-10-11 21:45:31 +08001326 /* Configure ciphersuites */
1327 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1328
XiaokangQian355e09a2022-01-20 11:14:50 +00001329 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001330 ssl->session_negotiate->ciphersuite = cipher_suite;
1331
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1333 cipher_suite, ciphersuite_info->name ) );
1334
1335#if defined(MBEDTLS_HAVE_TIME)
1336 ssl->session_negotiate->start = time( NULL );
1337#endif /* MBEDTLS_HAVE_TIME */
1338
Jerry Yu4a173382021-10-11 21:45:31 +08001339 /* ...
1340 * uint8 legacy_compression_method = 0;
1341 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001342 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001343 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001344 if( p[0] != 0 )
1345 {
Jerry Yub85277e2021-10-13 13:36:05 +08001346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001347 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001348 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001349 }
1350 p++;
1351
Jerry Yub85277e2021-10-13 13:36:05 +08001352 /* ...
1353 * Extension extensions<6..2^16-1>;
1354 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001355 * struct {
1356 * ExtensionType extension_type; (2 bytes)
1357 * opaque extension_data<0..2^16-1>;
1358 * } Extension;
1359 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001360 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001361 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001362 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001363
Jerry Yu4a173382021-10-11 21:45:31 +08001364 /* Check extensions do not go beyond the buffer of data. */
1365 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1366 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001367
Jerry Yu4a173382021-10-11 21:45:31 +08001368 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001369
Jerry Yu4a173382021-10-11 21:45:31 +08001370 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001371 {
1372 unsigned int extension_type;
1373 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001374 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001375
Jerry Yu4a173382021-10-11 21:45:31 +08001376 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001377 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1378 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1379 p += 4;
1380
Jerry Yu4a173382021-10-11 21:45:31 +08001381 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001382 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001383
1384 switch( extension_type )
1385 {
XiaokangQianb851da82022-01-14 04:03:11 +00001386 case MBEDTLS_TLS_EXT_COOKIE:
1387
XiaokangQiand9e068e2022-01-18 06:23:32 +00001388 if( !is_hrr )
1389 {
XiaokangQian52da5582022-01-26 09:49:29 +00001390 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001391 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001392 }
1393
XiaokangQian43550bd2022-01-21 04:32:58 +00001394 ret = ssl_tls13_parse_cookie_ext( ssl,
1395 p, extension_data_end );
1396 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001397 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001398 MBEDTLS_SSL_DEBUG_RET( 1,
1399 "ssl_tls13_parse_cookie_ext",
1400 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001401 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001402 }
XiaokangQianb851da82022-01-14 04:03:11 +00001403 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001404
Jerry Yue1b9c292021-09-10 10:08:31 +08001405 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001406 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001407 MBEDTLS_SSL_DEBUG_MSG( 3,
1408 ( "found supported_versions extension" ) );
1409
Jerry Yuc068b662021-10-11 22:30:19 +08001410 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001411 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001412 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001413 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001414 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001415 break;
1416
1417 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001420
XiaokangQian52da5582022-01-26 09:49:29 +00001421 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001422 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001423
1424#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1425 case MBEDTLS_TLS_EXT_KEY_SHARE:
1426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001427 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1428 {
XiaokangQian52da5582022-01-26 09:49:29 +00001429 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001430 goto cleanup;
1431 }
1432
XiaokangQian53f20b72022-01-18 10:47:33 +00001433 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001434 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001435 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001436 else
XiaokangQianb851da82022-01-14 04:03:11 +00001437 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001438 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001439 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001440 {
1441 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001442 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001443 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001444 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001445 }
1446 break;
1447#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1448
1449 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001450 MBEDTLS_SSL_DEBUG_MSG(
1451 3,
1452 ( "unknown extension found: %u ( ignoring )",
1453 extension_type ) );
1454
XiaokangQian52da5582022-01-26 09:49:29 +00001455 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001456 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001457 }
1458
1459 p += extension_data_len;
1460 }
1461
XiaokangQian78b1fa72022-01-19 06:56:30 +00001462 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001463 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001465 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001466 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001467 }
1468
XiaokangQiand59be772022-01-24 10:12:51 +00001469cleanup:
1470
XiaokangQian52da5582022-01-26 09:49:29 +00001471 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001472 {
1473 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1474 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1475 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1476 }
XiaokangQian52da5582022-01-26 09:49:29 +00001477 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001478 {
1479 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1480 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1481 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1482 }
1483 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001484}
1485
XiaokangQian355e09a2022-01-20 11:14:50 +00001486static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001487{
Jerry Yub85277e2021-10-13 13:36:05 +08001488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001489 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001490 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001491 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001492
Jerry Yub85277e2021-10-13 13:36:05 +08001493 /* Determine the key exchange mode:
1494 * 1) If both the pre_shared_key and key_share extensions were received
1495 * then the key exchange mode is PSK with EPHEMERAL.
1496 * 2) If only the pre_shared_key extension was received then the key
1497 * exchange mode is PSK-only.
1498 * 3) If only the key_share extension was received then the key
1499 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001500 */
Jerry Yub85277e2021-10-13 13:36:05 +08001501 switch( handshake->extensions_present &
1502 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001503 {
Jerry Yu745bb612021-10-13 22:01:04 +08001504 /* Only the pre_shared_key extension was received */
1505 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001506 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001507 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001508
Jerry Yu745bb612021-10-13 22:01:04 +08001509 /* Only the key_share extension was received */
1510 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001511 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001512 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001513
Jerry Yu745bb612021-10-13 22:01:04 +08001514 /* Both the pre_shared_key and key_share extensions were received */
1515 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001516 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001517 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001518
Jerry Yu745bb612021-10-13 22:01:04 +08001519 /* Neither pre_shared_key nor key_share extension was received */
1520 default:
1521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1522 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1523 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001524 }
1525
1526 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1527 *
1528 * TODO: We don't have to do this in case we offered 0-RTT and the
1529 * server accepted it. In this case, we could skip generating
1530 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001531 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001532 if( ret != 0 )
1533 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001535 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001536 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001537 }
1538
1539 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001540 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001541 if( ret != 0 )
1542 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001544 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001545 }
1546
1547 /* Next evolution in key schedule: Establish handshake secret and
1548 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001549 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001550 if( ret != 0 )
1551 {
Jerry Yuc068b662021-10-11 22:30:19 +08001552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1553 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001554 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001555 }
1556
Jerry Yub85277e2021-10-13 13:36:05 +08001557 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001558 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001559 {
1560 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1561 goto cleanup;
1562 }
Jerry Yu0b177842021-09-05 19:41:30 +08001563
1564 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1565 ssl->conf->endpoint,
1566 ssl->session_negotiate->ciphersuite,
1567 &traffic_keys,
1568 ssl );
1569 if( ret != 0 )
1570 {
1571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001572 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001573 }
1574
Jerry Yub85277e2021-10-13 13:36:05 +08001575 handshake->transform_handshake = transform_handshake;
1576 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001577
1578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1579 ssl->session_in = ssl->session_negotiate;
1580
1581 /*
1582 * State machine update
1583 */
1584 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1585
Jerry Yu4a173382021-10-11 21:45:31 +08001586cleanup:
1587
Jerry Yu0b177842021-09-05 19:41:30 +08001588 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001589 if( ret != 0 )
1590 {
Jerry Yub85277e2021-10-13 13:36:05 +08001591 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001592
Jerry Yu4a173382021-10-11 21:45:31 +08001593 MBEDTLS_SSL_PEND_FATAL_ALERT(
1594 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1595 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1596 }
1597 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001598}
1599
XiaokangQian355e09a2022-01-20 11:14:50 +00001600static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001601{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001602#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1604
XiaokangQian647719a2021-12-07 09:16:29 +00001605#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1606 /* If not offering early data, the client sends a dummy CCS record
1607 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001608 * its second ClientHello or before its encrypted handshake flight.
1609 */
XiaokangQian647719a2021-12-07 09:16:29 +00001610 mbedtls_ssl_handshake_set_state( ssl,
1611 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1612#else
1613 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1614#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1615
XiaokangQian78b1fa72022-01-19 06:56:30 +00001616 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001617
XiaokangQian78b1fa72022-01-19 06:56:30 +00001618 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001619 * We are going to re-generate a shared secret corresponding to the group
1620 * selected by the server, which is different from the group for which we
1621 * generated a shared secret in the first client hello.
1622 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001623 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001624 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001625 if( ret != 0 )
1626 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001627#else
1628 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001629#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001630
1631 return( 0 );
1632}
1633
Jerry Yue1b9c292021-09-10 10:08:31 +08001634/*
Jerry Yu4a173382021-10-11 21:45:31 +08001635 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001636 * Handler for MBEDTLS_SSL_SERVER_HELLO
1637 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001638static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001639{
Jerry Yu4a173382021-10-11 21:45:31 +08001640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001641 unsigned char *buf = NULL;
1642 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001643 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001644
1645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1646
1647 /* Coordination step
1648 * - Fetch record
1649 * - Make sure it's either a ServerHello or a HRR.
1650 * - Switch processing routine in case of HRR
1651 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001652 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1653 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1654
XiaokangQian16acd4b2022-01-14 07:35:47 +00001655 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001656 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001657 goto cleanup;
1658 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001659 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001660
XiaokangQianb851da82022-01-14 04:03:11 +00001661 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001662 buf + buf_len,
1663 is_hrr ) );
1664 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001665 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1666
XiaokangQianb851da82022-01-14 04:03:11 +00001667 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1668 MBEDTLS_SSL_HS_SERVER_HELLO,
1669 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001670
XiaokangQiand9e068e2022-01-18 06:23:32 +00001671 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001672 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001673 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001674 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001675
1676cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1678 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001679 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001680}
1681
1682/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001683 *
1684 * EncryptedExtensions message
1685 *
1686 * The EncryptedExtensions message contains any extensions which
1687 * should be protected, i.e., any which are not needed to establish
1688 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001689 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001690
1691/*
1692 * Overview
1693 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001694
1695/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001696static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001697
XiaokangQian97799ac2021-10-11 10:05:54 +00001698static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1699 const unsigned char *buf,
1700 const unsigned char *end );
1701static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001702
1703/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001704 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001705 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001706static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001707{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001708 int ret;
1709 unsigned char *buf;
1710 size_t buf_len;
1711
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1713
Xiaofei Bai746f9482021-11-12 08:53:56 +00001714 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001715 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001716 &buf, &buf_len ) );
1717
1718 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001719 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001720 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001721
Xiaofei Bai746f9482021-11-12 08:53:56 +00001722 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001723 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001724
XiaokangQian97799ac2021-10-11 10:05:54 +00001725 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001726
1727cleanup:
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1730 return( ret );
1731
1732}
1733
XiaokangQian08da26c2021-10-09 10:12:11 +00001734/* Parse EncryptedExtensions message
1735 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001736 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001737 * } EncryptedExtensions;
1738 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001739static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1740 const unsigned char *buf,
1741 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001742{
1743 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001744 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001745 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001746 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001747
XiaokangQian08da26c2021-10-09 10:12:11 +00001748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001749 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001750 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001751
XiaokangQian97799ac2021-10-11 10:05:54 +00001752 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001753 extensions_end = p + extensions_len;
1754 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001755
XiaokangQian8db25ff2021-10-13 05:56:18 +00001756 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001757 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001758 unsigned int extension_type;
1759 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001760
XiaokangQian08da26c2021-10-09 10:12:11 +00001761 /*
1762 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001763 * ExtensionType extension_type; (2 bytes)
1764 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001765 * } Extension;
1766 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001767 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001768 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1769 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1770 p += 4;
1771
XiaokangQian8db25ff2021-10-13 05:56:18 +00001772 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001773
XiaokangQian97799ac2021-10-11 10:05:54 +00001774 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001775 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001776 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001777 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001778 switch( extension_type )
1779 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001780
XiaokangQian08da26c2021-10-09 10:12:11 +00001781 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1783 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784
lhuang0486cacac2022-01-21 07:34:27 -08001785#if defined(MBEDTLS_SSL_ALPN)
1786 case MBEDTLS_TLS_EXT_ALPN:
1787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1788
1789 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1790 {
1791 return( ret );
1792 }
1793
1794 break;
1795#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001796 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001797 MBEDTLS_SSL_DEBUG_MSG(
1798 3, ( "unsupported extension found: %u ", extension_type) );
1799 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001800 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001801 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1802 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001803 }
1804
XiaokangQian08da26c2021-10-09 10:12:11 +00001805 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001806 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001807
XiaokangQian97799ac2021-10-11 10:05:54 +00001808 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001809 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001810 {
1811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001812 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001813 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001814 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001815 }
1816
1817 return( ret );
1818}
1819
XiaokangQian97799ac2021-10-11 10:05:54 +00001820static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001821{
Jerry Yua93ac112021-10-27 16:31:48 +08001822#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001823 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001824 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1825 else
Jerry Yud2674312021-10-29 10:08:19 +08001826 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001827#else
1828 ((void) ssl);
1829 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1830#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001831 return( 0 );
1832}
1833
Jerry Yua93ac112021-10-27 16:31:48 +08001834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001835/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001836 *
1837 * STATE HANDLING: CertificateRequest
1838 *
Jerry Yud2674312021-10-29 10:08:19 +08001839 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001840#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1841#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001842/* Coordination:
1843 * Deals with the ambiguity of not knowing if a CertificateRequest
1844 * will be sent. Returns a negative code on failure, or
1845 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1846 * - SSL_CERTIFICATE_REQUEST_SKIP
1847 * indicating if a Certificate Request is expected or not.
1848 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001849static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001850{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001852
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001853 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001854 {
1855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1856 return( SSL_CERTIFICATE_REQUEST_SKIP );
1857 }
1858
1859 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001860 {
1861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1862 return( ret );
1863 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001864 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001865
1866 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1867 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1868 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001869 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001870 }
1871
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001872 return( SSL_CERTIFICATE_REQUEST_SKIP );
1873}
1874
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001875/*
1876 * ssl_tls13_parse_certificate_request()
1877 * Parse certificate request
1878 * struct {
1879 * opaque certificate_request_context<0..2^8-1>;
1880 * Extension extensions<2..2^16-1>;
1881 * } CertificateRequest;
1882 */
1883static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1884 const unsigned char *buf,
1885 const unsigned char *end )
1886{
1887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1888 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001889 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001890 size_t extensions_len = 0;
1891 const unsigned char *extensions_end;
1892 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001893
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001894 /* ...
1895 * opaque certificate_request_context<0..2^8-1>
1896 * ...
1897 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001898 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1899 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001900 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001901
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001902 if( certificate_request_context_len > 0 )
1903 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001904 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001905 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1906 p, certificate_request_context_len );
1907
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001908 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001909 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001910 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001911 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001912 {
1913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1914 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1915 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001916 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001917 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001918 p += certificate_request_context_len;
1919 }
1920
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001921 /* ...
1922 * Extension extensions<2..2^16-1>;
1923 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001924 */
1925 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1926 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1927 p += 2;
1928
1929 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1930 extensions_end = p + extensions_len;
1931
1932 while( p < extensions_end )
1933 {
1934 unsigned int extension_type;
1935 size_t extension_data_len;
1936
1937 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1938 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1939 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1940 p += 4;
1941
1942 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1943
1944 switch( extension_type )
1945 {
1946 case MBEDTLS_TLS_EXT_SIG_ALG:
1947 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001948 ( "found signature algorithms extension" ) );
1949 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001950 p + extension_data_len );
1951 if( ret != 0 )
1952 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001953 if( ! sig_alg_ext_found )
1954 sig_alg_ext_found = 1;
1955 else
1956 {
1957 MBEDTLS_SSL_DEBUG_MSG( 3,
1958 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001959 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001960 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961 break;
1962
1963 default:
1964 MBEDTLS_SSL_DEBUG_MSG(
1965 3,
1966 ( "unknown extension found: %u ( ignoring )",
1967 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001968 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001969 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001970 p += extension_data_len;
1971 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001972 /* Check that we consumed all the message. */
1973 if( p != end )
1974 {
1975 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001976 ( "CertificateRequest misaligned" ) );
1977 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001978 }
1979 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001980 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001981 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001982 MBEDTLS_SSL_DEBUG_MSG( 3,
1983 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001984 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001985 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001986
Jerry Yu7840f812022-01-29 10:26:51 +08001987 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001988 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001989
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001990decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001991 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1992 MBEDTLS_ERR_SSL_DECODE_ERROR );
1993 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001994}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001995
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001996/*
1997 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1998 */
1999static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002000{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002002
2003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2004
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002005 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2006
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002007 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2008 {
2009 unsigned char *buf;
2010 size_t buf_len;
2011
2012 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2013 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2014 &buf, &buf_len ) );
2015
2016 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2017 buf, buf + buf_len ) );
2018
2019 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
2020 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
2021 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002022 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002023 {
2024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00002025 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002026 }
2027 else
2028 {
2029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002030 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2031 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002032 }
2033
2034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08002035 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002036
Jerry Yud2674312021-10-29 10:08:19 +08002037 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2038
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002039cleanup:
2040
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2042 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002043}
2044
2045/*
Jerry Yu687101b2021-09-14 16:03:56 +08002046 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2047 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002048static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002049{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002050 int ret;
2051
2052 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002053 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002054 return( ret );
2055
Jerry Yu687101b2021-09-14 16:03:56 +08002056 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2057 return( 0 );
2058}
2059
2060/*
2061 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2062 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002063static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002064{
Jerry Yu30b071c2021-09-12 20:16:03 +08002065 int ret;
2066
2067 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2068 if( ret != 0 )
2069 return( ret );
2070
Jerry Yu687101b2021-09-14 16:03:56 +08002071 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2072 return( 0 );
2073}
Jerry Yua93ac112021-10-27 16:31:48 +08002074#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002075
Jerry Yu687101b2021-09-14 16:03:56 +08002076/*
2077 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2078 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002079static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002080{
XiaokangQianac0385c2021-11-03 06:40:11 +00002081 int ret;
2082
XiaokangQianc5c39d52021-11-09 11:55:10 +00002083 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002084 if( ret != 0 )
2085 return( ret );
2086
Ronald Cron49ad6192021-11-24 16:25:31 +01002087#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2088 mbedtls_ssl_handshake_set_state(
2089 ssl,
2090 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2091#else
Jerry Yuca133a32022-02-15 14:22:05 +08002092 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002093#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002094
XiaokangQianac0385c2021-11-03 06:40:11 +00002095 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002096}
2097
2098/*
Jerry Yu566c7812022-01-26 15:41:22 +08002099 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2100 */
2101static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2102{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002103 int non_empty_certificate_msg = 0;
2104
Jerry Yu5cc35062022-01-28 16:16:08 +08002105 MBEDTLS_SSL_DEBUG_MSG( 1,
2106 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002107 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002108
Ronald Cron9df7c802022-03-08 18:38:54 +01002109#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002110 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002111 {
2112 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2113 if( ret != 0 )
2114 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002115
Ronald Cron7a94aca2022-03-09 07:44:27 +01002116 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2117 non_empty_certificate_msg = 1;
2118 }
2119 else
2120 {
2121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
2122 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002123#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002124
Ronald Cron7a94aca2022-03-09 07:44:27 +01002125 if( non_empty_certificate_msg )
2126 {
2127 mbedtls_ssl_handshake_set_state( ssl,
2128 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2129 }
2130 else
2131 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2132
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002133 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002134}
2135
Ronald Cron9df7c802022-03-08 18:38:54 +01002136#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002137/*
2138 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2139 */
2140static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2141{
Ronald Crona8b38872022-03-09 07:59:25 +01002142 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2143
2144 if( ret == 0 )
2145 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2146
2147 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002148}
Jerry Yu90f152d2022-01-29 22:12:42 +08002149#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002150
2151/*
Jerry Yu687101b2021-09-14 16:03:56 +08002152 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2153 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002154static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002155{
XiaokangQian0fa66432021-11-15 03:33:57 +00002156 int ret;
2157
2158 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2159 if( ret != 0 )
2160 return( ret );
2161
2162 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2163 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002164}
2165
2166/*
2167 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2168 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002169static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002170{
Jerry Yu378254d2021-10-30 21:44:47 +08002171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002172 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002173 return( 0 );
2174}
2175
2176/*
2177 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2178 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002179static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002180{
Jerry Yu378254d2021-10-30 21:44:47 +08002181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2182 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2183
2184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2185 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2186
2187 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2188
2189 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2190 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002191}
2192
Jerry Yu92c6b402021-08-27 16:59:09 +08002193int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002194{
Jerry Yu92c6b402021-08-27 16:59:09 +08002195 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002196
Jerry Yue3b34122021-09-28 17:53:35 +08002197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2198 mbedtls_ssl_states_str( ssl->state ),
2199 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002200
2201 switch( ssl->state )
2202 {
2203 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002204 * ssl->state is initialized as HELLO_REQUEST. It is the same
2205 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002206 */
2207 case MBEDTLS_SSL_HELLO_REQUEST:
2208 case MBEDTLS_SSL_CLIENT_HELLO:
2209 ret = ssl_tls13_write_client_hello( ssl );
2210 break;
2211
2212 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002213 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002214 break;
2215
2216 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002217 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002218 break;
2219
Jerry Yua93ac112021-10-27 16:31:48 +08002220#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002221 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2222 ret = ssl_tls13_process_certificate_request( ssl );
2223 break;
2224
Jerry Yu687101b2021-09-14 16:03:56 +08002225 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002226 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002227 break;
2228
2229 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002230 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002231 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002232#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002233
2234 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002235 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002236 break;
2237
Jerry Yu566c7812022-01-26 15:41:22 +08002238 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2239 ret = ssl_tls13_write_client_certificate( ssl );
2240 break;
2241
Ronald Cron9df7c802022-03-08 18:38:54 +01002242#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002243 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2244 ret = ssl_tls13_write_client_certificate_verify( ssl );
2245 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002246#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002247
Jerry Yu687101b2021-09-14 16:03:56 +08002248 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002249 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002250 break;
2251
2252 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002253 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002254 break;
2255
2256 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002257 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002258 break;
2259
Ronald Cron49ad6192021-11-24 16:25:31 +01002260 /*
2261 * Injection of dummy-CCS's for middlebox compatibility
2262 */
2263#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002264 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002265 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2266 if( ret == 0 )
2267 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2268 break;
2269
2270 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2271 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2272 if( ret == 0 )
2273 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002274 break;
2275#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2276
Jerry Yu92c6b402021-08-27 16:59:09 +08002277 default:
2278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2280 }
2281
2282 return( ret );
2283}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002284
Jerry Yufb4b6472022-01-27 15:03:26 +08002285#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002286
Jerry Yufb4b6472022-01-27 15:03:26 +08002287