blob: 943b9d893d28d71a51e0be021017ac3612ac5cc6 [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 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000048 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040051 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
52 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080053
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020062 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Ronald Crona77fc272022-03-30 17:20:47 +020064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010065
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010067 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080068 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010071 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu0c63af62021-09-02 12:59:12 +080073 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080074 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010075 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080076 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040077 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Glenn Strausscd78df62022-04-07 19:07:11 -040082 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010083 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040084 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
87 }
88
89 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Ronald Cron98473382022-03-30 20:04:10 +0200100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400101 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
102 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
Ronald Cron98473382022-03-30 20:04:10 +0200111 if( &buf[2] != end )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
115 MBEDTLS_ERR_SSL_DECODE_ERROR );
116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
117 }
118
Jerry Yue1b9c292021-09-10 10:08:31 +0800119 return( 0 );
120}
121
lhuang0486cacac2022-01-21 07:34:27 -0800122#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800123static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
124 const unsigned char *buf, size_t len )
125{
126 size_t list_len, name_len;
127 const unsigned char *p = buf;
128 const unsigned char *end = buf + len;
129
130 /* If we didn't send it, the server shouldn't send it */
131 if( ssl->conf->alpn_list == NULL )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
134 /*
135 * opaque ProtocolName<1..2^8-1>;
136 *
137 * struct {
138 * ProtocolName protocol_name_list<2..2^16-1>
139 * } ProtocolNameList;
140 *
141 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
142 */
143
144 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
146
147 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
148 p += 2;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
150
151 name_len = *p++;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
153
154 /* Check that the server chosen protocol was in our list and save it */
155 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
156 {
157 if( name_len == strlen( *alpn ) &&
158 memcmp( buf + 3, *alpn, name_len ) == 0 )
159 {
160 ssl->alpn_chosen = *alpn;
161 return( 0 );
162 }
163 }
164
165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
166}
167#endif /* MBEDTLS_SSL_ALPN */
168
XiaokangQian16acd4b2022-01-14 07:35:47 +0000169static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000170{
171 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( group_id == 0 )
174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
175
XiaokangQian355e09a2022-01-20 11:14:50 +0000176#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000178 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
181
182 /* Destroy generated private key. */
183 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
184 if( status != PSA_SUCCESS )
185 {
186 ret = psa_ssl_status_to_mbedtls( status );
187 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
188 return( ret );
189 }
190
191 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000192 return( 0 );
193 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000194 else
195#endif /* MBEDTLS_ECDH_C */
196 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000197 {
198 /* Do something */
199 }
200
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202}
203
204/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205 * Functions for writing key_share extension.
206 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
208 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209{
210 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
211
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100214 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800215 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100216 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
218
Brett Warren14efd332021-10-06 09:32:11 +0100219 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 const mbedtls_ecp_curve_info *curve_info;
222 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
223 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100224 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Brett Warren14efd332021-10-06 09:32:11 +0100226 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 return( 0 );
228 }
229 }
230#else
231 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233#endif /* MBEDTLS_ECDH_C */
234
235 /*
236 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800237 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 */
239
240 return( ret );
241}
242
243/*
244 * ssl_tls13_write_key_share_ext
245 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 *
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 * struct {
253 * KeyShareEntry client_shares<0..2^16-1>;
254 * } KeyShareClientHello;
255 */
256static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
257 unsigned char *buf,
258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260{
261 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000262 unsigned char *client_shares; /* Start of client_shares */
263 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 * - extension_type (2 bytes)
271 * - extension_data_length (2 bytes)
272 * - client_shares_length (2 bytes)
273 */
274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
275 p += 6;
276
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
278
279 /* HRR could already have requested something else. */
280 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800281 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
282 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 &group_id ) );
286 }
287
288 /*
289 * Dispatch to type-specific key generation function.
290 *
291 * So far, we're only supporting ECDHE. With the introduction
292 * of PQC KEMs, we'll want to have multiple branches, one per
293 * type of KEM, and dispatch to the corresponding crypto. And
294 * only one key share entry is allowed.
295 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000301 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100303 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304
305 /* Check there is space for header of KeyShareEntry
306 * - group (2 bytes)
307 * - key_exchange_length (2 bytes)
308 */
309 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
310 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800311 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
312 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800313 p += key_exchange_len;
314 if( ret != 0 )
315 return( ret );
316
317 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000318 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000320 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 }
322 else
323#endif /* MBEDTLS_ECDH_C */
324 if( 0 /* other KEMs? */ )
325 {
326 /* Do something */
327 }
328 else
329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000332 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800333 if( client_shares_len == 0)
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800337 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write extension_type */
339 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
340 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344
345 /* Update offered_group_id field */
346 ssl->handshake->offered_group_id = group_id;
347
348 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000349 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
354
355cleanup:
356
357 return( ret );
358}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359
Jerry Yue1b9c292021-09-10 10:08:31 +0800360
XiaokangQiand59be772022-01-24 10:12:51 +0000361/*
362 * ssl_tls13_parse_hrr_key_share_ext()
363 * Parse key_share extension in Hello Retry Request
364 *
365 * struct {
366 * NamedGroup selected_group;
367 * } KeyShareHelloRetryRequest;
368 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000369static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000370 const unsigned char *buf,
371 const unsigned char *end )
372{
XiaokangQianb851da82022-01-14 04:03:11 +0000373 const mbedtls_ecp_curve_info *curve_info = NULL;
374 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000375 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000376 int found = 0;
377
378 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
379 if( group_list == NULL )
380 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
381
382 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
383
384 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000386 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000388
389 /* Upon receipt of this extension in a HelloRetryRequest, the client
390 * MUST first verify that the selected_group field corresponds to a
391 * group which was provided in the "supported_groups" extension in the
392 * original ClientHello.
393 * The supported_group was based on the info in ssl->conf->group_list.
394 *
395 * If the server provided a key share that was not sent in the ClientHello
396 * then the client MUST abort the handshake with an "illegal_parameter" alert.
397 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000398 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000399 {
400 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000401 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000402 continue;
403
404 /* We found a match */
405 found = 1;
406 break;
407 }
408
409 /* Client MUST verify that the selected_group field does not
410 * correspond to a group which was provided in the "key_share"
411 * extension in the original ClientHello. If the server sent an
412 * HRR message with a key share already provided in the
413 * ClientHello then the client MUST abort the handshake with
414 * an "illegal_parameter" alert.
415 */
XiaokangQiand59be772022-01-24 10:12:51 +0000416 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
419 MBEDTLS_SSL_PEND_FATAL_ALERT(
420 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
421 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
422 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
423 }
424
425 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000426 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000427
428 return( 0 );
429}
430
Jerry Yue1b9c292021-09-10 10:08:31 +0800431/*
Jerry Yub85277e2021-10-13 13:36:05 +0800432 * ssl_tls13_parse_key_share_ext()
433 * Parse key_share extension in Server Hello
434 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800435 * struct {
436 * KeyShareEntry server_share;
437 * } KeyShareServerHello;
438 * struct {
439 * NamedGroup group;
440 * opaque key_exchange<1..2^16-1>;
441 * } KeyShareEntry;
442 */
Jerry Yu4a173382021-10-11 21:45:31 +0800443static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 const unsigned char *buf,
445 const unsigned char *end )
446{
Jerry Yub85277e2021-10-13 13:36:05 +0800447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800448 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800449 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450
Jerry Yu4a173382021-10-11 21:45:31 +0800451 /* ...
452 * NamedGroup group; (2 bytes)
453 * ...
454 */
455 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
456 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800457 p += 2;
458
Jerry Yu4a173382021-10-11 21:45:31 +0800459 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800460 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800461 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1,
464 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800465 (unsigned) offered_group, (unsigned) group ) );
466 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
467 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
468 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 }
470
471#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800472 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800473 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100474 const mbedtls_ecp_curve_info *curve_info =
475 mbedtls_ecp_curve_info_from_tls_id( group );
476 if( curve_info == NULL )
477 {
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
480 }
481
482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
483
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000484 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800485 if( ret != 0 )
486 return( ret );
487 }
Jerry Yub85277e2021-10-13 13:36:05 +0800488 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800489#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800490 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 {
492 /* Do something */
493 }
494 else
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
496
497 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
498 return( ret );
499}
500
XiaokangQiand59be772022-01-24 10:12:51 +0000501/*
502 * ssl_tls13_parse_cookie_ext()
503 * Parse cookie extension in Hello Retry Request
504 *
505 * struct {
506 * opaque cookie<1..2^16-1>;
507 * } Cookie;
508 *
509 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
510 * extension to the client (this is an exception to the usual rule that
511 * the only extensions that may be sent are those that appear in the
512 * ClientHello). When sending the new ClientHello, the client MUST copy
513 * the contents of the extension received in the HelloRetryRequest into
514 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
515 * cookies in their initial ClientHello in subsequent connections.
516 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000517static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
518 const unsigned char *buf,
519 const unsigned char *end )
520{
XiaokangQian25c9c902022-02-08 10:49:53 +0000521 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000522 const unsigned char *p = buf;
523 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
524
525 /* Retrieve length field of cookie */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
527 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
528 p += 2;
529
530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
531 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
532
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000533 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000534 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000535 handshake->cookie = mbedtls_calloc( 1, cookie_len );
536 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000537 {
538 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000539 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000540 cookie_len ) );
541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
542 }
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000546
547 return( 0 );
548}
XiaokangQian43550bd2022-01-21 04:32:58 +0000549
XiaokangQian0b64eed2022-01-27 10:36:51 +0000550static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000551 unsigned char *buf,
552 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000553 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000554{
555 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000556 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000558
XiaokangQianc02768a2022-02-10 07:31:25 +0000559 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560 {
561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
562 return( 0 );
563 }
564
565 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000566 handshake->cookie,
567 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000568
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
572
XiaokangQian0b64eed2022-01-27 10:36:51 +0000573 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000574 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
575 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000576 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000577
578 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 return( 0 );
584}
585
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 unsigned char *end,
589 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 unsigned char *p = buf;
593 size_t ext_len;
594
595 *out_len = 0;
596
597 /* Write supported_versions extension
598 *
599 * Supported Versions Extension is mandatory with TLS 1.3.
600 */
601 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
602 if( ret != 0 )
603 return( ret );
604 p += ext_len;
605
606 /* Echo the cookie if the server provided one in its preceding
607 * HelloRetryRequest message.
608 */
609 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
610 if( ret != 0 )
611 return( ret );
612 p += ext_len;
613
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100614 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
615 {
616 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
617 if( ret != 0 )
618 return( ret );
619 p += ext_len;
620 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100621
622 *out_len = p - buf;
623
624 return( 0 );
625}
626
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800627/*
Jerry Yu4a173382021-10-11 21:45:31 +0800628 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800629 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200630
Ronald Cronda41b382022-03-30 09:57:11 +0200631/**
632 * \brief Detect if the ServerHello contains a supported_versions extension
633 * or not.
634 *
635 * \param[in] ssl SSL context
636 * \param[in] buf Buffer containing the ServerHello message
637 * \param[in] end End of the buffer containing the ServerHello message
638 *
639 * \return 0 if the ServerHello does not contain a supported_versions extension
640 * \return 1 if the ServerHello contains a supported_versions extension
641 * \return A negative value if an error occurred while parsing the ServerHello.
642 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100643static int ssl_tls13_is_supported_versions_ext_present(
644 mbedtls_ssl_context *ssl,
645 const unsigned char *buf,
646 const unsigned char *end )
647{
648 const unsigned char *p = buf;
649 size_t legacy_session_id_echo_len;
650 size_t extensions_len;
651 const unsigned char *extensions_end;
652
653 /*
654 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200655 * length:
656 * - legacy_version 2 bytes
657 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
658 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100659 */
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
661 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
662 legacy_session_id_echo_len = *p;
663
664 /*
665 * Jump to the extensions, jumping over:
666 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
667 * - cipher_suite 2 bytes
668 * - legacy_compression_method 1 byte
669 */
Ronald Cron28271062022-06-10 14:43:55 +0200670 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100671 p += legacy_session_id_echo_len + 4;
672
673 /* Case of no extension */
674 if( p == end )
675 return( 0 );
676
677 /* ...
678 * Extension extensions<6..2^16-1>;
679 * ...
680 * struct {
681 * ExtensionType extension_type; (2 bytes)
682 * opaque extension_data<0..2^16-1>;
683 * } Extension;
684 */
685 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
686 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
687 p += 2;
688
689 /* Check extensions do not go beyond the buffer of data. */
690 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
691 extensions_end = p + extensions_len;
692
693 while( p < extensions_end )
694 {
695 unsigned int extension_type;
696 size_t extension_data_len;
697
698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
699 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
700 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
701 p += 4;
702
703 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
704 return( 1 );
705
706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
707 p += extension_data_len;
708 }
709
710 return( 0 );
711}
712
Jerry Yu7a186a02021-10-15 18:46:14 +0800713/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200714 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
715 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
716 * - 0 otherwise
717 */
718static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
719 const unsigned char *buf,
720 const unsigned char *end )
721{
722 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
723 static const unsigned char magic_downgrade_string[] =
724 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
725 const unsigned char *last_eight_bytes_of_random;
726 unsigned char last_byte_of_random;
727
728 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
729 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
730
731 if( memcmp( last_eight_bytes_of_random,
732 magic_downgrade_string,
733 sizeof( magic_downgrade_string ) ) == 0 )
734 {
735 last_byte_of_random = last_eight_bytes_of_random[7];
736 return( last_byte_of_random == 0 ||
737 last_byte_of_random == 1 );
738 }
739
740 return( 0 );
741}
742
743/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800744 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
745 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000746 * to indicate which message is expected and to be parsed next.
747 */
Jerry Yub85277e2021-10-13 13:36:05 +0800748#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
749#define SSL_SERVER_HELLO_COORDINATE_HRR 1
750static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
751 const unsigned char *buf,
752 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800753{
Jerry Yue1b9c292021-09-10 10:08:31 +0800754
755 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
756 *
Jerry Yu4a173382021-10-11 21:45:31 +0800757 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800758 * special value of the SHA-256 of "HelloRetryRequest".
759 *
760 * struct {
761 * ProtocolVersion legacy_version = 0x0303;
762 * Random random;
763 * opaque legacy_session_id_echo<0..32>;
764 * CipherSuite cipher_suite;
765 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800766 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800767 * } ServerHello;
768 *
769 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800770 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
771 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800772
Jerry Yu93a13f22022-04-11 23:00:01 +0800773 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
774 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800775 {
Jerry Yub85277e2021-10-13 13:36:05 +0800776 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800777 }
778
Jerry Yub85277e2021-10-13 13:36:05 +0800779 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800780}
781
Jerry Yu745bb612021-10-13 22:01:04 +0800782/* Fetch and preprocess
783 * Returns a negative value on failure, and otherwise
784 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100785 * - SSL_SERVER_HELLO_COORDINATE_HRR or
786 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800787 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100788#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800789static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
790 unsigned char **buf,
791 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800792{
Jerry Yu4a173382021-10-11 21:45:31 +0800793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200794 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800795
XiaokangQian355e09a2022-01-20 11:14:50 +0000796 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
797 MBEDTLS_SSL_HS_SERVER_HELLO,
798 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200799 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800800
Ronald Cron9f0fba32022-02-10 16:45:15 +0100801 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200802 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100803 if( ret == 0 )
804 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200805 MBEDTLS_SSL_PROC_CHK_NEG(
806 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
807
808 /* If the server is negotiating TLS 1.2 or below and:
809 * . we did not propose TLS 1.2 or
810 * . the server responded it is TLS 1.3 capable but negotiating a lower
811 * version of the protocol and thus we are under downgrade attack
812 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100813 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200814 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100815 {
816 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
817 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
818 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
819 }
820
821 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400822 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100823 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
824 *buf, *buf_len );
825
826 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
827 {
828 ret = ssl_tls13_reset_key_share( ssl );
829 if( ret != 0 )
830 return( ret );
831 }
832
833 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
834 }
835
Ronald Cronfd6193c2022-04-05 11:04:20 +0200836 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800837 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 {
Jerry Yu745bb612021-10-13 22:01:04 +0800839 case SSL_SERVER_HELLO_COORDINATE_HELLO:
840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
841 break;
842 case SSL_SERVER_HELLO_COORDINATE_HRR:
843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000844 /* If a client receives a second
845 * HelloRetryRequest in the same connection (i.e., where the ClientHello
846 * was itself in response to a HelloRetryRequest), it MUST abort the
847 * handshake with an "unexpected_message" alert.
848 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000849 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000850 {
851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
852 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
853 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
855 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000856 /*
857 * Clients must abort the handshake with an "illegal_parameter"
858 * alert if the HelloRetryRequest would not result in any change
859 * in the ClientHello.
860 * In a PSK only key exchange that what we expect.
861 */
862 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
863 {
864 MBEDTLS_SSL_DEBUG_MSG( 1,
865 ( "Unexpected HRR in pure PSK key exchange." ) );
866 MBEDTLS_SSL_PEND_FATAL_ALERT(
867 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
868 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
869 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
870 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000871
XiaokangQiand9e068e2022-01-18 06:23:32 +0000872 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000873
Jerry Yu745bb612021-10-13 22:01:04 +0800874 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800875 }
876
877cleanup:
878
879 return( ret );
880}
881
Jerry Yu4a173382021-10-11 21:45:31 +0800882static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
883 const unsigned char **buf,
884 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800885{
886 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800887 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800888
Jerry Yude4fb2c2021-09-19 18:05:08 +0800889 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800890 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800891
Jerry Yu4a173382021-10-11 21:45:31 +0800892 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800893
894 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800895 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
896 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800898 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
899 ssl->session_negotiate->id,
900 ssl->session_negotiate->id_len );
901 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800902 legacy_session_id_echo_len );
903
904 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
905 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
906
Jerry Yue1b9c292021-09-10 10:08:31 +0800907 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
908 }
909
Jerry Yu4a173382021-10-11 21:45:31 +0800910 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 *buf = p;
912
913 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800914 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 return( 0 );
916}
917
Jerry Yue1b9c292021-09-10 10:08:31 +0800918/* Parse ServerHello message and configure context
919 *
920 * struct {
921 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
922 * Random random;
923 * opaque legacy_session_id_echo<0..32>;
924 * CipherSuite cipher_suite;
925 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800926 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800927 * } ServerHello;
928 */
Jerry Yuc068b662021-10-11 22:30:19 +0800929static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
930 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000931 const unsigned char *end,
932 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800933{
Jerry Yub85277e2021-10-13 13:36:05 +0800934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800935 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000936 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800937 size_t extensions_len;
938 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800939 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000941 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800942
943 /*
944 * Check there is space for minimal fields
945 *
946 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800947 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800948 * - legacy_session_id_echo ( 1 byte ), minimum size
949 * - cipher_suite ( 2 bytes)
950 * - legacy_compression_method ( 1 byte )
951 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800952 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800953
954 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
956
Jerry Yu4a173382021-10-11 21:45:31 +0800957 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800958 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800959 * ...
960 * with ProtocolVersion defined as:
961 * uint16 ProtocolVersion;
962 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400963 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
964 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 {
966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
967 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
968 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000969 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
970 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800971 }
972 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800974 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800975 * Random random;
976 * ...
977 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800978 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800979 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000980 if( !is_hrr )
981 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000982 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000983 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
984 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
985 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
986 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800987 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988
Jerry Yu4a173382021-10-11 21:45:31 +0800989 /* ...
990 * opaque legacy_session_id_echo<0..32>;
991 * ...
992 */
993 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 {
XiaokangQian52da5582022-01-26 09:49:29 +0000995 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000996 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997 }
998
Jerry Yu4a173382021-10-11 21:45:31 +0800999 /* ...
1000 * CipherSuite cipher_suite;
1001 * ...
1002 * with CipherSuite defined as:
1003 * uint8 CipherSuite[2];
1004 */
1005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001006 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1007 p += 2;
1008
Jerry Yu4a173382021-10-11 21:45:31 +08001009
XiaokangQian355e09a2022-01-20 11:14:50 +00001010 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001011 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001012 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001013 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001014 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1015 ssl->tls_version,
1016 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001017 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 {
XiaokangQian52da5582022-01-26 09:49:29 +00001019 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001021 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001022 * If we received an HRR before and that the proposed selected
1023 * ciphersuite in this server hello is not the same as the one
1024 * proposed in the HRR, we abort the handshake and send an
1025 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001026 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001027 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1028 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001029 {
XiaokangQian52da5582022-01-26 09:49:29 +00001030 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001031 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001032
XiaokangQian52da5582022-01-26 09:49:29 +00001033 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001034 {
1035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1036 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001037 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001038 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001039
Jerry Yu4a173382021-10-11 21:45:31 +08001040 /* Configure ciphersuites */
1041 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1042
XiaokangQian355e09a2022-01-20 11:14:50 +00001043 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 ssl->session_negotiate->ciphersuite = cipher_suite;
1045
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1047 cipher_suite, ciphersuite_info->name ) );
1048
1049#if defined(MBEDTLS_HAVE_TIME)
1050 ssl->session_negotiate->start = time( NULL );
1051#endif /* MBEDTLS_HAVE_TIME */
1052
Jerry Yu4a173382021-10-11 21:45:31 +08001053 /* ...
1054 * uint8 legacy_compression_method = 0;
1055 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001056 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001057 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 if( p[0] != 0 )
1059 {
Jerry Yub85277e2021-10-13 13:36:05 +08001060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001061 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001062 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001063 }
1064 p++;
1065
Jerry Yub85277e2021-10-13 13:36:05 +08001066 /* ...
1067 * Extension extensions<6..2^16-1>;
1068 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001069 * struct {
1070 * ExtensionType extension_type; (2 bytes)
1071 * opaque extension_data<0..2^16-1>;
1072 * } Extension;
1073 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001074 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001075 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001076 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001077
Jerry Yu4a173382021-10-11 21:45:31 +08001078 /* Check extensions do not go beyond the buffer of data. */
1079 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1080 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081
Jerry Yu4a173382021-10-11 21:45:31 +08001082 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001083
Jerry Yu4a173382021-10-11 21:45:31 +08001084 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 {
1086 unsigned int extension_type;
1087 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001088 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001089
Jerry Yu4a173382021-10-11 21:45:31 +08001090 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1092 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1093 p += 4;
1094
Jerry Yu4a173382021-10-11 21:45:31 +08001095 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001096 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001097
1098 switch( extension_type )
1099 {
XiaokangQianb851da82022-01-14 04:03:11 +00001100 case MBEDTLS_TLS_EXT_COOKIE:
1101
XiaokangQiand9e068e2022-01-18 06:23:32 +00001102 if( !is_hrr )
1103 {
XiaokangQian52da5582022-01-26 09:49:29 +00001104 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001105 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001106 }
1107
XiaokangQian43550bd2022-01-21 04:32:58 +00001108 ret = ssl_tls13_parse_cookie_ext( ssl,
1109 p, extension_data_end );
1110 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001111 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001112 MBEDTLS_SSL_DEBUG_RET( 1,
1113 "ssl_tls13_parse_cookie_ext",
1114 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001115 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001116 }
XiaokangQianb851da82022-01-14 04:03:11 +00001117 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001118
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001120 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001121 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001122 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001124 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001125 break;
1126
1127 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001130
XiaokangQian52da5582022-01-26 09:49:29 +00001131 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001132 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001133
Jerry Yue1b9c292021-09-10 10:08:31 +08001134 case MBEDTLS_TLS_EXT_KEY_SHARE:
1135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001136 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1137 {
XiaokangQian52da5582022-01-26 09:49:29 +00001138 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
1140 }
1141
XiaokangQian53f20b72022-01-18 10:47:33 +00001142 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001143 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001144 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001145 else
XiaokangQianb851da82022-01-14 04:03:11 +00001146 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001147 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001148 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 {
1150 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001151 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001152 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001153 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 }
1155 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156
1157 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001158 MBEDTLS_SSL_DEBUG_MSG(
1159 3,
1160 ( "unknown extension found: %u ( ignoring )",
1161 extension_type ) );
1162
XiaokangQian52da5582022-01-26 09:49:29 +00001163 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001164 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 }
1166
1167 p += extension_data_len;
1168 }
1169
XiaokangQiand59be772022-01-24 10:12:51 +00001170cleanup:
1171
XiaokangQian52da5582022-01-26 09:49:29 +00001172 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001173 {
1174 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1175 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1176 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1177 }
XiaokangQian52da5582022-01-26 09:49:29 +00001178 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001179 {
1180 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1181 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1182 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1183 }
1184 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001185}
1186
XiaokangQian355e09a2022-01-20 11:14:50 +00001187static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001188{
Jerry Yub85277e2021-10-13 13:36:05 +08001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001190 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001191
Jerry Yub85277e2021-10-13 13:36:05 +08001192 /* Determine the key exchange mode:
1193 * 1) If both the pre_shared_key and key_share extensions were received
1194 * then the key exchange mode is PSK with EPHEMERAL.
1195 * 2) If only the pre_shared_key extension was received then the key
1196 * exchange mode is PSK-only.
1197 * 3) If only the key_share extension was received then the key
1198 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001199 */
Jerry Yub85277e2021-10-13 13:36:05 +08001200 switch( handshake->extensions_present &
1201 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001202 {
Jerry Yu745bb612021-10-13 22:01:04 +08001203 /* Only the pre_shared_key extension was received */
1204 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001205 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001206 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001207
Jerry Yu745bb612021-10-13 22:01:04 +08001208 /* Only the key_share extension was received */
1209 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001210 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001211 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001212
Jerry Yu745bb612021-10-13 22:01:04 +08001213 /* Both the pre_shared_key and key_share extensions were received */
1214 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001215 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001216 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001217
Jerry Yu745bb612021-10-13 22:01:04 +08001218 /* Neither pre_shared_key nor key_share extension was received */
1219 default:
1220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1221 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1222 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001223 }
1224
1225 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1226 *
1227 * TODO: We don't have to do this in case we offered 0-RTT and the
1228 * server accepted it. In this case, we could skip generating
1229 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001230 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001231 if( ret != 0 )
1232 {
Jerry Yue110d252022-05-05 10:19:22 +08001233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001234 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001235 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001236 }
1237
Jerry Yuf86eb752022-05-06 11:16:55 +08001238 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001239 if( ret != 0 )
1240 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001241 MBEDTLS_SSL_DEBUG_RET( 1,
1242 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001243 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001244 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001245 }
1246
Jerry Yue110d252022-05-05 10:19:22 +08001247 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1249 ssl->session_in = ssl->session_negotiate;
1250
1251 /*
1252 * State machine update
1253 */
1254 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1255
Jerry Yu4a173382021-10-11 21:45:31 +08001256cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001257 if( ret != 0 )
1258 {
Jerry Yu4a173382021-10-11 21:45:31 +08001259 MBEDTLS_SSL_PEND_FATAL_ALERT(
1260 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1261 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1262 }
Jerry Yue110d252022-05-05 10:19:22 +08001263
Jerry Yu4a173382021-10-11 21:45:31 +08001264 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001265}
1266
XiaokangQian355e09a2022-01-20 11:14:50 +00001267static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001268{
1269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1270
XiaokangQian647719a2021-12-07 09:16:29 +00001271#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1272 /* If not offering early data, the client sends a dummy CCS record
1273 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001274 * its second ClientHello or before its encrypted handshake flight.
1275 */
XiaokangQian647719a2021-12-07 09:16:29 +00001276 mbedtls_ssl_handshake_set_state( ssl,
1277 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1278#else
1279 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1280#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1281
XiaokangQian78b1fa72022-01-19 06:56:30 +00001282 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001283
XiaokangQian78b1fa72022-01-19 06:56:30 +00001284 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001285 * We are going to re-generate a shared secret corresponding to the group
1286 * selected by the server, which is different from the group for which we
1287 * generated a shared secret in the first client hello.
1288 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001289 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001290 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001291 if( ret != 0 )
1292 return( ret );
1293
1294 return( 0 );
1295}
1296
Jerry Yue1b9c292021-09-10 10:08:31 +08001297/*
Jerry Yu4a173382021-10-11 21:45:31 +08001298 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001299 * Handler for MBEDTLS_SSL_SERVER_HELLO
1300 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001301static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001302{
Jerry Yu4a173382021-10-11 21:45:31 +08001303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001304 unsigned char *buf = NULL;
1305 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001306 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001307
1308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1309
1310 /* Coordination step
1311 * - Fetch record
1312 * - Make sure it's either a ServerHello or a HRR.
1313 * - Switch processing routine in case of HRR
1314 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001315 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1316
XiaokangQian16acd4b2022-01-14 07:35:47 +00001317 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001318 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001319 goto cleanup;
1320 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001321 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001322
Ronald Cron9f0fba32022-02-10 16:45:15 +01001323 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1324 {
1325 ret = 0;
1326 goto cleanup;
1327 }
1328
XiaokangQianb851da82022-01-14 04:03:11 +00001329 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001330 buf + buf_len,
1331 is_hrr ) );
1332 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001333 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1334
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001335 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1336 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001337
XiaokangQiand9e068e2022-01-18 06:23:32 +00001338 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001339 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001340 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001341 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001342
1343cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1345 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001346 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001347}
1348
1349/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001350 *
1351 * EncryptedExtensions message
1352 *
1353 * The EncryptedExtensions message contains any extensions which
1354 * should be protected, i.e., any which are not needed to establish
1355 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001356 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001357
1358/*
1359 * Overview
1360 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001361
1362/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001363static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001364
XiaokangQian97799ac2021-10-11 10:05:54 +00001365static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1366 const unsigned char *buf,
1367 const unsigned char *end );
1368static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001369
1370/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001371 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001372 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001373static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001374{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001375 int ret;
1376 unsigned char *buf;
1377 size_t buf_len;
1378
1379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1380
Xiaofei Bai746f9482021-11-12 08:53:56 +00001381 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001382 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001383 &buf, &buf_len ) );
1384
1385 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001386 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001387 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001388
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001389 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1390 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001391
XiaokangQian97799ac2021-10-11 10:05:54 +00001392 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001393
1394cleanup:
1395
1396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1397 return( ret );
1398
1399}
1400
XiaokangQian08da26c2021-10-09 10:12:11 +00001401/* Parse EncryptedExtensions message
1402 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001403 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001404 * } EncryptedExtensions;
1405 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001406static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1407 const unsigned char *buf,
1408 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001409{
1410 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001411 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001412 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001413 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001414
XiaokangQian08da26c2021-10-09 10:12:11 +00001415 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001416 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001417 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001418
XiaokangQian97799ac2021-10-11 10:05:54 +00001419 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001420 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001421 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001422
XiaokangQian8db25ff2021-10-13 05:56:18 +00001423 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001424 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001425 unsigned int extension_type;
1426 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001427
XiaokangQian08da26c2021-10-09 10:12:11 +00001428 /*
1429 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001430 * ExtensionType extension_type; (2 bytes)
1431 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001432 * } Extension;
1433 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001434 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001435 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1436 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1437 p += 4;
1438
XiaokangQian8db25ff2021-10-13 05:56:18 +00001439 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001440
XiaokangQian97799ac2021-10-11 10:05:54 +00001441 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001442 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001443 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001444 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001445 switch( extension_type )
1446 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001447
XiaokangQian08da26c2021-10-09 10:12:11 +00001448 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1450 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001451
lhuang0486cacac2022-01-21 07:34:27 -08001452#if defined(MBEDTLS_SSL_ALPN)
1453 case MBEDTLS_TLS_EXT_ALPN:
1454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1455
1456 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1457 {
1458 return( ret );
1459 }
1460
1461 break;
1462#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001463 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001464 MBEDTLS_SSL_DEBUG_MSG(
1465 3, ( "unsupported extension found: %u ", extension_type) );
1466 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001467 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001468 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1469 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001470 }
1471
XiaokangQian08da26c2021-10-09 10:12:11 +00001472 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001473 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001474
XiaokangQian97799ac2021-10-11 10:05:54 +00001475 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001476 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001477 {
1478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001479 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001480 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001481 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001482 }
1483
1484 return( ret );
1485}
1486
XiaokangQian97799ac2021-10-11 10:05:54 +00001487static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001488{
Jerry Yua93ac112021-10-27 16:31:48 +08001489#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001490 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001491 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1492 else
Jerry Yud2674312021-10-29 10:08:19 +08001493 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001494#else
1495 ((void) ssl);
1496 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1497#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001498 return( 0 );
1499}
1500
Jerry Yua93ac112021-10-27 16:31:48 +08001501#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001502/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001503 *
1504 * STATE HANDLING: CertificateRequest
1505 *
Jerry Yud2674312021-10-29 10:08:19 +08001506 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001507#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1508#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001509/* Coordination:
1510 * Deals with the ambiguity of not knowing if a CertificateRequest
1511 * will be sent. Returns a negative code on failure, or
1512 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1513 * - SSL_CERTIFICATE_REQUEST_SKIP
1514 * indicating if a Certificate Request is expected or not.
1515 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001516static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001517{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001519
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001520 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001521 {
1522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1523 return( SSL_CERTIFICATE_REQUEST_SKIP );
1524 }
1525
1526 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001527 {
1528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1529 return( ret );
1530 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001531 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001532
1533 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1534 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1535 {
Ronald Cron19385882022-06-15 16:26:13 +02001536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001537 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001538 }
1539
Ronald Cron19385882022-06-15 16:26:13 +02001540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1541
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001542 return( SSL_CERTIFICATE_REQUEST_SKIP );
1543}
1544
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001545/*
1546 * ssl_tls13_parse_certificate_request()
1547 * Parse certificate request
1548 * struct {
1549 * opaque certificate_request_context<0..2^8-1>;
1550 * Extension extensions<2..2^16-1>;
1551 * } CertificateRequest;
1552 */
1553static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1554 const unsigned char *buf,
1555 const unsigned char *end )
1556{
1557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1558 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001560 size_t extensions_len = 0;
1561 const unsigned char *extensions_end;
1562 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001563
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001564 /* ...
1565 * opaque certificate_request_context<0..2^8-1>
1566 * ...
1567 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1569 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001570 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001572 if( certificate_request_context_len > 0 )
1573 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001574 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001575 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1576 p, certificate_request_context_len );
1577
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001578 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001579 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001580 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001581 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001582 {
1583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1584 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1585 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001586 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001587 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001588 p += certificate_request_context_len;
1589 }
1590
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001591 /* ...
1592 * Extension extensions<2..2^16-1>;
1593 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001594 */
1595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1596 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1597 p += 2;
1598
1599 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1600 extensions_end = p + extensions_len;
1601
1602 while( p < extensions_end )
1603 {
1604 unsigned int extension_type;
1605 size_t extension_data_len;
1606
1607 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1608 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1609 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1610 p += 4;
1611
1612 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1613
1614 switch( extension_type )
1615 {
1616 case MBEDTLS_TLS_EXT_SIG_ALG:
1617 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001618 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001619 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001620 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001621 if( ret != 0 )
1622 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001623 if( ! sig_alg_ext_found )
1624 sig_alg_ext_found = 1;
1625 else
1626 {
1627 MBEDTLS_SSL_DEBUG_MSG( 3,
1628 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001629 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001630 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001631 break;
1632
1633 default:
1634 MBEDTLS_SSL_DEBUG_MSG(
1635 3,
1636 ( "unknown extension found: %u ( ignoring )",
1637 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001638 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001639 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640 p += extension_data_len;
1641 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001642 /* Check that we consumed all the message. */
1643 if( p != end )
1644 {
1645 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001646 ( "CertificateRequest misaligned" ) );
1647 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001648 }
1649 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001650 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001651 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001652 MBEDTLS_SSL_DEBUG_MSG( 3,
1653 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001654 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001655 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001656
Jerry Yu7840f812022-01-29 10:26:51 +08001657 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001658 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001659
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001660decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001661 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1662 MBEDTLS_ERR_SSL_DECODE_ERROR );
1663 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001664}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001665
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001666/*
1667 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1668 */
1669static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001670{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672
1673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1674
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1676
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1678 {
1679 unsigned char *buf;
1680 size_t buf_len;
1681
1682 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1683 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1684 &buf, &buf_len ) );
1685
1686 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1687 buf, buf + buf_len ) );
1688
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001689 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1690 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001692 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001693 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00001694 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001695 }
1696 else
1697 {
1698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001699 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1700 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001701 }
1702
Jerry Yud2674312021-10-29 10:08:19 +08001703 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1704
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001705cleanup:
1706
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1708 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001709}
1710
1711/*
Jerry Yu687101b2021-09-14 16:03:56 +08001712 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1713 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001714static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001715{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001716 int ret;
1717
1718 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001719 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001720 return( ret );
1721
Jerry Yu687101b2021-09-14 16:03:56 +08001722 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1723 return( 0 );
1724}
1725
1726/*
1727 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1728 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001729static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001730{
Jerry Yu30b071c2021-09-12 20:16:03 +08001731 int ret;
1732
1733 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1734 if( ret != 0 )
1735 return( ret );
1736
Jerry Yu687101b2021-09-14 16:03:56 +08001737 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1738 return( 0 );
1739}
Jerry Yua93ac112021-10-27 16:31:48 +08001740#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001741
Jerry Yu687101b2021-09-14 16:03:56 +08001742/*
1743 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1744 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001745static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001746{
XiaokangQianac0385c2021-11-03 06:40:11 +00001747 int ret;
1748
XiaokangQianc5c39d52021-11-09 11:55:10 +00001749 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001750 if( ret != 0 )
1751 return( ret );
1752
Jerry Yue3d67cb2022-05-19 15:33:10 +08001753 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1754 if( ret != 0 )
1755 {
1756 MBEDTLS_SSL_PEND_FATAL_ALERT(
1757 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1758 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1759 return( ret );
1760 }
1761
Ronald Cron49ad6192021-11-24 16:25:31 +01001762#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1763 mbedtls_ssl_handshake_set_state(
1764 ssl,
1765 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1766#else
Jerry Yuca133a32022-02-15 14:22:05 +08001767 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001768#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001769
XiaokangQianac0385c2021-11-03 06:40:11 +00001770 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001771}
1772
1773/*
Jerry Yu566c7812022-01-26 15:41:22 +08001774 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1775 */
1776static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1777{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001778 int non_empty_certificate_msg = 0;
1779
Jerry Yu5cc35062022-01-28 16:16:08 +08001780 MBEDTLS_SSL_DEBUG_MSG( 1,
1781 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001782 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001783
Ronald Cron9df7c802022-03-08 18:38:54 +01001784#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001785 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001786 {
1787 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1788 if( ret != 0 )
1789 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001790
Ronald Cron7a94aca2022-03-09 07:44:27 +01001791 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1792 non_empty_certificate_msg = 1;
1793 }
1794 else
1795 {
XiaokangQian23c5be62022-06-07 02:04:34 +00001796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001797 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001798#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001799
Ronald Cron7a94aca2022-03-09 07:44:27 +01001800 if( non_empty_certificate_msg )
1801 {
1802 mbedtls_ssl_handshake_set_state( ssl,
1803 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1804 }
1805 else
Ronald Cron19385882022-06-15 16:26:13 +02001806 {
1807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001808 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001809 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01001810
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001811 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001812}
1813
Ronald Cron9df7c802022-03-08 18:38:54 +01001814#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001815/*
1816 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1817 */
1818static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1819{
Ronald Crona8b38872022-03-09 07:59:25 +01001820 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1821
1822 if( ret == 0 )
1823 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1824
1825 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001826}
Jerry Yu90f152d2022-01-29 22:12:42 +08001827#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001828
1829/*
Jerry Yu687101b2021-09-14 16:03:56 +08001830 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1831 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001832static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001833{
XiaokangQian0fa66432021-11-15 03:33:57 +00001834 int ret;
1835
1836 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1837 if( ret != 0 )
1838 return( ret );
1839
Jerry Yue3d67cb2022-05-19 15:33:10 +08001840 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1841 if( ret != 0 )
1842 {
1843 MBEDTLS_SSL_DEBUG_RET( 1,
1844 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1845 return ( ret );
1846 }
1847
XiaokangQian0fa66432021-11-15 03:33:57 +00001848 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1849 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001850}
1851
1852/*
1853 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1854 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001855static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001856{
Jerry Yu378254d2021-10-30 21:44:47 +08001857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001858 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001859 return( 0 );
1860}
1861
1862/*
1863 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1864 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001865static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001866{
Jerry Yu378254d2021-10-30 21:44:47 +08001867
1868 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1869
1870 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1871 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001872}
1873
Jerry Yu92c6b402021-08-27 16:59:09 +08001874int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001875{
Jerry Yu92c6b402021-08-27 16:59:09 +08001876 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001877
Jerry Yu92c6b402021-08-27 16:59:09 +08001878 switch( ssl->state )
1879 {
1880 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001881 * ssl->state is initialized as HELLO_REQUEST. It is the same
1882 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001883 */
1884 case MBEDTLS_SSL_HELLO_REQUEST:
1885 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001886 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001887 break;
1888
1889 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001890 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001891 break;
1892
1893 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001894 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001895 break;
1896
Jerry Yua93ac112021-10-27 16:31:48 +08001897#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001898 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1899 ret = ssl_tls13_process_certificate_request( ssl );
1900 break;
1901
Jerry Yu687101b2021-09-14 16:03:56 +08001902 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001903 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001904 break;
1905
1906 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001907 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001908 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001909#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001910
1911 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001912 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001913 break;
1914
Jerry Yu566c7812022-01-26 15:41:22 +08001915 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1916 ret = ssl_tls13_write_client_certificate( ssl );
1917 break;
1918
Ronald Cron9df7c802022-03-08 18:38:54 +01001919#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001920 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1921 ret = ssl_tls13_write_client_certificate_verify( ssl );
1922 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001923#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001924
Jerry Yu687101b2021-09-14 16:03:56 +08001925 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001926 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001927 break;
1928
1929 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001930 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001931 break;
1932
1933 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001934 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001935 break;
1936
Ronald Cron49ad6192021-11-24 16:25:31 +01001937 /*
1938 * Injection of dummy-CCS's for middlebox compatibility
1939 */
1940#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001941 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001942 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1943 if( ret == 0 )
1944 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1945 break;
1946
1947 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1948 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1949 if( ret == 0 )
1950 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001951 break;
1952#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1953
Jerry Yu92c6b402021-08-27 16:59:09 +08001954 default:
1955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1957 }
1958
1959 return( ret );
1960}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001961
Jerry Yufb4b6472022-01-27 15:03:26 +08001962#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001963
Jerry Yufb4b6472022-01-27 15:03:26 +08001964