blob: c40fb879b639faef9fe0537efa0ba2d059958a63 [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 Yue1b9c292021-09-10 10:08:31 +0800942/* Parse ServerHello message and configure context
943 *
944 * struct {
945 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
946 * Random random;
947 * opaque legacy_session_id_echo<0..32>;
948 * CipherSuite cipher_suite;
949 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800950 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800951 * } ServerHello;
952 */
Jerry Yuc068b662021-10-11 22:30:19 +0800953static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
954 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000955 const unsigned char *end,
956 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800957{
Jerry Yub85277e2021-10-13 13:36:05 +0800958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000960 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800961 size_t extensions_len;
962 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800963 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800964 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000965 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800966
967 /*
968 * Check there is space for minimal fields
969 *
970 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800971 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800972 * - legacy_session_id_echo ( 1 byte ), minimum size
973 * - cipher_suite ( 2 bytes)
974 * - legacy_compression_method ( 1 byte )
975 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800976 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800977
978 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800979 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
980
Jerry Yu4a173382021-10-11 21:45:31 +0800981 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800982 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800983 * ...
984 * with ProtocolVersion defined as:
985 * uint16 ProtocolVersion;
986 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400987 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
988 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800989 {
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
991 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
992 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000993 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
994 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800995 }
996 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800998 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800999 * Random random;
1000 * ...
1001 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001002 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001003 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001004 if( !is_hrr )
1005 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001006 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001007 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1008 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1009 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1010 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001011 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001012
Jerry Yu4a173382021-10-11 21:45:31 +08001013 /* ...
1014 * opaque legacy_session_id_echo<0..32>;
1015 * ...
1016 */
1017 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 {
XiaokangQian52da5582022-01-26 09:49:29 +00001019 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001020 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 }
1022
Jerry Yu4a173382021-10-11 21:45:31 +08001023 /* ...
1024 * CipherSuite cipher_suite;
1025 * ...
1026 * with CipherSuite defined as:
1027 * uint8 CipherSuite[2];
1028 */
1029 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001030 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1031 p += 2;
1032
Jerry Yu4a173382021-10-11 21:45:31 +08001033
XiaokangQian355e09a2022-01-20 11:14:50 +00001034 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001035 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001036 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001037 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001038 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1039 ssl->tls_version,
1040 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001041 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001042 {
XiaokangQian52da5582022-01-26 09:49:29 +00001043 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001045 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001046 * If we received an HRR before and that the proposed selected
1047 * ciphersuite in this server hello is not the same as the one
1048 * proposed in the HRR, we abort the handshake and send an
1049 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001050 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001051 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1052 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001053 {
XiaokangQian52da5582022-01-26 09:49:29 +00001054 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001055 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001056
XiaokangQian52da5582022-01-26 09:49:29 +00001057 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001058 {
1059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1060 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001061 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001062 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001063
Jerry Yu4a173382021-10-11 21:45:31 +08001064 /* Configure ciphersuites */
1065 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1066
XiaokangQian355e09a2022-01-20 11:14:50 +00001067 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 ssl->session_negotiate->ciphersuite = cipher_suite;
1069
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1071 cipher_suite, ciphersuite_info->name ) );
1072
1073#if defined(MBEDTLS_HAVE_TIME)
1074 ssl->session_negotiate->start = time( NULL );
1075#endif /* MBEDTLS_HAVE_TIME */
1076
Jerry Yu4a173382021-10-11 21:45:31 +08001077 /* ...
1078 * uint8 legacy_compression_method = 0;
1079 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001080 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001081 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001082 if( p[0] != 0 )
1083 {
Jerry Yub85277e2021-10-13 13:36:05 +08001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001085 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001086 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 }
1088 p++;
1089
Jerry Yub85277e2021-10-13 13:36:05 +08001090 /* ...
1091 * Extension extensions<6..2^16-1>;
1092 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001093 * struct {
1094 * ExtensionType extension_type; (2 bytes)
1095 * opaque extension_data<0..2^16-1>;
1096 * } Extension;
1097 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001098 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001099 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001100 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001101
Jerry Yu4a173382021-10-11 21:45:31 +08001102 /* Check extensions do not go beyond the buffer of data. */
1103 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1104 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001105
Jerry Yu4a173382021-10-11 21:45:31 +08001106 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001107
Jerry Yu4a173382021-10-11 21:45:31 +08001108 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001109 {
1110 unsigned int extension_type;
1111 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001112 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001113
Jerry Yu4a173382021-10-11 21:45:31 +08001114 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001115 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1116 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1117 p += 4;
1118
Jerry Yu4a173382021-10-11 21:45:31 +08001119 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001120 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121
1122 switch( extension_type )
1123 {
XiaokangQianb851da82022-01-14 04:03:11 +00001124 case MBEDTLS_TLS_EXT_COOKIE:
1125
XiaokangQiand9e068e2022-01-18 06:23:32 +00001126 if( !is_hrr )
1127 {
XiaokangQian52da5582022-01-26 09:49:29 +00001128 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001129 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001130 }
1131
XiaokangQian43550bd2022-01-21 04:32:58 +00001132 ret = ssl_tls13_parse_cookie_ext( ssl,
1133 p, extension_data_end );
1134 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001135 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001136 MBEDTLS_SSL_DEBUG_RET( 1,
1137 "ssl_tls13_parse_cookie_ext",
1138 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001140 }
XiaokangQianb851da82022-01-14 04:03:11 +00001141 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001142
Jerry Yue1b9c292021-09-10 10:08:31 +08001143 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001144 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001145 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001146 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001147 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001148 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 break;
1150
1151 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1153 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001154
XiaokangQian52da5582022-01-26 09:49:29 +00001155 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001156 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001157
Jerry Yue1b9c292021-09-10 10:08:31 +08001158 case MBEDTLS_TLS_EXT_KEY_SHARE:
1159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001160 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1161 {
XiaokangQian52da5582022-01-26 09:49:29 +00001162 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001163 goto cleanup;
1164 }
1165
XiaokangQian53f20b72022-01-18 10:47:33 +00001166 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001167 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001168 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001169 else
XiaokangQianb851da82022-01-14 04:03:11 +00001170 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001171 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001172 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001173 {
1174 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001175 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001176 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001177 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001178 }
1179 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180
1181 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001182 MBEDTLS_SSL_DEBUG_MSG(
1183 3,
1184 ( "unknown extension found: %u ( ignoring )",
1185 extension_type ) );
1186
XiaokangQian52da5582022-01-26 09:49:29 +00001187 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001188 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001189 }
1190
1191 p += extension_data_len;
1192 }
1193
XiaokangQiand59be772022-01-24 10:12:51 +00001194cleanup:
1195
XiaokangQian52da5582022-01-26 09:49:29 +00001196 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001197 {
1198 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1199 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1200 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1201 }
XiaokangQian52da5582022-01-26 09:49:29 +00001202 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001203 {
1204 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1205 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1206 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1207 }
1208 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001209}
1210
XiaokangQian355e09a2022-01-20 11:14:50 +00001211static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001212{
Jerry Yub85277e2021-10-13 13:36:05 +08001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001214 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001215 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001216 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001217
Jerry Yub85277e2021-10-13 13:36:05 +08001218 /* Determine the key exchange mode:
1219 * 1) If both the pre_shared_key and key_share extensions were received
1220 * then the key exchange mode is PSK with EPHEMERAL.
1221 * 2) If only the pre_shared_key extension was received then the key
1222 * exchange mode is PSK-only.
1223 * 3) If only the key_share extension was received then the key
1224 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001225 */
Jerry Yub85277e2021-10-13 13:36:05 +08001226 switch( handshake->extensions_present &
1227 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001228 {
Jerry Yu745bb612021-10-13 22:01:04 +08001229 /* Only the pre_shared_key extension was received */
1230 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001231 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001232 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001233
Jerry Yu745bb612021-10-13 22:01:04 +08001234 /* Only the key_share extension was received */
1235 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001236 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001237 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001238
Jerry Yu745bb612021-10-13 22:01:04 +08001239 /* Both the pre_shared_key and key_share extensions were received */
1240 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001241 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001242 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001243
Jerry Yu745bb612021-10-13 22:01:04 +08001244 /* Neither pre_shared_key nor key_share extension was received */
1245 default:
1246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1247 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1248 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001249 }
1250
1251 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1252 *
1253 * TODO: We don't have to do this in case we offered 0-RTT and the
1254 * server accepted it. In this case, we could skip generating
1255 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001256 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001257 if( ret != 0 )
1258 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001260 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001261 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001262 }
1263
1264 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001265 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001266 if( ret != 0 )
1267 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001269 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001270 }
1271
1272 /* Next evolution in key schedule: Establish handshake secret and
1273 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001274 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001275 if( ret != 0 )
1276 {
Jerry Yuc068b662021-10-11 22:30:19 +08001277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1278 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001279 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001280 }
1281
Jerry Yub85277e2021-10-13 13:36:05 +08001282 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001283 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001284 {
1285 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1286 goto cleanup;
1287 }
Jerry Yu0b177842021-09-05 19:41:30 +08001288
1289 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1290 ssl->conf->endpoint,
1291 ssl->session_negotiate->ciphersuite,
1292 &traffic_keys,
1293 ssl );
1294 if( ret != 0 )
1295 {
1296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001297 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001298 }
1299
Jerry Yub85277e2021-10-13 13:36:05 +08001300 handshake->transform_handshake = transform_handshake;
1301 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001302
1303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1304 ssl->session_in = ssl->session_negotiate;
1305
1306 /*
1307 * State machine update
1308 */
1309 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1310
Jerry Yu4a173382021-10-11 21:45:31 +08001311cleanup:
1312
Jerry Yu0b177842021-09-05 19:41:30 +08001313 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001314 if( ret != 0 )
1315 {
Jerry Yub85277e2021-10-13 13:36:05 +08001316 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001317
Jerry Yu4a173382021-10-11 21:45:31 +08001318 MBEDTLS_SSL_PEND_FATAL_ALERT(
1319 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1320 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1321 }
1322 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001323}
1324
XiaokangQian355e09a2022-01-20 11:14:50 +00001325static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001326{
1327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1328
XiaokangQian647719a2021-12-07 09:16:29 +00001329#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1330 /* If not offering early data, the client sends a dummy CCS record
1331 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001332 * its second ClientHello or before its encrypted handshake flight.
1333 */
XiaokangQian647719a2021-12-07 09:16:29 +00001334 mbedtls_ssl_handshake_set_state( ssl,
1335 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1336#else
1337 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1338#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1339
XiaokangQian78b1fa72022-01-19 06:56:30 +00001340 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001341
XiaokangQian78b1fa72022-01-19 06:56:30 +00001342 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001343 * We are going to re-generate a shared secret corresponding to the group
1344 * selected by the server, which is different from the group for which we
1345 * generated a shared secret in the first client hello.
1346 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001347 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001348 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001349 if( ret != 0 )
1350 return( ret );
1351
1352 return( 0 );
1353}
1354
Jerry Yue1b9c292021-09-10 10:08:31 +08001355/*
Jerry Yu4a173382021-10-11 21:45:31 +08001356 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001357 * Handler for MBEDTLS_SSL_SERVER_HELLO
1358 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001359static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001360{
Jerry Yu4a173382021-10-11 21:45:31 +08001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001362 unsigned char *buf = NULL;
1363 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001364 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001365
1366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1367
1368 /* Coordination step
1369 * - Fetch record
1370 * - Make sure it's either a ServerHello or a HRR.
1371 * - Switch processing routine in case of HRR
1372 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001373 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1374
XiaokangQian16acd4b2022-01-14 07:35:47 +00001375 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001376 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001377 goto cleanup;
1378 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001379 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001380
Ronald Cron9f0fba32022-02-10 16:45:15 +01001381 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1382 {
1383 ret = 0;
1384 goto cleanup;
1385 }
1386
XiaokangQianb851da82022-01-14 04:03:11 +00001387 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001388 buf + buf_len,
1389 is_hrr ) );
1390 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001391 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1392
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001393 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1394 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001395
XiaokangQiand9e068e2022-01-18 06:23:32 +00001396 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001397 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001398 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001399 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001400
1401cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1403 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001404 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001405}
1406
1407/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001408 *
1409 * EncryptedExtensions message
1410 *
1411 * The EncryptedExtensions message contains any extensions which
1412 * should be protected, i.e., any which are not needed to establish
1413 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001414 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001415
1416/*
1417 * Overview
1418 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001419
1420/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001421static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001422
XiaokangQian97799ac2021-10-11 10:05:54 +00001423static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1424 const unsigned char *buf,
1425 const unsigned char *end );
1426static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001427
1428/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001429 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001430 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001431static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001432{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001433 int ret;
1434 unsigned char *buf;
1435 size_t buf_len;
1436
1437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1438
Xiaofei Bai746f9482021-11-12 08:53:56 +00001439 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001440 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001441 &buf, &buf_len ) );
1442
1443 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001444 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001445 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001447 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1448 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001449
XiaokangQian97799ac2021-10-11 10:05:54 +00001450 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451
1452cleanup:
1453
1454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1455 return( ret );
1456
1457}
1458
XiaokangQian08da26c2021-10-09 10:12:11 +00001459/* Parse EncryptedExtensions message
1460 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001461 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001462 * } EncryptedExtensions;
1463 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001464static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1465 const unsigned char *buf,
1466 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467{
1468 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001469 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001470 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001471 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001472
XiaokangQian08da26c2021-10-09 10:12:11 +00001473 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001474 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001475 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001476
XiaokangQian97799ac2021-10-11 10:05:54 +00001477 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001478 extensions_end = p + extensions_len;
1479 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001480
XiaokangQian8db25ff2021-10-13 05:56:18 +00001481 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001482 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001483 unsigned int extension_type;
1484 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001485
XiaokangQian08da26c2021-10-09 10:12:11 +00001486 /*
1487 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001488 * ExtensionType extension_type; (2 bytes)
1489 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001490 * } Extension;
1491 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001492 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001493 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1494 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1495 p += 4;
1496
XiaokangQian8db25ff2021-10-13 05:56:18 +00001497 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001498
XiaokangQian97799ac2021-10-11 10:05:54 +00001499 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001500 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001501 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001502 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001503 switch( extension_type )
1504 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001505
XiaokangQian08da26c2021-10-09 10:12:11 +00001506 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1508 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001509
lhuang0486cacac2022-01-21 07:34:27 -08001510#if defined(MBEDTLS_SSL_ALPN)
1511 case MBEDTLS_TLS_EXT_ALPN:
1512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1513
1514 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1515 {
1516 return( ret );
1517 }
1518
1519 break;
1520#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001521 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001522 MBEDTLS_SSL_DEBUG_MSG(
1523 3, ( "unsupported extension found: %u ", extension_type) );
1524 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001525 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001526 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1527 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001528 }
1529
XiaokangQian08da26c2021-10-09 10:12:11 +00001530 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001531 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001532
XiaokangQian97799ac2021-10-11 10:05:54 +00001533 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001534 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001535 {
1536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001537 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001538 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001539 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001540 }
1541
1542 return( ret );
1543}
1544
XiaokangQian97799ac2021-10-11 10:05:54 +00001545static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001546{
Jerry Yua93ac112021-10-27 16:31:48 +08001547#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001548 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001549 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1550 else
Jerry Yud2674312021-10-29 10:08:19 +08001551 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001552#else
1553 ((void) ssl);
1554 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1555#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001556 return( 0 );
1557}
1558
Jerry Yua93ac112021-10-27 16:31:48 +08001559#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001560/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001561 *
1562 * STATE HANDLING: CertificateRequest
1563 *
Jerry Yud2674312021-10-29 10:08:19 +08001564 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001565#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1566#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567/* Coordination:
1568 * Deals with the ambiguity of not knowing if a CertificateRequest
1569 * will be sent. Returns a negative code on failure, or
1570 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1571 * - SSL_CERTIFICATE_REQUEST_SKIP
1572 * indicating if a Certificate Request is expected or not.
1573 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001574static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001575{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001577
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001578 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001579 {
1580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1581 return( SSL_CERTIFICATE_REQUEST_SKIP );
1582 }
1583
1584 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001585 {
1586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1587 return( ret );
1588 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001589 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001590
1591 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1592 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1593 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001594 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001595 }
1596
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001597 return( SSL_CERTIFICATE_REQUEST_SKIP );
1598}
1599
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001600/*
1601 * ssl_tls13_parse_certificate_request()
1602 * Parse certificate request
1603 * struct {
1604 * opaque certificate_request_context<0..2^8-1>;
1605 * Extension extensions<2..2^16-1>;
1606 * } CertificateRequest;
1607 */
1608static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1609 const unsigned char *buf,
1610 const unsigned char *end )
1611{
1612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1613 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001614 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001615 size_t extensions_len = 0;
1616 const unsigned char *extensions_end;
1617 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001618
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001619 /* ...
1620 * opaque certificate_request_context<0..2^8-1>
1621 * ...
1622 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001623 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1624 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001625 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001626
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001627 if( certificate_request_context_len > 0 )
1628 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001629 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001630 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1631 p, certificate_request_context_len );
1632
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001633 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001634 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001635 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001636 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001637 {
1638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1639 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1640 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001641 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001642 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001643 p += certificate_request_context_len;
1644 }
1645
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001646 /* ...
1647 * Extension extensions<2..2^16-1>;
1648 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001649 */
1650 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1651 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1652 p += 2;
1653
1654 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1655 extensions_end = p + extensions_len;
1656
1657 while( p < extensions_end )
1658 {
1659 unsigned int extension_type;
1660 size_t extension_data_len;
1661
1662 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1663 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1664 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1665 p += 4;
1666
1667 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1668
1669 switch( extension_type )
1670 {
1671 case MBEDTLS_TLS_EXT_SIG_ALG:
1672 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001673 ( "found signature algorithms extension" ) );
1674 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 p + extension_data_len );
1676 if( ret != 0 )
1677 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001678 if( ! sig_alg_ext_found )
1679 sig_alg_ext_found = 1;
1680 else
1681 {
1682 MBEDTLS_SSL_DEBUG_MSG( 3,
1683 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001684 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001685 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001686 break;
1687
1688 default:
1689 MBEDTLS_SSL_DEBUG_MSG(
1690 3,
1691 ( "unknown extension found: %u ( ignoring )",
1692 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001693 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695 p += extension_data_len;
1696 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001697 /* Check that we consumed all the message. */
1698 if( p != end )
1699 {
1700 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001701 ( "CertificateRequest misaligned" ) );
1702 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001703 }
1704 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001705 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001706 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001707 MBEDTLS_SSL_DEBUG_MSG( 3,
1708 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001709 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001710 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001711
Jerry Yu7840f812022-01-29 10:26:51 +08001712 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001713 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001714
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001715decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001716 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1717 MBEDTLS_ERR_SSL_DECODE_ERROR );
1718 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001719}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001720
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001721/*
1722 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1723 */
1724static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001725{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001727
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1729
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001730 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1731
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001732 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1733 {
1734 unsigned char *buf;
1735 size_t buf_len;
1736
1737 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1738 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1739 &buf, &buf_len ) );
1740
1741 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1742 buf, buf + buf_len ) );
1743
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001744 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1745 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001746 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001747 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001748 {
1749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001750 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001751 }
1752 else
1753 {
1754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001755 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1756 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001757 }
1758
1759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001760 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001761
Jerry Yud2674312021-10-29 10:08:19 +08001762 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1763
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001764cleanup:
1765
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1767 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001768}
1769
1770/*
Jerry Yu687101b2021-09-14 16:03:56 +08001771 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1772 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001773static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001774{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001775 int ret;
1776
1777 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001778 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001779 return( ret );
1780
Jerry Yu687101b2021-09-14 16:03:56 +08001781 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1782 return( 0 );
1783}
1784
1785/*
1786 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1787 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001788static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001789{
Jerry Yu30b071c2021-09-12 20:16:03 +08001790 int ret;
1791
1792 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1793 if( ret != 0 )
1794 return( ret );
1795
Jerry Yu687101b2021-09-14 16:03:56 +08001796 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1797 return( 0 );
1798}
Jerry Yua93ac112021-10-27 16:31:48 +08001799#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001800
Jerry Yu687101b2021-09-14 16:03:56 +08001801/*
1802 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1803 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001804static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001805{
XiaokangQianac0385c2021-11-03 06:40:11 +00001806 int ret;
1807
XiaokangQianc5c39d52021-11-09 11:55:10 +00001808 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001809 if( ret != 0 )
1810 return( ret );
1811
Ronald Cron49ad6192021-11-24 16:25:31 +01001812#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1813 mbedtls_ssl_handshake_set_state(
1814 ssl,
1815 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1816#else
Jerry Yuca133a32022-02-15 14:22:05 +08001817 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001818#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001819
XiaokangQianac0385c2021-11-03 06:40:11 +00001820 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001821}
1822
1823/*
Jerry Yu566c7812022-01-26 15:41:22 +08001824 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1825 */
1826static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1827{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001828 int non_empty_certificate_msg = 0;
1829
Jerry Yu5cc35062022-01-28 16:16:08 +08001830 MBEDTLS_SSL_DEBUG_MSG( 1,
1831 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001832 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001833
Ronald Cron9df7c802022-03-08 18:38:54 +01001834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001835 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001836 {
1837 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1838 if( ret != 0 )
1839 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001840
Ronald Cron7a94aca2022-03-09 07:44:27 +01001841 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1842 non_empty_certificate_msg = 1;
1843 }
1844 else
1845 {
1846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1847 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001848#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001849
Ronald Cron7a94aca2022-03-09 07:44:27 +01001850 if( non_empty_certificate_msg )
1851 {
1852 mbedtls_ssl_handshake_set_state( ssl,
1853 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1854 }
1855 else
1856 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1857
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001858 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001859}
1860
Ronald Cron9df7c802022-03-08 18:38:54 +01001861#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001862/*
1863 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1864 */
1865static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1866{
Ronald Crona8b38872022-03-09 07:59:25 +01001867 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1868
1869 if( ret == 0 )
1870 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1871
1872 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001873}
Jerry Yu90f152d2022-01-29 22:12:42 +08001874#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001875
1876/*
Jerry Yu687101b2021-09-14 16:03:56 +08001877 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1878 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001879static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001880{
XiaokangQian0fa66432021-11-15 03:33:57 +00001881 int ret;
1882
1883 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1884 if( ret != 0 )
1885 return( ret );
1886
1887 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1888 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001889}
1890
1891/*
1892 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1893 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001894static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001895{
Jerry Yu378254d2021-10-30 21:44:47 +08001896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001897 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001898 return( 0 );
1899}
1900
1901/*
1902 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1903 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001904static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001905{
Jerry Yu378254d2021-10-30 21:44:47 +08001906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1907 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1908
1909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1910 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1911
1912 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1913
1914 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1915 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001916}
1917
Jerry Yu92c6b402021-08-27 16:59:09 +08001918int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001919{
Jerry Yu92c6b402021-08-27 16:59:09 +08001920 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001921
Jerry Yu92c6b402021-08-27 16:59:09 +08001922 switch( ssl->state )
1923 {
1924 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001925 * ssl->state is initialized as HELLO_REQUEST. It is the same
1926 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001927 */
1928 case MBEDTLS_SSL_HELLO_REQUEST:
1929 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001930 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001931 break;
1932
1933 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001934 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001935 break;
1936
1937 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001938 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001939 break;
1940
Jerry Yua93ac112021-10-27 16:31:48 +08001941#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001942 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1943 ret = ssl_tls13_process_certificate_request( ssl );
1944 break;
1945
Jerry Yu687101b2021-09-14 16:03:56 +08001946 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001947 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001948 break;
1949
1950 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001951 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001952 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001953#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001954
1955 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001956 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001957 break;
1958
Jerry Yu566c7812022-01-26 15:41:22 +08001959 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1960 ret = ssl_tls13_write_client_certificate( ssl );
1961 break;
1962
Ronald Cron9df7c802022-03-08 18:38:54 +01001963#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001964 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1965 ret = ssl_tls13_write_client_certificate_verify( ssl );
1966 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001967#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001968
Jerry Yu687101b2021-09-14 16:03:56 +08001969 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001970 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001971 break;
1972
1973 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001974 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001975 break;
1976
1977 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001978 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001979 break;
1980
Ronald Cron49ad6192021-11-24 16:25:31 +01001981 /*
1982 * Injection of dummy-CCS's for middlebox compatibility
1983 */
1984#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001985 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001986 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1987 if( ret == 0 )
1988 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1989 break;
1990
1991 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1992 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1993 if( ret == 0 )
1994 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001995 break;
1996#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1997
Jerry Yu92c6b402021-08-27 16:59:09 +08001998 default:
1999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2001 }
2002
2003 return( ret );
2004}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002005
Jerry Yufb4b6472022-01-27 15:03:26 +08002006#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002007
Jerry Yufb4b6472022-01-27 15:03:26 +08002008