blob: 416316b9aaf7e9a191c75a16ba5a6907e14df5f5 [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,
Ronald Cron81a334f2022-05-31 16:04:11 +0200124 const unsigned char *buf, size_t len )
lhuang0486cacac2022-01-21 07:34:27 -0800125{
lhuang0486cacac2022-01-21 07:34:27 -0800126 const unsigned char *p = buf;
127 const unsigned char *end = buf + len;
Ronald Cron81a334f2022-05-31 16:04:11 +0200128 size_t protocol_name_list_len, protocol_name_len;
129 const unsigned char *protocol_name_list_end;
lhuang0486cacac2022-01-21 07:34:27 -0800130
131 /* If we didn't send it, the server shouldn't send it */
132 if( ssl->conf->alpn_list == NULL )
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134
135 /*
136 * opaque ProtocolName<1..2^8-1>;
137 *
138 * struct {
139 * ProtocolName protocol_name_list<2..2^16-1>
140 * } ProtocolNameList;
141 *
142 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
143 */
144
Ronald Cron81a334f2022-05-31 16:04:11 +0200145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
146 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
lhuang0486cacac2022-01-21 07:34:27 -0800147 p += 2;
lhuang0486cacac2022-01-21 07:34:27 -0800148
Ronald Cron81a334f2022-05-31 16:04:11 +0200149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
150 protocol_name_list_end = p + protocol_name_list_len;
151
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, 1 );
153 protocol_name_len = *p++;
lhuang0486cacac2022-01-21 07:34:27 -0800154
155 /* Check that the server chosen protocol was in our list and save it */
Ronald Cron81a334f2022-05-31 16:04:11 +0200156 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, protocol_name_len );
157 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
lhuang0486cacac2022-01-21 07:34:27 -0800158 {
Ronald Cron81a334f2022-05-31 16:04:11 +0200159 if( protocol_name_len == strlen( *alpn ) &&
160 memcmp( p, *alpn, protocol_name_len ) == 0 )
lhuang0486cacac2022-01-21 07:34:27 -0800161 {
162 ssl->alpn_chosen = *alpn;
163 return( 0 );
164 }
165 }
166
167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
168}
169#endif /* MBEDTLS_SSL_ALPN */
170
XiaokangQian16acd4b2022-01-14 07:35:47 +0000171static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000172{
173 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100174
XiaokangQian647719a2021-12-07 09:16:29 +0000175 if( group_id == 0 )
176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
177
XiaokangQian355e09a2022-01-20 11:14:50 +0000178#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000179 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000180 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
183
184 /* Destroy generated private key. */
185 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
186 if( status != PSA_SUCCESS )
187 {
188 ret = psa_ssl_status_to_mbedtls( status );
189 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
190 return( ret );
191 }
192
193 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000194 return( 0 );
195 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000196 else
197#endif /* MBEDTLS_ECDH_C */
198 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000199 {
200 /* Do something */
201 }
202
203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
204}
205
206/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800207 * Functions for writing key_share extension.
208 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800209static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
210 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211{
212 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
213
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100216 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100218 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800219 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
220
Brett Warren14efd332021-10-06 09:32:11 +0100221 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000223 const mbedtls_ecp_curve_info *curve_info;
224 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
225 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100226 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 {
Brett Warren14efd332021-10-06 09:32:11 +0100228 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800229 return( 0 );
230 }
231 }
232#else
233 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800234 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800235#endif /* MBEDTLS_ECDH_C */
236
237 /*
238 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800239 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240 */
241
242 return( ret );
243}
244
245/*
246 * ssl_tls13_write_key_share_ext
247 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800248 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800249 *
250 * struct {
251 * NamedGroup group;
252 * opaque key_exchange<1..2^16-1>;
253 * } KeyShareEntry;
254 * struct {
255 * KeyShareEntry client_shares<0..2^16-1>;
256 * } KeyShareClientHello;
257 */
258static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
259 unsigned char *buf,
260 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000261 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800262{
263 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000264 unsigned char *client_shares; /* Start of client_shares */
265 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800266 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
268
Xiaofei Baid25fab62021-12-02 06:36:27 +0000269 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270
Jerry Yub60e3cf2021-09-08 16:41:02 +0800271 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272 * - extension_type (2 bytes)
273 * - extension_data_length (2 bytes)
274 * - client_shares_length (2 bytes)
275 */
276 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
277 p += 6;
278
279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
280
281 /* HRR could already have requested something else. */
282 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800283 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
284 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800286 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800287 &group_id ) );
288 }
289
290 /*
291 * Dispatch to type-specific key generation function.
292 *
293 * So far, we're only supporting ECDHE. With the introduction
294 * of PQC KEMs, we'll want to have multiple branches, one per
295 * type of KEM, and dispatch to the corresponding crypto. And
296 * only one key share entry is allowed.
297 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000298 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800300 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800301 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800302 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000303 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100305 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306
307 /* Check there is space for header of KeyShareEntry
308 * - group (2 bytes)
309 * - key_exchange_length (2 bytes)
310 */
311 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
312 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800313 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
314 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800315 p += key_exchange_len;
316 if( ret != 0 )
317 return( ret );
318
319 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000320 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000322 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800323 }
324 else
325#endif /* MBEDTLS_ECDH_C */
326 if( 0 /* other KEMs? */ )
327 {
328 /* Do something */
329 }
330 else
331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
332
Jerry Yub60e3cf2021-09-08 16:41:02 +0800333 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000334 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800335 if( client_shares_len == 0)
336 {
337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800339 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340 /* Write extension_type */
341 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
342 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800345 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346
347 /* Update offered_group_id field */
348 ssl->handshake->offered_group_id = group_id;
349
350 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
Xiaofei Baid25fab62021-12-02 06:36:27 +0000353 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354
355 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
356
357cleanup:
358
359 return( ret );
360}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800361
Jerry Yue1b9c292021-09-10 10:08:31 +0800362
XiaokangQiand59be772022-01-24 10:12:51 +0000363/*
364 * ssl_tls13_parse_hrr_key_share_ext()
365 * Parse key_share extension in Hello Retry Request
366 *
367 * struct {
368 * NamedGroup selected_group;
369 * } KeyShareHelloRetryRequest;
370 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000371static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000372 const unsigned char *buf,
373 const unsigned char *end )
374{
XiaokangQianb851da82022-01-14 04:03:11 +0000375 const mbedtls_ecp_curve_info *curve_info = NULL;
376 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000377 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000378 int found = 0;
379
380 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
381 if( group_list == NULL )
382 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
383
384 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
385
386 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000387 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000388 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000390
391 /* Upon receipt of this extension in a HelloRetryRequest, the client
392 * MUST first verify that the selected_group field corresponds to a
393 * group which was provided in the "supported_groups" extension in the
394 * original ClientHello.
395 * The supported_group was based on the info in ssl->conf->group_list.
396 *
397 * If the server provided a key share that was not sent in the ClientHello
398 * then the client MUST abort the handshake with an "illegal_parameter" alert.
399 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000400 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000401 {
402 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000403 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000404 continue;
405
406 /* We found a match */
407 found = 1;
408 break;
409 }
410
411 /* Client MUST verify that the selected_group field does not
412 * correspond to a group which was provided in the "key_share"
413 * extension in the original ClientHello. If the server sent an
414 * HRR message with a key share already provided in the
415 * ClientHello then the client MUST abort the handshake with
416 * an "illegal_parameter" alert.
417 */
XiaokangQiand59be772022-01-24 10:12:51 +0000418 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000419 {
420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
421 MBEDTLS_SSL_PEND_FATAL_ALERT(
422 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
423 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
424 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
425 }
426
427 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000428 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000429
430 return( 0 );
431}
432
Jerry Yue1b9c292021-09-10 10:08:31 +0800433/*
Jerry Yub85277e2021-10-13 13:36:05 +0800434 * ssl_tls13_parse_key_share_ext()
435 * Parse key_share extension in Server Hello
436 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800437 * struct {
438 * KeyShareEntry server_share;
439 * } KeyShareServerHello;
440 * struct {
441 * NamedGroup group;
442 * opaque key_exchange<1..2^16-1>;
443 * } KeyShareEntry;
444 */
Jerry Yu4a173382021-10-11 21:45:31 +0800445static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800446 const unsigned char *buf,
447 const unsigned char *end )
448{
Jerry Yub85277e2021-10-13 13:36:05 +0800449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800451 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800452
Jerry Yu4a173382021-10-11 21:45:31 +0800453 /* ...
454 * NamedGroup group; (2 bytes)
455 * ...
456 */
457 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
458 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800459 p += 2;
460
Jerry Yu4a173382021-10-11 21:45:31 +0800461 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800463 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800464 {
465 MBEDTLS_SSL_DEBUG_MSG( 1,
466 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800467 (unsigned) offered_group, (unsigned) group ) );
468 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
469 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
470 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800471 }
472
473#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800474 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800475 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100476 const mbedtls_ecp_curve_info *curve_info =
477 mbedtls_ecp_curve_info_from_tls_id( group );
478 if( curve_info == NULL )
479 {
480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
482 }
483
484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
485
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000486 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800487 if( ret != 0 )
488 return( ret );
489 }
Jerry Yub85277e2021-10-13 13:36:05 +0800490 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800491#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800492 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800493 {
494 /* Do something */
495 }
496 else
497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
498
499 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
500 return( ret );
501}
502
XiaokangQiand59be772022-01-24 10:12:51 +0000503/*
504 * ssl_tls13_parse_cookie_ext()
505 * Parse cookie extension in Hello Retry Request
506 *
507 * struct {
508 * opaque cookie<1..2^16-1>;
509 * } Cookie;
510 *
511 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
512 * extension to the client (this is an exception to the usual rule that
513 * the only extensions that may be sent are those that appear in the
514 * ClientHello). When sending the new ClientHello, the client MUST copy
515 * the contents of the extension received in the HelloRetryRequest into
516 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
517 * cookies in their initial ClientHello in subsequent connections.
518 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000519static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
520 const unsigned char *buf,
521 const unsigned char *end )
522{
XiaokangQian25c9c902022-02-08 10:49:53 +0000523 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000524 const unsigned char *p = buf;
525 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
526
527 /* Retrieve length field of cookie */
528 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
529 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
530 p += 2;
531
532 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
533 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
534
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000535 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000536 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000537 handshake->cookie = mbedtls_calloc( 1, cookie_len );
538 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000541 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000542 cookie_len ) );
543 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
544 }
545
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000546 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000547 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000548
549 return( 0 );
550}
XiaokangQian43550bd2022-01-21 04:32:58 +0000551
XiaokangQian0b64eed2022-01-27 10:36:51 +0000552static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000553 unsigned char *buf,
554 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000555 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000556{
557 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000558 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000559 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560
XiaokangQianc02768a2022-02-10 07:31:25 +0000561 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000562 {
563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
564 return( 0 );
565 }
566
567 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000568 handshake->cookie,
569 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
XiaokangQianc02768a2022-02-10 07:31:25 +0000571 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000572
573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
574
XiaokangQian0b64eed2022-01-27 10:36:51 +0000575 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000576 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
577 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000578 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000579
580 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
XiaokangQianc02768a2022-02-10 07:31:25 +0000583 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000584
585 return( 0 );
586}
587
Ronald Cron3d580bf2022-02-18 17:24:56 +0100588int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
589 unsigned char *buf,
590 unsigned char *end,
591 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100592{
593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
594 unsigned char *p = buf;
595 size_t ext_len;
596
597 *out_len = 0;
598
599 /* Write supported_versions extension
600 *
601 * Supported Versions Extension is mandatory with TLS 1.3.
602 */
603 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
604 if( ret != 0 )
605 return( ret );
606 p += ext_len;
607
608 /* Echo the cookie if the server provided one in its preceding
609 * HelloRetryRequest message.
610 */
611 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
612 if( ret != 0 )
613 return( ret );
614 p += ext_len;
615
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100616 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
617 {
618 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
619 if( ret != 0 )
620 return( ret );
621 p += ext_len;
622 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100623
624 *out_len = p - buf;
625
626 return( 0 );
627}
628
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800629/*
Jerry Yu4a173382021-10-11 21:45:31 +0800630 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800631 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200632
Ronald Cronda41b382022-03-30 09:57:11 +0200633/**
634 * \brief Detect if the ServerHello contains a supported_versions extension
635 * or not.
636 *
637 * \param[in] ssl SSL context
638 * \param[in] buf Buffer containing the ServerHello message
639 * \param[in] end End of the buffer containing the ServerHello message
640 *
641 * \return 0 if the ServerHello does not contain a supported_versions extension
642 * \return 1 if the ServerHello contains a supported_versions extension
643 * \return A negative value if an error occurred while parsing the ServerHello.
644 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100645static int ssl_tls13_is_supported_versions_ext_present(
646 mbedtls_ssl_context *ssl,
647 const unsigned char *buf,
648 const unsigned char *end )
649{
650 const unsigned char *p = buf;
651 size_t legacy_session_id_echo_len;
652 size_t extensions_len;
653 const unsigned char *extensions_end;
654
655 /*
656 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200657 * length:
658 * - legacy_version 2 bytes
659 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
660 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100661 */
662 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
663 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
664 legacy_session_id_echo_len = *p;
665
666 /*
667 * Jump to the extensions, jumping over:
668 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
669 * - cipher_suite 2 bytes
670 * - legacy_compression_method 1 byte
671 */
Ronald Cron28271062022-06-10 14:43:55 +0200672 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100673 p += legacy_session_id_echo_len + 4;
674
675 /* Case of no extension */
676 if( p == end )
677 return( 0 );
678
679 /* ...
680 * Extension extensions<6..2^16-1>;
681 * ...
682 * struct {
683 * ExtensionType extension_type; (2 bytes)
684 * opaque extension_data<0..2^16-1>;
685 * } Extension;
686 */
687 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
688 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
689 p += 2;
690
691 /* Check extensions do not go beyond the buffer of data. */
692 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
693 extensions_end = p + extensions_len;
694
695 while( p < extensions_end )
696 {
697 unsigned int extension_type;
698 size_t extension_data_len;
699
700 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
701 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
702 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
703 p += 4;
704
705 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
706 return( 1 );
707
708 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
709 p += extension_data_len;
710 }
711
712 return( 0 );
713}
714
Jerry Yu7a186a02021-10-15 18:46:14 +0800715/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200716 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
717 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
718 * - 0 otherwise
719 */
720static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
721 const unsigned char *buf,
722 const unsigned char *end )
723{
724 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
725 static const unsigned char magic_downgrade_string[] =
726 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
727 const unsigned char *last_eight_bytes_of_random;
728 unsigned char last_byte_of_random;
729
730 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
731 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
732
733 if( memcmp( last_eight_bytes_of_random,
734 magic_downgrade_string,
735 sizeof( magic_downgrade_string ) ) == 0 )
736 {
737 last_byte_of_random = last_eight_bytes_of_random[7];
738 return( last_byte_of_random == 0 ||
739 last_byte_of_random == 1 );
740 }
741
742 return( 0 );
743}
744
745/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200746 * - SSL_SERVER_HELLO or
747 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000748 * to indicate which message is expected and to be parsed next.
749 */
Ronald Cron828aff62022-05-31 12:04:31 +0200750#define SSL_SERVER_HELLO 0
751#define SSL_SERVER_HELLO_HRR 1
Jerry Yub85277e2021-10-13 13:36:05 +0800752static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
753 const unsigned char *buf,
754 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800755{
Jerry Yue1b9c292021-09-10 10:08:31 +0800756
757 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
758 *
Jerry Yu4a173382021-10-11 21:45:31 +0800759 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800760 * special value of the SHA-256 of "HelloRetryRequest".
761 *
762 * struct {
763 * ProtocolVersion legacy_version = 0x0303;
764 * Random random;
765 * opaque legacy_session_id_echo<0..32>;
766 * CipherSuite cipher_suite;
767 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800768 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800769 * } ServerHello;
770 *
771 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800772 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
773 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800774
Jerry Yu93a13f22022-04-11 23:00:01 +0800775 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
776 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800777 {
Ronald Cron828aff62022-05-31 12:04:31 +0200778 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800779 }
780
Ronald Cron828aff62022-05-31 12:04:31 +0200781 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800782}
783
Ronald Cron828aff62022-05-31 12:04:31 +0200784/*
Jerry Yu745bb612021-10-13 22:01:04 +0800785 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200786 * - SSL_SERVER_HELLO or
787 * - SSL_SERVER_HELLO_HRR or
788 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800789 */
Ronald Cron828aff62022-05-31 12:04:31 +0200790#define SSL_SERVER_HELLO_TLS1_2 2
791static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200792 const unsigned char *buf,
793 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800794{
Jerry Yu4a173382021-10-11 21:45:31 +0800795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +0200796 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800797
Ronald Cron9f0fba32022-02-10 16:45:15 +0100798 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200799 ssl, buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100800 if( ret == 0 )
801 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200802 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200803 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200804
805 /* If the server is negotiating TLS 1.2 or below and:
806 * . we did not propose TLS 1.2 or
807 * . the server responded it is TLS 1.3 capable but negotiating a lower
808 * version of the protocol and thus we are under downgrade attack
809 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100810 */
Ronald Cron5afb9042022-05-31 12:11:39 +0200811 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100812 {
813 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
814 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
815 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
816 }
817
818 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400819 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100820 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200821 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100822
823 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
824 {
825 ret = ssl_tls13_reset_key_share( ssl );
826 if( ret != 0 )
827 return( ret );
828 }
829
Ronald Cron828aff62022-05-31 12:04:31 +0200830 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100831 }
832
Ronald Cron5afb9042022-05-31 12:11:39 +0200833 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
834
Ronald Crondb5dfa12022-05-31 11:44:38 +0200835 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800836 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800837 {
Ronald Cron828aff62022-05-31 12:04:31 +0200838 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +0800839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
840 break;
Ronald Cron828aff62022-05-31 12:04:31 +0200841 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +0800842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000843 /* If a client receives a second
844 * HelloRetryRequest in the same connection (i.e., where the ClientHello
845 * was itself in response to a HelloRetryRequest), it MUST abort the
846 * handshake with an "unexpected_message" alert.
847 */
Ronald Cron5afb9042022-05-31 12:11:39 +0200848 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000849 {
850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
851 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
852 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
854 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000855 /*
856 * Clients must abort the handshake with an "illegal_parameter"
857 * alert if the HelloRetryRequest would not result in any change
858 * in the ClientHello.
859 * In a PSK only key exchange that what we expect.
860 */
861 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
862 {
863 MBEDTLS_SSL_DEBUG_MSG( 1,
864 ( "Unexpected HRR in pure PSK key exchange." ) );
865 MBEDTLS_SSL_PEND_FATAL_ALERT(
866 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
867 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
868 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
869 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000870
Ronald Cron5afb9042022-05-31 12:11:39 +0200871 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000872
Jerry Yu745bb612021-10-13 22:01:04 +0800873 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800874 }
875
876cleanup:
877
878 return( ret );
879}
880
Jerry Yu4a173382021-10-11 21:45:31 +0800881static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
882 const unsigned char **buf,
883 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800884{
885 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800886 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800887
Jerry Yude4fb2c2021-09-19 18:05:08 +0800888 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800889 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800890
Jerry Yu4a173382021-10-11 21:45:31 +0800891 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800892
893 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800894 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
895 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800896 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
898 ssl->session_negotiate->id,
899 ssl->session_negotiate->id_len );
900 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800901 legacy_session_id_echo_len );
902
903 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
904 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
905
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
907 }
908
Jerry Yu4a173382021-10-11 21:45:31 +0800909 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800910 *buf = p;
911
912 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800913 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 return( 0 );
915}
916
Jerry Yue1b9c292021-09-10 10:08:31 +0800917/* Parse ServerHello message and configure context
918 *
919 * struct {
920 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
921 * Random random;
922 * opaque legacy_session_id_echo<0..32>;
923 * CipherSuite cipher_suite;
924 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800925 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800926 * } ServerHello;
927 */
Jerry Yuc068b662021-10-11 22:30:19 +0800928static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
929 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000930 const unsigned char *end,
931 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800932{
Jerry Yub85277e2021-10-13 13:36:05 +0800933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800934 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000935 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800936 size_t extensions_len;
937 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800938 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800939 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000940 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941
942 /*
943 * Check there is space for minimal fields
944 *
945 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800946 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800947 * - legacy_session_id_echo ( 1 byte ), minimum size
948 * - cipher_suite ( 2 bytes)
949 * - legacy_compression_method ( 1 byte )
950 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800951 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800952
953 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
955
Jerry Yu4a173382021-10-11 21:45:31 +0800956 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800957 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800958 * ...
959 * with ProtocolVersion defined as:
960 * uint16 ProtocolVersion;
961 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400962 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
963 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
966 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
967 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000968 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
969 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 }
971 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800972
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800973 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800974 * Random random;
975 * ...
976 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800977 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800978 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000979 if( !is_hrr )
980 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000981 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000982 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
984 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
985 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800986 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800987
Jerry Yu4a173382021-10-11 21:45:31 +0800988 /* ...
989 * opaque legacy_session_id_echo<0..32>;
990 * ...
991 */
992 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 {
XiaokangQian52da5582022-01-26 09:49:29 +0000994 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000995 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800996 }
997
Jerry Yu4a173382021-10-11 21:45:31 +0800998 /* ...
999 * CipherSuite cipher_suite;
1000 * ...
1001 * with CipherSuite defined as:
1002 * uint8 CipherSuite[2];
1003 */
1004 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1006 p += 2;
1007
Jerry Yu4a173382021-10-11 21:45:31 +08001008
XiaokangQian355e09a2022-01-20 11:14:50 +00001009 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001010 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001011 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001012 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001013 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1014 ssl->tls_version,
1015 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001016 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 {
XiaokangQian52da5582022-01-26 09:49:29 +00001018 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001020 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001021 * If we received an HRR before and that the proposed selected
1022 * ciphersuite in this server hello is not the same as the one
1023 * proposed in the HRR, we abort the handshake and send an
1024 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001025 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001026 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1027 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001028 {
XiaokangQian52da5582022-01-26 09:49:29 +00001029 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001030 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001031
XiaokangQian52da5582022-01-26 09:49:29 +00001032 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001033 {
1034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1035 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001036 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001037 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001038
Jerry Yu4a173382021-10-11 21:45:31 +08001039 /* Configure ciphersuites */
1040 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1041
XiaokangQian355e09a2022-01-20 11:14:50 +00001042 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001043 ssl->session_negotiate->ciphersuite = cipher_suite;
1044
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1046 cipher_suite, ciphersuite_info->name ) );
1047
1048#if defined(MBEDTLS_HAVE_TIME)
1049 ssl->session_negotiate->start = time( NULL );
1050#endif /* MBEDTLS_HAVE_TIME */
1051
Jerry Yu4a173382021-10-11 21:45:31 +08001052 /* ...
1053 * uint8 legacy_compression_method = 0;
1054 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001055 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001056 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001057 if( p[0] != 0 )
1058 {
Jerry Yub85277e2021-10-13 13:36:05 +08001059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001060 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001061 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001062 }
1063 p++;
1064
Jerry Yub85277e2021-10-13 13:36:05 +08001065 /* ...
1066 * Extension extensions<6..2^16-1>;
1067 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001068 * struct {
1069 * ExtensionType extension_type; (2 bytes)
1070 * opaque extension_data<0..2^16-1>;
1071 * } Extension;
1072 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001073 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001074 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001075 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001076
Jerry Yu4a173382021-10-11 21:45:31 +08001077 /* Check extensions do not go beyond the buffer of data. */
1078 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1079 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001080
Jerry Yu4a173382021-10-11 21:45:31 +08001081 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001082
Jerry Yu4a173382021-10-11 21:45:31 +08001083 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 {
1085 unsigned int extension_type;
1086 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001087 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001088
Jerry Yu4a173382021-10-11 21:45:31 +08001089 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001090 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1091 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1092 p += 4;
1093
Jerry Yu4a173382021-10-11 21:45:31 +08001094 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001095 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001096
1097 switch( extension_type )
1098 {
XiaokangQianb851da82022-01-14 04:03:11 +00001099 case MBEDTLS_TLS_EXT_COOKIE:
1100
XiaokangQiand9e068e2022-01-18 06:23:32 +00001101 if( !is_hrr )
1102 {
XiaokangQian52da5582022-01-26 09:49:29 +00001103 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001104 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001105 }
1106
XiaokangQian43550bd2022-01-21 04:32:58 +00001107 ret = ssl_tls13_parse_cookie_ext( ssl,
1108 p, extension_data_end );
1109 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001110 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001111 MBEDTLS_SSL_DEBUG_RET( 1,
1112 "ssl_tls13_parse_cookie_ext",
1113 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001114 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001115 }
XiaokangQianb851da82022-01-14 04:03:11 +00001116 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001117
Jerry Yue1b9c292021-09-10 10:08:31 +08001118 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001119 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001120 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001121 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001122 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001123 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001124 break;
1125
1126 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001129
XiaokangQian52da5582022-01-26 09:49:29 +00001130 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001131 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001132
Jerry Yue1b9c292021-09-10 10:08:31 +08001133 case MBEDTLS_TLS_EXT_KEY_SHARE:
1134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001135 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1136 {
XiaokangQian52da5582022-01-26 09:49:29 +00001137 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001138 goto cleanup;
1139 }
1140
XiaokangQian53f20b72022-01-18 10:47:33 +00001141 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001142 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001143 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001144 else
XiaokangQianb851da82022-01-14 04:03:11 +00001145 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001146 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001147 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 {
1149 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001150 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001151 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001152 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001153 }
1154 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001155
1156 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001157 MBEDTLS_SSL_DEBUG_MSG(
1158 3,
1159 ( "unknown extension found: %u ( ignoring )",
1160 extension_type ) );
1161
XiaokangQian52da5582022-01-26 09:49:29 +00001162 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001163 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 }
1165
1166 p += extension_data_len;
1167 }
1168
XiaokangQiand59be772022-01-24 10:12:51 +00001169cleanup:
1170
XiaokangQian52da5582022-01-26 09:49:29 +00001171 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001172 {
1173 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1174 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1175 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1176 }
XiaokangQian52da5582022-01-26 09:49:29 +00001177 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001178 {
1179 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1180 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1181 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1182 }
1183 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001184}
1185
XiaokangQian355e09a2022-01-20 11:14:50 +00001186static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001187{
Jerry Yub85277e2021-10-13 13:36:05 +08001188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001189 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001190
Jerry Yub85277e2021-10-13 13:36:05 +08001191 /* Determine the key exchange mode:
1192 * 1) If both the pre_shared_key and key_share extensions were received
1193 * then the key exchange mode is PSK with EPHEMERAL.
1194 * 2) If only the pre_shared_key extension was received then the key
1195 * exchange mode is PSK-only.
1196 * 3) If only the key_share extension was received then the key
1197 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001198 */
Jerry Yub85277e2021-10-13 13:36:05 +08001199 switch( handshake->extensions_present &
1200 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001201 {
Jerry Yu745bb612021-10-13 22:01:04 +08001202 /* Only the pre_shared_key extension was received */
1203 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001204 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001205 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001206
Jerry Yu745bb612021-10-13 22:01:04 +08001207 /* Only the key_share extension was received */
1208 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001209 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001210 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001211
Jerry Yu745bb612021-10-13 22:01:04 +08001212 /* Both the pre_shared_key and key_share extensions were received */
1213 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001214 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001215 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001216
Jerry Yu745bb612021-10-13 22:01:04 +08001217 /* Neither pre_shared_key nor key_share extension was received */
1218 default:
1219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1220 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1221 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001222 }
1223
1224 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1225 *
1226 * TODO: We don't have to do this in case we offered 0-RTT and the
1227 * server accepted it. In this case, we could skip generating
1228 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001229 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001230 if( ret != 0 )
1231 {
Jerry Yue110d252022-05-05 10:19:22 +08001232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001233 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001234 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001235 }
1236
Jerry Yuf86eb752022-05-06 11:16:55 +08001237 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001238 if( ret != 0 )
1239 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001240 MBEDTLS_SSL_DEBUG_RET( 1,
1241 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001242 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001243 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001244 }
1245
Jerry Yue110d252022-05-05 10:19:22 +08001246 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1248 ssl->session_in = ssl->session_negotiate;
1249
Jerry Yu4a173382021-10-11 21:45:31 +08001250cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001251 if( ret != 0 )
1252 {
Jerry Yu4a173382021-10-11 21:45:31 +08001253 MBEDTLS_SSL_PEND_FATAL_ALERT(
1254 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1255 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1256 }
Jerry Yue110d252022-05-05 10:19:22 +08001257
Jerry Yu4a173382021-10-11 21:45:31 +08001258 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001259}
1260
XiaokangQian355e09a2022-01-20 11:14:50 +00001261static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001262{
1263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1264
XiaokangQian78b1fa72022-01-19 06:56:30 +00001265 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001266
XiaokangQian78b1fa72022-01-19 06:56:30 +00001267 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001268 * We are going to re-generate a shared secret corresponding to the group
1269 * selected by the server, which is different from the group for which we
1270 * generated a shared secret in the first client hello.
1271 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001272 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001273 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001274 if( ret != 0 )
1275 return( ret );
1276
1277 return( 0 );
1278}
1279
Jerry Yue1b9c292021-09-10 10:08:31 +08001280/*
Jerry Yu4a173382021-10-11 21:45:31 +08001281 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001282 * Handler for MBEDTLS_SSL_SERVER_HELLO
1283 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001284static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001285{
Jerry Yu4a173382021-10-11 21:45:31 +08001286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001287 unsigned char *buf = NULL;
1288 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001289 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001290
1291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1292
Ronald Crondb5dfa12022-05-31 11:44:38 +02001293 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1294 MBEDTLS_SSL_HS_SERVER_HELLO,
1295 &buf, &buf_len ) );
1296
Ronald Cron828aff62022-05-31 12:04:31 +02001297 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001298 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001299 goto cleanup;
1300 else
Ronald Cron828aff62022-05-31 12:04:31 +02001301 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001302
Ronald Cron828aff62022-05-31 12:04:31 +02001303 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001304 {
1305 ret = 0;
1306 goto cleanup;
1307 }
1308
XiaokangQianb851da82022-01-14 04:03:11 +00001309 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001310 buf + buf_len,
1311 is_hrr ) );
1312 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001313 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1314
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001315 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1316 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001317
XiaokangQiand9e068e2022-01-18 06:23:32 +00001318 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001319 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001320 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001321#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1322 /* If not offering early data, the client sends a dummy CCS record
1323 * immediately before its second flight. This may either be before
1324 * its second ClientHello or before its encrypted handshake flight.
1325 */
1326 mbedtls_ssl_handshake_set_state( ssl,
1327 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1328#else
1329 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1330#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1331 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001332 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001333 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001334 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001335 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1336 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001337
1338cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1340 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001341 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001342}
1343
1344/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001345 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001346 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001347 *
1348 * The EncryptedExtensions message contains any extensions which
1349 * should be protected, i.e., any which are not needed to establish
1350 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001351 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001352
XiaokangQian08da26c2021-10-09 10:12:11 +00001353/* Parse EncryptedExtensions message
1354 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001355 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001356 * } EncryptedExtensions;
1357 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001358static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1359 const unsigned char *buf,
1360 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001361{
1362 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001363 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001364 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001365 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001366
XiaokangQian08da26c2021-10-09 10:12:11 +00001367 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001368 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001369 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001370
XiaokangQian97799ac2021-10-11 10:05:54 +00001371 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001372 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001373 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001374
XiaokangQian8db25ff2021-10-13 05:56:18 +00001375 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001376 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001377 unsigned int extension_type;
1378 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001379
XiaokangQian08da26c2021-10-09 10:12:11 +00001380 /*
1381 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001382 * ExtensionType extension_type; (2 bytes)
1383 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001384 * } Extension;
1385 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001386 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001387 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1388 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1389 p += 4;
1390
XiaokangQian8db25ff2021-10-13 05:56:18 +00001391 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001392
XiaokangQian97799ac2021-10-11 10:05:54 +00001393 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001394 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001395 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001396 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001397 switch( extension_type )
1398 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001399
XiaokangQian08da26c2021-10-09 10:12:11 +00001400 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1402 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001403
lhuang0486cacac2022-01-21 07:34:27 -08001404#if defined(MBEDTLS_SSL_ALPN)
1405 case MBEDTLS_TLS_EXT_ALPN:
1406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1407
1408 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1409 {
1410 return( ret );
1411 }
1412
1413 break;
1414#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001415 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001416 MBEDTLS_SSL_DEBUG_MSG(
1417 3, ( "unsupported extension found: %u ", extension_type) );
1418 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001419 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001420 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1421 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001422 }
1423
XiaokangQian08da26c2021-10-09 10:12:11 +00001424 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001425 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001426
XiaokangQian97799ac2021-10-11 10:05:54 +00001427 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001428 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001429 {
1430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001431 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001432 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001433 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001434 }
1435
1436 return( ret );
1437}
1438
Ronald Cron9d6a5452022-05-30 16:05:38 +02001439static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1440{
1441 int ret;
1442 unsigned char *buf;
1443 size_t buf_len;
1444
1445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1446
1447 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1448 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1449 &buf, &buf_len ) );
1450
1451 /* Process the message contents */
1452 MBEDTLS_SSL_PROC_CHK(
1453 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1454
1455 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1456 buf, buf_len );
1457
Ronald Cronfb508b82022-05-31 14:49:55 +02001458#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1459 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1460 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1461 else
1462 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1463#else
1464 ((void) ssl);
1465 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1466#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001467
1468cleanup:
1469
1470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1471 return( ret );
1472
1473}
1474
Jerry Yua93ac112021-10-27 16:31:48 +08001475#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001476/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001477 * STATE HANDLING: CertificateRequest
1478 *
Jerry Yud2674312021-10-29 10:08:19 +08001479 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001480#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1481#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001482/* Coordination:
1483 * Deals with the ambiguity of not knowing if a CertificateRequest
1484 * will be sent. Returns a negative code on failure, or
1485 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1486 * - SSL_CERTIFICATE_REQUEST_SKIP
1487 * indicating if a Certificate Request is expected or not.
1488 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001489static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001490{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001492
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001493 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001494 {
1495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1496 return( SSL_CERTIFICATE_REQUEST_SKIP );
1497 }
1498
1499 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001500 {
1501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1502 return( ret );
1503 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001504 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001505
1506 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1507 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1508 {
Ronald Cron19385882022-06-15 16:26:13 +02001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001510 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001511 }
1512
Ronald Cron19385882022-06-15 16:26:13 +02001513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1514
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001515 return( SSL_CERTIFICATE_REQUEST_SKIP );
1516}
1517
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001518/*
1519 * ssl_tls13_parse_certificate_request()
1520 * Parse certificate request
1521 * struct {
1522 * opaque certificate_request_context<0..2^8-1>;
1523 * Extension extensions<2..2^16-1>;
1524 * } CertificateRequest;
1525 */
1526static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1527 const unsigned char *buf,
1528 const unsigned char *end )
1529{
1530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1531 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001532 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001533 size_t extensions_len = 0;
1534 const unsigned char *extensions_end;
1535 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001536
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001537 /* ...
1538 * opaque certificate_request_context<0..2^8-1>
1539 * ...
1540 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1542 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001543 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001544
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001545 if( certificate_request_context_len > 0 )
1546 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001547 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001548 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1549 p, certificate_request_context_len );
1550
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001551 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001552 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001553 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001554 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001555 {
1556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1557 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1558 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001559 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001560 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001561 p += certificate_request_context_len;
1562 }
1563
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001564 /* ...
1565 * Extension extensions<2..2^16-1>;
1566 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567 */
1568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1569 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1570 p += 2;
1571
1572 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1573 extensions_end = p + extensions_len;
1574
1575 while( p < extensions_end )
1576 {
1577 unsigned int extension_type;
1578 size_t extension_data_len;
1579
1580 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1581 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1582 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1583 p += 4;
1584
1585 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1586
1587 switch( extension_type )
1588 {
1589 case MBEDTLS_TLS_EXT_SIG_ALG:
1590 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001591 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001592 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001593 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001594 if( ret != 0 )
1595 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001596 if( ! sig_alg_ext_found )
1597 sig_alg_ext_found = 1;
1598 else
1599 {
1600 MBEDTLS_SSL_DEBUG_MSG( 3,
1601 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001602 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001603 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001604 break;
1605
1606 default:
1607 MBEDTLS_SSL_DEBUG_MSG(
1608 3,
1609 ( "unknown extension found: %u ( ignoring )",
1610 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001611 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001612 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001613 p += extension_data_len;
1614 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001615 /* Check that we consumed all the message. */
1616 if( p != end )
1617 {
1618 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001619 ( "CertificateRequest misaligned" ) );
1620 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001621 }
1622 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001623 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001624 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001625 MBEDTLS_SSL_DEBUG_MSG( 3,
1626 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001627 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001628 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001629
Jerry Yu7840f812022-01-29 10:26:51 +08001630 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001631 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001632
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001633decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001634 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1635 MBEDTLS_ERR_SSL_DECODE_ERROR );
1636 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001637}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001638
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001639/*
1640 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1641 */
1642static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001643{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001645
1646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1647
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001648 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1649
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001650 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1651 {
1652 unsigned char *buf;
1653 size_t buf_len;
1654
1655 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1656 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1657 &buf, &buf_len ) );
1658
1659 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1660 buf, buf + buf_len ) );
1661
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001662 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1663 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001664 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001665 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001666 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00001667 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001668 }
1669 else
1670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001672 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1673 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 }
1675
Jerry Yud2674312021-10-29 10:08:19 +08001676 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1677
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678cleanup:
1679
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1681 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001682}
1683
1684/*
Jerry Yu687101b2021-09-14 16:03:56 +08001685 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1686 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001687static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001688{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001689 int ret;
1690
1691 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001692 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001693 return( ret );
1694
Jerry Yu687101b2021-09-14 16:03:56 +08001695 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1696 return( 0 );
1697}
1698
1699/*
1700 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1701 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001702static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001703{
Jerry Yu30b071c2021-09-12 20:16:03 +08001704 int ret;
1705
1706 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1707 if( ret != 0 )
1708 return( ret );
1709
Jerry Yu687101b2021-09-14 16:03:56 +08001710 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1711 return( 0 );
1712}
Jerry Yua93ac112021-10-27 16:31:48 +08001713#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001714
Jerry Yu687101b2021-09-14 16:03:56 +08001715/*
1716 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1717 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001718static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001719{
XiaokangQianac0385c2021-11-03 06:40:11 +00001720 int ret;
1721
XiaokangQianc5c39d52021-11-09 11:55:10 +00001722 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001723 if( ret != 0 )
1724 return( ret );
1725
Jerry Yue3d67cb2022-05-19 15:33:10 +08001726 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1727 if( ret != 0 )
1728 {
1729 MBEDTLS_SSL_PEND_FATAL_ALERT(
1730 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1731 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1732 return( ret );
1733 }
1734
Ronald Cron49ad6192021-11-24 16:25:31 +01001735#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1736 mbedtls_ssl_handshake_set_state(
1737 ssl,
1738 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1739#else
Jerry Yuca133a32022-02-15 14:22:05 +08001740 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001741#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001742
XiaokangQianac0385c2021-11-03 06:40:11 +00001743 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001744}
1745
1746/*
Jerry Yu566c7812022-01-26 15:41:22 +08001747 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1748 */
1749static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1750{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001751 int non_empty_certificate_msg = 0;
1752
Jerry Yu5cc35062022-01-28 16:16:08 +08001753 MBEDTLS_SSL_DEBUG_MSG( 1,
1754 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001755 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001756
Ronald Cron9df7c802022-03-08 18:38:54 +01001757#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001758 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001759 {
1760 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1761 if( ret != 0 )
1762 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001763
Ronald Cron7a94aca2022-03-09 07:44:27 +01001764 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1765 non_empty_certificate_msg = 1;
1766 }
1767 else
1768 {
XiaokangQian23c5be62022-06-07 02:04:34 +00001769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001770 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001771#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001772
Ronald Cron7a94aca2022-03-09 07:44:27 +01001773 if( non_empty_certificate_msg )
1774 {
1775 mbedtls_ssl_handshake_set_state( ssl,
1776 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1777 }
1778 else
Ronald Cron19385882022-06-15 16:26:13 +02001779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001781 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001782 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01001783
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001784 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001785}
1786
Ronald Cron9df7c802022-03-08 18:38:54 +01001787#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001788/*
1789 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1790 */
1791static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1792{
Ronald Crona8b38872022-03-09 07:59:25 +01001793 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1794
1795 if( ret == 0 )
1796 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1797
1798 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001799}
Jerry Yu90f152d2022-01-29 22:12:42 +08001800#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001801
1802/*
Jerry Yu687101b2021-09-14 16:03:56 +08001803 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1804 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001805static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001806{
XiaokangQian0fa66432021-11-15 03:33:57 +00001807 int ret;
1808
1809 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1810 if( ret != 0 )
1811 return( ret );
1812
Jerry Yue3d67cb2022-05-19 15:33:10 +08001813 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1814 if( ret != 0 )
1815 {
1816 MBEDTLS_SSL_DEBUG_RET( 1,
1817 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1818 return ( ret );
1819 }
1820
XiaokangQian0fa66432021-11-15 03:33:57 +00001821 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1822 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001823}
1824
1825/*
1826 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1827 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001828static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001829{
Jerry Yu378254d2021-10-30 21:44:47 +08001830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001831 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001832 return( 0 );
1833}
1834
1835/*
1836 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1837 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001838static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001839{
Jerry Yu378254d2021-10-30 21:44:47 +08001840
1841 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1842
1843 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1844 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001845}
1846
Jerry Yu92c6b402021-08-27 16:59:09 +08001847int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001848{
Jerry Yu92c6b402021-08-27 16:59:09 +08001849 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001850
Jerry Yu92c6b402021-08-27 16:59:09 +08001851 switch( ssl->state )
1852 {
1853 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001854 * ssl->state is initialized as HELLO_REQUEST. It is the same
1855 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001856 */
1857 case MBEDTLS_SSL_HELLO_REQUEST:
1858 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001859 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001860 break;
1861
1862 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001863 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001864 break;
1865
1866 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001867 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001868 break;
1869
Jerry Yua93ac112021-10-27 16:31:48 +08001870#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001871 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1872 ret = ssl_tls13_process_certificate_request( ssl );
1873 break;
1874
Jerry Yu687101b2021-09-14 16:03:56 +08001875 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001876 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001877 break;
1878
1879 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001880 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001881 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001882#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001883
1884 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001885 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001886 break;
1887
Jerry Yu566c7812022-01-26 15:41:22 +08001888 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1889 ret = ssl_tls13_write_client_certificate( ssl );
1890 break;
1891
Ronald Cron9df7c802022-03-08 18:38:54 +01001892#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001893 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1894 ret = ssl_tls13_write_client_certificate_verify( ssl );
1895 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001896#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001897
Jerry Yu687101b2021-09-14 16:03:56 +08001898 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001899 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001900 break;
1901
1902 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001903 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001904 break;
1905
1906 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001907 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001908 break;
1909
Ronald Cron49ad6192021-11-24 16:25:31 +01001910 /*
1911 * Injection of dummy-CCS's for middlebox compatibility
1912 */
1913#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001914 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001915 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1916 if( ret == 0 )
1917 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1918 break;
1919
1920 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1921 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1922 if( ret == 0 )
1923 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001924 break;
1925#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1926
Jerry Yu92c6b402021-08-27 16:59:09 +08001927 default:
1928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1930 }
1931
1932 return( ret );
1933}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001934
Jerry Yufb4b6472022-01-27 15:03:26 +08001935#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001936
Jerry Yufb4b6472022-01-27 15:03:26 +08001937