blob: d024abf18012fd40370f51d906f81cc55fcdfe57 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000048 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040051 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
52 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080053
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020062 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Ronald Crona77fc272022-03-30 17:20:47 +020064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010065
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010067 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080068 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010071 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu0c63af62021-09-02 12:59:12 +080073 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080074 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010075 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080076 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040077 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Glenn Strausscd78df62022-04-07 19:07:11 -040082 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010083 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040084 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
87 }
88
89 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Ronald Cron98473382022-03-30 20:04:10 +0200100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400101 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
102 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
Ronald Cron98473382022-03-30 20:04:10 +0200111 if( &buf[2] != end )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
115 MBEDTLS_ERR_SSL_DECODE_ERROR );
116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
117 }
118
Jerry Yue1b9c292021-09-10 10:08:31 +0800119 return( 0 );
120}
121
lhuang0486cacac2022-01-21 07:34:27 -0800122#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800123static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
124 const unsigned char *buf, size_t len )
125{
126 size_t list_len, name_len;
127 const unsigned char *p = buf;
128 const unsigned char *end = buf + len;
129
130 /* If we didn't send it, the server shouldn't send it */
131 if( ssl->conf->alpn_list == NULL )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
134 /*
135 * opaque ProtocolName<1..2^8-1>;
136 *
137 * struct {
138 * ProtocolName protocol_name_list<2..2^16-1>
139 * } ProtocolNameList;
140 *
141 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
142 */
143
144 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
146
147 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
148 p += 2;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
150
151 name_len = *p++;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
153
154 /* Check that the server chosen protocol was in our list and save it */
155 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
156 {
157 if( name_len == strlen( *alpn ) &&
158 memcmp( buf + 3, *alpn, name_len ) == 0 )
159 {
160 ssl->alpn_chosen = *alpn;
161 return( 0 );
162 }
163 }
164
165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
166}
167#endif /* MBEDTLS_SSL_ALPN */
168
XiaokangQian16acd4b2022-01-14 07:35:47 +0000169static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000170{
171 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( group_id == 0 )
174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
175
XiaokangQian355e09a2022-01-20 11:14:50 +0000176#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000178 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
181
182 /* Destroy generated private key. */
183 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
184 if( status != PSA_SUCCESS )
185 {
186 ret = psa_ssl_status_to_mbedtls( status );
187 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
188 return( ret );
189 }
190
191 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000192 return( 0 );
193 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000194 else
195#endif /* MBEDTLS_ECDH_C */
196 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000197 {
198 /* Do something */
199 }
200
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202}
203
204/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205 * Functions for writing key_share extension.
206 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
208 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209{
210 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
211
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100214 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800215 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100216 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
218
Brett Warren14efd332021-10-06 09:32:11 +0100219 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 const mbedtls_ecp_curve_info *curve_info;
222 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
223 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100224 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Brett Warren14efd332021-10-06 09:32:11 +0100226 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 return( 0 );
228 }
229 }
230#else
231 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233#endif /* MBEDTLS_ECDH_C */
234
235 /*
236 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800237 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 */
239
240 return( ret );
241}
242
243/*
244 * ssl_tls13_write_key_share_ext
245 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 *
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 * struct {
253 * KeyShareEntry client_shares<0..2^16-1>;
254 * } KeyShareClientHello;
255 */
256static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
257 unsigned char *buf,
258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260{
261 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000262 unsigned char *client_shares; /* Start of client_shares */
263 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 * - extension_type (2 bytes)
271 * - extension_data_length (2 bytes)
272 * - client_shares_length (2 bytes)
273 */
274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
275 p += 6;
276
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
278
279 /* HRR could already have requested something else. */
280 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800281 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
282 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 &group_id ) );
286 }
287
288 /*
289 * Dispatch to type-specific key generation function.
290 *
291 * So far, we're only supporting ECDHE. With the introduction
292 * of PQC KEMs, we'll want to have multiple branches, one per
293 * type of KEM, and dispatch to the corresponding crypto. And
294 * only one key share entry is allowed.
295 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000301 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100303 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304
305 /* Check there is space for header of KeyShareEntry
306 * - group (2 bytes)
307 * - key_exchange_length (2 bytes)
308 */
309 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
310 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800311 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
312 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800313 p += key_exchange_len;
314 if( ret != 0 )
315 return( ret );
316
317 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000318 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000320 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 }
322 else
323#endif /* MBEDTLS_ECDH_C */
324 if( 0 /* other KEMs? */ )
325 {
326 /* Do something */
327 }
328 else
329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000332 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800333 if( client_shares_len == 0)
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800337 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write extension_type */
339 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
340 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344
345 /* Update offered_group_id field */
346 ssl->handshake->offered_group_id = group_id;
347
348 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000349 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
354
355cleanup:
356
357 return( ret );
358}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359
Jerry Yue1b9c292021-09-10 10:08:31 +0800360
XiaokangQiand59be772022-01-24 10:12:51 +0000361/*
362 * ssl_tls13_parse_hrr_key_share_ext()
363 * Parse key_share extension in Hello Retry Request
364 *
365 * struct {
366 * NamedGroup selected_group;
367 * } KeyShareHelloRetryRequest;
368 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000369static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000370 const unsigned char *buf,
371 const unsigned char *end )
372{
XiaokangQianb851da82022-01-14 04:03:11 +0000373 const mbedtls_ecp_curve_info *curve_info = NULL;
374 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000375 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000376 int found = 0;
377
378 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
379 if( group_list == NULL )
380 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
381
382 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
383
384 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000386 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000388
389 /* Upon receipt of this extension in a HelloRetryRequest, the client
390 * MUST first verify that the selected_group field corresponds to a
391 * group which was provided in the "supported_groups" extension in the
392 * original ClientHello.
393 * The supported_group was based on the info in ssl->conf->group_list.
394 *
395 * If the server provided a key share that was not sent in the ClientHello
396 * then the client MUST abort the handshake with an "illegal_parameter" alert.
397 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000398 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000399 {
400 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000401 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000402 continue;
403
404 /* We found a match */
405 found = 1;
406 break;
407 }
408
409 /* Client MUST verify that the selected_group field does not
410 * correspond to a group which was provided in the "key_share"
411 * extension in the original ClientHello. If the server sent an
412 * HRR message with a key share already provided in the
413 * ClientHello then the client MUST abort the handshake with
414 * an "illegal_parameter" alert.
415 */
XiaokangQiand59be772022-01-24 10:12:51 +0000416 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
419 MBEDTLS_SSL_PEND_FATAL_ALERT(
420 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
421 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
422 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
423 }
424
425 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000426 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000427
428 return( 0 );
429}
430
Jerry Yue1b9c292021-09-10 10:08:31 +0800431/*
Jerry Yub85277e2021-10-13 13:36:05 +0800432 * ssl_tls13_parse_key_share_ext()
433 * Parse key_share extension in Server Hello
434 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800435 * struct {
436 * KeyShareEntry server_share;
437 * } KeyShareServerHello;
438 * struct {
439 * NamedGroup group;
440 * opaque key_exchange<1..2^16-1>;
441 * } KeyShareEntry;
442 */
Jerry Yu4a173382021-10-11 21:45:31 +0800443static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 const unsigned char *buf,
445 const unsigned char *end )
446{
Jerry Yub85277e2021-10-13 13:36:05 +0800447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800448 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800449 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450
Jerry Yu4a173382021-10-11 21:45:31 +0800451 /* ...
452 * NamedGroup group; (2 bytes)
453 * ...
454 */
455 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
456 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800457 p += 2;
458
Jerry Yu4a173382021-10-11 21:45:31 +0800459 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800460 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800461 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1,
464 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800465 (unsigned) offered_group, (unsigned) group ) );
466 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
467 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
468 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 }
470
471#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800472 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800473 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100474 const mbedtls_ecp_curve_info *curve_info =
475 mbedtls_ecp_curve_info_from_tls_id( group );
476 if( curve_info == NULL )
477 {
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
480 }
481
482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
483
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000484 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800485 if( ret != 0 )
486 return( ret );
487 }
Jerry Yub85277e2021-10-13 13:36:05 +0800488 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800489#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800490 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 {
492 /* Do something */
493 }
494 else
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
496
497 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
498 return( ret );
499}
500
XiaokangQiand59be772022-01-24 10:12:51 +0000501/*
502 * ssl_tls13_parse_cookie_ext()
503 * Parse cookie extension in Hello Retry Request
504 *
505 * struct {
506 * opaque cookie<1..2^16-1>;
507 * } Cookie;
508 *
509 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
510 * extension to the client (this is an exception to the usual rule that
511 * the only extensions that may be sent are those that appear in the
512 * ClientHello). When sending the new ClientHello, the client MUST copy
513 * the contents of the extension received in the HelloRetryRequest into
514 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
515 * cookies in their initial ClientHello in subsequent connections.
516 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000517static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
518 const unsigned char *buf,
519 const unsigned char *end )
520{
XiaokangQian25c9c902022-02-08 10:49:53 +0000521 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000522 const unsigned char *p = buf;
523 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
524
525 /* Retrieve length field of cookie */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
527 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
528 p += 2;
529
530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
531 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
532
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000533 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000534 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000535 handshake->cookie = mbedtls_calloc( 1, cookie_len );
536 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000537 {
538 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000539 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000540 cookie_len ) );
541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
542 }
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000546
547 return( 0 );
548}
XiaokangQian43550bd2022-01-21 04:32:58 +0000549
XiaokangQian0b64eed2022-01-27 10:36:51 +0000550static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000551 unsigned char *buf,
552 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000553 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000554{
555 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000556 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000558
XiaokangQianc02768a2022-02-10 07:31:25 +0000559 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560 {
561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
562 return( 0 );
563 }
564
565 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000566 handshake->cookie,
567 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000568
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
572
XiaokangQian0b64eed2022-01-27 10:36:51 +0000573 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000574 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
575 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000576 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000577
578 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 return( 0 );
584}
585
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 unsigned char *end,
589 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 unsigned char *p = buf;
593 size_t ext_len;
594
595 *out_len = 0;
596
597 /* Write supported_versions extension
598 *
599 * Supported Versions Extension is mandatory with TLS 1.3.
600 */
601 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
602 if( ret != 0 )
603 return( ret );
604 p += ext_len;
605
606 /* Echo the cookie if the server provided one in its preceding
607 * HelloRetryRequest message.
608 */
609 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
610 if( ret != 0 )
611 return( ret );
612 p += ext_len;
613
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100614 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
615 {
616 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
617 if( ret != 0 )
618 return( ret );
619 p += ext_len;
620 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100621
622 *out_len = p - buf;
623
624 return( 0 );
625}
626
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800627/*
Jerry Yu4a173382021-10-11 21:45:31 +0800628 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800629 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200630
Ronald Cronda41b382022-03-30 09:57:11 +0200631/**
632 * \brief Detect if the ServerHello contains a supported_versions extension
633 * or not.
634 *
635 * \param[in] ssl SSL context
636 * \param[in] buf Buffer containing the ServerHello message
637 * \param[in] end End of the buffer containing the ServerHello message
638 *
639 * \return 0 if the ServerHello does not contain a supported_versions extension
640 * \return 1 if the ServerHello contains a supported_versions extension
641 * \return A negative value if an error occurred while parsing the ServerHello.
642 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100643static int ssl_tls13_is_supported_versions_ext_present(
644 mbedtls_ssl_context *ssl,
645 const unsigned char *buf,
646 const unsigned char *end )
647{
648 const unsigned char *p = buf;
649 size_t legacy_session_id_echo_len;
650 size_t extensions_len;
651 const unsigned char *extensions_end;
652
653 /*
654 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200655 * length:
656 * - legacy_version 2 bytes
657 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
658 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100659 */
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
661 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
662 legacy_session_id_echo_len = *p;
663
664 /*
665 * Jump to the extensions, jumping over:
666 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
667 * - cipher_suite 2 bytes
668 * - legacy_compression_method 1 byte
669 */
670 p += legacy_session_id_echo_len + 4;
671
672 /* Case of no extension */
673 if( p == end )
674 return( 0 );
675
676 /* ...
677 * Extension extensions<6..2^16-1>;
678 * ...
679 * struct {
680 * ExtensionType extension_type; (2 bytes)
681 * opaque extension_data<0..2^16-1>;
682 * } Extension;
683 */
684 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
685 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
686 p += 2;
687
688 /* Check extensions do not go beyond the buffer of data. */
689 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
690 extensions_end = p + extensions_len;
691
692 while( p < extensions_end )
693 {
694 unsigned int extension_type;
695 size_t extension_data_len;
696
697 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
698 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
699 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
700 p += 4;
701
702 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
703 return( 1 );
704
705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
706 p += extension_data_len;
707 }
708
709 return( 0 );
710}
711
Jerry Yu7a186a02021-10-15 18:46:14 +0800712/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200713 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
714 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
715 * - 0 otherwise
716 */
717static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
718 const unsigned char *buf,
719 const unsigned char *end )
720{
721 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
722 static const unsigned char magic_downgrade_string[] =
723 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
724 const unsigned char *last_eight_bytes_of_random;
725 unsigned char last_byte_of_random;
726
727 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
728 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
729
730 if( memcmp( last_eight_bytes_of_random,
731 magic_downgrade_string,
732 sizeof( magic_downgrade_string ) ) == 0 )
733 {
734 last_byte_of_random = last_eight_bytes_of_random[7];
735 return( last_byte_of_random == 0 ||
736 last_byte_of_random == 1 );
737 }
738
739 return( 0 );
740}
741
742/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800743 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
744 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000745 * to indicate which message is expected and to be parsed next.
746 */
Jerry Yub85277e2021-10-13 13:36:05 +0800747#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
748#define SSL_SERVER_HELLO_COORDINATE_HRR 1
749static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
750 const unsigned char *buf,
751 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800752{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800753 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800754 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
755 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
756 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
757 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
758
759 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
760 *
Jerry Yu4a173382021-10-11 21:45:31 +0800761 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800762 * special value of the SHA-256 of "HelloRetryRequest".
763 *
764 * struct {
765 * ProtocolVersion legacy_version = 0x0303;
766 * Random random;
767 * opaque legacy_session_id_echo<0..32>;
768 * CipherSuite cipher_suite;
769 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800770 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800771 * } ServerHello;
772 *
773 */
Jerry Yub85277e2021-10-13 13:36:05 +0800774 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800775
776 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800777 {
Jerry Yub85277e2021-10-13 13:36:05 +0800778 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800779 }
780
Jerry Yub85277e2021-10-13 13:36:05 +0800781 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800782}
783
Jerry Yu745bb612021-10-13 22:01:04 +0800784/* Fetch and preprocess
785 * Returns a negative value on failure, and otherwise
786 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
Ronald Cron9f0fba32022-02-10 16:45:15 +0100787 * - SSL_SERVER_HELLO_COORDINATE_HRR or
788 * - SSL_SERVER_HELLO_COORDINATE_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800789 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100790#define SSL_SERVER_HELLO_COORDINATE_TLS1_2 2
Jerry Yub85277e2021-10-13 13:36:05 +0800791static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
792 unsigned char **buf,
793 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800794{
Jerry Yu4a173382021-10-11 21:45:31 +0800795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cronfd6193c2022-04-05 11:04:20 +0200796 const unsigned char *end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800797
XiaokangQian355e09a2022-01-20 11:14:50 +0000798 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
799 MBEDTLS_SSL_HS_SERVER_HELLO,
800 buf, buf_len ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200801 end = *buf + *buf_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800802
Ronald Cron9f0fba32022-02-10 16:45:15 +0100803 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Cronfd6193c2022-04-05 11:04:20 +0200804 ssl, *buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100805 if( ret == 0 )
806 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200807 MBEDTLS_SSL_PROC_CHK_NEG(
808 ssl_tls13_is_downgrade_negotiation( ssl, *buf, end ) );
809
810 /* If the server is negotiating TLS 1.2 or below and:
811 * . we did not propose TLS 1.2 or
812 * . the server responded it is TLS 1.3 capable but negotiating a lower
813 * version of the protocol and thus we are under downgrade attack
814 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100815 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200816 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100817 {
818 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
819 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
820 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
821 }
822
823 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400824 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100825 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
826 *buf, *buf_len );
827
828 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
829 {
830 ret = ssl_tls13_reset_key_share( ssl );
831 if( ret != 0 )
832 return( ret );
833 }
834
835 return( SSL_SERVER_HELLO_COORDINATE_TLS1_2 );
836 }
837
Ronald Cronfd6193c2022-04-05 11:04:20 +0200838 ret = ssl_server_hello_is_hrr( ssl, *buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800839 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800840 {
Jerry Yu745bb612021-10-13 22:01:04 +0800841 case SSL_SERVER_HELLO_COORDINATE_HELLO:
842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
843 break;
844 case SSL_SERVER_HELLO_COORDINATE_HRR:
845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000846 /* If a client receives a second
847 * HelloRetryRequest in the same connection (i.e., where the ClientHello
848 * was itself in response to a HelloRetryRequest), it MUST abort the
849 * handshake with an "unexpected_message" alert.
850 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000851 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000852 {
853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
854 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
855 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
856 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
857 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000858 /*
859 * Clients must abort the handshake with an "illegal_parameter"
860 * alert if the HelloRetryRequest would not result in any change
861 * in the ClientHello.
862 * In a PSK only key exchange that what we expect.
863 */
864 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
865 {
866 MBEDTLS_SSL_DEBUG_MSG( 1,
867 ( "Unexpected HRR in pure PSK key exchange." ) );
868 MBEDTLS_SSL_PEND_FATAL_ALERT(
869 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
870 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
871 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
872 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000873
XiaokangQiand9e068e2022-01-18 06:23:32 +0000874 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000875
Jerry Yu745bb612021-10-13 22:01:04 +0800876 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800877 }
878
879cleanup:
880
881 return( ret );
882}
883
Jerry Yu4a173382021-10-11 21:45:31 +0800884static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
885 const unsigned char **buf,
886 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800887{
888 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800889 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800890
Jerry Yude4fb2c2021-09-19 18:05:08 +0800891 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800892 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800893
Jerry Yu4a173382021-10-11 21:45:31 +0800894 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800895
896 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800897 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
898 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800899 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800900 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
901 ssl->session_negotiate->id,
902 ssl->session_negotiate->id_len );
903 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800904 legacy_session_id_echo_len );
905
906 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
907 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
908
Jerry Yue1b9c292021-09-10 10:08:31 +0800909 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
910 }
911
Jerry Yu4a173382021-10-11 21:45:31 +0800912 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800913 *buf = p;
914
915 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800916 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 return( 0 );
918}
919
Jerry Yue1b9c292021-09-10 10:08:31 +0800920/* Parse ServerHello message and configure context
921 *
922 * struct {
923 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
924 * Random random;
925 * opaque legacy_session_id_echo<0..32>;
926 * CipherSuite cipher_suite;
927 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800928 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 * } ServerHello;
930 */
Jerry Yuc068b662021-10-11 22:30:19 +0800931static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
932 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000933 const unsigned char *end,
934 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800935{
Jerry Yub85277e2021-10-13 13:36:05 +0800936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800937 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000938 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800939 size_t extensions_len;
940 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800942 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000943 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800944
945 /*
946 * Check there is space for minimal fields
947 *
948 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800949 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800950 * - legacy_session_id_echo ( 1 byte ), minimum size
951 * - cipher_suite ( 2 bytes)
952 * - legacy_compression_method ( 1 byte )
953 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800954 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800955
956 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800957 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
958
Jerry Yu4a173382021-10-11 21:45:31 +0800959 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800960 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800961 * ...
962 * with ProtocolVersion defined as:
963 * uint16 ProtocolVersion;
964 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400965 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
966 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800967 {
968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
969 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
970 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000971 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
972 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800973 }
974 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800975
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800976 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800977 * Random random;
978 * ...
979 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800980 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800981 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000982 if( !is_hrr )
983 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000984 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000985 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
986 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
987 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
988 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800989 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800990
Jerry Yu4a173382021-10-11 21:45:31 +0800991 /* ...
992 * opaque legacy_session_id_echo<0..32>;
993 * ...
994 */
995 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 {
XiaokangQian52da5582022-01-26 09:49:29 +0000997 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000998 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800999 }
1000
Jerry Yu4a173382021-10-11 21:45:31 +08001001 /* ...
1002 * CipherSuite cipher_suite;
1003 * ...
1004 * with CipherSuite defined as:
1005 * uint8 CipherSuite[2];
1006 */
1007 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001008 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1009 p += 2;
1010
Jerry Yu4a173382021-10-11 21:45:31 +08001011
XiaokangQian355e09a2022-01-20 11:14:50 +00001012 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001013 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001014 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001015 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001016 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1017 ssl->tls_version,
1018 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001019 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 {
XiaokangQian52da5582022-01-26 09:49:29 +00001021 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001022 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001023 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001024 * If we received an HRR before and that the proposed selected
1025 * ciphersuite in this server hello is not the same as the one
1026 * proposed in the HRR, we abort the handshake and send an
1027 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001028 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001029 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1030 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001031 {
XiaokangQian52da5582022-01-26 09:49:29 +00001032 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001033 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001034
XiaokangQian52da5582022-01-26 09:49:29 +00001035 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001036 {
1037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1038 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001039 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001040 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001041
Jerry Yu4a173382021-10-11 21:45:31 +08001042 /* Configure ciphersuites */
1043 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1044
XiaokangQian355e09a2022-01-20 11:14:50 +00001045 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 ssl->session_negotiate->ciphersuite = cipher_suite;
1047
Jerry Yue1b9c292021-09-10 10:08:31 +08001048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1049 cipher_suite, ciphersuite_info->name ) );
1050
1051#if defined(MBEDTLS_HAVE_TIME)
1052 ssl->session_negotiate->start = time( NULL );
1053#endif /* MBEDTLS_HAVE_TIME */
1054
Jerry Yu4a173382021-10-11 21:45:31 +08001055 /* ...
1056 * uint8 legacy_compression_method = 0;
1057 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001058 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001059 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001060 if( p[0] != 0 )
1061 {
Jerry Yub85277e2021-10-13 13:36:05 +08001062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001063 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001064 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001065 }
1066 p++;
1067
Jerry Yub85277e2021-10-13 13:36:05 +08001068 /* ...
1069 * Extension extensions<6..2^16-1>;
1070 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001071 * struct {
1072 * ExtensionType extension_type; (2 bytes)
1073 * opaque extension_data<0..2^16-1>;
1074 * } Extension;
1075 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001076 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001077 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001079
Jerry Yu4a173382021-10-11 21:45:31 +08001080 /* Check extensions do not go beyond the buffer of data. */
1081 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1082 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001083
Jerry Yu4a173382021-10-11 21:45:31 +08001084 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085
Jerry Yu4a173382021-10-11 21:45:31 +08001086 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001087 {
1088 unsigned int extension_type;
1089 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001090 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001091
Jerry Yu4a173382021-10-11 21:45:31 +08001092 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001093 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1094 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1095 p += 4;
1096
Jerry Yu4a173382021-10-11 21:45:31 +08001097 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001098 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001099
1100 switch( extension_type )
1101 {
XiaokangQianb851da82022-01-14 04:03:11 +00001102 case MBEDTLS_TLS_EXT_COOKIE:
1103
XiaokangQiand9e068e2022-01-18 06:23:32 +00001104 if( !is_hrr )
1105 {
XiaokangQian52da5582022-01-26 09:49:29 +00001106 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001107 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001108 }
1109
XiaokangQian43550bd2022-01-21 04:32:58 +00001110 ret = ssl_tls13_parse_cookie_ext( ssl,
1111 p, extension_data_end );
1112 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001113 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001114 MBEDTLS_SSL_DEBUG_RET( 1,
1115 "ssl_tls13_parse_cookie_ext",
1116 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001117 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001118 }
XiaokangQianb851da82022-01-14 04:03:11 +00001119 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001120
Jerry Yue1b9c292021-09-10 10:08:31 +08001121 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001122 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001123 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001124 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001125 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001126 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 break;
1128
1129 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001132
XiaokangQian52da5582022-01-26 09:49:29 +00001133 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001134 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001135
Jerry Yue1b9c292021-09-10 10:08:31 +08001136 case MBEDTLS_TLS_EXT_KEY_SHARE:
1137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001138 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1139 {
XiaokangQian52da5582022-01-26 09:49:29 +00001140 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001141 goto cleanup;
1142 }
1143
XiaokangQian53f20b72022-01-18 10:47:33 +00001144 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001145 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001146 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001147 else
XiaokangQianb851da82022-01-14 04:03:11 +00001148 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001149 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001150 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 {
1152 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001153 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001155 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 }
1157 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001158
1159 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001160 MBEDTLS_SSL_DEBUG_MSG(
1161 3,
1162 ( "unknown extension found: %u ( ignoring )",
1163 extension_type ) );
1164
XiaokangQian52da5582022-01-26 09:49:29 +00001165 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001166 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 }
1168
1169 p += extension_data_len;
1170 }
1171
XiaokangQiand59be772022-01-24 10:12:51 +00001172cleanup:
1173
XiaokangQian52da5582022-01-26 09:49:29 +00001174 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001175 {
1176 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1177 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1178 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1179 }
XiaokangQian52da5582022-01-26 09:49:29 +00001180 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001181 {
1182 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1183 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1184 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1185 }
1186 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001187}
1188
XiaokangQian355e09a2022-01-20 11:14:50 +00001189static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001190{
Jerry Yub85277e2021-10-13 13:36:05 +08001191 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001192 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001193 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001194 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001195
Jerry Yub85277e2021-10-13 13:36:05 +08001196 /* Determine the key exchange mode:
1197 * 1) If both the pre_shared_key and key_share extensions were received
1198 * then the key exchange mode is PSK with EPHEMERAL.
1199 * 2) If only the pre_shared_key extension was received then the key
1200 * exchange mode is PSK-only.
1201 * 3) If only the key_share extension was received then the key
1202 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001203 */
Jerry Yub85277e2021-10-13 13:36:05 +08001204 switch( handshake->extensions_present &
1205 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001206 {
Jerry Yu745bb612021-10-13 22:01:04 +08001207 /* Only the pre_shared_key extension was received */
1208 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001209 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001210 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001211
Jerry Yu745bb612021-10-13 22:01:04 +08001212 /* Only the key_share extension was received */
1213 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001214 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001215 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001216
Jerry Yu745bb612021-10-13 22:01:04 +08001217 /* Both the pre_shared_key and key_share extensions were received */
1218 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001219 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001220 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001221
Jerry Yu745bb612021-10-13 22:01:04 +08001222 /* Neither pre_shared_key nor key_share extension was received */
1223 default:
1224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1225 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1226 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001227 }
1228
1229 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1230 *
1231 * TODO: We don't have to do this in case we offered 0-RTT and the
1232 * server accepted it. In this case, we could skip generating
1233 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001234 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001235 if( ret != 0 )
1236 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001238 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001239 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001240 }
1241
1242 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001243 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001244 if( ret != 0 )
1245 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001247 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001248 }
1249
1250 /* Next evolution in key schedule: Establish handshake secret and
1251 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001252 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001253 if( ret != 0 )
1254 {
Jerry Yuc068b662021-10-11 22:30:19 +08001255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1256 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001257 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001258 }
1259
Jerry Yub85277e2021-10-13 13:36:05 +08001260 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001261 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001262 {
1263 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1264 goto cleanup;
1265 }
Jerry Yu0b177842021-09-05 19:41:30 +08001266
1267 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1268 ssl->conf->endpoint,
1269 ssl->session_negotiate->ciphersuite,
1270 &traffic_keys,
1271 ssl );
1272 if( ret != 0 )
1273 {
1274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001275 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001276 }
1277
Jerry Yub85277e2021-10-13 13:36:05 +08001278 handshake->transform_handshake = transform_handshake;
1279 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001280
1281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1282 ssl->session_in = ssl->session_negotiate;
1283
1284 /*
1285 * State machine update
1286 */
1287 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1288
Jerry Yu4a173382021-10-11 21:45:31 +08001289cleanup:
1290
Jerry Yu0b177842021-09-05 19:41:30 +08001291 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001292 if( ret != 0 )
1293 {
Jerry Yub85277e2021-10-13 13:36:05 +08001294 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001295
Jerry Yu4a173382021-10-11 21:45:31 +08001296 MBEDTLS_SSL_PEND_FATAL_ALERT(
1297 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1298 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1299 }
1300 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001301}
1302
XiaokangQian355e09a2022-01-20 11:14:50 +00001303static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001304{
1305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1306
XiaokangQian647719a2021-12-07 09:16:29 +00001307#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1308 /* If not offering early data, the client sends a dummy CCS record
1309 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001310 * its second ClientHello or before its encrypted handshake flight.
1311 */
XiaokangQian647719a2021-12-07 09:16:29 +00001312 mbedtls_ssl_handshake_set_state( ssl,
1313 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1314#else
1315 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1316#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1317
XiaokangQian78b1fa72022-01-19 06:56:30 +00001318 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001319
XiaokangQian78b1fa72022-01-19 06:56:30 +00001320 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001321 * We are going to re-generate a shared secret corresponding to the group
1322 * selected by the server, which is different from the group for which we
1323 * generated a shared secret in the first client hello.
1324 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001325 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001326 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001327 if( ret != 0 )
1328 return( ret );
1329
1330 return( 0 );
1331}
1332
Jerry Yue1b9c292021-09-10 10:08:31 +08001333/*
Jerry Yu4a173382021-10-11 21:45:31 +08001334 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001335 * Handler for MBEDTLS_SSL_SERVER_HELLO
1336 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001337static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001338{
Jerry Yu4a173382021-10-11 21:45:31 +08001339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001340 unsigned char *buf = NULL;
1341 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001342 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001343
1344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1345
1346 /* Coordination step
1347 * - Fetch record
1348 * - Make sure it's either a ServerHello or a HRR.
1349 * - Switch processing routine in case of HRR
1350 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001351 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1352
XiaokangQian16acd4b2022-01-14 07:35:47 +00001353 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001354 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001355 goto cleanup;
1356 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001357 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001358
Ronald Cron9f0fba32022-02-10 16:45:15 +01001359 if( ret == SSL_SERVER_HELLO_COORDINATE_TLS1_2 )
1360 {
1361 ret = 0;
1362 goto cleanup;
1363 }
1364
XiaokangQianb851da82022-01-14 04:03:11 +00001365 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001366 buf + buf_len,
1367 is_hrr ) );
1368 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001369 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1370
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001371 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1372 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001373
XiaokangQiand9e068e2022-01-18 06:23:32 +00001374 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001375 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001376 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001377 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001378
1379cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1381 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001382 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001383}
1384
1385/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001386 *
1387 * EncryptedExtensions message
1388 *
1389 * The EncryptedExtensions message contains any extensions which
1390 * should be protected, i.e., any which are not needed to establish
1391 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001392 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001393
1394/*
1395 * Overview
1396 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397
1398/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001399static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001400
XiaokangQian97799ac2021-10-11 10:05:54 +00001401static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1402 const unsigned char *buf,
1403 const unsigned char *end );
1404static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001405
1406/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001407 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001408 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001409static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001410{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001411 int ret;
1412 unsigned char *buf;
1413 size_t buf_len;
1414
1415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1416
Xiaofei Bai746f9482021-11-12 08:53:56 +00001417 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001418 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001419 &buf, &buf_len ) );
1420
1421 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001422 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001423 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001424
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001425 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1426 buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001427
XiaokangQian97799ac2021-10-11 10:05:54 +00001428 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001429
1430cleanup:
1431
1432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1433 return( ret );
1434
1435}
1436
XiaokangQian08da26c2021-10-09 10:12:11 +00001437/* Parse EncryptedExtensions message
1438 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001439 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001440 * } EncryptedExtensions;
1441 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001442static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1443 const unsigned char *buf,
1444 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001445{
1446 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001447 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001448 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001449 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001450
XiaokangQian08da26c2021-10-09 10:12:11 +00001451 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001452 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001453 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001454
XiaokangQian97799ac2021-10-11 10:05:54 +00001455 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001456 extensions_end = p + extensions_len;
1457 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001458
XiaokangQian8db25ff2021-10-13 05:56:18 +00001459 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001460 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001461 unsigned int extension_type;
1462 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001463
XiaokangQian08da26c2021-10-09 10:12:11 +00001464 /*
1465 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001466 * ExtensionType extension_type; (2 bytes)
1467 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001468 * } Extension;
1469 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001470 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001471 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1472 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1473 p += 4;
1474
XiaokangQian8db25ff2021-10-13 05:56:18 +00001475 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001476
XiaokangQian97799ac2021-10-11 10:05:54 +00001477 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001478 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001479 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001480 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001481 switch( extension_type )
1482 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001483
XiaokangQian08da26c2021-10-09 10:12:11 +00001484 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1486 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001487
lhuang0486cacac2022-01-21 07:34:27 -08001488#if defined(MBEDTLS_SSL_ALPN)
1489 case MBEDTLS_TLS_EXT_ALPN:
1490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1491
1492 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1493 {
1494 return( ret );
1495 }
1496
1497 break;
1498#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001499 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001500 MBEDTLS_SSL_DEBUG_MSG(
1501 3, ( "unsupported extension found: %u ", extension_type) );
1502 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001503 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001504 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1505 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001506 }
1507
XiaokangQian08da26c2021-10-09 10:12:11 +00001508 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001509 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001510
XiaokangQian97799ac2021-10-11 10:05:54 +00001511 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001512 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001513 {
1514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001515 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001516 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001518 }
1519
1520 return( ret );
1521}
1522
XiaokangQian97799ac2021-10-11 10:05:54 +00001523static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001524{
Jerry Yua93ac112021-10-27 16:31:48 +08001525#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001526 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001527 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1528 else
Jerry Yud2674312021-10-29 10:08:19 +08001529 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001530#else
1531 ((void) ssl);
1532 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1533#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001534 return( 0 );
1535}
1536
Jerry Yua93ac112021-10-27 16:31:48 +08001537#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001538/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001539 *
1540 * STATE HANDLING: CertificateRequest
1541 *
Jerry Yud2674312021-10-29 10:08:19 +08001542 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001543#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1544#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001545/* Coordination:
1546 * Deals with the ambiguity of not knowing if a CertificateRequest
1547 * will be sent. Returns a negative code on failure, or
1548 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1549 * - SSL_CERTIFICATE_REQUEST_SKIP
1550 * indicating if a Certificate Request is expected or not.
1551 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001552static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001553{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001555
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001556 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001557 {
1558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1559 return( SSL_CERTIFICATE_REQUEST_SKIP );
1560 }
1561
1562 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001563 {
1564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1565 return( ret );
1566 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001568
1569 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1570 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1571 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001572 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001573 }
1574
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001575 return( SSL_CERTIFICATE_REQUEST_SKIP );
1576}
1577
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001578/*
1579 * ssl_tls13_parse_certificate_request()
1580 * Parse certificate request
1581 * struct {
1582 * opaque certificate_request_context<0..2^8-1>;
1583 * Extension extensions<2..2^16-1>;
1584 * } CertificateRequest;
1585 */
1586static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1587 const unsigned char *buf,
1588 const unsigned char *end )
1589{
1590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1591 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001592 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001593 size_t extensions_len = 0;
1594 const unsigned char *extensions_end;
1595 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001596
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001597 /* ...
1598 * opaque certificate_request_context<0..2^8-1>
1599 * ...
1600 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001601 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1602 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001603 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001604
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001605 if( certificate_request_context_len > 0 )
1606 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001607 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001608 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1609 p, certificate_request_context_len );
1610
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001611 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001612 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001613 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001614 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001615 {
1616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1617 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1618 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001619 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001620 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001621 p += certificate_request_context_len;
1622 }
1623
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001624 /* ...
1625 * Extension extensions<2..2^16-1>;
1626 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001627 */
1628 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1629 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1630 p += 2;
1631
1632 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1633 extensions_end = p + extensions_len;
1634
1635 while( p < extensions_end )
1636 {
1637 unsigned int extension_type;
1638 size_t extension_data_len;
1639
1640 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1641 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1642 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1643 p += 4;
1644
1645 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1646
1647 switch( extension_type )
1648 {
1649 case MBEDTLS_TLS_EXT_SIG_ALG:
1650 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001651 ( "found signature algorithms extension" ) );
1652 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001653 p + extension_data_len );
1654 if( ret != 0 )
1655 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001656 if( ! sig_alg_ext_found )
1657 sig_alg_ext_found = 1;
1658 else
1659 {
1660 MBEDTLS_SSL_DEBUG_MSG( 3,
1661 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001662 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001663 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001664 break;
1665
1666 default:
1667 MBEDTLS_SSL_DEBUG_MSG(
1668 3,
1669 ( "unknown extension found: %u ( ignoring )",
1670 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001671 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001673 p += extension_data_len;
1674 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001675 /* Check that we consumed all the message. */
1676 if( p != end )
1677 {
1678 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001679 ( "CertificateRequest misaligned" ) );
1680 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001681 }
1682 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001683 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001684 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001685 MBEDTLS_SSL_DEBUG_MSG( 3,
1686 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001687 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001688 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001689
Jerry Yu7840f812022-01-29 10:26:51 +08001690 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001691 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001692
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001693decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001694 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1695 MBEDTLS_ERR_SSL_DECODE_ERROR );
1696 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001697}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001699/*
1700 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1701 */
1702static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001703{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001705
1706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1707
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001708 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1709
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001710 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1711 {
1712 unsigned char *buf;
1713 size_t buf_len;
1714
1715 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1716 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1717 &buf, &buf_len ) );
1718
1719 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1720 buf, buf + buf_len ) );
1721
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001722 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1723 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001724 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001725 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001726 {
1727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001728 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001729 }
1730 else
1731 {
1732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001733 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1734 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001735 }
1736
1737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001738 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001739
Jerry Yud2674312021-10-29 10:08:19 +08001740 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1741
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001742cleanup:
1743
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1745 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001746}
1747
1748/*
Jerry Yu687101b2021-09-14 16:03:56 +08001749 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1750 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001751static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001752{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001753 int ret;
1754
1755 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001756 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001757 return( ret );
1758
Jerry Yu687101b2021-09-14 16:03:56 +08001759 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1760 return( 0 );
1761}
1762
1763/*
1764 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1765 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001766static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001767{
Jerry Yu30b071c2021-09-12 20:16:03 +08001768 int ret;
1769
1770 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1771 if( ret != 0 )
1772 return( ret );
1773
Jerry Yu687101b2021-09-14 16:03:56 +08001774 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1775 return( 0 );
1776}
Jerry Yua93ac112021-10-27 16:31:48 +08001777#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001778
Jerry Yu687101b2021-09-14 16:03:56 +08001779/*
1780 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1781 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001782static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001783{
XiaokangQianac0385c2021-11-03 06:40:11 +00001784 int ret;
1785
XiaokangQianc5c39d52021-11-09 11:55:10 +00001786 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001787 if( ret != 0 )
1788 return( ret );
1789
Ronald Cron49ad6192021-11-24 16:25:31 +01001790#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1791 mbedtls_ssl_handshake_set_state(
1792 ssl,
1793 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1794#else
Jerry Yuca133a32022-02-15 14:22:05 +08001795 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001796#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001797
XiaokangQianac0385c2021-11-03 06:40:11 +00001798 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001799}
1800
1801/*
Jerry Yu566c7812022-01-26 15:41:22 +08001802 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1803 */
1804static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1805{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001806 int non_empty_certificate_msg = 0;
1807
Jerry Yu5cc35062022-01-28 16:16:08 +08001808 MBEDTLS_SSL_DEBUG_MSG( 1,
1809 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001810 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001811
Ronald Cron9df7c802022-03-08 18:38:54 +01001812#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001813 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001814 {
1815 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1816 if( ret != 0 )
1817 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001818
Ronald Cron7a94aca2022-03-09 07:44:27 +01001819 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1820 non_empty_certificate_msg = 1;
1821 }
1822 else
1823 {
1824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate message to send." ) );
1825 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001826#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001827
Ronald Cron7a94aca2022-03-09 07:44:27 +01001828 if( non_empty_certificate_msg )
1829 {
1830 mbedtls_ssl_handshake_set_state( ssl,
1831 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1832 }
1833 else
1834 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1835
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001836 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001837}
1838
Ronald Cron9df7c802022-03-08 18:38:54 +01001839#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001840/*
1841 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1842 */
1843static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1844{
Ronald Crona8b38872022-03-09 07:59:25 +01001845 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1846
1847 if( ret == 0 )
1848 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1849
1850 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001851}
Jerry Yu90f152d2022-01-29 22:12:42 +08001852#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001853
1854/*
Jerry Yu687101b2021-09-14 16:03:56 +08001855 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1856 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001857static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001858{
XiaokangQian0fa66432021-11-15 03:33:57 +00001859 int ret;
1860
1861 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1862 if( ret != 0 )
1863 return( ret );
1864
1865 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1866 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001867}
1868
1869/*
1870 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1871 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001872static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001873{
Jerry Yu378254d2021-10-30 21:44:47 +08001874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001875 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001876 return( 0 );
1877}
1878
1879/*
1880 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1881 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001882static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001883{
Jerry Yu378254d2021-10-30 21:44:47 +08001884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1885 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1886
1887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1888 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1889
1890 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1891
1892 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1893 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001894}
1895
Jerry Yu92c6b402021-08-27 16:59:09 +08001896int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001897{
Jerry Yu92c6b402021-08-27 16:59:09 +08001898 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001899
Jerry Yu92c6b402021-08-27 16:59:09 +08001900 switch( ssl->state )
1901 {
1902 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001903 * ssl->state is initialized as HELLO_REQUEST. It is the same
1904 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001905 */
1906 case MBEDTLS_SSL_HELLO_REQUEST:
1907 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001908 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001909 break;
1910
1911 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001912 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001913 break;
1914
1915 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001916 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001917 break;
1918
Jerry Yua93ac112021-10-27 16:31:48 +08001919#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001920 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1921 ret = ssl_tls13_process_certificate_request( ssl );
1922 break;
1923
Jerry Yu687101b2021-09-14 16:03:56 +08001924 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001925 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001926 break;
1927
1928 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001929 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001930 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001931#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001932
1933 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001934 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001935 break;
1936
Jerry Yu566c7812022-01-26 15:41:22 +08001937 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1938 ret = ssl_tls13_write_client_certificate( ssl );
1939 break;
1940
Ronald Cron9df7c802022-03-08 18:38:54 +01001941#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001942 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1943 ret = ssl_tls13_write_client_certificate_verify( ssl );
1944 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001945#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001946
Jerry Yu687101b2021-09-14 16:03:56 +08001947 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001948 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001949 break;
1950
1951 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001952 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001953 break;
1954
1955 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001956 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001957 break;
1958
Ronald Cron49ad6192021-11-24 16:25:31 +01001959 /*
1960 * Injection of dummy-CCS's for middlebox compatibility
1961 */
1962#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001963 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001964 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1965 if( ret == 0 )
1966 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1967 break;
1968
1969 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1970 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1971 if( ret == 0 )
1972 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001973 break;
1974#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1975
Jerry Yu92c6b402021-08-27 16:59:09 +08001976 default:
1977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1979 }
1980
1981 return( ret );
1982}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001983
Jerry Yufb4b6472022-01-27 15:03:26 +08001984#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001985
Jerry Yufb4b6472022-01-27 15:03:26 +08001986