blob: e9250fcd3c1867615c98df5e64750d95494a26b5 [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 */
670 p += legacy_session_id_echo_len + 4;
671
672 /* Case of no extension */
673 if( p == end )
674 return( 0 );
675
676 /* ...
677 * Extension extensions<6..2^16-1>;
678 * ...
679 * struct {
680 * ExtensionType extension_type; (2 bytes)
681 * opaque extension_data<0..2^16-1>;
682 * } Extension;
683 */
684 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
685 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
686 p += 2;
687
688 /* Check extensions do not go beyond the buffer of data. */
689 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
690 extensions_end = p + extensions_len;
691
692 while( p < extensions_end )
693 {
694 unsigned int extension_type;
695 size_t extension_data_len;
696
697 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
698 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
699 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
700 p += 4;
701
702 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
703 return( 1 );
704
705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
706 p += extension_data_len;
707 }
708
709 return( 0 );
710}
711
Jerry Yu7a186a02021-10-15 18:46:14 +0800712/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200713 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
714 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
715 * - 0 otherwise
716 */
717static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
718 const unsigned char *buf,
719 const unsigned char *end )
720{
721 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
722 static const unsigned char magic_downgrade_string[] =
723 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
724 const unsigned char *last_eight_bytes_of_random;
725 unsigned char last_byte_of_random;
726
727 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
728 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
729
730 if( memcmp( last_eight_bytes_of_random,
731 magic_downgrade_string,
732 sizeof( magic_downgrade_string ) ) == 0 )
733 {
734 last_byte_of_random = last_eight_bytes_of_random[7];
735 return( last_byte_of_random == 0 ||
736 last_byte_of_random == 1 );
737 }
738
739 return( 0 );
740}
741
742/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800743 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
744 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000745 * to indicate which message is expected and to be parsed next.
746 */
Jerry Yub85277e2021-10-13 13:36:05 +0800747#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
748#define SSL_SERVER_HELLO_COORDINATE_HRR 1
749static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
750 const unsigned char *buf,
751 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800752{
Jerry Yue1b9c292021-09-10 10:08:31 +0800753
754 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
755 *
Jerry Yu4a173382021-10-11 21:45:31 +0800756 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800757 * special value of the SHA-256 of "HelloRetryRequest".
758 *
759 * struct {
760 * ProtocolVersion legacy_version = 0x0303;
761 * Random random;
762 * opaque legacy_session_id_echo<0..32>;
763 * CipherSuite cipher_suite;
764 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800765 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800766 * } ServerHello;
767 *
768 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800769 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
770 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800771
Jerry Yu93a13f22022-04-11 23:00:01 +0800772 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
773 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800774 {
Jerry Yub85277e2021-10-13 13:36:05 +0800775 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800776 }
777
Jerry Yub85277e2021-10-13 13:36:05 +0800778 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800779}
780
Jerry Yu745bb612021-10-13 22:01:04 +0800781/* Fetch and preprocess
782 * Returns a negative value on failure, and otherwise
783 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100784 * - SSL_SERVER_HELLO_COORDINATE_HRR or
785 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800786 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100787#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800788static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
789 unsigned char **buf,
790 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800791{
Jerry Yu4a173382021-10-11 21:45:31 +0800792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200793 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800794
XiaokangQian355e09a2022-01-20 11:14:50 +0000795 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
796 MBEDTLS_SSL_HS_SERVER_HELLO,
797 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200798 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800799
Ronald Cron9f0fba32022-02-10 16:45:15 +0100800 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200801 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100802 if( ret == 0 )
803 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200804 MBEDTLS_SSL_PROC_CHK_NEG(
805 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
806
807 /* If the server is negotiating TLS 1.2 or below and:
808 * . we did not propose TLS 1.2 or
809 * . the server responded it is TLS 1.3 capable but negotiating a lower
810 * version of the protocol and thus we are under downgrade attack
811 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100812 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200813 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100814 {
815 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
816 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
817 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
818 }
819
820 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400821 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100822 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
823 *buf, *buf_len );
824
825 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
826 {
827 ret = ssl_tls13_reset_key_share( ssl );
828 if( ret != 0 )
829 return( ret );
830 }
831
832 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
833 }
834
Ronald Cronfd6193c2022-04-05 11:04:20 +0200835 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800836 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800837 {
Jerry Yu745bb612021-10-13 22:01:04 +0800838 case SSL_SERVER_HELLO_COORDINATE_HELLO:
839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
840 break;
841 case SSL_SERVER_HELLO_COORDINATE_HRR:
842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000843 /* If a client receives a second
844 * HelloRetryRequest in the same connection (i.e., where the ClientHello
845 * was itself in response to a HelloRetryRequest), it MUST abort the
846 * handshake with an "unexpected_message" alert.
847 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000848 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000849 {
850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
851 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
852 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
854 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000855 /*
856 * Clients must abort the handshake with an "illegal_parameter"
857 * alert if the HelloRetryRequest would not result in any change
858 * in the ClientHello.
859 * In a PSK only key exchange that what we expect.
860 */
861 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
862 {
863 MBEDTLS_SSL_DEBUG_MSG( 1,
864 ( "Unexpected HRR in pure PSK key exchange." ) );
865 MBEDTLS_SSL_PEND_FATAL_ALERT(
866 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
867 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
868 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
869 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000870
XiaokangQiand9e068e2022-01-18 06:23:32 +0000871 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000872
Jerry Yu745bb612021-10-13 22:01:04 +0800873 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800874 }
875
876cleanup:
877
878 return( ret );
879}
880
Jerry Yu4a173382021-10-11 21:45:31 +0800881static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
882 const unsigned char **buf,
883 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800884{
885 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800886 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800887
Jerry Yude4fb2c2021-09-19 18:05:08 +0800888 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800889 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800890
Jerry Yu4a173382021-10-11 21:45:31 +0800891 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800892
893 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800894 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
895 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800896 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
898 ssl->session_negotiate->id,
899 ssl->session_negotiate->id_len );
900 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800901 legacy_session_id_echo_len );
902
903 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
904 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
905
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
907 }
908
Jerry Yu4a173382021-10-11 21:45:31 +0800909 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800910 *buf = p;
911
912 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800913 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 return( 0 );
915}
916
Jerry Yue1b9c292021-09-10 10:08:31 +0800917/* Parse ServerHello message and configure context
918 *
919 * struct {
920 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
921 * Random random;
922 * opaque legacy_session_id_echo<0..32>;
923 * CipherSuite cipher_suite;
924 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800925 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800926 * } ServerHello;
927 */
Jerry Yuc068b662021-10-11 22:30:19 +0800928static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
929 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000930 const unsigned char *end,
931 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800932{
Jerry Yub85277e2021-10-13 13:36:05 +0800933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800934 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000935 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800936 size_t extensions_len;
937 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800938 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800939 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000940 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941
942 /*
943 * Check there is space for minimal fields
944 *
945 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800946 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 * - legacy_session_id_echo ( 1 byte ), minimum size
948 * - cipher_suite ( 2 bytes)
949 * - legacy_compression_method ( 1 byte )
950 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800951 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800952
953 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
955
Jerry Yu4a173382021-10-11 21:45:31 +0800956 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800957 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800958 * ...
959 * with ProtocolVersion defined as:
960 * uint16 ProtocolVersion;
961 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400962 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
963 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
966 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
967 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000968 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
969 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 }
971 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800972
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800973 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800974 * Random random;
975 * ...
976 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800977 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800978 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000979 if( !is_hrr )
980 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000981 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000982 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
984 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
985 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800986 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800987
Jerry Yu4a173382021-10-11 21:45:31 +0800988 /* ...
989 * opaque legacy_session_id_echo<0..32>;
990 * ...
991 */
992 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 {
XiaokangQian52da5582022-01-26 09:49:29 +0000994 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000995 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 }
997
Jerry Yu4a173382021-10-11 21:45:31 +0800998 /* ...
999 * CipherSuite cipher_suite;
1000 * ...
1001 * with CipherSuite defined as:
1002 * uint8 CipherSuite[2];
1003 */
1004 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1006 p += 2;
1007
Jerry Yu4a173382021-10-11 21:45:31 +08001008
XiaokangQian355e09a2022-01-20 11:14:50 +00001009 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001010 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001011 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001012 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001013 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1014 ssl->tls_version,
1015 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001016 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 {
XiaokangQian52da5582022-01-26 09:49:29 +00001018 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001020 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001021 * If we received an HRR before and that the proposed selected
1022 * ciphersuite in this server hello is not the same as the one
1023 * proposed in the HRR, we abort the handshake and send an
1024 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001025 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001026 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1027 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001028 {
XiaokangQian52da5582022-01-26 09:49:29 +00001029 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001030 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001031
XiaokangQian52da5582022-01-26 09:49:29 +00001032 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001033 {
1034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1035 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001036 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001037 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001038
Jerry Yu4a173382021-10-11 21:45:31 +08001039 /* Configure ciphersuites */
1040 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1041
XiaokangQian355e09a2022-01-20 11:14:50 +00001042 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 ssl->session_negotiate->ciphersuite = cipher_suite;
1044
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1046 cipher_suite, ciphersuite_info->name ) );
1047
1048#if defined(MBEDTLS_HAVE_TIME)
1049 ssl->session_negotiate->start = time( NULL );
1050#endif /* MBEDTLS_HAVE_TIME */
1051
Jerry Yu4a173382021-10-11 21:45:31 +08001052 /* ...
1053 * uint8 legacy_compression_method = 0;
1054 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001056 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001057 if( p[0] != 0 )
1058 {
Jerry Yub85277e2021-10-13 13:36:05 +08001059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001060 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001061 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062 }
1063 p++;
1064
Jerry Yub85277e2021-10-13 13:36:05 +08001065 /* ...
1066 * Extension extensions<6..2^16-1>;
1067 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001068 * struct {
1069 * ExtensionType extension_type; (2 bytes)
1070 * opaque extension_data<0..2^16-1>;
1071 * } Extension;
1072 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001073 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001074 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001075 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001076
Jerry Yu4a173382021-10-11 21:45:31 +08001077 /* Check extensions do not go beyond the buffer of data. */
1078 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1079 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001080
Jerry Yu4a173382021-10-11 21:45:31 +08001081 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001082
Jerry Yu4a173382021-10-11 21:45:31 +08001083 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 {
1085 unsigned int extension_type;
1086 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001087 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001088
Jerry Yu4a173382021-10-11 21:45:31 +08001089 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001090 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1091 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1092 p += 4;
1093
Jerry Yu4a173382021-10-11 21:45:31 +08001094 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001095 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001096
1097 switch( extension_type )
1098 {
XiaokangQianb851da82022-01-14 04:03:11 +00001099 case MBEDTLS_TLS_EXT_COOKIE:
1100
XiaokangQiand9e068e2022-01-18 06:23:32 +00001101 if( !is_hrr )
1102 {
XiaokangQian52da5582022-01-26 09:49:29 +00001103 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001104 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001105 }
1106
XiaokangQian43550bd2022-01-21 04:32:58 +00001107 ret = ssl_tls13_parse_cookie_ext( ssl,
1108 p, extension_data_end );
1109 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001110 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001111 MBEDTLS_SSL_DEBUG_RET( 1,
1112 "ssl_tls13_parse_cookie_ext",
1113 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001114 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001115 }
XiaokangQianb851da82022-01-14 04:03:11 +00001116 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001117
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001119 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001120 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001121 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001123 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 break;
1125
1126 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001129
XiaokangQian52da5582022-01-26 09:49:29 +00001130 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001131 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001132
Jerry Yue1b9c292021-09-10 10:08:31 +08001133 case MBEDTLS_TLS_EXT_KEY_SHARE:
1134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001135 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1136 {
XiaokangQian52da5582022-01-26 09:49:29 +00001137 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001138 goto cleanup;
1139 }
1140
XiaokangQian53f20b72022-01-18 10:47:33 +00001141 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001142 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001143 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001144 else
XiaokangQianb851da82022-01-14 04:03:11 +00001145 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001146 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001147 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 {
1149 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001150 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001152 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001153 }
1154 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001155
1156 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001157 MBEDTLS_SSL_DEBUG_MSG(
1158 3,
1159 ( "unknown extension found: %u ( ignoring )",
1160 extension_type ) );
1161
XiaokangQian52da5582022-01-26 09:49:29 +00001162 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001163 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 }
1165
1166 p += extension_data_len;
1167 }
1168
XiaokangQiand59be772022-01-24 10:12:51 +00001169cleanup:
1170
XiaokangQian52da5582022-01-26 09:49:29 +00001171 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001172 {
1173 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1174 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1175 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1176 }
XiaokangQian52da5582022-01-26 09:49:29 +00001177 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001178 {
1179 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1180 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1181 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1182 }
1183 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184}
1185
XiaokangQian355e09a2022-01-20 11:14:50 +00001186static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001187{
Jerry Yub85277e2021-10-13 13:36:05 +08001188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001189 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001190
Jerry Yub85277e2021-10-13 13:36:05 +08001191 /* Determine the key exchange mode:
1192 * 1) If both the pre_shared_key and key_share extensions were received
1193 * then the key exchange mode is PSK with EPHEMERAL.
1194 * 2) If only the pre_shared_key extension was received then the key
1195 * exchange mode is PSK-only.
1196 * 3) If only the key_share extension was received then the key
1197 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001198 */
Jerry Yub85277e2021-10-13 13:36:05 +08001199 switch( handshake->extensions_present &
1200 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001201 {
Jerry Yu745bb612021-10-13 22:01:04 +08001202 /* Only the pre_shared_key extension was received */
1203 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001204 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001205 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001206
Jerry Yu745bb612021-10-13 22:01:04 +08001207 /* Only the key_share extension was received */
1208 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001209 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001210 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001211
Jerry Yu745bb612021-10-13 22:01:04 +08001212 /* Both the pre_shared_key and key_share extensions were received */
1213 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001214 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001215 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001216
Jerry Yu745bb612021-10-13 22:01:04 +08001217 /* Neither pre_shared_key nor key_share extension was received */
1218 default:
1219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1220 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1221 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001222 }
1223
1224 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1225 *
1226 * TODO: We don't have to do this in case we offered 0-RTT and the
1227 * server accepted it. In this case, we could skip generating
1228 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001229 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001230 if( ret != 0 )
1231 {
Jerry Yue110d252022-05-05 10:19:22 +08001232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001233 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001234 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001235 }
1236
Jerry Yuf86eb752022-05-06 11:16:55 +08001237 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001238 if( ret != 0 )
1239 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001240 MBEDTLS_SSL_DEBUG_RET( 1,
1241 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001242 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001243 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001244 }
1245
Jerry Yue110d252022-05-05 10:19:22 +08001246 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1248 ssl->session_in = ssl->session_negotiate;
1249
1250 /*
1251 * State machine update
1252 */
1253 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1254
Jerry Yu4a173382021-10-11 21:45:31 +08001255cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001256 if( ret != 0 )
1257 {
Jerry Yu4a173382021-10-11 21:45:31 +08001258 MBEDTLS_SSL_PEND_FATAL_ALERT(
1259 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1260 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1261 }
Jerry Yue110d252022-05-05 10:19:22 +08001262
Jerry Yu4a173382021-10-11 21:45:31 +08001263 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001264}
1265
XiaokangQian355e09a2022-01-20 11:14:50 +00001266static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001267{
1268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1269
XiaokangQian647719a2021-12-07 09:16:29 +00001270#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1271 /* If not offering early data, the client sends a dummy CCS record
1272 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001273 * its second ClientHello or before its encrypted handshake flight.
1274 */
XiaokangQian647719a2021-12-07 09:16:29 +00001275 mbedtls_ssl_handshake_set_state( ssl,
1276 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1277#else
1278 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1279#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1280
XiaokangQian78b1fa72022-01-19 06:56:30 +00001281 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001282
XiaokangQian78b1fa72022-01-19 06:56:30 +00001283 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001284 * We are going to re-generate a shared secret corresponding to the group
1285 * selected by the server, which is different from the group for which we
1286 * generated a shared secret in the first client hello.
1287 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001288 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001289 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001290 if( ret != 0 )
1291 return( ret );
1292
1293 return( 0 );
1294}
1295
Jerry Yue1b9c292021-09-10 10:08:31 +08001296/*
Jerry Yu4a173382021-10-11 21:45:31 +08001297 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001298 * Handler for MBEDTLS_SSL_SERVER_HELLO
1299 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001300static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001301{
Jerry Yu4a173382021-10-11 21:45:31 +08001302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001303 unsigned char *buf = NULL;
1304 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001305 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001306
1307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1308
1309 /* Coordination step
1310 * - Fetch record
1311 * - Make sure it's either a ServerHello or a HRR.
1312 * - Switch processing routine in case of HRR
1313 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001314 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1315
XiaokangQian16acd4b2022-01-14 07:35:47 +00001316 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001317 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001318 goto cleanup;
1319 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001320 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001321
Ronald Cron9f0fba32022-02-10 16:45:15 +01001322 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1323 {
1324 ret = 0;
1325 goto cleanup;
1326 }
1327
XiaokangQianb851da82022-01-14 04:03:11 +00001328 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001329 buf + buf_len,
1330 is_hrr ) );
1331 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001332 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1333
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001334 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1335 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001336
XiaokangQiand9e068e2022-01-18 06:23:32 +00001337 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001338 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001339 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001340 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001341
1342cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1344 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001346}
1347
1348/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001349 *
1350 * EncryptedExtensions message
1351 *
1352 * The EncryptedExtensions message contains any extensions which
1353 * should be protected, i.e., any which are not needed to establish
1354 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001355 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001356
1357/*
1358 * Overview
1359 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001360
1361/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001362static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001363
XiaokangQian97799ac2021-10-11 10:05:54 +00001364static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1365 const unsigned char *buf,
1366 const unsigned char *end );
1367static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001368
1369/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001370 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001371 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001372static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001373{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001374 int ret;
1375 unsigned char *buf;
1376 size_t buf_len;
1377
1378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1379
Xiaofei Bai746f9482021-11-12 08:53:56 +00001380 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001381 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001382 &buf, &buf_len ) );
1383
1384 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001385 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001386 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001387
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001388 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1389 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001390
XiaokangQian97799ac2021-10-11 10:05:54 +00001391 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001392
1393cleanup:
1394
1395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1396 return( ret );
1397
1398}
1399
XiaokangQian08da26c2021-10-09 10:12:11 +00001400/* Parse EncryptedExtensions message
1401 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001402 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001403 * } EncryptedExtensions;
1404 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001405static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1406 const unsigned char *buf,
1407 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001408{
1409 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001410 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001411 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001412 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001413
XiaokangQian08da26c2021-10-09 10:12:11 +00001414 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001415 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001416 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001417
XiaokangQian97799ac2021-10-11 10:05:54 +00001418 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001419 extensions_end = p + extensions_len;
1420 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001421
XiaokangQian8db25ff2021-10-13 05:56:18 +00001422 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001423 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001424 unsigned int extension_type;
1425 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001426
XiaokangQian08da26c2021-10-09 10:12:11 +00001427 /*
1428 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001429 * ExtensionType extension_type; (2 bytes)
1430 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001431 * } Extension;
1432 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001433 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001434 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1435 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1436 p += 4;
1437
XiaokangQian8db25ff2021-10-13 05:56:18 +00001438 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001439
XiaokangQian97799ac2021-10-11 10:05:54 +00001440 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001441 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001442 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001443 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001444 switch( extension_type )
1445 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001446
XiaokangQian08da26c2021-10-09 10:12:11 +00001447 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1449 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001450
lhuang0486cacac2022-01-21 07:34:27 -08001451#if defined(MBEDTLS_SSL_ALPN)
1452 case MBEDTLS_TLS_EXT_ALPN:
1453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1454
1455 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1456 {
1457 return( ret );
1458 }
1459
1460 break;
1461#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001462 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001463 MBEDTLS_SSL_DEBUG_MSG(
1464 3, ( "unsupported extension found: %u ", extension_type) );
1465 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001466 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001467 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1468 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001469 }
1470
XiaokangQian08da26c2021-10-09 10:12:11 +00001471 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001472 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001473
XiaokangQian97799ac2021-10-11 10:05:54 +00001474 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001475 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001478 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001479 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001480 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001481 }
1482
1483 return( ret );
1484}
1485
XiaokangQian97799ac2021-10-11 10:05:54 +00001486static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001487{
Jerry Yua93ac112021-10-27 16:31:48 +08001488#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001489 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001490 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1491 else
Jerry Yud2674312021-10-29 10:08:19 +08001492 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001493#else
1494 ((void) ssl);
1495 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1496#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001497 return( 0 );
1498}
1499
Jerry Yua93ac112021-10-27 16:31:48 +08001500#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001501/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001502 *
1503 * STATE HANDLING: CertificateRequest
1504 *
Jerry Yud2674312021-10-29 10:08:19 +08001505 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001506#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1507#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001508/* Coordination:
1509 * Deals with the ambiguity of not knowing if a CertificateRequest
1510 * will be sent. Returns a negative code on failure, or
1511 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1512 * - SSL_CERTIFICATE_REQUEST_SKIP
1513 * indicating if a Certificate Request is expected or not.
1514 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001515static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001516{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001518
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001519 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001520 {
1521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1522 return( SSL_CERTIFICATE_REQUEST_SKIP );
1523 }
1524
1525 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001526 {
1527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1528 return( ret );
1529 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001530 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001531
1532 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1533 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1534 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001535 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001536 }
1537
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001538 return( SSL_CERTIFICATE_REQUEST_SKIP );
1539}
1540
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001541/*
1542 * ssl_tls13_parse_certificate_request()
1543 * Parse certificate request
1544 * struct {
1545 * opaque certificate_request_context<0..2^8-1>;
1546 * Extension extensions<2..2^16-1>;
1547 * } CertificateRequest;
1548 */
1549static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1550 const unsigned char *buf,
1551 const unsigned char *end )
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001555 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001556 size_t extensions_len = 0;
1557 const unsigned char *extensions_end;
1558 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001560 /* ...
1561 * opaque certificate_request_context<0..2^8-1>
1562 * ...
1563 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1565 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001566 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001568 if( certificate_request_context_len > 0 )
1569 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001570 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1572 p, certificate_request_context_len );
1573
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001574 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001575 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001576 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001577 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001578 {
1579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1580 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1581 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001582 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001583 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001584 p += certificate_request_context_len;
1585 }
1586
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001587 /* ...
1588 * Extension extensions<2..2^16-1>;
1589 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001590 */
1591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1592 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1593 p += 2;
1594
1595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1596 extensions_end = p + extensions_len;
1597
1598 while( p < extensions_end )
1599 {
1600 unsigned int extension_type;
1601 size_t extension_data_len;
1602
1603 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1604 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1605 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1606 p += 4;
1607
1608 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1609
1610 switch( extension_type )
1611 {
1612 case MBEDTLS_TLS_EXT_SIG_ALG:
1613 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001614 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001615 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001616 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001617 if( ret != 0 )
1618 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001619 if( ! sig_alg_ext_found )
1620 sig_alg_ext_found = 1;
1621 else
1622 {
1623 MBEDTLS_SSL_DEBUG_MSG( 3,
1624 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001625 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001626 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001627 break;
1628
1629 default:
1630 MBEDTLS_SSL_DEBUG_MSG(
1631 3,
1632 ( "unknown extension found: %u ( ignoring )",
1633 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001634 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001635 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001636 p += extension_data_len;
1637 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001638 /* Check that we consumed all the message. */
1639 if( p != end )
1640 {
1641 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001642 ( "CertificateRequest misaligned" ) );
1643 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001644 }
1645 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001646 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001647 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001648 MBEDTLS_SSL_DEBUG_MSG( 3,
1649 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001650 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001651 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652
Jerry Yu7840f812022-01-29 10:26:51 +08001653 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001655
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001656decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001657 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1658 MBEDTLS_ERR_SSL_DECODE_ERROR );
1659 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001660}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001661
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001662/*
1663 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1664 */
1665static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001668
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1670
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001671 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1672
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001673 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1674 {
1675 unsigned char *buf;
1676 size_t buf_len;
1677
1678 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1679 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1680 &buf, &buf_len ) );
1681
1682 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1683 buf, buf + buf_len ) );
1684
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001685 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1686 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001687 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001688 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001689 {
1690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001691 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 }
1693 else
1694 {
1695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001696 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1697 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698 }
1699
1700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001701 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001702
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 {
1796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1797 }
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
1806 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1807
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001808 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001809}
1810
Ronald Cron9df7c802022-03-08 18:38:54 +01001811#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001812/*
1813 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1814 */
1815static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1816{
Ronald Crona8b38872022-03-09 07:59:25 +01001817 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1818
1819 if( ret == 0 )
1820 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1821
1822 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001823}
Jerry Yu90f152d2022-01-29 22:12:42 +08001824#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001825
1826/*
Jerry Yu687101b2021-09-14 16:03:56 +08001827 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1828 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001829static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001830{
XiaokangQian0fa66432021-11-15 03:33:57 +00001831 int ret;
1832
1833 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1834 if( ret != 0 )
1835 return( ret );
1836
Jerry Yue3d67cb2022-05-19 15:33:10 +08001837 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1838 if( ret != 0 )
1839 {
1840 MBEDTLS_SSL_DEBUG_RET( 1,
1841 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1842 return ( ret );
1843 }
1844
XiaokangQian0fa66432021-11-15 03:33:57 +00001845 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1846 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001847}
1848
1849/*
1850 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1851 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001852static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001853{
Jerry Yu378254d2021-10-30 21:44:47 +08001854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001855 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001856 return( 0 );
1857}
1858
1859/*
1860 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1861 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001862static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001863{
Jerry Yu378254d2021-10-30 21:44:47 +08001864
1865 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1866
1867 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1868 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001869}
1870
Jerry Yu92c6b402021-08-27 16:59:09 +08001871int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001872{
Jerry Yu92c6b402021-08-27 16:59:09 +08001873 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001874
Jerry Yu92c6b402021-08-27 16:59:09 +08001875 switch( ssl->state )
1876 {
1877 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001878 * ssl->state is initialized as HELLO_REQUEST. It is the same
1879 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001880 */
1881 case MBEDTLS_SSL_HELLO_REQUEST:
1882 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001883 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001884 break;
1885
1886 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001887 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001888 break;
1889
1890 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001891 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001892 break;
1893
Jerry Yua93ac112021-10-27 16:31:48 +08001894#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001895 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1896 ret = ssl_tls13_process_certificate_request( ssl );
1897 break;
1898
Jerry Yu687101b2021-09-14 16:03:56 +08001899 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001900 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001901 break;
1902
1903 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001904 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001905 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001906#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001907
1908 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001909 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001910 break;
1911
Jerry Yu566c7812022-01-26 15:41:22 +08001912 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1913 ret = ssl_tls13_write_client_certificate( ssl );
1914 break;
1915
Ronald Cron9df7c802022-03-08 18:38:54 +01001916#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001917 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1918 ret = ssl_tls13_write_client_certificate_verify( ssl );
1919 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001920#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001921
Jerry Yu687101b2021-09-14 16:03:56 +08001922 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001923 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001924 break;
1925
1926 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001927 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001928 break;
1929
1930 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001931 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001932 break;
1933
Ronald Cron49ad6192021-11-24 16:25:31 +01001934 /*
1935 * Injection of dummy-CCS's for middlebox compatibility
1936 */
1937#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001938 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001939 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1940 if( ret == 0 )
1941 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1942 break;
1943
1944 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1945 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1946 if( ret == 0 )
1947 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001948 break;
1949#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1950
Jerry Yu92c6b402021-08-27 16:59:09 +08001951 default:
1952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1954 }
1955
1956 return( ret );
1957}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001958
Jerry Yufb4b6472022-01-27 15:03:26 +08001959#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001960
Jerry Yufb4b6472022-01-27 15:03:26 +08001961