blob: b659fa727c1c25267e1a29a2548e2e7efd89b5f1 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000048 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040051 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
52 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080053
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020062 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Ronald Crona77fc272022-03-30 17:20:47 +020064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010065
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010067 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080068 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010071 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu0c63af62021-09-02 12:59:12 +080073 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080074 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010075 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080076 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040077 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Glenn Strausscd78df62022-04-07 19:07:11 -040082 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010083 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040084 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
87 }
88
89 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Ronald Cron98473382022-03-30 20:04:10 +0200100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400101 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
102 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
Ronald Cron98473382022-03-30 20:04:10 +0200111 if( &buf[2] != end )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
115 MBEDTLS_ERR_SSL_DECODE_ERROR );
116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
117 }
118
Jerry Yue1b9c292021-09-10 10:08:31 +0800119 return( 0 );
120}
121
lhuang0486cacac2022-01-21 07:34:27 -0800122#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800123static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
124 const unsigned char *buf, size_t len )
125{
126 size_t list_len, name_len;
127 const unsigned char *p = buf;
128 const unsigned char *end = buf + len;
129
130 /* If we didn't send it, the server shouldn't send it */
131 if( ssl->conf->alpn_list == NULL )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
134 /*
135 * opaque ProtocolName<1..2^8-1>;
136 *
137 * struct {
138 * ProtocolName protocol_name_list<2..2^16-1>
139 * } ProtocolNameList;
140 *
141 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
142 */
143
144 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
146
147 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
148 p += 2;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
150
151 name_len = *p++;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
153
154 /* Check that the server chosen protocol was in our list and save it */
155 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
156 {
157 if( name_len == strlen( *alpn ) &&
158 memcmp( buf + 3, *alpn, name_len ) == 0 )
159 {
160 ssl->alpn_chosen = *alpn;
161 return( 0 );
162 }
163 }
164
165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
166}
167#endif /* MBEDTLS_SSL_ALPN */
168
XiaokangQian16acd4b2022-01-14 07:35:47 +0000169static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000170{
171 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( group_id == 0 )
174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
175
XiaokangQian355e09a2022-01-20 11:14:50 +0000176#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000178 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
181
182 /* Destroy generated private key. */
183 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
184 if( status != PSA_SUCCESS )
185 {
186 ret = psa_ssl_status_to_mbedtls( status );
187 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
188 return( ret );
189 }
190
191 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000192 return( 0 );
193 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000194 else
195#endif /* MBEDTLS_ECDH_C */
196 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000197 {
198 /* Do something */
199 }
200
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202}
203
204/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205 * Functions for writing key_share extension.
206 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
208 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209{
210 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
211
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100214 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800215 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100216 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
218
Brett Warren14efd332021-10-06 09:32:11 +0100219 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 const mbedtls_ecp_curve_info *curve_info;
222 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
223 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100224 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Brett Warren14efd332021-10-06 09:32:11 +0100226 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 return( 0 );
228 }
229 }
230#else
231 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233#endif /* MBEDTLS_ECDH_C */
234
235 /*
236 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800237 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 */
239
240 return( ret );
241}
242
243/*
244 * ssl_tls13_write_key_share_ext
245 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 *
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 * struct {
253 * KeyShareEntry client_shares<0..2^16-1>;
254 * } KeyShareClientHello;
255 */
256static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
257 unsigned char *buf,
258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260{
261 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000262 unsigned char *client_shares; /* Start of client_shares */
263 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 * - extension_type (2 bytes)
271 * - extension_data_length (2 bytes)
272 * - client_shares_length (2 bytes)
273 */
274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
275 p += 6;
276
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
278
279 /* HRR could already have requested something else. */
280 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800281 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
282 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 &group_id ) );
286 }
287
288 /*
289 * Dispatch to type-specific key generation function.
290 *
291 * So far, we're only supporting ECDHE. With the introduction
292 * of PQC KEMs, we'll want to have multiple branches, one per
293 * type of KEM, and dispatch to the corresponding crypto. And
294 * only one key share entry is allowed.
295 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000301 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100303 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304
305 /* Check there is space for header of KeyShareEntry
306 * - group (2 bytes)
307 * - key_exchange_length (2 bytes)
308 */
309 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
310 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800311 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
312 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800313 p += key_exchange_len;
314 if( ret != 0 )
315 return( ret );
316
317 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000318 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000320 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 }
322 else
323#endif /* MBEDTLS_ECDH_C */
324 if( 0 /* other KEMs? */ )
325 {
326 /* Do something */
327 }
328 else
329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000332 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800333 if( client_shares_len == 0)
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800337 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write extension_type */
339 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
340 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344
345 /* Update offered_group_id field */
346 ssl->handshake->offered_group_id = group_id;
347
348 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000349 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
354
355cleanup:
356
357 return( ret );
358}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359
Jerry Yue1b9c292021-09-10 10:08:31 +0800360
XiaokangQiand59be772022-01-24 10:12:51 +0000361/*
362 * ssl_tls13_parse_hrr_key_share_ext()
363 * Parse key_share extension in Hello Retry Request
364 *
365 * struct {
366 * NamedGroup selected_group;
367 * } KeyShareHelloRetryRequest;
368 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000369static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000370 const unsigned char *buf,
371 const unsigned char *end )
372{
XiaokangQianb851da82022-01-14 04:03:11 +0000373 const mbedtls_ecp_curve_info *curve_info = NULL;
374 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000375 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000376 int found = 0;
377
378 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
379 if( group_list == NULL )
380 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
381
382 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
383
384 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000386 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000388
389 /* Upon receipt of this extension in a HelloRetryRequest, the client
390 * MUST first verify that the selected_group field corresponds to a
391 * group which was provided in the "supported_groups" extension in the
392 * original ClientHello.
393 * The supported_group was based on the info in ssl->conf->group_list.
394 *
395 * If the server provided a key share that was not sent in the ClientHello
396 * then the client MUST abort the handshake with an "illegal_parameter" alert.
397 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000398 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000399 {
400 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000401 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000402 continue;
403
404 /* We found a match */
405 found = 1;
406 break;
407 }
408
409 /* Client MUST verify that the selected_group field does not
410 * correspond to a group which was provided in the "key_share"
411 * extension in the original ClientHello. If the server sent an
412 * HRR message with a key share already provided in the
413 * ClientHello then the client MUST abort the handshake with
414 * an "illegal_parameter" alert.
415 */
XiaokangQiand59be772022-01-24 10:12:51 +0000416 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
419 MBEDTLS_SSL_PEND_FATAL_ALERT(
420 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
421 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
422 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
423 }
424
425 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000426 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000427
428 return( 0 );
429}
430
Jerry Yue1b9c292021-09-10 10:08:31 +0800431/*
Jerry Yub85277e2021-10-13 13:36:05 +0800432 * ssl_tls13_parse_key_share_ext()
433 * Parse key_share extension in Server Hello
434 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800435 * struct {
436 * KeyShareEntry server_share;
437 * } KeyShareServerHello;
438 * struct {
439 * NamedGroup group;
440 * opaque key_exchange<1..2^16-1>;
441 * } KeyShareEntry;
442 */
Jerry Yu4a173382021-10-11 21:45:31 +0800443static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 const unsigned char *buf,
445 const unsigned char *end )
446{
Jerry Yub85277e2021-10-13 13:36:05 +0800447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800448 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800449 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450
Jerry Yu4a173382021-10-11 21:45:31 +0800451 /* ...
452 * NamedGroup group; (2 bytes)
453 * ...
454 */
455 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
456 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800457 p += 2;
458
Jerry Yu4a173382021-10-11 21:45:31 +0800459 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800460 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800461 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1,
464 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800465 (unsigned) offered_group, (unsigned) group ) );
466 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
467 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
468 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 }
470
471#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800472 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800473 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100474 const mbedtls_ecp_curve_info *curve_info =
475 mbedtls_ecp_curve_info_from_tls_id( group );
476 if( curve_info == NULL )
477 {
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
480 }
481
482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
483
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000484 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800485 if( ret != 0 )
486 return( ret );
487 }
Jerry Yub85277e2021-10-13 13:36:05 +0800488 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800489#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800490 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 {
492 /* Do something */
493 }
494 else
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
496
497 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
498 return( ret );
499}
500
XiaokangQiand59be772022-01-24 10:12:51 +0000501/*
502 * ssl_tls13_parse_cookie_ext()
503 * Parse cookie extension in Hello Retry Request
504 *
505 * struct {
506 * opaque cookie<1..2^16-1>;
507 * } Cookie;
508 *
509 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
510 * extension to the client (this is an exception to the usual rule that
511 * the only extensions that may be sent are those that appear in the
512 * ClientHello). When sending the new ClientHello, the client MUST copy
513 * the contents of the extension received in the HelloRetryRequest into
514 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
515 * cookies in their initial ClientHello in subsequent connections.
516 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000517static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
518 const unsigned char *buf,
519 const unsigned char *end )
520{
XiaokangQian25c9c902022-02-08 10:49:53 +0000521 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000522 const unsigned char *p = buf;
523 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
524
525 /* Retrieve length field of cookie */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
527 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
528 p += 2;
529
530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
531 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
532
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000533 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000534 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000535 handshake->cookie = mbedtls_calloc( 1, cookie_len );
536 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000537 {
538 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000539 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000540 cookie_len ) );
541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
542 }
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000546
547 return( 0 );
548}
XiaokangQian43550bd2022-01-21 04:32:58 +0000549
XiaokangQian0b64eed2022-01-27 10:36:51 +0000550static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000551 unsigned char *buf,
552 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000553 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000554{
555 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000556 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000558
XiaokangQianc02768a2022-02-10 07:31:25 +0000559 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560 {
561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
562 return( 0 );
563 }
564
565 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000566 handshake->cookie,
567 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000568
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
572
XiaokangQian0b64eed2022-01-27 10:36:51 +0000573 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000574 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
575 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000576 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000577
578 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 return( 0 );
584}
585
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 unsigned char *end,
589 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 unsigned char *p = buf;
593 size_t ext_len;
594
595 *out_len = 0;
596
597 /* Write supported_versions extension
598 *
599 * Supported Versions Extension is mandatory with TLS 1.3.
600 */
601 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
602 if( ret != 0 )
603 return( ret );
604 p += ext_len;
605
606 /* Echo the cookie if the server provided one in its preceding
607 * HelloRetryRequest message.
608 */
609 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
610 if( ret != 0 )
611 return( ret );
612 p += ext_len;
613
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100614 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
615 {
616 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
617 if( ret != 0 )
618 return( ret );
619 p += ext_len;
620 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100621
622 *out_len = p - buf;
623
624 return( 0 );
625}
626
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800627/*
Jerry Yu4a173382021-10-11 21:45:31 +0800628 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800629 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200630
Ronald Cronda41b382022-03-30 09:57:11 +0200631/**
632 * \brief Detect if the ServerHello contains a supported_versions extension
633 * or not.
634 *
635 * \param[in] ssl SSL context
636 * \param[in] buf Buffer containing the ServerHello message
637 * \param[in] end End of the buffer containing the ServerHello message
638 *
639 * \return 0 if the ServerHello does not contain a supported_versions extension
640 * \return 1 if the ServerHello contains a supported_versions extension
641 * \return A negative value if an error occurred while parsing the ServerHello.
642 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100643static int ssl_tls13_is_supported_versions_ext_present(
644 mbedtls_ssl_context *ssl,
645 const unsigned char *buf,
646 const unsigned char *end )
647{
648 const unsigned char *p = buf;
649 size_t legacy_session_id_echo_len;
650 size_t extensions_len;
651 const unsigned char *extensions_end;
652
653 /*
654 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200655 * length:
656 * - legacy_version 2 bytes
657 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
658 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100659 */
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
661 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
662 legacy_session_id_echo_len = *p;
663
664 /*
665 * Jump to the extensions, jumping over:
666 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
667 * - cipher_suite 2 bytes
668 * - legacy_compression_method 1 byte
669 */
Ronald Cron28271062022-06-10 14:43:55 +0200670 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100671 p += legacy_session_id_echo_len + 4;
672
673 /* Case of no extension */
674 if( p == end )
675 return( 0 );
676
677 /* ...
678 * Extension extensions<6..2^16-1>;
679 * ...
680 * struct {
681 * ExtensionType extension_type; (2 bytes)
682 * opaque extension_data<0..2^16-1>;
683 * } Extension;
684 */
685 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
686 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
687 p += 2;
688
689 /* Check extensions do not go beyond the buffer of data. */
690 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
691 extensions_end = p + extensions_len;
692
693 while( p < extensions_end )
694 {
695 unsigned int extension_type;
696 size_t extension_data_len;
697
698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
699 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
700 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
701 p += 4;
702
703 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
704 return( 1 );
705
706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
707 p += extension_data_len;
708 }
709
710 return( 0 );
711}
712
Jerry Yu7a186a02021-10-15 18:46:14 +0800713/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200714 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
715 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
716 * - 0 otherwise
717 */
718static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
719 const unsigned char *buf,
720 const unsigned char *end )
721{
722 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
723 static const unsigned char magic_downgrade_string[] =
724 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
725 const unsigned char *last_eight_bytes_of_random;
726 unsigned char last_byte_of_random;
727
728 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
729 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
730
731 if( memcmp( last_eight_bytes_of_random,
732 magic_downgrade_string,
733 sizeof( magic_downgrade_string ) ) == 0 )
734 {
735 last_byte_of_random = last_eight_bytes_of_random[7];
736 return( last_byte_of_random == 0 ||
737 last_byte_of_random == 1 );
738 }
739
740 return( 0 );
741}
742
743/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800744 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
745 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000746 * to indicate which message is expected and to be parsed next.
747 */
Jerry Yub85277e2021-10-13 13:36:05 +0800748#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
749#define SSL_SERVER_HELLO_COORDINATE_HRR 1
750static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
751 const unsigned char *buf,
752 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800753{
Jerry Yue1b9c292021-09-10 10:08:31 +0800754
755 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
756 *
Jerry Yu4a173382021-10-11 21:45:31 +0800757 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800758 * special value of the SHA-256 of "HelloRetryRequest".
759 *
760 * struct {
761 * ProtocolVersion legacy_version = 0x0303;
762 * Random random;
763 * opaque legacy_session_id_echo<0..32>;
764 * CipherSuite cipher_suite;
765 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800766 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800767 * } ServerHello;
768 *
769 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800770 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
771 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800772
Jerry Yu93a13f22022-04-11 23:00:01 +0800773 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
774 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800775 {
Jerry Yub85277e2021-10-13 13:36:05 +0800776 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800777 }
778
Jerry Yub85277e2021-10-13 13:36:05 +0800779 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800780}
781
Jerry Yu745bb612021-10-13 22:01:04 +0800782/* Fetch and preprocess
783 * Returns a negative value on failure, and otherwise
784 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100785 * - SSL_SERVER_HELLO_COORDINATE_HRR or
786 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800787 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100788#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800789static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
790 unsigned char **buf,
791 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800792{
Jerry Yu4a173382021-10-11 21:45:31 +0800793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200794 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800795
XiaokangQian355e09a2022-01-20 11:14:50 +0000796 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
797 MBEDTLS_SSL_HS_SERVER_HELLO,
798 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200799 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800800
Ronald Cron9f0fba32022-02-10 16:45:15 +0100801 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200802 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100803 if( ret == 0 )
804 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200805 MBEDTLS_SSL_PROC_CHK_NEG(
806 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
807
808 /* If the server is negotiating TLS 1.2 or below and:
809 * . we did not propose TLS 1.2 or
810 * . the server responded it is TLS 1.3 capable but negotiating a lower
811 * version of the protocol and thus we are under downgrade attack
812 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100813 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200814 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100815 {
816 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
817 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
818 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
819 }
820
821 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400822 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100823 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
824 *buf, *buf_len );
825
826 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
827 {
828 ret = ssl_tls13_reset_key_share( ssl );
829 if( ret != 0 )
830 return( ret );
831 }
832
833 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
834 }
835
Ronald Cronfd6193c2022-04-05 11:04:20 +0200836 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800837 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 {
Jerry Yu745bb612021-10-13 22:01:04 +0800839 case SSL_SERVER_HELLO_COORDINATE_HELLO:
840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
841 break;
842 case SSL_SERVER_HELLO_COORDINATE_HRR:
843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000844 /* If a client receives a second
845 * HelloRetryRequest in the same connection (i.e., where the ClientHello
846 * was itself in response to a HelloRetryRequest), it MUST abort the
847 * handshake with an "unexpected_message" alert.
848 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000849 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000850 {
851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
852 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
853 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
855 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000856 /*
857 * Clients must abort the handshake with an "illegal_parameter"
858 * alert if the HelloRetryRequest would not result in any change
859 * in the ClientHello.
860 * In a PSK only key exchange that what we expect.
861 */
862 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
863 {
864 MBEDTLS_SSL_DEBUG_MSG( 1,
865 ( "Unexpected HRR in pure PSK key exchange." ) );
866 MBEDTLS_SSL_PEND_FATAL_ALERT(
867 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
868 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
869 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
870 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000871
XiaokangQiand9e068e2022-01-18 06:23:32 +0000872 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000873
Jerry Yu745bb612021-10-13 22:01:04 +0800874 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800875 }
876
877cleanup:
878
879 return( ret );
880}
881
Jerry Yu4a173382021-10-11 21:45:31 +0800882static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
883 const unsigned char **buf,
884 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800885{
886 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800887 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800888
Jerry Yude4fb2c2021-09-19 18:05:08 +0800889 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800890 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800891
Jerry Yu4a173382021-10-11 21:45:31 +0800892 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800893
894 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800895 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
896 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800898 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
899 ssl->session_negotiate->id,
900 ssl->session_negotiate->id_len );
901 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800902 legacy_session_id_echo_len );
903
904 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
905 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
906
Jerry Yue1b9c292021-09-10 10:08:31 +0800907 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
908 }
909
Jerry Yu4a173382021-10-11 21:45:31 +0800910 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 *buf = p;
912
913 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800914 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 return( 0 );
916}
917
Jerry Yue1b9c292021-09-10 10:08:31 +0800918/* Parse ServerHello message and configure context
919 *
920 * struct {
921 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
922 * Random random;
923 * opaque legacy_session_id_echo<0..32>;
924 * CipherSuite cipher_suite;
925 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800926 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800927 * } ServerHello;
928 */
Jerry Yuc068b662021-10-11 22:30:19 +0800929static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
930 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000931 const unsigned char *end,
932 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800933{
Jerry Yub85277e2021-10-13 13:36:05 +0800934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800935 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000936 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800937 size_t extensions_len;
938 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800939 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000941 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800942
943 /*
944 * Check there is space for minimal fields
945 *
946 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800947 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800948 * - legacy_session_id_echo ( 1 byte ), minimum size
949 * - cipher_suite ( 2 bytes)
950 * - legacy_compression_method ( 1 byte )
951 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800952 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800953
954 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800955 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
956
Jerry Yu4a173382021-10-11 21:45:31 +0800957 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800958 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800959 * ...
960 * with ProtocolVersion defined as:
961 * uint16 ProtocolVersion;
962 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400963 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
964 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 {
966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
967 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
968 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000969 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
970 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800971 }
972 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800974 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800975 * Random random;
976 * ...
977 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800978 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800979 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000980 if( !is_hrr )
981 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000982 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000983 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
984 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
985 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
986 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800987 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988
Jerry Yu4a173382021-10-11 21:45:31 +0800989 /* ...
990 * opaque legacy_session_id_echo<0..32>;
991 * ...
992 */
993 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 {
XiaokangQian52da5582022-01-26 09:49:29 +0000995 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000996 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800997 }
998
Jerry Yu4a173382021-10-11 21:45:31 +0800999 /* ...
1000 * CipherSuite cipher_suite;
1001 * ...
1002 * with CipherSuite defined as:
1003 * uint8 CipherSuite[2];
1004 */
1005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001006 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1007 p += 2;
1008
Jerry Yu4a173382021-10-11 21:45:31 +08001009
XiaokangQian355e09a2022-01-20 11:14:50 +00001010 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001011 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001012 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001013 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001014 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1015 ssl->tls_version,
1016 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001017 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 {
XiaokangQian52da5582022-01-26 09:49:29 +00001019 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001021 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001022 * If we received an HRR before and that the proposed selected
1023 * ciphersuite in this server hello is not the same as the one
1024 * proposed in the HRR, we abort the handshake and send an
1025 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001026 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001027 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1028 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001029 {
XiaokangQian52da5582022-01-26 09:49:29 +00001030 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001031 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001032
XiaokangQian52da5582022-01-26 09:49:29 +00001033 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001034 {
1035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1036 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001037 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001038 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001039
Jerry Yu4a173382021-10-11 21:45:31 +08001040 /* Configure ciphersuites */
1041 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1042
XiaokangQian355e09a2022-01-20 11:14:50 +00001043 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 ssl->session_negotiate->ciphersuite = cipher_suite;
1045
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1047 cipher_suite, ciphersuite_info->name ) );
1048
1049#if defined(MBEDTLS_HAVE_TIME)
1050 ssl->session_negotiate->start = time( NULL );
1051#endif /* MBEDTLS_HAVE_TIME */
1052
Jerry Yu4a173382021-10-11 21:45:31 +08001053 /* ...
1054 * uint8 legacy_compression_method = 0;
1055 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001056 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001057 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 if( p[0] != 0 )
1059 {
Jerry Yub85277e2021-10-13 13:36:05 +08001060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001061 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001062 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001063 }
1064 p++;
1065
Jerry Yub85277e2021-10-13 13:36:05 +08001066 /* ...
1067 * Extension extensions<6..2^16-1>;
1068 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001069 * struct {
1070 * ExtensionType extension_type; (2 bytes)
1071 * opaque extension_data<0..2^16-1>;
1072 * } Extension;
1073 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001074 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001075 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001076 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001077
Jerry Yu4a173382021-10-11 21:45:31 +08001078 /* Check extensions do not go beyond the buffer of data. */
1079 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1080 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081
Jerry Yu4a173382021-10-11 21:45:31 +08001082 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001083
Jerry Yu4a173382021-10-11 21:45:31 +08001084 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 {
1086 unsigned int extension_type;
1087 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001088 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001089
Jerry Yu4a173382021-10-11 21:45:31 +08001090 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1092 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1093 p += 4;
1094
Jerry Yu4a173382021-10-11 21:45:31 +08001095 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001096 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001097
1098 switch( extension_type )
1099 {
XiaokangQianb851da82022-01-14 04:03:11 +00001100 case MBEDTLS_TLS_EXT_COOKIE:
1101
XiaokangQiand9e068e2022-01-18 06:23:32 +00001102 if( !is_hrr )
1103 {
XiaokangQian52da5582022-01-26 09:49:29 +00001104 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001105 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001106 }
1107
XiaokangQian43550bd2022-01-21 04:32:58 +00001108 ret = ssl_tls13_parse_cookie_ext( ssl,
1109 p, extension_data_end );
1110 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001111 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001112 MBEDTLS_SSL_DEBUG_RET( 1,
1113 "ssl_tls13_parse_cookie_ext",
1114 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001115 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001116 }
XiaokangQianb851da82022-01-14 04:03:11 +00001117 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001118
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001120 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001121 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001122 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001124 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001125 break;
1126
1127 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001130
XiaokangQian52da5582022-01-26 09:49:29 +00001131 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001132 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001133
Jerry Yue1b9c292021-09-10 10:08:31 +08001134 case MBEDTLS_TLS_EXT_KEY_SHARE:
1135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001136 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1137 {
XiaokangQian52da5582022-01-26 09:49:29 +00001138 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
1140 }
1141
XiaokangQian53f20b72022-01-18 10:47:33 +00001142 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001143 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001144 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001145 else
XiaokangQianb851da82022-01-14 04:03:11 +00001146 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001147 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001148 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 {
1150 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001151 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001152 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001153 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 }
1155 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156
1157 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001158 MBEDTLS_SSL_DEBUG_MSG(
1159 3,
1160 ( "unknown extension found: %u ( ignoring )",
1161 extension_type ) );
1162
XiaokangQian52da5582022-01-26 09:49:29 +00001163 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001164 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001165 }
1166
1167 p += extension_data_len;
1168 }
1169
XiaokangQiand59be772022-01-24 10:12:51 +00001170cleanup:
1171
XiaokangQian52da5582022-01-26 09:49:29 +00001172 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001173 {
1174 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1175 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1176 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1177 }
XiaokangQian52da5582022-01-26 09:49:29 +00001178 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001179 {
1180 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1181 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1182 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1183 }
1184 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001185}
1186
XiaokangQian355e09a2022-01-20 11:14:50 +00001187static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001188{
Jerry Yub85277e2021-10-13 13:36:05 +08001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001190 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001191
Jerry Yub85277e2021-10-13 13:36:05 +08001192 /* Determine the key exchange mode:
1193 * 1) If both the pre_shared_key and key_share extensions were received
1194 * then the key exchange mode is PSK with EPHEMERAL.
1195 * 2) If only the pre_shared_key extension was received then the key
1196 * exchange mode is PSK-only.
1197 * 3) If only the key_share extension was received then the key
1198 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001199 */
Jerry Yub85277e2021-10-13 13:36:05 +08001200 switch( handshake->extensions_present &
1201 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001202 {
Jerry Yu745bb612021-10-13 22:01:04 +08001203 /* Only the pre_shared_key extension was received */
1204 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001205 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001206 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001207
Jerry Yu745bb612021-10-13 22:01:04 +08001208 /* Only the key_share extension was received */
1209 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001210 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001211 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001212
Jerry Yu745bb612021-10-13 22:01:04 +08001213 /* Both the pre_shared_key and key_share extensions were received */
1214 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001215 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001216 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001217
Jerry Yu745bb612021-10-13 22:01:04 +08001218 /* Neither pre_shared_key nor key_share extension was received */
1219 default:
1220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1221 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1222 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001223 }
1224
1225 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1226 *
1227 * TODO: We don't have to do this in case we offered 0-RTT and the
1228 * server accepted it. In this case, we could skip generating
1229 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001230 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001231 if( ret != 0 )
1232 {
Jerry Yue110d252022-05-05 10:19:22 +08001233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001234 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001235 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001236 }
1237
Jerry Yuf86eb752022-05-06 11:16:55 +08001238 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001239 if( ret != 0 )
1240 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001241 MBEDTLS_SSL_DEBUG_RET( 1,
1242 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001243 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001244 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001245 }
1246
Jerry Yue110d252022-05-05 10:19:22 +08001247 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1249 ssl->session_in = ssl->session_negotiate;
1250
1251 /*
1252 * State machine update
1253 */
1254 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1255
Jerry Yu4a173382021-10-11 21:45:31 +08001256cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001257 if( ret != 0 )
1258 {
Jerry Yu4a173382021-10-11 21:45:31 +08001259 MBEDTLS_SSL_PEND_FATAL_ALERT(
1260 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1261 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1262 }
Jerry Yue110d252022-05-05 10:19:22 +08001263
Jerry Yu4a173382021-10-11 21:45:31 +08001264 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001265}
1266
XiaokangQian355e09a2022-01-20 11:14:50 +00001267static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001268{
1269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1270
XiaokangQian647719a2021-12-07 09:16:29 +00001271#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1272 /* If not offering early data, the client sends a dummy CCS record
1273 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001274 * its second ClientHello or before its encrypted handshake flight.
1275 */
XiaokangQian647719a2021-12-07 09:16:29 +00001276 mbedtls_ssl_handshake_set_state( ssl,
1277 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1278#else
1279 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1280#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1281
XiaokangQian78b1fa72022-01-19 06:56:30 +00001282 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001283
XiaokangQian78b1fa72022-01-19 06:56:30 +00001284 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001285 * We are going to re-generate a shared secret corresponding to the group
1286 * selected by the server, which is different from the group for which we
1287 * generated a shared secret in the first client hello.
1288 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001289 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001290 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001291 if( ret != 0 )
1292 return( ret );
1293
1294 return( 0 );
1295}
1296
Jerry Yue1b9c292021-09-10 10:08:31 +08001297/*
Jerry Yu4a173382021-10-11 21:45:31 +08001298 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001299 * Handler for MBEDTLS_SSL_SERVER_HELLO
1300 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001301static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001302{
Jerry Yu4a173382021-10-11 21:45:31 +08001303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001304 unsigned char *buf = NULL;
1305 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001306 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001307
1308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1309
1310 /* Coordination step
1311 * - Fetch record
1312 * - Make sure it's either a ServerHello or a HRR.
1313 * - Switch processing routine in case of HRR
1314 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001315 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1316
XiaokangQian16acd4b2022-01-14 07:35:47 +00001317 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001318 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001319 goto cleanup;
1320 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001321 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001322
Ronald Cron9f0fba32022-02-10 16:45:15 +01001323 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1324 {
1325 ret = 0;
1326 goto cleanup;
1327 }
1328
XiaokangQianb851da82022-01-14 04:03:11 +00001329 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001330 buf + buf_len,
1331 is_hrr ) );
1332 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001333 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1334
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001335 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1336 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001337
XiaokangQiand9e068e2022-01-18 06:23:32 +00001338 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001339 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001340 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001341 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001342
1343cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1345 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001346 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001347}
1348
1349/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001350 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001351 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001352 *
1353 * The EncryptedExtensions message contains any extensions which
1354 * should be protected, i.e., any which are not needed to establish
1355 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001356 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001357
XiaokangQian08da26c2021-10-09 10:12:11 +00001358/* Parse EncryptedExtensions message
1359 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001360 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001361 * } EncryptedExtensions;
1362 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001363static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1364 const unsigned char *buf,
1365 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001366{
1367 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001368 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001369 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001370 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001371
XiaokangQian08da26c2021-10-09 10:12:11 +00001372 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001373 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001374 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001375
XiaokangQian97799ac2021-10-11 10:05:54 +00001376 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001377 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001378 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001379
XiaokangQian8db25ff2021-10-13 05:56:18 +00001380 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001381 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001382 unsigned int extension_type;
1383 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001384
XiaokangQian08da26c2021-10-09 10:12:11 +00001385 /*
1386 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001387 * ExtensionType extension_type; (2 bytes)
1388 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001389 * } Extension;
1390 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001391 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001392 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1393 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1394 p += 4;
1395
XiaokangQian8db25ff2021-10-13 05:56:18 +00001396 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397
XiaokangQian97799ac2021-10-11 10:05:54 +00001398 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001399 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001400 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001401 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001402 switch( extension_type )
1403 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001404
XiaokangQian08da26c2021-10-09 10:12:11 +00001405 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1407 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001408
lhuang0486cacac2022-01-21 07:34:27 -08001409#if defined(MBEDTLS_SSL_ALPN)
1410 case MBEDTLS_TLS_EXT_ALPN:
1411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1412
1413 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1414 {
1415 return( ret );
1416 }
1417
1418 break;
1419#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001420 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001421 MBEDTLS_SSL_DEBUG_MSG(
1422 3, ( "unsupported extension found: %u ", extension_type) );
1423 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001424 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001425 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1426 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001427 }
1428
XiaokangQian08da26c2021-10-09 10:12:11 +00001429 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001430 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001431
XiaokangQian97799ac2021-10-11 10:05:54 +00001432 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001433 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001434 {
1435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001436 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001437 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001438 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001439 }
1440
1441 return( ret );
1442}
1443
XiaokangQian97799ac2021-10-11 10:05:54 +00001444static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001445{
Jerry Yua93ac112021-10-27 16:31:48 +08001446#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001447 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001448 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1449 else
Jerry Yud2674312021-10-29 10:08:19 +08001450 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001451#else
1452 ((void) ssl);
1453 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1454#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001455 return( 0 );
1456}
1457
Ronald Cron9d6a5452022-05-30 16:05:38 +02001458static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1459{
1460 int ret;
1461 unsigned char *buf;
1462 size_t buf_len;
1463
1464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1465
1466 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1467 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1468 &buf, &buf_len ) );
1469
1470 /* Process the message contents */
1471 MBEDTLS_SSL_PROC_CHK(
1472 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1473
1474 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1475 buf, buf_len );
1476
1477 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
1478
1479cleanup:
1480
1481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1482 return( ret );
1483
1484}
1485
Jerry Yua93ac112021-10-27 16:31:48 +08001486#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001487/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001488 * STATE HANDLING: CertificateRequest
1489 *
Jerry Yud2674312021-10-29 10:08:19 +08001490 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001491#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1492#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001493/* Coordination:
1494 * Deals with the ambiguity of not knowing if a CertificateRequest
1495 * will be sent. Returns a negative code on failure, or
1496 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1497 * - SSL_CERTIFICATE_REQUEST_SKIP
1498 * indicating if a Certificate Request is expected or not.
1499 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001500static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001501{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001503
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001504 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001505 {
1506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1507 return( SSL_CERTIFICATE_REQUEST_SKIP );
1508 }
1509
1510 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001511 {
1512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1513 return( ret );
1514 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001515 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001516
1517 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1518 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1519 {
Ronald Cron19385882022-06-15 16:26:13 +02001520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001521 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001522 }
1523
Ronald Cron19385882022-06-15 16:26:13 +02001524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1525
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001526 return( SSL_CERTIFICATE_REQUEST_SKIP );
1527}
1528
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001529/*
1530 * ssl_tls13_parse_certificate_request()
1531 * Parse certificate request
1532 * struct {
1533 * opaque certificate_request_context<0..2^8-1>;
1534 * Extension extensions<2..2^16-1>;
1535 * } CertificateRequest;
1536 */
1537static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1538 const unsigned char *buf,
1539 const unsigned char *end )
1540{
1541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1542 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001543 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001544 size_t extensions_len = 0;
1545 const unsigned char *extensions_end;
1546 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001547
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001548 /* ...
1549 * opaque certificate_request_context<0..2^8-1>
1550 * ...
1551 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001552 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1553 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001554 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001555
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001556 if( certificate_request_context_len > 0 )
1557 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1560 p, certificate_request_context_len );
1561
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001562 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001563 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001564 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001565 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001566 {
1567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1568 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1569 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001570 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001572 p += certificate_request_context_len;
1573 }
1574
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001575 /* ...
1576 * Extension extensions<2..2^16-1>;
1577 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001578 */
1579 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1580 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1581 p += 2;
1582
1583 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1584 extensions_end = p + extensions_len;
1585
1586 while( p < extensions_end )
1587 {
1588 unsigned int extension_type;
1589 size_t extension_data_len;
1590
1591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1592 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1593 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1594 p += 4;
1595
1596 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1597
1598 switch( extension_type )
1599 {
1600 case MBEDTLS_TLS_EXT_SIG_ALG:
1601 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001602 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001603 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001604 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001605 if( ret != 0 )
1606 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001607 if( ! sig_alg_ext_found )
1608 sig_alg_ext_found = 1;
1609 else
1610 {
1611 MBEDTLS_SSL_DEBUG_MSG( 3,
1612 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001613 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001614 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001615 break;
1616
1617 default:
1618 MBEDTLS_SSL_DEBUG_MSG(
1619 3,
1620 ( "unknown extension found: %u ( ignoring )",
1621 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001622 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001623 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001624 p += extension_data_len;
1625 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001626 /* Check that we consumed all the message. */
1627 if( p != end )
1628 {
1629 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001630 ( "CertificateRequest misaligned" ) );
1631 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001632 }
1633 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001634 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001635 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001636 MBEDTLS_SSL_DEBUG_MSG( 3,
1637 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001638 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001639 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001640
Jerry Yu7840f812022-01-29 10:26:51 +08001641 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001642 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001643
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001644decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001645 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1646 MBEDTLS_ERR_SSL_DECODE_ERROR );
1647 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001648}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001649
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001650/*
1651 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1652 */
1653static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001656
1657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1658
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001659 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1660
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001661 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1662 {
1663 unsigned char *buf;
1664 size_t buf_len;
1665
1666 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1667 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1668 &buf, &buf_len ) );
1669
1670 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1671 buf, buf + buf_len ) );
1672
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001673 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1674 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001675 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001676 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001677 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00001678 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001679 }
1680 else
1681 {
1682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001683 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1684 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001685 }
1686
Jerry Yud2674312021-10-29 10:08:19 +08001687 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1688
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001689cleanup:
1690
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1692 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001693}
1694
1695/*
Jerry Yu687101b2021-09-14 16:03:56 +08001696 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1697 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001698static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001699{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001700 int ret;
1701
1702 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001703 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001704 return( ret );
1705
Jerry Yu687101b2021-09-14 16:03:56 +08001706 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1707 return( 0 );
1708}
1709
1710/*
1711 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1712 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001713static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001714{
Jerry Yu30b071c2021-09-12 20:16:03 +08001715 int ret;
1716
1717 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1718 if( ret != 0 )
1719 return( ret );
1720
Jerry Yu687101b2021-09-14 16:03:56 +08001721 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1722 return( 0 );
1723}
Jerry Yua93ac112021-10-27 16:31:48 +08001724#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001725
Jerry Yu687101b2021-09-14 16:03:56 +08001726/*
1727 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1728 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001729static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001730{
XiaokangQianac0385c2021-11-03 06:40:11 +00001731 int ret;
1732
XiaokangQianc5c39d52021-11-09 11:55:10 +00001733 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001734 if( ret != 0 )
1735 return( ret );
1736
Jerry Yue3d67cb2022-05-19 15:33:10 +08001737 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1738 if( ret != 0 )
1739 {
1740 MBEDTLS_SSL_PEND_FATAL_ALERT(
1741 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1742 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1743 return( ret );
1744 }
1745
Ronald Cron49ad6192021-11-24 16:25:31 +01001746#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1747 mbedtls_ssl_handshake_set_state(
1748 ssl,
1749 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1750#else
Jerry Yuca133a32022-02-15 14:22:05 +08001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001752#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001753
XiaokangQianac0385c2021-11-03 06:40:11 +00001754 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001755}
1756
1757/*
Jerry Yu566c7812022-01-26 15:41:22 +08001758 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1759 */
1760static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1761{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001762 int non_empty_certificate_msg = 0;
1763
Jerry Yu5cc35062022-01-28 16:16:08 +08001764 MBEDTLS_SSL_DEBUG_MSG( 1,
1765 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001766 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001767
Ronald Cron9df7c802022-03-08 18:38:54 +01001768#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001769 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001770 {
1771 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1772 if( ret != 0 )
1773 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001774
Ronald Cron7a94aca2022-03-09 07:44:27 +01001775 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1776 non_empty_certificate_msg = 1;
1777 }
1778 else
1779 {
XiaokangQian23c5be62022-06-07 02:04:34 +00001780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001781 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001782#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001783
Ronald Cron7a94aca2022-03-09 07:44:27 +01001784 if( non_empty_certificate_msg )
1785 {
1786 mbedtls_ssl_handshake_set_state( ssl,
1787 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1788 }
1789 else
Ronald Cron19385882022-06-15 16:26:13 +02001790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001792 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001793 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01001794
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001795 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001796}
1797
Ronald Cron9df7c802022-03-08 18:38:54 +01001798#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001799/*
1800 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1801 */
1802static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1803{
Ronald Crona8b38872022-03-09 07:59:25 +01001804 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1805
1806 if( ret == 0 )
1807 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1808
1809 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001810}
Jerry Yu90f152d2022-01-29 22:12:42 +08001811#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001812
1813/*
Jerry Yu687101b2021-09-14 16:03:56 +08001814 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1815 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001816static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001817{
XiaokangQian0fa66432021-11-15 03:33:57 +00001818 int ret;
1819
1820 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1821 if( ret != 0 )
1822 return( ret );
1823
Jerry Yue3d67cb2022-05-19 15:33:10 +08001824 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1825 if( ret != 0 )
1826 {
1827 MBEDTLS_SSL_DEBUG_RET( 1,
1828 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1829 return ( ret );
1830 }
1831
XiaokangQian0fa66432021-11-15 03:33:57 +00001832 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1833 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001834}
1835
1836/*
1837 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1838 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001839static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001840{
Jerry Yu378254d2021-10-30 21:44:47 +08001841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001842 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001843 return( 0 );
1844}
1845
1846/*
1847 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1848 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001849static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001850{
Jerry Yu378254d2021-10-30 21:44:47 +08001851
1852 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1853
1854 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1855 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001856}
1857
Jerry Yu92c6b402021-08-27 16:59:09 +08001858int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001859{
Jerry Yu92c6b402021-08-27 16:59:09 +08001860 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001861
Jerry Yu92c6b402021-08-27 16:59:09 +08001862 switch( ssl->state )
1863 {
1864 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001865 * ssl->state is initialized as HELLO_REQUEST. It is the same
1866 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001867 */
1868 case MBEDTLS_SSL_HELLO_REQUEST:
1869 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001870 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001871 break;
1872
1873 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001874 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001875 break;
1876
1877 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001878 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001879 break;
1880
Jerry Yua93ac112021-10-27 16:31:48 +08001881#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001882 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1883 ret = ssl_tls13_process_certificate_request( ssl );
1884 break;
1885
Jerry Yu687101b2021-09-14 16:03:56 +08001886 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001887 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001888 break;
1889
1890 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001891 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001892 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001893#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001894
1895 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001896 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001897 break;
1898
Jerry Yu566c7812022-01-26 15:41:22 +08001899 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1900 ret = ssl_tls13_write_client_certificate( ssl );
1901 break;
1902
Ronald Cron9df7c802022-03-08 18:38:54 +01001903#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001904 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1905 ret = ssl_tls13_write_client_certificate_verify( ssl );
1906 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001907#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001908
Jerry Yu687101b2021-09-14 16:03:56 +08001909 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001910 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001911 break;
1912
1913 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001914 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001915 break;
1916
1917 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001918 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001919 break;
1920
Ronald Cron49ad6192021-11-24 16:25:31 +01001921 /*
1922 * Injection of dummy-CCS's for middlebox compatibility
1923 */
1924#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001925 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001926 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1927 if( ret == 0 )
1928 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1929 break;
1930
1931 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1932 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1933 if( ret == 0 )
1934 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001935 break;
1936#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1937
Jerry Yu92c6b402021-08-27 16:59:09 +08001938 default:
1939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1941 }
1942
1943 return( ret );
1944}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001945
Jerry Yufb4b6472022-01-27 15:03:26 +08001946#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001947
Jerry Yufb4b6472022-01-27 15:03:26 +08001948