blob: b29818426024394804ffea2b279bb8eda0923edf [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"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020045MBEDTLS_CHECK_RETURN_CRITICAL
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
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020095MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Ronald Cron98473382022-03-30 20:04:10 +0200102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400103 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
104 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
Ronald Cron98473382022-03-30 20:04:10 +0200113 if( &buf[2] != end )
114 {
115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
116 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
117 MBEDTLS_ERR_SSL_DECODE_ERROR );
118 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
119 }
120
Jerry Yue1b9c292021-09-10 10:08:31 +0800121 return( 0 );
122}
123
lhuang0486cacac2022-01-21 07:34:27 -0800124#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
127 const unsigned char *buf, size_t len )
128{
129 size_t list_len, name_len;
130 const unsigned char *p = buf;
131 const unsigned char *end = buf + len;
132
133 /* If we didn't send it, the server shouldn't send it */
134 if( ssl->conf->alpn_list == NULL )
135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
136
137 /*
138 * opaque ProtocolName<1..2^8-1>;
139 *
140 * struct {
141 * ProtocolName protocol_name_list<2..2^16-1>
142 * } ProtocolNameList;
143 *
144 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
145 */
146
147 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
149
150 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
151 p += 2;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
153
154 name_len = *p++;
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
156
157 /* Check that the server chosen protocol was in our list and save it */
158 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
159 {
160 if( name_len == strlen( *alpn ) &&
161 memcmp( buf + 3, *alpn, name_len ) == 0 )
162 {
163 ssl->alpn_chosen = *alpn;
164 return( 0 );
165 }
166 }
167
168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
169}
170#endif /* MBEDTLS_SSL_ALPN */
171
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200172MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian16acd4b2022-01-14 07:35:47 +0000173static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000174{
175 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100176
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( group_id == 0 )
178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
179
XiaokangQian355e09a2022-01-20 11:14:50 +0000180#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000181 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000182 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
185
186 /* Destroy generated private key. */
187 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
188 if( status != PSA_SUCCESS )
189 {
190 ret = psa_ssl_status_to_mbedtls( status );
191 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
192 return( ret );
193 }
194
195 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000196 return( 0 );
197 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000198 else
199#endif /* MBEDTLS_ECDH_C */
200 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000201 {
202 /* Do something */
203 }
204
205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
206}
207
208/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209 * Functions for writing key_share extension.
210 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200211MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub60e3cf2021-09-08 16:41:02 +0800212static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
213 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214{
215 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
216
Jerry Yu56fc07f2021-09-01 17:48:49 +0800217
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100219 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800220 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100221 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800222 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
223
Brett Warren14efd332021-10-06 09:32:11 +0100224 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000226 const mbedtls_ecp_curve_info *curve_info;
227 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
228 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100229 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800230 {
Brett Warren14efd332021-10-06 09:32:11 +0100231 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800232 return( 0 );
233 }
234 }
235#else
236 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800237 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238#endif /* MBEDTLS_ECDH_C */
239
240 /*
241 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800242 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800243 */
244
245 return( ret );
246}
247
248/*
249 * ssl_tls13_write_key_share_ext
250 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800251 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800252 *
253 * struct {
254 * NamedGroup group;
255 * opaque key_exchange<1..2^16-1>;
256 * } KeyShareEntry;
257 * struct {
258 * KeyShareEntry client_shares<0..2^16-1>;
259 * } KeyShareClientHello;
260 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200261MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu56fc07f2021-09-01 17:48:49 +0800262static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
263 unsigned char *buf,
264 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000265 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800266{
267 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000268 unsigned char *client_shares; /* Start of client_shares */
269 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
272
Xiaofei Baid25fab62021-12-02 06:36:27 +0000273 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274
Jerry Yub60e3cf2021-09-08 16:41:02 +0800275 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276 * - extension_type (2 bytes)
277 * - extension_data_length (2 bytes)
278 * - client_shares_length (2 bytes)
279 */
280 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
281 p += 6;
282
283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
284
285 /* HRR could already have requested something else. */
286 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800287 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
288 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800290 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 &group_id ) );
292 }
293
294 /*
295 * Dispatch to type-specific key generation function.
296 *
297 * So far, we're only supporting ECDHE. With the introduction
298 * of PQC KEMs, we'll want to have multiple branches, one per
299 * type of KEM, and dispatch to the corresponding crypto. And
300 * only one key share entry is allowed.
301 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000302 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800303#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800304 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000307 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100309 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310
311 /* Check there is space for header of KeyShareEntry
312 * - group (2 bytes)
313 * - key_exchange_length (2 bytes)
314 */
315 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
316 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800317 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
318 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 p += key_exchange_len;
320 if( ret != 0 )
321 return( ret );
322
323 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000324 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000326 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 }
328 else
329#endif /* MBEDTLS_ECDH_C */
330 if( 0 /* other KEMs? */ )
331 {
332 /* Do something */
333 }
334 else
335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
336
Jerry Yub60e3cf2021-09-08 16:41:02 +0800337 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000338 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 if( client_shares_len == 0)
340 {
341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800343 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344 /* Write extension_type */
345 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
346 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800347 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800349 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
351 /* Update offered_group_id field */
352 ssl->handshake->offered_group_id = group_id;
353
354 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000355 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800356
Xiaofei Baid25fab62021-12-02 06:36:27 +0000357 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358
359 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
360
361cleanup:
362
363 return( ret );
364}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800365
Jerry Yue1b9c292021-09-10 10:08:31 +0800366
XiaokangQiand59be772022-01-24 10:12:51 +0000367/*
368 * ssl_tls13_parse_hrr_key_share_ext()
369 * Parse key_share extension in Hello Retry Request
370 *
371 * struct {
372 * NamedGroup selected_group;
373 * } KeyShareHelloRetryRequest;
374 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200375MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand9e068e2022-01-18 06:23:32 +0000376static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000377 const unsigned char *buf,
378 const unsigned char *end )
379{
XiaokangQianb851da82022-01-14 04:03:11 +0000380 const mbedtls_ecp_curve_info *curve_info = NULL;
381 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000382 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000383 int found = 0;
384
385 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
386 if( group_list == NULL )
387 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
388
389 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
390
391 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000392 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000393 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000395
396 /* Upon receipt of this extension in a HelloRetryRequest, the client
397 * MUST first verify that the selected_group field corresponds to a
398 * group which was provided in the "supported_groups" extension in the
399 * original ClientHello.
400 * The supported_group was based on the info in ssl->conf->group_list.
401 *
402 * If the server provided a key share that was not sent in the ClientHello
403 * then the client MUST abort the handshake with an "illegal_parameter" alert.
404 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000405 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000406 {
407 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000408 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000409 continue;
410
411 /* We found a match */
412 found = 1;
413 break;
414 }
415
416 /* Client MUST verify that the selected_group field does not
417 * correspond to a group which was provided in the "key_share"
418 * extension in the original ClientHello. If the server sent an
419 * HRR message with a key share already provided in the
420 * ClientHello then the client MUST abort the handshake with
421 * an "illegal_parameter" alert.
422 */
XiaokangQiand59be772022-01-24 10:12:51 +0000423 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000424 {
425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
426 MBEDTLS_SSL_PEND_FATAL_ALERT(
427 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
428 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
429 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
430 }
431
432 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000433 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000434
435 return( 0 );
436}
437
Jerry Yue1b9c292021-09-10 10:08:31 +0800438/*
Jerry Yub85277e2021-10-13 13:36:05 +0800439 * ssl_tls13_parse_key_share_ext()
440 * Parse key_share extension in Server Hello
441 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800442 * struct {
443 * KeyShareEntry server_share;
444 * } KeyShareServerHello;
445 * struct {
446 * NamedGroup group;
447 * opaque key_exchange<1..2^16-1>;
448 * } KeyShareEntry;
449 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200450MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800451static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800452 const unsigned char *buf,
453 const unsigned char *end )
454{
Jerry Yub85277e2021-10-13 13:36:05 +0800455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800456 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800457 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800458
Jerry Yu4a173382021-10-11 21:45:31 +0800459 /* ...
460 * NamedGroup group; (2 bytes)
461 * ...
462 */
463 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
464 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800465 p += 2;
466
Jerry Yu4a173382021-10-11 21:45:31 +0800467 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800468 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800469 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800470 {
471 MBEDTLS_SSL_DEBUG_MSG( 1,
472 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800473 (unsigned) offered_group, (unsigned) group ) );
474 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
475 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
476 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800477 }
478
479#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800480 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800481 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100482 const mbedtls_ecp_curve_info *curve_info =
483 mbedtls_ecp_curve_info_from_tls_id( group );
484 if( curve_info == NULL )
485 {
486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
488 }
489
490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
491
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000492 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800493 if( ret != 0 )
494 return( ret );
495 }
Jerry Yub85277e2021-10-13 13:36:05 +0800496 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800497#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800498 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800499 {
500 /* Do something */
501 }
502 else
503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
504
505 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
506 return( ret );
507}
508
XiaokangQiand59be772022-01-24 10:12:51 +0000509/*
510 * ssl_tls13_parse_cookie_ext()
511 * Parse cookie extension in Hello Retry Request
512 *
513 * struct {
514 * opaque cookie<1..2^16-1>;
515 * } Cookie;
516 *
517 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
518 * extension to the client (this is an exception to the usual rule that
519 * the only extensions that may be sent are those that appear in the
520 * ClientHello). When sending the new ClientHello, the client MUST copy
521 * the contents of the extension received in the HelloRetryRequest into
522 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
523 * cookies in their initial ClientHello in subsequent connections.
524 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200525MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian43550bd2022-01-21 04:32:58 +0000526static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
527 const unsigned char *buf,
528 const unsigned char *end )
529{
XiaokangQian25c9c902022-02-08 10:49:53 +0000530 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000531 const unsigned char *p = buf;
532 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
533
534 /* Retrieve length field of cookie */
535 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
536 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
537 p += 2;
538
539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
540 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
541
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000542 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000543 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 handshake->cookie = mbedtls_calloc( 1, cookie_len );
545 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000548 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000549 cookie_len ) );
550 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
551 }
552
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000553 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000554 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000555
556 return( 0 );
557}
XiaokangQian43550bd2022-01-21 04:32:58 +0000558
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200559MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000561 unsigned char *buf,
562 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000563 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000564{
565 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000566 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000567 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000568
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570 {
571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
572 return( 0 );
573 }
574
575 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000576 handshake->cookie,
577 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000578
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
582
XiaokangQian0b64eed2022-01-27 10:36:51 +0000583 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000584 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
585 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000586 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000587
588 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000589 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000590
XiaokangQianc02768a2022-02-10 07:31:25 +0000591 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000592
593 return( 0 );
594}
595
Ronald Cron3d580bf2022-02-18 17:24:56 +0100596int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
597 unsigned char *buf,
598 unsigned char *end,
599 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100600{
601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
602 unsigned char *p = buf;
603 size_t ext_len;
604
605 *out_len = 0;
606
607 /* Write supported_versions extension
608 *
609 * Supported Versions Extension is mandatory with TLS 1.3.
610 */
611 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
612 if( ret != 0 )
613 return( ret );
614 p += ext_len;
615
616 /* Echo the cookie if the server provided one in its preceding
617 * HelloRetryRequest message.
618 */
619 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
620 if( ret != 0 )
621 return( ret );
622 p += ext_len;
623
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100624 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
625 {
626 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
627 if( ret != 0 )
628 return( ret );
629 p += ext_len;
630 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100631
632 *out_len = p - buf;
633
634 return( 0 );
635}
636
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800637/*
Jerry Yu4a173382021-10-11 21:45:31 +0800638 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800639 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200640
Ronald Cronda41b382022-03-30 09:57:11 +0200641/**
642 * \brief Detect if the ServerHello contains a supported_versions extension
643 * or not.
644 *
645 * \param[in] ssl SSL context
646 * \param[in] buf Buffer containing the ServerHello message
647 * \param[in] end End of the buffer containing the ServerHello message
648 *
649 * \return 0 if the ServerHello does not contain a supported_versions extension
650 * \return 1 if the ServerHello contains a supported_versions extension
651 * \return A negative value if an error occurred while parsing the ServerHello.
652 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200653MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +0100654static int ssl_tls13_is_supported_versions_ext_present(
655 mbedtls_ssl_context *ssl,
656 const unsigned char *buf,
657 const unsigned char *end )
658{
659 const unsigned char *p = buf;
660 size_t legacy_session_id_echo_len;
661 size_t extensions_len;
662 const unsigned char *extensions_end;
663
664 /*
665 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200666 * length:
667 * - legacy_version 2 bytes
668 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
669 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100670 */
671 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
672 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
673 legacy_session_id_echo_len = *p;
674
675 /*
676 * Jump to the extensions, jumping over:
677 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
678 * - cipher_suite 2 bytes
679 * - legacy_compression_method 1 byte
680 */
681 p += legacy_session_id_echo_len + 4;
682
683 /* Case of no extension */
684 if( p == end )
685 return( 0 );
686
687 /* ...
688 * Extension extensions<6..2^16-1>;
689 * ...
690 * struct {
691 * ExtensionType extension_type; (2 bytes)
692 * opaque extension_data<0..2^16-1>;
693 * } Extension;
694 */
695 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
696 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
697 p += 2;
698
699 /* Check extensions do not go beyond the buffer of data. */
700 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
701 extensions_end = p + extensions_len;
702
703 while( p < extensions_end )
704 {
705 unsigned int extension_type;
706 size_t extension_data_len;
707
708 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
709 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
710 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
711 p += 4;
712
713 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
714 return( 1 );
715
716 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
717 p += extension_data_len;
718 }
719
720 return( 0 );
721}
722
Jerry Yu7a186a02021-10-15 18:46:14 +0800723/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200724 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
725 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
726 * - 0 otherwise
727 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200728MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +0200729static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
730 const unsigned char *buf,
731 const unsigned char *end )
732{
733 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
734 static const unsigned char magic_downgrade_string[] =
735 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
736 const unsigned char *last_eight_bytes_of_random;
737 unsigned char last_byte_of_random;
738
739 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
740 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
741
742 if( memcmp( last_eight_bytes_of_random,
743 magic_downgrade_string,
744 sizeof( magic_downgrade_string ) ) == 0 )
745 {
746 last_byte_of_random = last_eight_bytes_of_random[7];
747 return( last_byte_of_random == 0 ||
748 last_byte_of_random == 1 );
749 }
750
751 return( 0 );
752}
753
754/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800755 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
756 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000757 * to indicate which message is expected and to be parsed next.
758 */
Jerry Yub85277e2021-10-13 13:36:05 +0800759#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
760#define SSL_SERVER_HELLO_COORDINATE_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200761MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +0800762static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
763 const unsigned char *buf,
764 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800765{
Jerry Yue1b9c292021-09-10 10:08:31 +0800766
767 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
768 *
Jerry Yu4a173382021-10-11 21:45:31 +0800769 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800770 * special value of the SHA-256 of "HelloRetryRequest".
771 *
772 * struct {
773 * ProtocolVersion legacy_version = 0x0303;
774 * Random random;
775 * opaque legacy_session_id_echo<0..32>;
776 * CipherSuite cipher_suite;
777 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800778 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800779 * } ServerHello;
780 *
781 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800782 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
783 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800784
Jerry Yu93a13f22022-04-11 23:00:01 +0800785 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
786 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800787 {
Jerry Yub85277e2021-10-13 13:36:05 +0800788 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800789 }
790
Jerry Yub85277e2021-10-13 13:36:05 +0800791 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800792}
793
Jerry Yu745bb612021-10-13 22:01:04 +0800794/* Fetch and preprocess
795 * Returns a negative value on failure, and otherwise
796 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100797 * - SSL_SERVER_HELLO_COORDINATE_HRR or
798 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800799 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100800#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200801MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +0800802static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
803 unsigned char **buf,
804 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800805{
Jerry Yu4a173382021-10-11 21:45:31 +0800806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200807 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800808
XiaokangQian355e09a2022-01-20 11:14:50 +0000809 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
810 MBEDTLS_SSL_HS_SERVER_HELLO,
811 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200812 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800813
Ronald Cron9f0fba32022-02-10 16:45:15 +0100814 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200815 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100816 if( ret == 0 )
817 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200818 MBEDTLS_SSL_PROC_CHK_NEG(
819 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
820
821 /* If the server is negotiating TLS 1.2 or below and:
822 * . we did not propose TLS 1.2 or
823 * . the server responded it is TLS 1.3 capable but negotiating a lower
824 * version of the protocol and thus we are under downgrade attack
825 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100826 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200827 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100828 {
829 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
830 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
831 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
832 }
833
834 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400835 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100836 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
837 *buf, *buf_len );
838
839 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
840 {
841 ret = ssl_tls13_reset_key_share( ssl );
842 if( ret != 0 )
843 return( ret );
844 }
845
846 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
847 }
848
Ronald Cronfd6193c2022-04-05 11:04:20 +0200849 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800850 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800851 {
Jerry Yu745bb612021-10-13 22:01:04 +0800852 case SSL_SERVER_HELLO_COORDINATE_HELLO:
853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
854 break;
855 case SSL_SERVER_HELLO_COORDINATE_HRR:
856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000857 /* If a client receives a second
858 * HelloRetryRequest in the same connection (i.e., where the ClientHello
859 * was itself in response to a HelloRetryRequest), it MUST abort the
860 * handshake with an "unexpected_message" alert.
861 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000862 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000863 {
864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
865 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
866 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
868 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000869 /*
870 * Clients must abort the handshake with an "illegal_parameter"
871 * alert if the HelloRetryRequest would not result in any change
872 * in the ClientHello.
873 * In a PSK only key exchange that what we expect.
874 */
875 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
876 {
877 MBEDTLS_SSL_DEBUG_MSG( 1,
878 ( "Unexpected HRR in pure PSK key exchange." ) );
879 MBEDTLS_SSL_PEND_FATAL_ALERT(
880 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
881 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
882 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
883 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000884
XiaokangQiand9e068e2022-01-18 06:23:32 +0000885 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000886
Jerry Yu745bb612021-10-13 22:01:04 +0800887 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 }
889
890cleanup:
891
892 return( ret );
893}
894
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200895MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800896static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
897 const unsigned char **buf,
898 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800899{
900 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800901 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800902
Jerry Yude4fb2c2021-09-19 18:05:08 +0800903 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800904 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800905
Jerry Yu4a173382021-10-11 21:45:31 +0800906 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800907
908 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800909 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
910 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800912 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
913 ssl->session_negotiate->id,
914 ssl->session_negotiate->id_len );
915 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800916 legacy_session_id_echo_len );
917
918 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
919 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
920
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
922 }
923
Jerry Yu4a173382021-10-11 21:45:31 +0800924 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 *buf = p;
926
927 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800928 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 return( 0 );
930}
931
Jerry Yue1b9c292021-09-10 10:08:31 +0800932/* Parse ServerHello message and configure context
933 *
934 * struct {
935 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
936 * Random random;
937 * opaque legacy_session_id_echo<0..32>;
938 * CipherSuite cipher_suite;
939 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800940 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 * } ServerHello;
942 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +0800944static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
945 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000946 const unsigned char *end,
947 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800948{
Jerry Yub85277e2021-10-13 13:36:05 +0800949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800950 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000951 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800952 size_t extensions_len;
953 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800955 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000956 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800957
958 /*
959 * Check there is space for minimal fields
960 *
961 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800962 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800963 * - legacy_session_id_echo ( 1 byte ), minimum size
964 * - cipher_suite ( 2 bytes)
965 * - legacy_compression_method ( 1 byte )
966 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800967 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800968
969 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
971
Jerry Yu4a173382021-10-11 21:45:31 +0800972 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800973 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800974 * ...
975 * with ProtocolVersion defined as:
976 * uint16 ProtocolVersion;
977 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400978 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
979 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800980 {
981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
982 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
983 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000984 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
985 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800986 }
987 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800989 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800990 * Random random;
991 * ...
992 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800993 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800994 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000995 if( !is_hrr )
996 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000997 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000998 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
999 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1000 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1001 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001002 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003
Jerry Yu4a173382021-10-11 21:45:31 +08001004 /* ...
1005 * opaque legacy_session_id_echo<0..32>;
1006 * ...
1007 */
1008 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 {
XiaokangQian52da5582022-01-26 09:49:29 +00001010 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001011 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001012 }
1013
Jerry Yu4a173382021-10-11 21:45:31 +08001014 /* ...
1015 * CipherSuite cipher_suite;
1016 * ...
1017 * with CipherSuite defined as:
1018 * uint8 CipherSuite[2];
1019 */
1020 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1022 p += 2;
1023
Jerry Yu4a173382021-10-11 21:45:31 +08001024
XiaokangQian355e09a2022-01-20 11:14:50 +00001025 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001026 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001027 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001028 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001029 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1030 ssl->tls_version,
1031 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001032 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001033 {
XiaokangQian52da5582022-01-26 09:49:29 +00001034 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001036 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001037 * If we received an HRR before and that the proposed selected
1038 * ciphersuite in this server hello is not the same as the one
1039 * proposed in the HRR, we abort the handshake and send an
1040 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001041 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001042 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1043 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001044 {
XiaokangQian52da5582022-01-26 09:49:29 +00001045 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001046 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001047
XiaokangQian52da5582022-01-26 09:49:29 +00001048 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001049 {
1050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1051 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001052 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001053 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001054
Jerry Yu4a173382021-10-11 21:45:31 +08001055 /* Configure ciphersuites */
1056 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1057
XiaokangQian355e09a2022-01-20 11:14:50 +00001058 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001059 ssl->session_negotiate->ciphersuite = cipher_suite;
1060
Jerry Yue1b9c292021-09-10 10:08:31 +08001061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1062 cipher_suite, ciphersuite_info->name ) );
1063
1064#if defined(MBEDTLS_HAVE_TIME)
1065 ssl->session_negotiate->start = time( NULL );
1066#endif /* MBEDTLS_HAVE_TIME */
1067
Jerry Yu4a173382021-10-11 21:45:31 +08001068 /* ...
1069 * uint8 legacy_compression_method = 0;
1070 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 if( p[0] != 0 )
1074 {
Jerry Yub85277e2021-10-13 13:36:05 +08001075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001076 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001077 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 }
1079 p++;
1080
Jerry Yub85277e2021-10-13 13:36:05 +08001081 /* ...
1082 * Extension extensions<6..2^16-1>;
1083 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001084 * struct {
1085 * ExtensionType extension_type; (2 bytes)
1086 * opaque extension_data<0..2^16-1>;
1087 * } Extension;
1088 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001089 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001090 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001092
Jerry Yu4a173382021-10-11 21:45:31 +08001093 /* Check extensions do not go beyond the buffer of data. */
1094 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1095 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001096
Jerry Yu4a173382021-10-11 21:45:31 +08001097 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098
Jerry Yu4a173382021-10-11 21:45:31 +08001099 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001100 {
1101 unsigned int extension_type;
1102 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001103 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
Jerry Yu4a173382021-10-11 21:45:31 +08001105 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001106 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1107 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1108 p += 4;
1109
Jerry Yu4a173382021-10-11 21:45:31 +08001110 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001111 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
1113 switch( extension_type )
1114 {
XiaokangQianb851da82022-01-14 04:03:11 +00001115 case MBEDTLS_TLS_EXT_COOKIE:
1116
XiaokangQiand9e068e2022-01-18 06:23:32 +00001117 if( !is_hrr )
1118 {
XiaokangQian52da5582022-01-26 09:49:29 +00001119 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001120 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001121 }
1122
XiaokangQian43550bd2022-01-21 04:32:58 +00001123 ret = ssl_tls13_parse_cookie_ext( ssl,
1124 p, extension_data_end );
1125 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001126 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001127 MBEDTLS_SSL_DEBUG_RET( 1,
1128 "ssl_tls13_parse_cookie_ext",
1129 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001130 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001131 }
XiaokangQianb851da82022-01-14 04:03:11 +00001132 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001133
Jerry Yue1b9c292021-09-10 10:08:31 +08001134 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001135 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001136 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001137 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001138 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 break;
1141
1142 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001145
XiaokangQian52da5582022-01-26 09:49:29 +00001146 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001147 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001148
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 case MBEDTLS_TLS_EXT_KEY_SHARE:
1150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001151 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1152 {
XiaokangQian52da5582022-01-26 09:49:29 +00001153 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001154 goto cleanup;
1155 }
1156
XiaokangQian53f20b72022-01-18 10:47:33 +00001157 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001158 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001159 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001160 else
XiaokangQianb851da82022-01-14 04:03:11 +00001161 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001162 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001163 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 {
1165 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001166 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001168 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001169 }
1170 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001171
1172 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001173 MBEDTLS_SSL_DEBUG_MSG(
1174 3,
1175 ( "unknown extension found: %u ( ignoring )",
1176 extension_type ) );
1177
XiaokangQian52da5582022-01-26 09:49:29 +00001178 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 }
1181
1182 p += extension_data_len;
1183 }
1184
XiaokangQiand59be772022-01-24 10:12:51 +00001185cleanup:
1186
XiaokangQian52da5582022-01-26 09:49:29 +00001187 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001188 {
1189 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1190 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1191 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1192 }
XiaokangQian52da5582022-01-26 09:49:29 +00001193 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001194 {
1195 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1196 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1197 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1198 }
1199 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001200}
1201
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001202MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001203static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001204{
Jerry Yub85277e2021-10-13 13:36:05 +08001205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001206 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001207
Jerry Yub85277e2021-10-13 13:36:05 +08001208 /* Determine the key exchange mode:
1209 * 1) If both the pre_shared_key and key_share extensions were received
1210 * then the key exchange mode is PSK with EPHEMERAL.
1211 * 2) If only the pre_shared_key extension was received then the key
1212 * exchange mode is PSK-only.
1213 * 3) If only the key_share extension was received then the key
1214 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001215 */
Jerry Yub85277e2021-10-13 13:36:05 +08001216 switch( handshake->extensions_present &
1217 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001218 {
Jerry Yu745bb612021-10-13 22:01:04 +08001219 /* Only the pre_shared_key extension was received */
1220 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001221 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001222 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001223
Jerry Yu745bb612021-10-13 22:01:04 +08001224 /* Only the key_share extension was received */
1225 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001226 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001227 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001228
Jerry Yu745bb612021-10-13 22:01:04 +08001229 /* Both the pre_shared_key and key_share extensions were received */
1230 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001231 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
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 /* Neither pre_shared_key nor key_share extension was received */
1235 default:
1236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1237 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1238 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001239 }
1240
1241 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1242 *
1243 * TODO: We don't have to do this in case we offered 0-RTT and the
1244 * server accepted it. In this case, we could skip generating
1245 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001246 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001247 if( ret != 0 )
1248 {
Jerry Yue110d252022-05-05 10:19:22 +08001249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001250 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001251 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001252 }
1253
Jerry Yuf86eb752022-05-06 11:16:55 +08001254 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001255 if( ret != 0 )
1256 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001257 MBEDTLS_SSL_DEBUG_RET( 1,
1258 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001259 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001260 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001261 }
1262
Jerry Yue110d252022-05-05 10:19:22 +08001263 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1265 ssl->session_in = ssl->session_negotiate;
1266
1267 /*
1268 * State machine update
1269 */
1270 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1271
Jerry Yu4a173382021-10-11 21:45:31 +08001272cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001273 if( ret != 0 )
1274 {
Jerry Yu4a173382021-10-11 21:45:31 +08001275 MBEDTLS_SSL_PEND_FATAL_ALERT(
1276 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1277 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1278 }
Jerry Yue110d252022-05-05 10:19:22 +08001279
Jerry Yu4a173382021-10-11 21:45:31 +08001280 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001281}
1282
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001283MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001284static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001285{
1286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1287
XiaokangQian647719a2021-12-07 09:16:29 +00001288#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1289 /* If not offering early data, the client sends a dummy CCS record
1290 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001291 * its second ClientHello or before its encrypted handshake flight.
1292 */
XiaokangQian647719a2021-12-07 09:16:29 +00001293 mbedtls_ssl_handshake_set_state( ssl,
1294 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1295#else
1296 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1297#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1298
XiaokangQian78b1fa72022-01-19 06:56:30 +00001299 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001300
XiaokangQian78b1fa72022-01-19 06:56:30 +00001301 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001302 * We are going to re-generate a shared secret corresponding to the group
1303 * selected by the server, which is different from the group for which we
1304 * generated a shared secret in the first client hello.
1305 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001306 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001307 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001308 if( ret != 0 )
1309 return( ret );
1310
1311 return( 0 );
1312}
1313
Jerry Yue1b9c292021-09-10 10:08:31 +08001314/*
Jerry Yu4a173382021-10-11 21:45:31 +08001315 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001316 * Handler for MBEDTLS_SSL_SERVER_HELLO
1317 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001318MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001319static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001320{
Jerry Yu4a173382021-10-11 21:45:31 +08001321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001322 unsigned char *buf = NULL;
1323 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001324 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001325
1326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1327
1328 /* Coordination step
1329 * - Fetch record
1330 * - Make sure it's either a ServerHello or a HRR.
1331 * - Switch processing routine in case of HRR
1332 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001333 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1334
XiaokangQian16acd4b2022-01-14 07:35:47 +00001335 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001336 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001337 goto cleanup;
1338 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001339 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001340
Ronald Cron9f0fba32022-02-10 16:45:15 +01001341 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1342 {
1343 ret = 0;
1344 goto cleanup;
1345 }
1346
XiaokangQianb851da82022-01-14 04:03:11 +00001347 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001348 buf + buf_len,
1349 is_hrr ) );
1350 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001351 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1352
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001353 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1354 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001355
XiaokangQiand9e068e2022-01-18 06:23:32 +00001356 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001357 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001358 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001359 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001360
1361cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1363 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001364 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001365}
1366
1367/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001368 *
1369 * EncryptedExtensions message
1370 *
1371 * The EncryptedExtensions message contains any extensions which
1372 * should be protected, i.e., any which are not needed to establish
1373 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001374 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001375
1376/*
1377 * Overview
1378 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001379
1380/* Main entry point; orchestrates the other functions */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001381MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001382static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001383
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001384MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001385static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1386 const unsigned char *buf,
1387 const unsigned char *end );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001388MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001389static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001390
1391/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001392 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001393 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001394MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001395static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001396{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397 int ret;
1398 unsigned char *buf;
1399 size_t buf_len;
1400
1401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1402
Xiaofei Bai746f9482021-11-12 08:53:56 +00001403 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001404 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001405 &buf, &buf_len ) );
1406
1407 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001408 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001409 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001410
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001411 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1412 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001413
XiaokangQian97799ac2021-10-11 10:05:54 +00001414 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001415
1416cleanup:
1417
1418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1419 return( ret );
1420
1421}
1422
XiaokangQian08da26c2021-10-09 10:12:11 +00001423/* Parse EncryptedExtensions message
1424 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001425 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001426 * } EncryptedExtensions;
1427 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001428MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001429static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1430 const unsigned char *buf,
1431 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001432{
1433 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001434 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001435 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001436 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001437
XiaokangQian08da26c2021-10-09 10:12:11 +00001438 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001439 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001440 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001441
XiaokangQian97799ac2021-10-11 10:05:54 +00001442 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001443 extensions_end = p + extensions_len;
1444 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001445
XiaokangQian8db25ff2021-10-13 05:56:18 +00001446 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001447 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001448 unsigned int extension_type;
1449 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001450
XiaokangQian08da26c2021-10-09 10:12:11 +00001451 /*
1452 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001453 * ExtensionType extension_type; (2 bytes)
1454 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001455 * } Extension;
1456 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001457 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001458 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1459 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1460 p += 4;
1461
XiaokangQian8db25ff2021-10-13 05:56:18 +00001462 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001463
XiaokangQian97799ac2021-10-11 10:05:54 +00001464 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001465 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001466 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001467 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001468 switch( extension_type )
1469 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001470
XiaokangQian08da26c2021-10-09 10:12:11 +00001471 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1473 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001474
lhuang0486cacac2022-01-21 07:34:27 -08001475#if defined(MBEDTLS_SSL_ALPN)
1476 case MBEDTLS_TLS_EXT_ALPN:
1477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1478
1479 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1480 {
1481 return( ret );
1482 }
1483
1484 break;
1485#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001486 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001487 MBEDTLS_SSL_DEBUG_MSG(
1488 3, ( "unsupported extension found: %u ", extension_type) );
1489 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001490 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001491 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1492 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001493 }
1494
XiaokangQian08da26c2021-10-09 10:12:11 +00001495 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001496 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001497
XiaokangQian97799ac2021-10-11 10:05:54 +00001498 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001499 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001500 {
1501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001502 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001503 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001504 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001505 }
1506
1507 return( ret );
1508}
1509
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001510MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001511static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001512{
Jerry Yua93ac112021-10-27 16:31:48 +08001513#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001514 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001515 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1516 else
Jerry Yud2674312021-10-29 10:08:19 +08001517 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001518#else
1519 ((void) ssl);
1520 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1521#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001522 return( 0 );
1523}
1524
Jerry Yua93ac112021-10-27 16:31:48 +08001525#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001526/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001527 *
1528 * STATE HANDLING: CertificateRequest
1529 *
Jerry Yud2674312021-10-29 10:08:19 +08001530 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001531#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1532#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001533/* Coordination:
1534 * Deals with the ambiguity of not knowing if a CertificateRequest
1535 * will be sent. Returns a negative code on failure, or
1536 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1537 * - SSL_CERTIFICATE_REQUEST_SKIP
1538 * indicating if a Certificate Request is expected or not.
1539 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001540MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001541static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001542{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001544
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001545 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001546 {
1547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1548 return( SSL_CERTIFICATE_REQUEST_SKIP );
1549 }
1550
1551 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001552 {
1553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1554 return( ret );
1555 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001556 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001557
1558 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1559 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1560 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001561 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001562 }
1563
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564 return( SSL_CERTIFICATE_REQUEST_SKIP );
1565}
1566
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567/*
1568 * ssl_tls13_parse_certificate_request()
1569 * Parse certificate request
1570 * struct {
1571 * opaque certificate_request_context<0..2^8-1>;
1572 * Extension extensions<2..2^16-1>;
1573 * } CertificateRequest;
1574 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001575MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001576static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1577 const unsigned char *buf,
1578 const unsigned char *end )
1579{
1580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1581 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001582 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001583 size_t extensions_len = 0;
1584 const unsigned char *extensions_end;
1585 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001586
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001587 /* ...
1588 * opaque certificate_request_context<0..2^8-1>
1589 * ...
1590 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1592 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001593 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001594
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001595 if( certificate_request_context_len > 0 )
1596 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001597 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001598 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1599 p, certificate_request_context_len );
1600
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001601 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001602 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001603 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001604 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001605 {
1606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1607 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1608 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001609 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001610 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001611 p += certificate_request_context_len;
1612 }
1613
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001614 /* ...
1615 * Extension extensions<2..2^16-1>;
1616 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001617 */
1618 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1619 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1620 p += 2;
1621
1622 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1623 extensions_end = p + extensions_len;
1624
1625 while( p < extensions_end )
1626 {
1627 unsigned int extension_type;
1628 size_t extension_data_len;
1629
1630 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1631 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1632 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1633 p += 4;
1634
1635 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1636
1637 switch( extension_type )
1638 {
1639 case MBEDTLS_TLS_EXT_SIG_ALG:
1640 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001641 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001642 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001643 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001644 if( ret != 0 )
1645 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001646 if( ! sig_alg_ext_found )
1647 sig_alg_ext_found = 1;
1648 else
1649 {
1650 MBEDTLS_SSL_DEBUG_MSG( 3,
1651 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001652 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001653 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654 break;
1655
1656 default:
1657 MBEDTLS_SSL_DEBUG_MSG(
1658 3,
1659 ( "unknown extension found: %u ( ignoring )",
1660 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001661 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001662 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001663 p += extension_data_len;
1664 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001665 /* Check that we consumed all the message. */
1666 if( p != end )
1667 {
1668 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001669 ( "CertificateRequest misaligned" ) );
1670 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001671 }
1672 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001673 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001674 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001675 MBEDTLS_SSL_DEBUG_MSG( 3,
1676 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001677 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001678 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679
Jerry Yu7840f812022-01-29 10:26:51 +08001680 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001681 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001682
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001683decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001684 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1685 MBEDTLS_ERR_SSL_DECODE_ERROR );
1686 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001687}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001688
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001689/*
1690 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1691 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001692MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001693static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001694{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001696
1697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1698
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001699 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1700
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001701 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1702 {
1703 unsigned char *buf;
1704 size_t buf_len;
1705
1706 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1707 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1708 &buf, &buf_len ) );
1709
1710 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1711 buf, buf + buf_len ) );
1712
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001713 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1714 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001715 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001716 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001717 {
1718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001719 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001720 }
1721 else
1722 {
1723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001724 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1725 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 }
1727
1728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001729 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001730
Jerry Yud2674312021-10-29 10:08:19 +08001731 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1732
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001733cleanup:
1734
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1736 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001737}
1738
1739/*
Jerry Yu687101b2021-09-14 16:03:56 +08001740 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1741 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001742MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001743static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001744{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001745 int ret;
1746
1747 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001748 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001749 return( ret );
1750
Jerry Yu687101b2021-09-14 16:03:56 +08001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1752 return( 0 );
1753}
1754
1755/*
1756 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1757 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001758MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001759static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001760{
Jerry Yu30b071c2021-09-12 20:16:03 +08001761 int ret;
1762
1763 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1764 if( ret != 0 )
1765 return( ret );
1766
Jerry Yu687101b2021-09-14 16:03:56 +08001767 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1768 return( 0 );
1769}
Jerry Yua93ac112021-10-27 16:31:48 +08001770#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001771
Jerry Yu687101b2021-09-14 16:03:56 +08001772/*
1773 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1774 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001775MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001776static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001777{
XiaokangQianac0385c2021-11-03 06:40:11 +00001778 int ret;
1779
XiaokangQianc5c39d52021-11-09 11:55:10 +00001780 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001781 if( ret != 0 )
1782 return( ret );
1783
Jerry Yue3d67cb2022-05-19 15:33:10 +08001784 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1785 if( ret != 0 )
1786 {
1787 MBEDTLS_SSL_PEND_FATAL_ALERT(
1788 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1789 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1790 return( ret );
1791 }
1792
Ronald Cron49ad6192021-11-24 16:25:31 +01001793#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1794 mbedtls_ssl_handshake_set_state(
1795 ssl,
1796 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1797#else
Jerry Yuca133a32022-02-15 14:22:05 +08001798 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001799#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001800
XiaokangQianac0385c2021-11-03 06:40:11 +00001801 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001802}
1803
1804/*
Jerry Yu566c7812022-01-26 15:41:22 +08001805 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1806 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001807MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08001808static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1809{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001810 int non_empty_certificate_msg = 0;
1811
Jerry Yu5cc35062022-01-28 16:16:08 +08001812 MBEDTLS_SSL_DEBUG_MSG( 1,
1813 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001814 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001815
Ronald Cron9df7c802022-03-08 18:38:54 +01001816#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001817 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001818 {
1819 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1820 if( ret != 0 )
1821 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001822
Ronald Cron7a94aca2022-03-09 07:44:27 +01001823 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1824 non_empty_certificate_msg = 1;
1825 }
1826 else
1827 {
1828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1829 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001830#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001831
Ronald Cron7a94aca2022-03-09 07:44:27 +01001832 if( non_empty_certificate_msg )
1833 {
1834 mbedtls_ssl_handshake_set_state( ssl,
1835 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1836 }
1837 else
1838 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1839
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001840 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001841}
1842
Ronald Cron9df7c802022-03-08 18:38:54 +01001843#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001844/*
1845 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1846 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001847MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08001848static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1849{
Ronald Crona8b38872022-03-09 07:59:25 +01001850 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1851
1852 if( ret == 0 )
1853 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1854
1855 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001856}
Jerry Yu90f152d2022-01-29 22:12:42 +08001857#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001858
1859/*
Jerry Yu687101b2021-09-14 16:03:56 +08001860 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1861 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00001863static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001864{
XiaokangQian0fa66432021-11-15 03:33:57 +00001865 int ret;
1866
1867 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1868 if( ret != 0 )
1869 return( ret );
1870
Jerry Yue3d67cb2022-05-19 15:33:10 +08001871 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1872 if( ret != 0 )
1873 {
1874 MBEDTLS_SSL_DEBUG_RET( 1,
1875 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1876 return ( ret );
1877 }
1878
XiaokangQian0fa66432021-11-15 03:33:57 +00001879 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1880 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001881}
1882
1883/*
1884 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1885 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001886MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001887static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001888{
Jerry Yu378254d2021-10-30 21:44:47 +08001889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001890 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001891 return( 0 );
1892}
1893
1894/*
1895 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1896 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001897MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001898static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001899{
Jerry Yu378254d2021-10-30 21:44:47 +08001900
1901 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1902
1903 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1904 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001905}
1906
Jerry Yu92c6b402021-08-27 16:59:09 +08001907int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001908{
Jerry Yu92c6b402021-08-27 16:59:09 +08001909 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001910
Jerry Yu92c6b402021-08-27 16:59:09 +08001911 switch( ssl->state )
1912 {
1913 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001914 * ssl->state is initialized as HELLO_REQUEST. It is the same
1915 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001916 */
1917 case MBEDTLS_SSL_HELLO_REQUEST:
1918 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001919 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001920 break;
1921
1922 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001923 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001924 break;
1925
1926 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001927 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001928 break;
1929
Jerry Yua93ac112021-10-27 16:31:48 +08001930#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001931 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1932 ret = ssl_tls13_process_certificate_request( ssl );
1933 break;
1934
Jerry Yu687101b2021-09-14 16:03:56 +08001935 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001936 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001937 break;
1938
1939 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001940 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001941 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001942#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001943
1944 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001945 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001946 break;
1947
Jerry Yu566c7812022-01-26 15:41:22 +08001948 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1949 ret = ssl_tls13_write_client_certificate( ssl );
1950 break;
1951
Ronald Cron9df7c802022-03-08 18:38:54 +01001952#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001953 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1954 ret = ssl_tls13_write_client_certificate_verify( ssl );
1955 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001956#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001957
Jerry Yu687101b2021-09-14 16:03:56 +08001958 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001959 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001960 break;
1961
1962 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001963 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001964 break;
1965
1966 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001967 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001968 break;
1969
Ronald Cron49ad6192021-11-24 16:25:31 +01001970 /*
1971 * Injection of dummy-CCS's for middlebox compatibility
1972 */
1973#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001974 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001975 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1976 if( ret == 0 )
1977 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1978 break;
1979
1980 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1981 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1982 if( ret == 0 )
1983 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001984 break;
1985#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1986
Jerry Yu92c6b402021-08-27 16:59:09 +08001987 default:
1988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1989 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1990 }
1991
1992 return( ret );
1993}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001994
Jerry Yufb4b6472022-01-27 15:03:26 +08001995#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001996
Jerry Yufb4b6472022-01-27 15:03:26 +08001997