blob: 198c20a2a39e0498b17c6e8c10b9dc90b17260ac [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"
Ronald Cron3d580bf2022-02-18 17:24:56 +010034#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080035#include "ssl_tls13_keys.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;
Glenn Strausscd78df62022-04-07 19:07:11 -040052 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040078 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Glenn Strausscd78df62022-04-07 19:07:11 -040083 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010084 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040085 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
88 }
89
90 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuc068b662021-10-11 22:30:19 +080095static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
96 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080097 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080098{
Jerry Yue1b9c292021-09-10 10:08:31 +080099 ((void) ssl);
100
Ronald Cron98473382022-03-30 20:04:10 +0200101 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400102 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
103 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800106
107 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
109 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800110 }
111
Ronald Cron98473382022-03-30 20:04:10 +0200112 if( &buf[2] != end )
113 {
114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
115 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
116 MBEDTLS_ERR_SSL_DECODE_ERROR );
117 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
118 }
119
Jerry Yue1b9c292021-09-10 10:08:31 +0800120 return( 0 );
121}
122
lhuang0486cacac2022-01-21 07:34:27 -0800123#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800124static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
125 const unsigned char *buf, size_t len )
126{
127 size_t list_len, name_len;
128 const unsigned char *p = buf;
129 const unsigned char *end = buf + len;
130
131 /* If we didn't send it, the server shouldn't send it */
132 if( ssl->conf->alpn_list == NULL )
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134
135 /*
136 * opaque ProtocolName<1..2^8-1>;
137 *
138 * struct {
139 * ProtocolName protocol_name_list<2..2^16-1>
140 * } ProtocolNameList;
141 *
142 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
143 */
144
145 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
146 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
147
148 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
149 p += 2;
150 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
151
152 name_len = *p++;
153 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
154
155 /* Check that the server chosen protocol was in our list and save it */
156 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
157 {
158 if( name_len == strlen( *alpn ) &&
159 memcmp( buf + 3, *alpn, name_len ) == 0 )
160 {
161 ssl->alpn_chosen = *alpn;
162 return( 0 );
163 }
164 }
165
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167}
168#endif /* MBEDTLS_SSL_ALPN */
169
XiaokangQian16acd4b2022-01-14 07:35:47 +0000170static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000171{
172 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100173
XiaokangQian647719a2021-12-07 09:16:29 +0000174 if( group_id == 0 )
175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
176
XiaokangQian355e09a2022-01-20 11:14:50 +0000177#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000178 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000179 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
182
183 /* Destroy generated private key. */
184 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
185 if( status != PSA_SUCCESS )
186 {
187 ret = psa_ssl_status_to_mbedtls( status );
188 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
189 return( ret );
190 }
191
192 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000193 return( 0 );
194 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000195 else
196#endif /* MBEDTLS_ECDH_C */
197 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000198 {
199 /* Do something */
200 }
201
202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
203}
204
205/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800206 * Functions for writing key_share extension.
207 */
208#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800209static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800210 mbedtls_ssl_context *ssl,
211 uint16_t named_group,
212 unsigned char *buf,
213 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000214 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800215{
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100216 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
217 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
218 psa_key_attributes_t key_attributes;
219 size_t own_pubkey_len;
220 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
221 size_t ecdh_bits = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800224
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100225 /* Convert EC group to PSA key type. */
226 if( ( handshake->ecdh_psa_type =
227 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
228 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800229
Neil Armstrong91477a72022-03-25 15:42:20 +0100230 ssl->handshake->ecdh_bits = ecdh_bits;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100232 key_attributes = psa_key_attributes_init();
233 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
234 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
235 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
236 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100237
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100238 /* Generate ECDH private key. */
239 status = psa_generate_key( &key_attributes,
240 &handshake->ecdh_psa_privkey );
241 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100243 ret = psa_ssl_status_to_mbedtls( status );
244 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100246
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 }
248
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100249 /* Export the public part of the ECDH private key from PSA. */
250 status = psa_export_public_key( handshake->ecdh_psa_privkey,
251 buf, (size_t)( end - buf ),
252 &own_pubkey_len );
253 if( status != PSA_SUCCESS )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 {
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100255 ret = psa_ssl_status_to_mbedtls( status );
256 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800257 return( ret );
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100258
Jerry Yu56fc07f2021-09-01 17:48:49 +0800259 }
260
Przemek Stekiele894c5c2022-03-02 08:45:56 +0100261 *out_len = own_pubkey_len;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100262
Jerry Yu75336352021-09-01 15:59:36 +0800263 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800264}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265#endif /* MBEDTLS_ECDH_C */
266
Jerry Yub60e3cf2021-09-08 16:41:02 +0800267static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
268 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269{
270 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
271
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100274 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800275 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100276 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
278
Brett Warren14efd332021-10-06 09:32:11 +0100279 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000281 const mbedtls_ecp_curve_info *curve_info;
282 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
283 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100284 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 {
Brett Warren14efd332021-10-06 09:32:11 +0100286 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 return( 0 );
288 }
289 }
290#else
291 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293#endif /* MBEDTLS_ECDH_C */
294
295 /*
296 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800297 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298 */
299
300 return( ret );
301}
302
303/*
304 * ssl_tls13_write_key_share_ext
305 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 *
308 * struct {
309 * NamedGroup group;
310 * opaque key_exchange<1..2^16-1>;
311 * } KeyShareEntry;
312 * struct {
313 * KeyShareEntry client_shares<0..2^16-1>;
314 * } KeyShareClientHello;
315 */
316static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
317 unsigned char *buf,
318 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000319 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800320{
321 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000322 unsigned char *client_shares; /* Start of client_shares */
323 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800324 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
326
Xiaofei Baid25fab62021-12-02 06:36:27 +0000327 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328
Jerry Yub60e3cf2021-09-08 16:41:02 +0800329 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 * - extension_type (2 bytes)
331 * - extension_data_length (2 bytes)
332 * - client_shares_length (2 bytes)
333 */
334 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
335 p += 6;
336
337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
338
339 /* HRR could already have requested something else. */
340 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
342 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800343 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800344 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800345 &group_id ) );
346 }
347
348 /*
349 * Dispatch to type-specific key generation function.
350 *
351 * So far, we're only supporting ECDHE. With the introduction
352 * of PQC KEMs, we'll want to have multiple branches, one per
353 * type of KEM, and dispatch to the corresponding crypto. And
354 * only one key share entry is allowed.
355 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000356 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800358 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800360 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000361 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100363 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800364
365 /* Check there is space for header of KeyShareEntry
366 * - group (2 bytes)
367 * - key_exchange_length (2 bytes)
368 */
369 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
370 p += 4;
Przemyslaw Stekielea859c22022-02-10 10:19:46 +0100371 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, p, end,
Jerry Yub60e3cf2021-09-08 16:41:02 +0800372 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800373 p += key_exchange_len;
374 if( ret != 0 )
375 return( ret );
376
377 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000378 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800379 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000380 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800381 }
382 else
383#endif /* MBEDTLS_ECDH_C */
384 if( 0 /* other KEMs? */ )
385 {
386 /* Do something */
387 }
388 else
389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
390
Jerry Yub60e3cf2021-09-08 16:41:02 +0800391 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000392 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800393 if( client_shares_len == 0)
394 {
395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800397 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800398 /* Write extension_type */
399 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
400 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800401 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800402 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800403 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800404
405 /* Update offered_group_id field */
406 ssl->handshake->offered_group_id = group_id;
407
408 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000409 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410
Xiaofei Baid25fab62021-12-02 06:36:27 +0000411 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412
413 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
414
415cleanup:
416
417 return( ret );
418}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800419
Jerry Yue1b9c292021-09-10 10:08:31 +0800420
XiaokangQiand59be772022-01-24 10:12:51 +0000421/*
422 * ssl_tls13_parse_hrr_key_share_ext()
423 * Parse key_share extension in Hello Retry Request
424 *
425 * struct {
426 * NamedGroup selected_group;
427 * } KeyShareHelloRetryRequest;
428 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000429static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000430 const unsigned char *buf,
431 const unsigned char *end )
432{
XiaokangQianb851da82022-01-14 04:03:11 +0000433 const mbedtls_ecp_curve_info *curve_info = NULL;
434 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000435 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000436 int found = 0;
437
438 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
439 if( group_list == NULL )
440 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
441
442 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
443
444 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000445 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000446 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000448
449 /* Upon receipt of this extension in a HelloRetryRequest, the client
450 * MUST first verify that the selected_group field corresponds to a
451 * group which was provided in the "supported_groups" extension in the
452 * original ClientHello.
453 * The supported_group was based on the info in ssl->conf->group_list.
454 *
455 * If the server provided a key share that was not sent in the ClientHello
456 * then the client MUST abort the handshake with an "illegal_parameter" alert.
457 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000458 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000459 {
460 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000461 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000462 continue;
463
464 /* We found a match */
465 found = 1;
466 break;
467 }
468
469 /* Client MUST verify that the selected_group field does not
470 * correspond to a group which was provided in the "key_share"
471 * extension in the original ClientHello. If the server sent an
472 * HRR message with a key share already provided in the
473 * ClientHello then the client MUST abort the handshake with
474 * an "illegal_parameter" alert.
475 */
XiaokangQiand59be772022-01-24 10:12:51 +0000476 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000477 {
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
479 MBEDTLS_SSL_PEND_FATAL_ALERT(
480 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
481 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
482 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
483 }
484
485 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000486 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000487
488 return( 0 );
489}
490
Jerry Yue1b9c292021-09-10 10:08:31 +0800491/*
Jerry Yub85277e2021-10-13 13:36:05 +0800492 * ssl_tls13_parse_key_share_ext()
493 * Parse key_share extension in Server Hello
494 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 * struct {
496 * KeyShareEntry server_share;
497 * } KeyShareServerHello;
498 * struct {
499 * NamedGroup group;
500 * opaque key_exchange<1..2^16-1>;
501 * } KeyShareEntry;
502 */
Jerry Yu4a173382021-10-11 21:45:31 +0800503static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800504 const unsigned char *buf,
505 const unsigned char *end )
506{
Jerry Yub85277e2021-10-13 13:36:05 +0800507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800508 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800509 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800510
Jerry Yu4a173382021-10-11 21:45:31 +0800511 /* ...
512 * NamedGroup group; (2 bytes)
513 * ...
514 */
515 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
516 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800517 p += 2;
518
Jerry Yu4a173382021-10-11 21:45:31 +0800519 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800520 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800521 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800522 {
523 MBEDTLS_SSL_DEBUG_MSG( 1,
524 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800525 (unsigned) offered_group, (unsigned) group ) );
526 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
527 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
528 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800529 }
530
531#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800532 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800533 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100534 const mbedtls_ecp_curve_info *curve_info =
535 mbedtls_ecp_curve_info_from_tls_id( group );
536 if( curve_info == NULL )
537 {
538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
540 }
541
542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
543
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000544 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800545 if( ret != 0 )
546 return( ret );
547 }
Jerry Yub85277e2021-10-13 13:36:05 +0800548 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800549#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800550 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800551 {
552 /* Do something */
553 }
554 else
555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
556
557 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
558 return( ret );
559}
560
XiaokangQiand59be772022-01-24 10:12:51 +0000561/*
562 * ssl_tls13_parse_cookie_ext()
563 * Parse cookie extension in Hello Retry Request
564 *
565 * struct {
566 * opaque cookie<1..2^16-1>;
567 * } Cookie;
568 *
569 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
570 * extension to the client (this is an exception to the usual rule that
571 * the only extensions that may be sent are those that appear in the
572 * ClientHello). When sending the new ClientHello, the client MUST copy
573 * the contents of the extension received in the HelloRetryRequest into
574 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
575 * cookies in their initial ClientHello in subsequent connections.
576 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000577static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
578 const unsigned char *buf,
579 const unsigned char *end )
580{
XiaokangQian25c9c902022-02-08 10:49:53 +0000581 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000582 const unsigned char *p = buf;
583 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
584
585 /* Retrieve length field of cookie */
586 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
587 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
588 p += 2;
589
590 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
591 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
592
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000593 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000594 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000595 handshake->cookie = mbedtls_calloc( 1, cookie_len );
596 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000597 {
598 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000599 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000600 cookie_len ) );
601 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
602 }
603
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000604 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000605 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000606
607 return( 0 );
608}
XiaokangQian43550bd2022-01-21 04:32:58 +0000609
XiaokangQian0b64eed2022-01-27 10:36:51 +0000610static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000611 unsigned char *buf,
612 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000613 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000614{
615 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000616 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000617 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000618
XiaokangQianc02768a2022-02-10 07:31:25 +0000619 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000620 {
621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
622 return( 0 );
623 }
624
625 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000626 handshake->cookie,
627 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000628
XiaokangQianc02768a2022-02-10 07:31:25 +0000629 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000630
631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
632
XiaokangQian0b64eed2022-01-27 10:36:51 +0000633 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000634 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
635 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000636 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000637
638 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000639 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000640
XiaokangQianc02768a2022-02-10 07:31:25 +0000641 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000642
643 return( 0 );
644}
645
Ronald Cron3d580bf2022-02-18 17:24:56 +0100646int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
647 unsigned char *buf,
648 unsigned char *end,
649 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100650{
651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
652 unsigned char *p = buf;
653 size_t ext_len;
654
655 *out_len = 0;
656
657 /* Write supported_versions extension
658 *
659 * Supported Versions Extension is mandatory with TLS 1.3.
660 */
661 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
662 if( ret != 0 )
663 return( ret );
664 p += ext_len;
665
666 /* Echo the cookie if the server provided one in its preceding
667 * HelloRetryRequest message.
668 */
669 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
670 if( ret != 0 )
671 return( ret );
672 p += ext_len;
673
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100674 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
675 {
676 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
677 if( ret != 0 )
678 return( ret );
679 p += ext_len;
680 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100681
682 *out_len = p - buf;
683
684 return( 0 );
685}
686
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800687/*
Jerry Yu4a173382021-10-11 21:45:31 +0800688 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800689 */
Ronald Cronda41b382022-03-30 09:57:11 +0200690/**
691 * \brief Detect if the ServerHello contains a supported_versions extension
692 * or not.
693 *
694 * \param[in] ssl SSL context
695 * \param[in] buf Buffer containing the ServerHello message
696 * \param[in] end End of the buffer containing the ServerHello message
697 *
698 * \return 0 if the ServerHello does not contain a supported_versions extension
699 * \return 1 if the ServerHello contains a supported_versions extension
700 * \return A negative value if an error occurred while parsing the ServerHello.
701 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100702static int ssl_tls13_is_supported_versions_ext_present(
703 mbedtls_ssl_context *ssl,
704 const unsigned char *buf,
705 const unsigned char *end )
706{
707 const unsigned char *p = buf;
708 size_t legacy_session_id_echo_len;
709 size_t extensions_len;
710 const unsigned char *extensions_end;
711
712 /*
713 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200714 * length:
715 * - legacy_version 2 bytes
716 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
717 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100718 */
719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
720 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
721 legacy_session_id_echo_len = *p;
722
723 /*
724 * Jump to the extensions, jumping over:
725 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
726 * - cipher_suite 2 bytes
727 * - legacy_compression_method 1 byte
728 */
729 p += legacy_session_id_echo_len + 4;
730
731 /* Case of no extension */
732 if( p == end )
733 return( 0 );
734
735 /* ...
736 * Extension extensions<6..2^16-1>;
737 * ...
738 * struct {
739 * ExtensionType extension_type; (2 bytes)
740 * opaque extension_data<0..2^16-1>;
741 * } Extension;
742 */
743 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
744 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
745 p += 2;
746
747 /* Check extensions do not go beyond the buffer of data. */
748 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
749 extensions_end = p + extensions_len;
750
751 while( p < extensions_end )
752 {
753 unsigned int extension_type;
754 size_t extension_data_len;
755
756 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
757 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
758 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
759 p += 4;
760
761 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
762 return( 1 );
763
764 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
765 p += extension_data_len;
766 }
767
768 return( 0 );
769}
770
Jerry Yu7a186a02021-10-15 18:46:14 +0800771/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800772 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
773 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000774 * to indicate which message is expected and to be parsed next.
775 */
Jerry Yub85277e2021-10-13 13:36:05 +0800776#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
777#define SSL_SERVER_HELLO_COORDINATE_HRR 1
778static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
779 const unsigned char *buf,
780 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800781{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800782 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800783 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
784 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
785 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
786 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
787
788 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
789 *
Jerry Yu4a173382021-10-11 21:45:31 +0800790 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800791 * special value of the SHA-256 of "HelloRetryRequest".
792 *
793 * struct {
794 * ProtocolVersion legacy_version = 0x0303;
795 * Random random;
796 * opaque legacy_session_id_echo<0..32>;
797 * CipherSuite cipher_suite;
798 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800799 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800800 * } ServerHello;
801 *
802 */
Jerry Yub85277e2021-10-13 13:36:05 +0800803 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800804
805 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800806 {
Jerry Yub85277e2021-10-13 13:36:05 +0800807 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800808 }
809
Jerry Yub85277e2021-10-13 13:36:05 +0800810 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800811}
812
Jerry Yu745bb612021-10-13 22:01:04 +0800813/* Fetch and preprocess
814 * Returns a negative value on failure, and otherwise
815 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100816 * - SSL_SERVER_HELLO_COORDINATE_HRR or
817 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800818 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100819#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800820static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
821 unsigned char **buf,
822 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800823{
Jerry Yu4a173382021-10-11 21:45:31 +0800824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800825
XiaokangQian355e09a2022-01-20 11:14:50 +0000826 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
827 MBEDTLS_SSL_HS_SERVER_HELLO,
828 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800829
Ronald Cron9f0fba32022-02-10 16:45:15 +0100830 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
831 ssl, *buf, *buf + *buf_len ) );
832 if( ret == 0 )
833 {
834 /* If the supported versions extension is not present but we were
835 * expecting it, abort the handshake. Otherwise, switch to TLS 1.2
836 * handshake.
837 */
Glenn Strausscd78df62022-04-07 19:07:11 -0400838 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100839 {
840 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
841 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
842 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
843 }
844
845 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400846 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100847 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
848 *buf, *buf_len );
849
850 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
851 {
852 ret = ssl_tls13_reset_key_share( ssl );
853 if( ret != 0 )
854 return( ret );
855 }
856
857 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
858 }
859
Jerry Yub85277e2021-10-13 13:36:05 +0800860 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
861 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800862 {
Jerry Yu745bb612021-10-13 22:01:04 +0800863 case SSL_SERVER_HELLO_COORDINATE_HELLO:
864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
865 break;
866 case SSL_SERVER_HELLO_COORDINATE_HRR:
867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000868 /* If a client receives a second
869 * HelloRetryRequest in the same connection (i.e., where the ClientHello
870 * was itself in response to a HelloRetryRequest), it MUST abort the
871 * handshake with an "unexpected_message" alert.
872 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000873 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000874 {
875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
876 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
877 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
878 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
879 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000880 /*
881 * Clients must abort the handshake with an "illegal_parameter"
882 * alert if the HelloRetryRequest would not result in any change
883 * in the ClientHello.
884 * In a PSK only key exchange that what we expect.
885 */
886 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
887 {
888 MBEDTLS_SSL_DEBUG_MSG( 1,
889 ( "Unexpected HRR in pure PSK key exchange." ) );
890 MBEDTLS_SSL_PEND_FATAL_ALERT(
891 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
892 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
893 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
894 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000895
XiaokangQiand9e068e2022-01-18 06:23:32 +0000896 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000897
Jerry Yu745bb612021-10-13 22:01:04 +0800898 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800899 }
900
901cleanup:
902
903 return( ret );
904}
905
Jerry Yu4a173382021-10-11 21:45:31 +0800906static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
907 const unsigned char **buf,
908 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800909{
910 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800911 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800912
Jerry Yude4fb2c2021-09-19 18:05:08 +0800913 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800914 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800915
Jerry Yu4a173382021-10-11 21:45:31 +0800916 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800917
918 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800919 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
920 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800922 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
923 ssl->session_negotiate->id,
924 ssl->session_negotiate->id_len );
925 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800926 legacy_session_id_echo_len );
927
928 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
929 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
930
Jerry Yue1b9c292021-09-10 10:08:31 +0800931 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
932 }
933
Jerry Yu4a173382021-10-11 21:45:31 +0800934 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800935 *buf = p;
936
937 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800938 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800939 return( 0 );
940}
941
Jerry Yu4a173382021-10-11 21:45:31 +0800942static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
943 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800944{
Jerry Yu4a173382021-10-11 21:45:31 +0800945 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
946
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800948 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800949 {
Jerry Yu4a173382021-10-11 21:45:31 +0800950 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800951 {
952 return( 1 );
953 }
954 }
955 return( 0 );
956}
957
958/* Parse ServerHello message and configure context
959 *
960 * struct {
961 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
962 * Random random;
963 * opaque legacy_session_id_echo<0..32>;
964 * CipherSuite cipher_suite;
965 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800966 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800967 * } ServerHello;
968 */
Jerry Yuc068b662021-10-11 22:30:19 +0800969static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
970 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000971 const unsigned char *end,
972 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800973{
Jerry Yub85277e2021-10-13 13:36:05 +0800974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800975 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000976 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800977 size_t extensions_len;
978 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800979 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800980 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000981 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800982
983 /*
984 * Check there is space for minimal fields
985 *
986 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800987 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 * - legacy_session_id_echo ( 1 byte ), minimum size
989 * - cipher_suite ( 2 bytes)
990 * - legacy_compression_method ( 1 byte )
991 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800992 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800993
994 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800995 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
996
Jerry Yu4a173382021-10-11 21:45:31 +0800997 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800998 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800999 * ...
1000 * with ProtocolVersion defined as:
1001 * uint16 ProtocolVersion;
1002 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001003 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1004 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 {
1006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1007 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1008 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001009 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1010 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001011 }
1012 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001013
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001014 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001015 * Random random;
1016 * ...
1017 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001018 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001019 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001020 if( !is_hrr )
1021 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001022 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001023 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1024 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1025 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1026 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001027 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001028
Jerry Yu4a173382021-10-11 21:45:31 +08001029 /* ...
1030 * opaque legacy_session_id_echo<0..32>;
1031 * ...
1032 */
1033 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 {
XiaokangQian52da5582022-01-26 09:49:29 +00001035 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001036 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001037 }
1038
Jerry Yu4a173382021-10-11 21:45:31 +08001039 /* ...
1040 * CipherSuite cipher_suite;
1041 * ...
1042 * with CipherSuite defined as:
1043 * uint8 CipherSuite[2];
1044 */
1045 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1047 p += 2;
1048
Jerry Yu4a173382021-10-11 21:45:31 +08001049
XiaokangQian355e09a2022-01-20 11:14:50 +00001050 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001051 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001052 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001053 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001054 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1055 ssl->tls_version,
1056 ssl->tls_version ) != 0 ) ||
Ronald Cronba120bb2022-03-30 22:09:48 +02001057 !ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 {
XiaokangQian52da5582022-01-26 09:49:29 +00001059 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001061 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001062 * If we received an HRR before and that the proposed selected
1063 * ciphersuite in this server hello is not the same as the one
1064 * proposed in the HRR, we abort the handshake and send an
1065 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001066 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001067 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1068 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001069 {
XiaokangQian52da5582022-01-26 09:49:29 +00001070 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001071 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001072
XiaokangQian52da5582022-01-26 09:49:29 +00001073 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001074 {
1075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1076 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001077 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001078 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001079
Jerry Yu4a173382021-10-11 21:45:31 +08001080 /* Configure ciphersuites */
1081 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1082
XiaokangQian355e09a2022-01-20 11:14:50 +00001083 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 ssl->session_negotiate->ciphersuite = cipher_suite;
1085
Jerry Yue1b9c292021-09-10 10:08:31 +08001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1087 cipher_suite, ciphersuite_info->name ) );
1088
1089#if defined(MBEDTLS_HAVE_TIME)
1090 ssl->session_negotiate->start = time( NULL );
1091#endif /* MBEDTLS_HAVE_TIME */
1092
Jerry Yu4a173382021-10-11 21:45:31 +08001093 /* ...
1094 * uint8 legacy_compression_method = 0;
1095 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001096 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001097 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098 if( p[0] != 0 )
1099 {
Jerry Yub85277e2021-10-13 13:36:05 +08001100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001101 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001102 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001103 }
1104 p++;
1105
Jerry Yub85277e2021-10-13 13:36:05 +08001106 /* ...
1107 * Extension extensions<6..2^16-1>;
1108 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001109 * struct {
1110 * ExtensionType extension_type; (2 bytes)
1111 * opaque extension_data<0..2^16-1>;
1112 * } Extension;
1113 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001114 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001115 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001116 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001117
Jerry Yu4a173382021-10-11 21:45:31 +08001118 /* Check extensions do not go beyond the buffer of data. */
1119 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1120 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121
Jerry Yu4a173382021-10-11 21:45:31 +08001122 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123
Jerry Yu4a173382021-10-11 21:45:31 +08001124 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001125 {
1126 unsigned int extension_type;
1127 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001128 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001129
Jerry Yu4a173382021-10-11 21:45:31 +08001130 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001131 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1132 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1133 p += 4;
1134
Jerry Yu4a173382021-10-11 21:45:31 +08001135 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001136 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001137
1138 switch( extension_type )
1139 {
XiaokangQianb851da82022-01-14 04:03:11 +00001140 case MBEDTLS_TLS_EXT_COOKIE:
1141
XiaokangQiand9e068e2022-01-18 06:23:32 +00001142 if( !is_hrr )
1143 {
XiaokangQian52da5582022-01-26 09:49:29 +00001144 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001145 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001146 }
1147
XiaokangQian43550bd2022-01-21 04:32:58 +00001148 ret = ssl_tls13_parse_cookie_ext( ssl,
1149 p, extension_data_end );
1150 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001151 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001152 MBEDTLS_SSL_DEBUG_RET( 1,
1153 "ssl_tls13_parse_cookie_ext",
1154 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001155 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001156 }
XiaokangQianb851da82022-01-14 04:03:11 +00001157 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001158
Jerry Yue1b9c292021-09-10 10:08:31 +08001159 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001160 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001161 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001162 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001163 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001164 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 break;
1166
1167 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001170
XiaokangQian52da5582022-01-26 09:49:29 +00001171 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001172 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001173
Jerry Yue1b9c292021-09-10 10:08:31 +08001174 case MBEDTLS_TLS_EXT_KEY_SHARE:
1175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001176 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1177 {
XiaokangQian52da5582022-01-26 09:49:29 +00001178 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
1180 }
1181
XiaokangQian53f20b72022-01-18 10:47:33 +00001182 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001183 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001184 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001185 else
XiaokangQianb851da82022-01-14 04:03:11 +00001186 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001187 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001188 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 {
1190 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001191 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001192 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001193 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001194 }
1195 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001196
1197 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001198 MBEDTLS_SSL_DEBUG_MSG(
1199 3,
1200 ( "unknown extension found: %u ( ignoring )",
1201 extension_type ) );
1202
XiaokangQian52da5582022-01-26 09:49:29 +00001203 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001204 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001205 }
1206
1207 p += extension_data_len;
1208 }
1209
XiaokangQiand59be772022-01-24 10:12:51 +00001210cleanup:
1211
XiaokangQian52da5582022-01-26 09:49:29 +00001212 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001213 {
1214 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1215 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1216 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1217 }
XiaokangQian52da5582022-01-26 09:49:29 +00001218 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001219 {
1220 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1221 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1222 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1223 }
1224 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001225}
1226
XiaokangQian355e09a2022-01-20 11:14:50 +00001227static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001228{
Jerry Yub85277e2021-10-13 13:36:05 +08001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001230 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001231 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001232 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001233
Jerry Yub85277e2021-10-13 13:36:05 +08001234 /* Determine the key exchange mode:
1235 * 1) If both the pre_shared_key and key_share extensions were received
1236 * then the key exchange mode is PSK with EPHEMERAL.
1237 * 2) If only the pre_shared_key extension was received then the key
1238 * exchange mode is PSK-only.
1239 * 3) If only the key_share extension was received then the key
1240 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001241 */
Jerry Yub85277e2021-10-13 13:36:05 +08001242 switch( handshake->extensions_present &
1243 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001244 {
Jerry Yu745bb612021-10-13 22:01:04 +08001245 /* Only the pre_shared_key extension was received */
1246 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001247 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001248 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001249
Jerry Yu745bb612021-10-13 22:01:04 +08001250 /* Only the key_share extension was received */
1251 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001252 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001253 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001254
Jerry Yu745bb612021-10-13 22:01:04 +08001255 /* Both the pre_shared_key and key_share extensions were received */
1256 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001257 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001258 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001259
Jerry Yu745bb612021-10-13 22:01:04 +08001260 /* Neither pre_shared_key nor key_share extension was received */
1261 default:
1262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1263 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1264 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001265 }
1266
1267 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1268 *
1269 * TODO: We don't have to do this in case we offered 0-RTT and the
1270 * server accepted it. In this case, we could skip generating
1271 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001272 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001273 if( ret != 0 )
1274 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001276 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001277 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001278 }
1279
1280 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001281 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001282 if( ret != 0 )
1283 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001285 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001286 }
1287
1288 /* Next evolution in key schedule: Establish handshake secret and
1289 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001290 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001291 if( ret != 0 )
1292 {
Jerry Yuc068b662021-10-11 22:30:19 +08001293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1294 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001295 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001296 }
1297
Jerry Yub85277e2021-10-13 13:36:05 +08001298 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001299 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001300 {
1301 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1302 goto cleanup;
1303 }
Jerry Yu0b177842021-09-05 19:41:30 +08001304
1305 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1306 ssl->conf->endpoint,
1307 ssl->session_negotiate->ciphersuite,
1308 &traffic_keys,
1309 ssl );
1310 if( ret != 0 )
1311 {
1312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001313 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001314 }
1315
Jerry Yub85277e2021-10-13 13:36:05 +08001316 handshake->transform_handshake = transform_handshake;
1317 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001318
1319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1320 ssl->session_in = ssl->session_negotiate;
1321
1322 /*
1323 * State machine update
1324 */
1325 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1326
Jerry Yu4a173382021-10-11 21:45:31 +08001327cleanup:
1328
Jerry Yu0b177842021-09-05 19:41:30 +08001329 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001330 if( ret != 0 )
1331 {
Jerry Yub85277e2021-10-13 13:36:05 +08001332 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001333
Jerry Yu4a173382021-10-11 21:45:31 +08001334 MBEDTLS_SSL_PEND_FATAL_ALERT(
1335 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1336 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1337 }
1338 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001339}
1340
XiaokangQian355e09a2022-01-20 11:14:50 +00001341static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001342{
1343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1344
XiaokangQian647719a2021-12-07 09:16:29 +00001345#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1346 /* If not offering early data, the client sends a dummy CCS record
1347 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001348 * its second ClientHello or before its encrypted handshake flight.
1349 */
XiaokangQian647719a2021-12-07 09:16:29 +00001350 mbedtls_ssl_handshake_set_state( ssl,
1351 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1352#else
1353 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1354#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1355
XiaokangQian78b1fa72022-01-19 06:56:30 +00001356 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001357
XiaokangQian78b1fa72022-01-19 06:56:30 +00001358 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001359 * We are going to re-generate a shared secret corresponding to the group
1360 * selected by the server, which is different from the group for which we
1361 * generated a shared secret in the first client hello.
1362 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001363 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001364 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001365 if( ret != 0 )
1366 return( ret );
1367
1368 return( 0 );
1369}
1370
Jerry Yue1b9c292021-09-10 10:08:31 +08001371/*
Jerry Yu4a173382021-10-11 21:45:31 +08001372 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001373 * Handler for MBEDTLS_SSL_SERVER_HELLO
1374 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001375static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001376{
Jerry Yu4a173382021-10-11 21:45:31 +08001377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001378 unsigned char *buf = NULL;
1379 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001380 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001381
1382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1383
1384 /* Coordination step
1385 * - Fetch record
1386 * - Make sure it's either a ServerHello or a HRR.
1387 * - Switch processing routine in case of HRR
1388 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001389 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1390
XiaokangQian16acd4b2022-01-14 07:35:47 +00001391 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001392 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001393 goto cleanup;
1394 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001395 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001396
Ronald Cron9f0fba32022-02-10 16:45:15 +01001397 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1398 {
1399 ret = 0;
1400 goto cleanup;
1401 }
1402
XiaokangQianb851da82022-01-14 04:03:11 +00001403 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001404 buf + buf_len,
1405 is_hrr ) );
1406 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001407 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1408
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001409 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1410 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001411
XiaokangQiand9e068e2022-01-18 06:23:32 +00001412 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001413 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001414 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001415 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001416
1417cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1419 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001420 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001421}
1422
1423/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001424 *
1425 * EncryptedExtensions message
1426 *
1427 * The EncryptedExtensions message contains any extensions which
1428 * should be protected, i.e., any which are not needed to establish
1429 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001430 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001431
1432/*
1433 * Overview
1434 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001435
1436/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001437static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001438
XiaokangQian97799ac2021-10-11 10:05:54 +00001439static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1440 const unsigned char *buf,
1441 const unsigned char *end );
1442static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001443
1444/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001445 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001447static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001448{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001449 int ret;
1450 unsigned char *buf;
1451 size_t buf_len;
1452
1453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1454
Xiaofei Bai746f9482021-11-12 08:53:56 +00001455 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001456 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001457 &buf, &buf_len ) );
1458
1459 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001460 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001461 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001462
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001463 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1464 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465
XiaokangQian97799ac2021-10-11 10:05:54 +00001466 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467
1468cleanup:
1469
1470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1471 return( ret );
1472
1473}
1474
XiaokangQian08da26c2021-10-09 10:12:11 +00001475/* Parse EncryptedExtensions message
1476 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001477 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001478 * } EncryptedExtensions;
1479 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001480static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1481 const unsigned char *buf,
1482 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483{
1484 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001485 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001486 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001487 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001488
XiaokangQian08da26c2021-10-09 10:12:11 +00001489 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001490 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001491 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001492
XiaokangQian97799ac2021-10-11 10:05:54 +00001493 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001494 extensions_end = p + extensions_len;
1495 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001496
XiaokangQian8db25ff2021-10-13 05:56:18 +00001497 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001498 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001499 unsigned int extension_type;
1500 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001501
XiaokangQian08da26c2021-10-09 10:12:11 +00001502 /*
1503 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001504 * ExtensionType extension_type; (2 bytes)
1505 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001506 * } Extension;
1507 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001508 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001509 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1510 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1511 p += 4;
1512
XiaokangQian8db25ff2021-10-13 05:56:18 +00001513 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001514
XiaokangQian97799ac2021-10-11 10:05:54 +00001515 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001516 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001517 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001518 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001519 switch( extension_type )
1520 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001521
XiaokangQian08da26c2021-10-09 10:12:11 +00001522 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1524 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001525
lhuang0486cacac2022-01-21 07:34:27 -08001526#if defined(MBEDTLS_SSL_ALPN)
1527 case MBEDTLS_TLS_EXT_ALPN:
1528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1529
1530 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1531 {
1532 return( ret );
1533 }
1534
1535 break;
1536#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001537 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001538 MBEDTLS_SSL_DEBUG_MSG(
1539 3, ( "unsupported extension found: %u ", extension_type) );
1540 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001541 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001542 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1543 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001544 }
1545
XiaokangQian08da26c2021-10-09 10:12:11 +00001546 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001547 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001548
XiaokangQian97799ac2021-10-11 10:05:54 +00001549 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001550 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001551 {
1552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001553 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001554 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001555 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001556 }
1557
1558 return( ret );
1559}
1560
XiaokangQian97799ac2021-10-11 10:05:54 +00001561static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001562{
Jerry Yua93ac112021-10-27 16:31:48 +08001563#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001564 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001565 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1566 else
Jerry Yud2674312021-10-29 10:08:19 +08001567 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001568#else
1569 ((void) ssl);
1570 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1571#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001572 return( 0 );
1573}
1574
Jerry Yua93ac112021-10-27 16:31:48 +08001575#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001576/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001577 *
1578 * STATE HANDLING: CertificateRequest
1579 *
Jerry Yud2674312021-10-29 10:08:19 +08001580 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001581#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1582#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001583/* Coordination:
1584 * Deals with the ambiguity of not knowing if a CertificateRequest
1585 * will be sent. Returns a negative code on failure, or
1586 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1587 * - SSL_CERTIFICATE_REQUEST_SKIP
1588 * indicating if a Certificate Request is expected or not.
1589 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001590static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001591{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001593
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001594 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001595 {
1596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1597 return( SSL_CERTIFICATE_REQUEST_SKIP );
1598 }
1599
1600 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001601 {
1602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1603 return( ret );
1604 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001605 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001606
1607 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1608 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1609 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001610 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001611 }
1612
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001613 return( SSL_CERTIFICATE_REQUEST_SKIP );
1614}
1615
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001616/*
1617 * ssl_tls13_parse_certificate_request()
1618 * Parse certificate request
1619 * struct {
1620 * opaque certificate_request_context<0..2^8-1>;
1621 * Extension extensions<2..2^16-1>;
1622 * } CertificateRequest;
1623 */
1624static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1625 const unsigned char *buf,
1626 const unsigned char *end )
1627{
1628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1629 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001630 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001631 size_t extensions_len = 0;
1632 const unsigned char *extensions_end;
1633 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001634
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001635 /* ...
1636 * opaque certificate_request_context<0..2^8-1>
1637 * ...
1638 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001639 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1640 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001641 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001642
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001643 if( certificate_request_context_len > 0 )
1644 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001645 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001646 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1647 p, certificate_request_context_len );
1648
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001649 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001650 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001651 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001652 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001653 {
1654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1655 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1656 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001657 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001658 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659 p += certificate_request_context_len;
1660 }
1661
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001662 /* ...
1663 * Extension extensions<2..2^16-1>;
1664 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665 */
1666 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1667 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1668 p += 2;
1669
1670 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1671 extensions_end = p + extensions_len;
1672
1673 while( p < extensions_end )
1674 {
1675 unsigned int extension_type;
1676 size_t extension_data_len;
1677
1678 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1679 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1680 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1681 p += 4;
1682
1683 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1684
1685 switch( extension_type )
1686 {
1687 case MBEDTLS_TLS_EXT_SIG_ALG:
1688 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001689 ( "found signature algorithms extension" ) );
1690 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 p + extension_data_len );
1692 if( ret != 0 )
1693 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001694 if( ! sig_alg_ext_found )
1695 sig_alg_ext_found = 1;
1696 else
1697 {
1698 MBEDTLS_SSL_DEBUG_MSG( 3,
1699 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001700 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001701 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001702 break;
1703
1704 default:
1705 MBEDTLS_SSL_DEBUG_MSG(
1706 3,
1707 ( "unknown extension found: %u ( ignoring )",
1708 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001709 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001710 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001711 p += extension_data_len;
1712 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001713 /* Check that we consumed all the message. */
1714 if( p != end )
1715 {
1716 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001717 ( "CertificateRequest misaligned" ) );
1718 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001719 }
1720 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001721 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001722 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001723 MBEDTLS_SSL_DEBUG_MSG( 3,
1724 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001725 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001726 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727
Jerry Yu7840f812022-01-29 10:26:51 +08001728 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001729 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001730
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001731decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001732 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1733 MBEDTLS_ERR_SSL_DECODE_ERROR );
1734 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001735}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001736
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001737/*
1738 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1739 */
1740static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001741{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001743
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1745
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001746 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1747
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001748 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1749 {
1750 unsigned char *buf;
1751 size_t buf_len;
1752
1753 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1754 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1755 &buf, &buf_len ) );
1756
1757 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1758 buf, buf + buf_len ) );
1759
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001760 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1761 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001762 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001763 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764 {
1765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001766 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001767 }
1768 else
1769 {
1770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001771 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1772 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001773 }
1774
1775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001776 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001777
Jerry Yud2674312021-10-29 10:08:19 +08001778 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1779
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001780cleanup:
1781
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1783 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001784}
1785
1786/*
Jerry Yu687101b2021-09-14 16:03:56 +08001787 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1788 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001789static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001790{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001791 int ret;
1792
1793 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001794 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001795 return( ret );
1796
Jerry Yu687101b2021-09-14 16:03:56 +08001797 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1798 return( 0 );
1799}
1800
1801/*
1802 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1803 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001804static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001805{
Jerry Yu30b071c2021-09-12 20:16:03 +08001806 int ret;
1807
1808 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1809 if( ret != 0 )
1810 return( ret );
1811
Jerry Yu687101b2021-09-14 16:03:56 +08001812 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1813 return( 0 );
1814}
Jerry Yua93ac112021-10-27 16:31:48 +08001815#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001816
Jerry Yu687101b2021-09-14 16:03:56 +08001817/*
1818 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1819 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001820static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001821{
XiaokangQianac0385c2021-11-03 06:40:11 +00001822 int ret;
1823
XiaokangQianc5c39d52021-11-09 11:55:10 +00001824 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001825 if( ret != 0 )
1826 return( ret );
1827
Ronald Cron49ad6192021-11-24 16:25:31 +01001828#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1829 mbedtls_ssl_handshake_set_state(
1830 ssl,
1831 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1832#else
Jerry Yuca133a32022-02-15 14:22:05 +08001833 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001834#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001835
XiaokangQianac0385c2021-11-03 06:40:11 +00001836 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001837}
1838
1839/*
Jerry Yu566c7812022-01-26 15:41:22 +08001840 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1841 */
1842static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1843{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001844 int non_empty_certificate_msg = 0;
1845
Jerry Yu5cc35062022-01-28 16:16:08 +08001846 MBEDTLS_SSL_DEBUG_MSG( 1,
1847 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001848 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001849
Ronald Cron9df7c802022-03-08 18:38:54 +01001850#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001851 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001852 {
1853 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1854 if( ret != 0 )
1855 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001856
Ronald Cron7a94aca2022-03-09 07:44:27 +01001857 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1858 non_empty_certificate_msg = 1;
1859 }
1860 else
1861 {
1862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1863 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001864#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001865
Ronald Cron7a94aca2022-03-09 07:44:27 +01001866 if( non_empty_certificate_msg )
1867 {
1868 mbedtls_ssl_handshake_set_state( ssl,
1869 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1870 }
1871 else
1872 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1873
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001874 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001875}
1876
Ronald Cron9df7c802022-03-08 18:38:54 +01001877#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001878/*
1879 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1880 */
1881static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1882{
Ronald Crona8b38872022-03-09 07:59:25 +01001883 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1884
1885 if( ret == 0 )
1886 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1887
1888 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001889}
Jerry Yu90f152d2022-01-29 22:12:42 +08001890#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001891
1892/*
Jerry Yu687101b2021-09-14 16:03:56 +08001893 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1894 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001895static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001896{
XiaokangQian0fa66432021-11-15 03:33:57 +00001897 int ret;
1898
1899 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1900 if( ret != 0 )
1901 return( ret );
1902
1903 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1904 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001905}
1906
1907/*
1908 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1909 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001910static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001911{
Jerry Yu378254d2021-10-30 21:44:47 +08001912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001913 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001914 return( 0 );
1915}
1916
1917/*
1918 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1919 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001920static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001921{
Jerry Yu378254d2021-10-30 21:44:47 +08001922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1923 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1924
1925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1926 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1927
1928 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1929
1930 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1931 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001932}
1933
Jerry Yu92c6b402021-08-27 16:59:09 +08001934int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001935{
Jerry Yu92c6b402021-08-27 16:59:09 +08001936 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001937
Jerry Yu92c6b402021-08-27 16:59:09 +08001938 switch( ssl->state )
1939 {
1940 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001941 * ssl->state is initialized as HELLO_REQUEST. It is the same
1942 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001943 */
1944 case MBEDTLS_SSL_HELLO_REQUEST:
1945 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001946 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001947 break;
1948
1949 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001950 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001951 break;
1952
1953 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001954 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001955 break;
1956
Jerry Yua93ac112021-10-27 16:31:48 +08001957#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001958 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1959 ret = ssl_tls13_process_certificate_request( ssl );
1960 break;
1961
Jerry Yu687101b2021-09-14 16:03:56 +08001962 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001963 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001964 break;
1965
1966 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001967 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001968 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001969#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001970
1971 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001972 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001973 break;
1974
Jerry Yu566c7812022-01-26 15:41:22 +08001975 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1976 ret = ssl_tls13_write_client_certificate( ssl );
1977 break;
1978
Ronald Cron9df7c802022-03-08 18:38:54 +01001979#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001980 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1981 ret = ssl_tls13_write_client_certificate_verify( ssl );
1982 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001983#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001984
Jerry Yu687101b2021-09-14 16:03:56 +08001985 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001986 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001987 break;
1988
1989 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001990 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001991 break;
1992
1993 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001994 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001995 break;
1996
Ronald Cron49ad6192021-11-24 16:25:31 +01001997 /*
1998 * Injection of dummy-CCS's for middlebox compatibility
1999 */
2000#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002001 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002002 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2003 if( ret == 0 )
2004 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2005 break;
2006
2007 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2008 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2009 if( ret == 0 )
2010 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002011 break;
2012#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2013
Jerry Yu92c6b402021-08-27 16:59:09 +08002014 default:
2015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2017 }
2018
2019 return( ret );
2020}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002021
Jerry Yufb4b6472022-01-27 15:03:26 +08002022#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002023
Jerry Yufb4b6472022-01-27 15:03:26 +08002024