blob: bafd15928e93c3249403e14ef436857ac9d1bfac [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000048 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040051 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
52 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080053
Xiaofei Baid25fab62021-12-02 06:36:27 +000054 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Jerry Yu388bd0d2021-09-15 18:41:02 +080058 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080059 * - extension_type (2 bytes)
60 * - extension_data_length (2 bytes)
61 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020062 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Ronald Crona77fc272022-03-30 17:20:47 +020064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010065
Jerry Yueecfbf02021-08-30 18:32:07 +080066 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010067 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080068 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080069
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080070 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010071 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu0c63af62021-09-02 12:59:12 +080073 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080074 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010075 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080076 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040077 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
78 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Glenn Strausscd78df62022-04-07 19:07:11 -040082 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010083 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040084 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
87 }
88
89 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080090
91 return( 0 );
92}
Jerry Yubc20bdd2021-08-24 15:59:48 +080093
Jerry Yuc068b662021-10-11 22:30:19 +080094static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
95 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080096 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080097{
Jerry Yue1b9c292021-09-10 10:08:31 +080098 ((void) ssl);
99
Ronald Cron98473382022-03-30 20:04:10 +0200100 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400101 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
102 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 {
104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800105
106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
108 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800109 }
110
Ronald Cron98473382022-03-30 20:04:10 +0200111 if( &buf[2] != end )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
115 MBEDTLS_ERR_SSL_DECODE_ERROR );
116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
117 }
118
Jerry Yue1b9c292021-09-10 10:08:31 +0800119 return( 0 );
120}
121
lhuang0486cacac2022-01-21 07:34:27 -0800122#if defined(MBEDTLS_SSL_ALPN)
lhuang0486cacac2022-01-21 07:34:27 -0800123static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
124 const unsigned char *buf, size_t len )
125{
126 size_t list_len, name_len;
127 const unsigned char *p = buf;
128 const unsigned char *end = buf + len;
129
130 /* If we didn't send it, the server shouldn't send it */
131 if( ssl->conf->alpn_list == NULL )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
134 /*
135 * opaque ProtocolName<1..2^8-1>;
136 *
137 * struct {
138 * ProtocolName protocol_name_list<2..2^16-1>
139 * } ProtocolNameList;
140 *
141 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
142 */
143
144 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
145 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
146
147 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
148 p += 2;
149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
150
151 name_len = *p++;
152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
153
154 /* Check that the server chosen protocol was in our list and save it */
155 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
156 {
157 if( name_len == strlen( *alpn ) &&
158 memcmp( buf + 3, *alpn, name_len ) == 0 )
159 {
160 ssl->alpn_chosen = *alpn;
161 return( 0 );
162 }
163 }
164
165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
166}
167#endif /* MBEDTLS_SSL_ALPN */
168
XiaokangQian16acd4b2022-01-14 07:35:47 +0000169static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000170{
171 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100172
XiaokangQian647719a2021-12-07 09:16:29 +0000173 if( group_id == 0 )
174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
175
XiaokangQian355e09a2022-01-20 11:14:50 +0000176#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000177 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000178 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
181
182 /* Destroy generated private key. */
183 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
184 if( status != PSA_SUCCESS )
185 {
186 ret = psa_ssl_status_to_mbedtls( status );
187 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
188 return( ret );
189 }
190
191 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000192 return( 0 );
193 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000194 else
195#endif /* MBEDTLS_ECDH_C */
196 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000197 {
198 /* Do something */
199 }
200
201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
202}
203
204/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800205 * Functions for writing key_share extension.
206 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800207static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
208 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800209{
210 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
211
Jerry Yu56fc07f2021-09-01 17:48:49 +0800212
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100214 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800215 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100216 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800217 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
218
Brett Warren14efd332021-10-06 09:32:11 +0100219 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000221 const mbedtls_ecp_curve_info *curve_info;
222 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
223 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100224 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800225 {
Brett Warren14efd332021-10-06 09:32:11 +0100226 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 return( 0 );
228 }
229 }
230#else
231 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800232 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233#endif /* MBEDTLS_ECDH_C */
234
235 /*
236 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800237 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 */
239
240 return( ret );
241}
242
243/*
244 * ssl_tls13_write_key_share_ext
245 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800246 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800247 *
248 * struct {
249 * NamedGroup group;
250 * opaque key_exchange<1..2^16-1>;
251 * } KeyShareEntry;
252 * struct {
253 * KeyShareEntry client_shares<0..2^16-1>;
254 * } KeyShareClientHello;
255 */
256static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
257 unsigned char *buf,
258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260{
261 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000262 unsigned char *client_shares; /* Start of client_shares */
263 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
266
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268
Jerry Yub60e3cf2021-09-08 16:41:02 +0800269 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 * - extension_type (2 bytes)
271 * - extension_data_length (2 bytes)
272 * - client_shares_length (2 bytes)
273 */
274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
275 p += 6;
276
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
278
279 /* HRR could already have requested something else. */
280 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800281 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
282 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800283 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800285 &group_id ) );
286 }
287
288 /*
289 * Dispatch to type-specific key generation function.
290 *
291 * So far, we're only supporting ECDHE. With the introduction
292 * of PQC KEMs, we'll want to have multiple branches, one per
293 * type of KEM, and dispatch to the corresponding crypto. And
294 * only one key share entry is allowed.
295 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800300 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000301 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100303 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304
305 /* Check there is space for header of KeyShareEntry
306 * - group (2 bytes)
307 * - key_exchange_length (2 bytes)
308 */
309 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
310 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800311 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
312 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800313 p += key_exchange_len;
314 if( ret != 0 )
315 return( ret );
316
317 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000318 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000320 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 }
322 else
323#endif /* MBEDTLS_ECDH_C */
324 if( 0 /* other KEMs? */ )
325 {
326 /* Do something */
327 }
328 else
329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
330
Jerry Yub60e3cf2021-09-08 16:41:02 +0800331 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000332 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800333 if( client_shares_len == 0)
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800337 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write extension_type */
339 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
340 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800343 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344
345 /* Update offered_group_id field */
346 ssl->handshake->offered_group_id = group_id;
347
348 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000349 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
354
355cleanup:
356
357 return( ret );
358}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359
Jerry Yue1b9c292021-09-10 10:08:31 +0800360
XiaokangQiand59be772022-01-24 10:12:51 +0000361/*
362 * ssl_tls13_parse_hrr_key_share_ext()
363 * Parse key_share extension in Hello Retry Request
364 *
365 * struct {
366 * NamedGroup selected_group;
367 * } KeyShareHelloRetryRequest;
368 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000369static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000370 const unsigned char *buf,
371 const unsigned char *end )
372{
XiaokangQianb851da82022-01-14 04:03:11 +0000373 const mbedtls_ecp_curve_info *curve_info = NULL;
374 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000375 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000376 int found = 0;
377
378 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
379 if( group_list == NULL )
380 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
381
382 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
383
384 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000385 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000386 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000388
389 /* Upon receipt of this extension in a HelloRetryRequest, the client
390 * MUST first verify that the selected_group field corresponds to a
391 * group which was provided in the "supported_groups" extension in the
392 * original ClientHello.
393 * The supported_group was based on the info in ssl->conf->group_list.
394 *
395 * If the server provided a key share that was not sent in the ClientHello
396 * then the client MUST abort the handshake with an "illegal_parameter" alert.
397 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000398 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000399 {
400 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000401 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000402 continue;
403
404 /* We found a match */
405 found = 1;
406 break;
407 }
408
409 /* Client MUST verify that the selected_group field does not
410 * correspond to a group which was provided in the "key_share"
411 * extension in the original ClientHello. If the server sent an
412 * HRR message with a key share already provided in the
413 * ClientHello then the client MUST abort the handshake with
414 * an "illegal_parameter" alert.
415 */
XiaokangQiand59be772022-01-24 10:12:51 +0000416 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
419 MBEDTLS_SSL_PEND_FATAL_ALERT(
420 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
421 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
422 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
423 }
424
425 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000426 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000427
428 return( 0 );
429}
430
Jerry Yue1b9c292021-09-10 10:08:31 +0800431/*
Jerry Yub85277e2021-10-13 13:36:05 +0800432 * ssl_tls13_parse_key_share_ext()
433 * Parse key_share extension in Server Hello
434 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800435 * struct {
436 * KeyShareEntry server_share;
437 * } KeyShareServerHello;
438 * struct {
439 * NamedGroup group;
440 * opaque key_exchange<1..2^16-1>;
441 * } KeyShareEntry;
442 */
Jerry Yu4a173382021-10-11 21:45:31 +0800443static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 const unsigned char *buf,
445 const unsigned char *end )
446{
Jerry Yub85277e2021-10-13 13:36:05 +0800447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800448 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800449 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800450
Jerry Yu4a173382021-10-11 21:45:31 +0800451 /* ...
452 * NamedGroup group; (2 bytes)
453 * ...
454 */
455 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
456 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800457 p += 2;
458
Jerry Yu4a173382021-10-11 21:45:31 +0800459 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800460 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800461 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1,
464 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800465 (unsigned) offered_group, (unsigned) group ) );
466 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
467 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
468 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 }
470
471#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800472 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800473 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100474 const mbedtls_ecp_curve_info *curve_info =
475 mbedtls_ecp_curve_info_from_tls_id( group );
476 if( curve_info == NULL )
477 {
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
480 }
481
482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
483
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000484 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800485 if( ret != 0 )
486 return( ret );
487 }
Jerry Yub85277e2021-10-13 13:36:05 +0800488 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800489#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800490 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 {
492 /* Do something */
493 }
494 else
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
496
497 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
498 return( ret );
499}
500
XiaokangQiand59be772022-01-24 10:12:51 +0000501/*
502 * ssl_tls13_parse_cookie_ext()
503 * Parse cookie extension in Hello Retry Request
504 *
505 * struct {
506 * opaque cookie<1..2^16-1>;
507 * } Cookie;
508 *
509 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
510 * extension to the client (this is an exception to the usual rule that
511 * the only extensions that may be sent are those that appear in the
512 * ClientHello). When sending the new ClientHello, the client MUST copy
513 * the contents of the extension received in the HelloRetryRequest into
514 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
515 * cookies in their initial ClientHello in subsequent connections.
516 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000517static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
518 const unsigned char *buf,
519 const unsigned char *end )
520{
XiaokangQian25c9c902022-02-08 10:49:53 +0000521 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000522 const unsigned char *p = buf;
523 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
524
525 /* Retrieve length field of cookie */
526 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
527 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
528 p += 2;
529
530 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
531 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
532
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000533 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000534 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000535 handshake->cookie = mbedtls_calloc( 1, cookie_len );
536 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000537 {
538 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000539 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000540 cookie_len ) );
541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
542 }
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000546
547 return( 0 );
548}
XiaokangQian43550bd2022-01-21 04:32:58 +0000549
XiaokangQian0b64eed2022-01-27 10:36:51 +0000550static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000551 unsigned char *buf,
552 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000553 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000554{
555 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000556 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000558
XiaokangQianc02768a2022-02-10 07:31:25 +0000559 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000560 {
561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
562 return( 0 );
563 }
564
565 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000566 handshake->cookie,
567 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000568
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
572
XiaokangQian0b64eed2022-01-27 10:36:51 +0000573 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000574 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
575 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000576 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000577
578 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 return( 0 );
584}
585
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
587 unsigned char *buf,
588 unsigned char *end,
589 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 unsigned char *p = buf;
593 size_t ext_len;
594
595 *out_len = 0;
596
597 /* Write supported_versions extension
598 *
599 * Supported Versions Extension is mandatory with TLS 1.3.
600 */
601 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
602 if( ret != 0 )
603 return( ret );
604 p += ext_len;
605
606 /* Echo the cookie if the server provided one in its preceding
607 * HelloRetryRequest message.
608 */
609 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
610 if( ret != 0 )
611 return( ret );
612 p += ext_len;
613
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100614 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
615 {
616 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
617 if( ret != 0 )
618 return( ret );
619 p += ext_len;
620 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100621
622 *out_len = p - buf;
623
624 return( 0 );
625}
626
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800627/*
Jerry Yu4a173382021-10-11 21:45:31 +0800628 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800629 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200630
Ronald Cronda41b382022-03-30 09:57:11 +0200631/**
632 * \brief Detect if the ServerHello contains a supported_versions extension
633 * or not.
634 *
635 * \param[in] ssl SSL context
636 * \param[in] buf Buffer containing the ServerHello message
637 * \param[in] end End of the buffer containing the ServerHello message
638 *
639 * \return 0 if the ServerHello does not contain a supported_versions extension
640 * \return 1 if the ServerHello contains a supported_versions extension
641 * \return A negative value if an error occurred while parsing the ServerHello.
642 */
Ronald Cron9f0fba32022-02-10 16:45:15 +0100643static int ssl_tls13_is_supported_versions_ext_present(
644 mbedtls_ssl_context *ssl,
645 const unsigned char *buf,
646 const unsigned char *end )
647{
648 const unsigned char *p = buf;
649 size_t legacy_session_id_echo_len;
650 size_t extensions_len;
651 const unsigned char *extensions_end;
652
653 /*
654 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200655 * length:
656 * - legacy_version 2 bytes
657 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
658 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100659 */
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
661 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
662 legacy_session_id_echo_len = *p;
663
664 /*
665 * Jump to the extensions, jumping over:
666 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
667 * - cipher_suite 2 bytes
668 * - legacy_compression_method 1 byte
669 */
Ronald Cron28271062022-06-10 14:43:55 +0200670 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100671 p += legacy_session_id_echo_len + 4;
672
673 /* Case of no extension */
674 if( p == end )
675 return( 0 );
676
677 /* ...
678 * Extension extensions<6..2^16-1>;
679 * ...
680 * struct {
681 * ExtensionType extension_type; (2 bytes)
682 * opaque extension_data<0..2^16-1>;
683 * } Extension;
684 */
685 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
686 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
687 p += 2;
688
689 /* Check extensions do not go beyond the buffer of data. */
690 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
691 extensions_end = p + extensions_len;
692
693 while( p < extensions_end )
694 {
695 unsigned int extension_type;
696 size_t extension_data_len;
697
698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
699 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
700 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
701 p += 4;
702
703 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
704 return( 1 );
705
706 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
707 p += extension_data_len;
708 }
709
710 return( 0 );
711}
712
Jerry Yu7a186a02021-10-15 18:46:14 +0800713/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200714 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
715 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
716 * - 0 otherwise
717 */
718static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
719 const unsigned char *buf,
720 const unsigned char *end )
721{
722 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
723 static const unsigned char magic_downgrade_string[] =
724 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
725 const unsigned char *last_eight_bytes_of_random;
726 unsigned char last_byte_of_random;
727
728 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
729 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
730
731 if( memcmp( last_eight_bytes_of_random,
732 magic_downgrade_string,
733 sizeof( magic_downgrade_string ) ) == 0 )
734 {
735 last_byte_of_random = last_eight_bytes_of_random[7];
736 return( last_byte_of_random == 0 ||
737 last_byte_of_random == 1 );
738 }
739
740 return( 0 );
741}
742
743/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200744 * - SSL_SERVER_HELLO or
745 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000746 * to indicate which message is expected and to be parsed next.
747 */
Ronald Cron828aff62022-05-31 12:04:31 +0200748#define SSL_SERVER_HELLO 0
749#define SSL_SERVER_HELLO_HRR 1
Jerry Yub85277e2021-10-13 13:36:05 +0800750static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
751 const unsigned char *buf,
752 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800753{
Jerry Yue1b9c292021-09-10 10:08:31 +0800754
755 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
756 *
Jerry Yu4a173382021-10-11 21:45:31 +0800757 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800758 * special value of the SHA-256 of "HelloRetryRequest".
759 *
760 * struct {
761 * ProtocolVersion legacy_version = 0x0303;
762 * Random random;
763 * opaque legacy_session_id_echo<0..32>;
764 * CipherSuite cipher_suite;
765 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800766 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800767 * } ServerHello;
768 *
769 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800770 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
771 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800772
Jerry Yu93a13f22022-04-11 23:00:01 +0800773 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
774 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800775 {
Ronald Cron828aff62022-05-31 12:04:31 +0200776 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800777 }
778
Ronald Cron828aff62022-05-31 12:04:31 +0200779 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800780}
781
Ronald Cron828aff62022-05-31 12:04:31 +0200782/*
Jerry Yu745bb612021-10-13 22:01:04 +0800783 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200784 * - SSL_SERVER_HELLO or
785 * - SSL_SERVER_HELLO_HRR or
786 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800787 */
Ronald Cron828aff62022-05-31 12:04:31 +0200788#define SSL_SERVER_HELLO_TLS1_2 2
789static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200790 const unsigned char *buf,
791 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800792{
Jerry Yu4a173382021-10-11 21:45:31 +0800793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800794
Ronald Cron9f0fba32022-02-10 16:45:15 +0100795 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200796 ssl, buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100797 if( ret == 0 )
798 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200799 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200800 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200801
802 /* If the server is negotiating TLS 1.2 or below and:
803 * . we did not propose TLS 1.2 or
804 * . the server responded it is TLS 1.3 capable but negotiating a lower
805 * version of the protocol and thus we are under downgrade attack
806 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100807 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200808 if( ssl->handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100809 {
810 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
811 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
812 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
813 }
814
815 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400816 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100817 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200818 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100819
820 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
821 {
822 ret = ssl_tls13_reset_key_share( ssl );
823 if( ret != 0 )
824 return( ret );
825 }
826
Ronald Cron828aff62022-05-31 12:04:31 +0200827 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100828 }
829
Ronald Crondb5dfa12022-05-31 11:44:38 +0200830 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800831 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 {
Ronald Cron828aff62022-05-31 12:04:31 +0200833 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +0800834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
835 break;
Ronald Cron828aff62022-05-31 12:04:31 +0200836 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +0800837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000838 /* If a client receives a second
839 * HelloRetryRequest in the same connection (i.e., where the ClientHello
840 * was itself in response to a HelloRetryRequest), it MUST abort the
841 * handshake with an "unexpected_message" alert.
842 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000843 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000844 {
845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
846 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
847 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
848 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
849 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000850 /*
851 * Clients must abort the handshake with an "illegal_parameter"
852 * alert if the HelloRetryRequest would not result in any change
853 * in the ClientHello.
854 * In a PSK only key exchange that what we expect.
855 */
856 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
857 {
858 MBEDTLS_SSL_DEBUG_MSG( 1,
859 ( "Unexpected HRR in pure PSK key exchange." ) );
860 MBEDTLS_SSL_PEND_FATAL_ALERT(
861 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
862 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
863 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
864 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000865
XiaokangQiand9e068e2022-01-18 06:23:32 +0000866 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000867
Jerry Yu745bb612021-10-13 22:01:04 +0800868 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800869 }
870
871cleanup:
872
873 return( ret );
874}
875
Jerry Yu4a173382021-10-11 21:45:31 +0800876static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
877 const unsigned char **buf,
878 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800879{
880 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800881 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800882
Jerry Yude4fb2c2021-09-19 18:05:08 +0800883 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800884 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800885
Jerry Yu4a173382021-10-11 21:45:31 +0800886 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800887
888 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800889 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
890 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800891 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800892 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
893 ssl->session_negotiate->id,
894 ssl->session_negotiate->id_len );
895 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800896 legacy_session_id_echo_len );
897
898 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
899 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
900
Jerry Yue1b9c292021-09-10 10:08:31 +0800901 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
902 }
903
Jerry Yu4a173382021-10-11 21:45:31 +0800904 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800905 *buf = p;
906
907 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800908 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800909 return( 0 );
910}
911
Jerry Yue1b9c292021-09-10 10:08:31 +0800912/* Parse ServerHello message and configure context
913 *
914 * struct {
915 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
916 * Random random;
917 * opaque legacy_session_id_echo<0..32>;
918 * CipherSuite cipher_suite;
919 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800920 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 * } ServerHello;
922 */
Jerry Yuc068b662021-10-11 22:30:19 +0800923static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
924 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000925 const unsigned char *end,
926 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800927{
Jerry Yub85277e2021-10-13 13:36:05 +0800928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000930 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800931 size_t extensions_len;
932 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800933 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800934 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000935 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800936
937 /*
938 * Check there is space for minimal fields
939 *
940 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800941 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800942 * - legacy_session_id_echo ( 1 byte ), minimum size
943 * - cipher_suite ( 2 bytes)
944 * - legacy_compression_method ( 1 byte )
945 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800946 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800947
948 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800949 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
950
Jerry Yu4a173382021-10-11 21:45:31 +0800951 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800952 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800953 * ...
954 * with ProtocolVersion defined as:
955 * uint16 ProtocolVersion;
956 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400957 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
958 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 {
960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
961 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
962 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000963 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
964 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800965 }
966 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800967
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800968 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800969 * Random random;
970 * ...
971 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800972 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800973 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000974 if( !is_hrr )
975 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000976 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000977 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
978 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
979 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
980 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800981 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800982
Jerry Yu4a173382021-10-11 21:45:31 +0800983 /* ...
984 * opaque legacy_session_id_echo<0..32>;
985 * ...
986 */
987 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 {
XiaokangQian52da5582022-01-26 09:49:29 +0000989 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +0000990 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800991 }
992
Jerry Yu4a173382021-10-11 21:45:31 +0800993 /* ...
994 * CipherSuite cipher_suite;
995 * ...
996 * with CipherSuite defined as:
997 * uint8 CipherSuite[2];
998 */
999 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001000 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1001 p += 2;
1002
Jerry Yu4a173382021-10-11 21:45:31 +08001003
XiaokangQian355e09a2022-01-20 11:14:50 +00001004 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001005 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001006 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001007 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001008 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1009 ssl->tls_version,
1010 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001011 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001012 {
XiaokangQian52da5582022-01-26 09:49:29 +00001013 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001014 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001015 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001016 * If we received an HRR before and that the proposed selected
1017 * ciphersuite in this server hello is not the same as the one
1018 * proposed in the HRR, we abort the handshake and send an
1019 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001020 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001021 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1022 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001023 {
XiaokangQian52da5582022-01-26 09:49:29 +00001024 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001025 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001026
XiaokangQian52da5582022-01-26 09:49:29 +00001027 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001028 {
1029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1030 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001031 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001032 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001033
Jerry Yu4a173382021-10-11 21:45:31 +08001034 /* Configure ciphersuites */
1035 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1036
XiaokangQian355e09a2022-01-20 11:14:50 +00001037 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 ssl->session_negotiate->ciphersuite = cipher_suite;
1039
Jerry Yue1b9c292021-09-10 10:08:31 +08001040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1041 cipher_suite, ciphersuite_info->name ) );
1042
1043#if defined(MBEDTLS_HAVE_TIME)
1044 ssl->session_negotiate->start = time( NULL );
1045#endif /* MBEDTLS_HAVE_TIME */
1046
Jerry Yu4a173382021-10-11 21:45:31 +08001047 /* ...
1048 * uint8 legacy_compression_method = 0;
1049 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001050 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001051 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001052 if( p[0] != 0 )
1053 {
Jerry Yub85277e2021-10-13 13:36:05 +08001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001055 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001056 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001057 }
1058 p++;
1059
Jerry Yub85277e2021-10-13 13:36:05 +08001060 /* ...
1061 * Extension extensions<6..2^16-1>;
1062 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001063 * struct {
1064 * ExtensionType extension_type; (2 bytes)
1065 * opaque extension_data<0..2^16-1>;
1066 * } Extension;
1067 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001068 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001069 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001070 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001071
Jerry Yu4a173382021-10-11 21:45:31 +08001072 /* Check extensions do not go beyond the buffer of data. */
1073 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1074 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001075
Jerry Yu4a173382021-10-11 21:45:31 +08001076 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001077
Jerry Yu4a173382021-10-11 21:45:31 +08001078 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 {
1080 unsigned int extension_type;
1081 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001082 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001083
Jerry Yu4a173382021-10-11 21:45:31 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1086 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1087 p += 4;
1088
Jerry Yu4a173382021-10-11 21:45:31 +08001089 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001090 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001091
1092 switch( extension_type )
1093 {
XiaokangQianb851da82022-01-14 04:03:11 +00001094 case MBEDTLS_TLS_EXT_COOKIE:
1095
XiaokangQiand9e068e2022-01-18 06:23:32 +00001096 if( !is_hrr )
1097 {
XiaokangQian52da5582022-01-26 09:49:29 +00001098 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001099 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001100 }
1101
XiaokangQian43550bd2022-01-21 04:32:58 +00001102 ret = ssl_tls13_parse_cookie_ext( ssl,
1103 p, extension_data_end );
1104 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001105 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001106 MBEDTLS_SSL_DEBUG_RET( 1,
1107 "ssl_tls13_parse_cookie_ext",
1108 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001109 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001110 }
XiaokangQianb851da82022-01-14 04:03:11 +00001111 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001112
Jerry Yue1b9c292021-09-10 10:08:31 +08001113 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001114 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001115 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001116 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001117 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001118 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001119 break;
1120
1121 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001124
XiaokangQian52da5582022-01-26 09:49:29 +00001125 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001126 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001127
Jerry Yue1b9c292021-09-10 10:08:31 +08001128 case MBEDTLS_TLS_EXT_KEY_SHARE:
1129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001130 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1131 {
XiaokangQian52da5582022-01-26 09:49:29 +00001132 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001133 goto cleanup;
1134 }
1135
XiaokangQian53f20b72022-01-18 10:47:33 +00001136 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001137 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001138 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001139 else
XiaokangQianb851da82022-01-14 04:03:11 +00001140 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001141 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001142 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001143 {
1144 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001145 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001147 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001148 }
1149 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001150
1151 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001152 MBEDTLS_SSL_DEBUG_MSG(
1153 3,
1154 ( "unknown extension found: %u ( ignoring )",
1155 extension_type ) );
1156
XiaokangQian52da5582022-01-26 09:49:29 +00001157 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001158 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001159 }
1160
1161 p += extension_data_len;
1162 }
1163
XiaokangQiand59be772022-01-24 10:12:51 +00001164cleanup:
1165
XiaokangQian52da5582022-01-26 09:49:29 +00001166 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001167 {
1168 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1169 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1170 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1171 }
XiaokangQian52da5582022-01-26 09:49:29 +00001172 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001173 {
1174 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1175 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1176 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1177 }
1178 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001179}
1180
XiaokangQian355e09a2022-01-20 11:14:50 +00001181static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001182{
Jerry Yub85277e2021-10-13 13:36:05 +08001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001184 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001185
Jerry Yub85277e2021-10-13 13:36:05 +08001186 /* Determine the key exchange mode:
1187 * 1) If both the pre_shared_key and key_share extensions were received
1188 * then the key exchange mode is PSK with EPHEMERAL.
1189 * 2) If only the pre_shared_key extension was received then the key
1190 * exchange mode is PSK-only.
1191 * 3) If only the key_share extension was received then the key
1192 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001193 */
Jerry Yub85277e2021-10-13 13:36:05 +08001194 switch( handshake->extensions_present &
1195 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001196 {
Jerry Yu745bb612021-10-13 22:01:04 +08001197 /* Only the pre_shared_key extension was received */
1198 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001199 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001200 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001201
Jerry Yu745bb612021-10-13 22:01:04 +08001202 /* Only the key_share extension was received */
1203 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001204 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
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 /* Both the pre_shared_key and key_share extensions were received */
1208 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001209 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_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 /* Neither pre_shared_key nor key_share extension was received */
1213 default:
1214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1215 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1216 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001217 }
1218
1219 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1220 *
1221 * TODO: We don't have to do this in case we offered 0-RTT and the
1222 * server accepted it. In this case, we could skip generating
1223 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001224 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001225 if( ret != 0 )
1226 {
Jerry Yue110d252022-05-05 10:19:22 +08001227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001228 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001229 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001230 }
1231
Jerry Yuf86eb752022-05-06 11:16:55 +08001232 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001233 if( ret != 0 )
1234 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001235 MBEDTLS_SSL_DEBUG_RET( 1,
1236 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001237 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001238 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001239 }
1240
Jerry Yue110d252022-05-05 10:19:22 +08001241 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1243 ssl->session_in = ssl->session_negotiate;
1244
1245 /*
1246 * State machine update
1247 */
1248 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
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
XiaokangQian647719a2021-12-07 09:16:29 +00001265#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1266 /* If not offering early data, the client sends a dummy CCS record
1267 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001268 * its second ClientHello or before its encrypted handshake flight.
1269 */
XiaokangQian647719a2021-12-07 09:16:29 +00001270 mbedtls_ssl_handshake_set_state( ssl,
1271 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1272#else
1273 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1274#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1275
XiaokangQian78b1fa72022-01-19 06:56:30 +00001276 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001277
XiaokangQian78b1fa72022-01-19 06:56:30 +00001278 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001279 * We are going to re-generate a shared secret corresponding to the group
1280 * selected by the server, which is different from the group for which we
1281 * generated a shared secret in the first client hello.
1282 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001283 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001284 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001285 if( ret != 0 )
1286 return( ret );
1287
1288 return( 0 );
1289}
1290
Jerry Yue1b9c292021-09-10 10:08:31 +08001291/*
Jerry Yu4a173382021-10-11 21:45:31 +08001292 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001293 * Handler for MBEDTLS_SSL_SERVER_HELLO
1294 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001295static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001296{
Jerry Yu4a173382021-10-11 21:45:31 +08001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001298 unsigned char *buf = NULL;
1299 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001300 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001301
1302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1303
Ronald Crondb5dfa12022-05-31 11:44:38 +02001304 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1305 MBEDTLS_SSL_HS_SERVER_HELLO,
1306 &buf, &buf_len ) );
1307
Jerry Yue1b9c292021-09-10 10:08:31 +08001308 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1309
Ronald Cron828aff62022-05-31 12:04:31 +02001310 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001311 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001312 goto cleanup;
1313 else
Ronald Cron828aff62022-05-31 12:04:31 +02001314 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001315
Ronald Cron828aff62022-05-31 12:04:31 +02001316 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001317 {
1318 ret = 0;
1319 goto cleanup;
1320 }
1321
XiaokangQianb851da82022-01-14 04:03:11 +00001322 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001323 buf + buf_len,
1324 is_hrr ) );
1325 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001326 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1327
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001328 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1329 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001330
XiaokangQiand9e068e2022-01-18 06:23:32 +00001331 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001332 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001333 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001334 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001335
1336cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1338 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001339 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001340}
1341
1342/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001343 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001344 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001345 *
1346 * The EncryptedExtensions message contains any extensions which
1347 * should be protected, i.e., any which are not needed to establish
1348 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001349 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001350
XiaokangQian08da26c2021-10-09 10:12:11 +00001351/* Parse EncryptedExtensions message
1352 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001353 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001354 * } EncryptedExtensions;
1355 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001356static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1357 const unsigned char *buf,
1358 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001359{
1360 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001361 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001362 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001363 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001364
XiaokangQian08da26c2021-10-09 10:12:11 +00001365 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001366 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001367 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001368
XiaokangQian97799ac2021-10-11 10:05:54 +00001369 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001370 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001371 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001372
XiaokangQian8db25ff2021-10-13 05:56:18 +00001373 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001374 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001375 unsigned int extension_type;
1376 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001377
XiaokangQian08da26c2021-10-09 10:12:11 +00001378 /*
1379 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001380 * ExtensionType extension_type; (2 bytes)
1381 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001382 * } Extension;
1383 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001384 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001385 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1386 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1387 p += 4;
1388
XiaokangQian8db25ff2021-10-13 05:56:18 +00001389 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001390
XiaokangQian97799ac2021-10-11 10:05:54 +00001391 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001392 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001393 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001394 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001395 switch( extension_type )
1396 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397
XiaokangQian08da26c2021-10-09 10:12:11 +00001398 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1400 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001401
lhuang0486cacac2022-01-21 07:34:27 -08001402#if defined(MBEDTLS_SSL_ALPN)
1403 case MBEDTLS_TLS_EXT_ALPN:
1404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1405
1406 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1407 {
1408 return( ret );
1409 }
1410
1411 break;
1412#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001413 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001414 MBEDTLS_SSL_DEBUG_MSG(
1415 3, ( "unsupported extension found: %u ", extension_type) );
1416 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001417 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001418 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1419 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001420 }
1421
XiaokangQian08da26c2021-10-09 10:12:11 +00001422 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001423 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001424
XiaokangQian97799ac2021-10-11 10:05:54 +00001425 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001426 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001427 {
1428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001429 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001430 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001431 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001432 }
1433
1434 return( ret );
1435}
1436
XiaokangQian97799ac2021-10-11 10:05:54 +00001437static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001438{
Jerry Yua93ac112021-10-27 16:31:48 +08001439#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001440 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001441 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1442 else
Jerry Yud2674312021-10-29 10:08:19 +08001443 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001444#else
1445 ((void) ssl);
1446 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1447#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001448 return( 0 );
1449}
1450
Ronald Cron9d6a5452022-05-30 16:05:38 +02001451static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1452{
1453 int ret;
1454 unsigned char *buf;
1455 size_t buf_len;
1456
1457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1458
1459 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1460 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1461 &buf, &buf_len ) );
1462
1463 /* Process the message contents */
1464 MBEDTLS_SSL_PROC_CHK(
1465 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1466
1467 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1468 buf, buf_len );
1469
1470 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
1471
1472cleanup:
1473
1474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1475 return( ret );
1476
1477}
1478
Jerry Yua93ac112021-10-27 16:31:48 +08001479#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001480/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001481 * STATE HANDLING: CertificateRequest
1482 *
Jerry Yud2674312021-10-29 10:08:19 +08001483 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001484#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1485#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001486/* Coordination:
1487 * Deals with the ambiguity of not knowing if a CertificateRequest
1488 * will be sent. Returns a negative code on failure, or
1489 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1490 * - SSL_CERTIFICATE_REQUEST_SKIP
1491 * indicating if a Certificate Request is expected or not.
1492 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001493static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001494{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001496
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001497 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001498 {
1499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1500 return( SSL_CERTIFICATE_REQUEST_SKIP );
1501 }
1502
1503 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001504 {
1505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1506 return( ret );
1507 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001508 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001509
1510 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1511 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1512 {
Ronald Cron19385882022-06-15 16:26:13 +02001513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001514 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001515 }
1516
Ronald Cron19385882022-06-15 16:26:13 +02001517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1518
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001519 return( SSL_CERTIFICATE_REQUEST_SKIP );
1520}
1521
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001522/*
1523 * ssl_tls13_parse_certificate_request()
1524 * Parse certificate request
1525 * struct {
1526 * opaque certificate_request_context<0..2^8-1>;
1527 * Extension extensions<2..2^16-1>;
1528 * } CertificateRequest;
1529 */
1530static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1531 const unsigned char *buf,
1532 const unsigned char *end )
1533{
1534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1535 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001536 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001537 size_t extensions_len = 0;
1538 const unsigned char *extensions_end;
1539 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001540
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001541 /* ...
1542 * opaque certificate_request_context<0..2^8-1>
1543 * ...
1544 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1546 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001547 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001548
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001549 if( certificate_request_context_len > 0 )
1550 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001551 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001552 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1553 p, certificate_request_context_len );
1554
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001555 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001556 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001557 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001558 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559 {
1560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1561 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1562 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001563 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001565 p += certificate_request_context_len;
1566 }
1567
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001568 /* ...
1569 * Extension extensions<2..2^16-1>;
1570 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571 */
1572 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1573 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1574 p += 2;
1575
1576 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1577 extensions_end = p + extensions_len;
1578
1579 while( p < extensions_end )
1580 {
1581 unsigned int extension_type;
1582 size_t extension_data_len;
1583
1584 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1585 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1586 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1587 p += 4;
1588
1589 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1590
1591 switch( extension_type )
1592 {
1593 case MBEDTLS_TLS_EXT_SIG_ALG:
1594 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001595 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001596 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001597 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001598 if( ret != 0 )
1599 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001600 if( ! sig_alg_ext_found )
1601 sig_alg_ext_found = 1;
1602 else
1603 {
1604 MBEDTLS_SSL_DEBUG_MSG( 3,
1605 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001606 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001607 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001608 break;
1609
1610 default:
1611 MBEDTLS_SSL_DEBUG_MSG(
1612 3,
1613 ( "unknown extension found: %u ( ignoring )",
1614 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001615 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001616 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001617 p += extension_data_len;
1618 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001619 /* Check that we consumed all the message. */
1620 if( p != end )
1621 {
1622 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001623 ( "CertificateRequest misaligned" ) );
1624 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001625 }
1626 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001627 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001628 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001629 MBEDTLS_SSL_DEBUG_MSG( 3,
1630 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001631 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001632 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001633
Jerry Yu7840f812022-01-29 10:26:51 +08001634 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001635 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001636
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001637decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001638 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1639 MBEDTLS_ERR_SSL_DECODE_ERROR );
1640 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001641}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001642
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001643/*
1644 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1645 */
1646static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001647{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001649
1650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1651
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1653
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1655 {
1656 unsigned char *buf;
1657 size_t buf_len;
1658
1659 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1660 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1661 &buf, &buf_len ) );
1662
1663 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1664 buf, buf + buf_len ) );
1665
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001666 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1667 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001668 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001669 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001670 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00001671 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672 }
1673 else
1674 {
1675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001676 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1677 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001678 }
1679
Jerry Yud2674312021-10-29 10:08:19 +08001680 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1681
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001682cleanup:
1683
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1685 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001686}
1687
1688/*
Jerry Yu687101b2021-09-14 16:03:56 +08001689 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1690 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001691static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001692{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001693 int ret;
1694
1695 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001696 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001697 return( ret );
1698
Jerry Yu687101b2021-09-14 16:03:56 +08001699 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1700 return( 0 );
1701}
1702
1703/*
1704 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1705 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001706static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001707{
Jerry Yu30b071c2021-09-12 20:16:03 +08001708 int ret;
1709
1710 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1711 if( ret != 0 )
1712 return( ret );
1713
Jerry Yu687101b2021-09-14 16:03:56 +08001714 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1715 return( 0 );
1716}
Jerry Yua93ac112021-10-27 16:31:48 +08001717#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001718
Jerry Yu687101b2021-09-14 16:03:56 +08001719/*
1720 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1721 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001722static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001723{
XiaokangQianac0385c2021-11-03 06:40:11 +00001724 int ret;
1725
XiaokangQianc5c39d52021-11-09 11:55:10 +00001726 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001727 if( ret != 0 )
1728 return( ret );
1729
Jerry Yue3d67cb2022-05-19 15:33:10 +08001730 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1731 if( ret != 0 )
1732 {
1733 MBEDTLS_SSL_PEND_FATAL_ALERT(
1734 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1735 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1736 return( ret );
1737 }
1738
Ronald Cron49ad6192021-11-24 16:25:31 +01001739#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1740 mbedtls_ssl_handshake_set_state(
1741 ssl,
1742 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1743#else
Jerry Yuca133a32022-02-15 14:22:05 +08001744 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001745#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001746
XiaokangQianac0385c2021-11-03 06:40:11 +00001747 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001748}
1749
1750/*
Jerry Yu566c7812022-01-26 15:41:22 +08001751 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1752 */
1753static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1754{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001755 int non_empty_certificate_msg = 0;
1756
Jerry Yu5cc35062022-01-28 16:16:08 +08001757 MBEDTLS_SSL_DEBUG_MSG( 1,
1758 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001759 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001760
Ronald Cron9df7c802022-03-08 18:38:54 +01001761#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001762 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001763 {
1764 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1765 if( ret != 0 )
1766 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001767
Ronald Cron7a94aca2022-03-09 07:44:27 +01001768 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1769 non_empty_certificate_msg = 1;
1770 }
1771 else
1772 {
XiaokangQian23c5be62022-06-07 02:04:34 +00001773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001774 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001775#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001776
Ronald Cron7a94aca2022-03-09 07:44:27 +01001777 if( non_empty_certificate_msg )
1778 {
1779 mbedtls_ssl_handshake_set_state( ssl,
1780 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1781 }
1782 else
Ronald Cron19385882022-06-15 16:26:13 +02001783 {
1784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001785 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001786 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01001787
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001788 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001789}
1790
Ronald Cron9df7c802022-03-08 18:38:54 +01001791#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001792/*
1793 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1794 */
1795static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1796{
Ronald Crona8b38872022-03-09 07:59:25 +01001797 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1798
1799 if( ret == 0 )
1800 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1801
1802 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001803}
Jerry Yu90f152d2022-01-29 22:12:42 +08001804#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001805
1806/*
Jerry Yu687101b2021-09-14 16:03:56 +08001807 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1808 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001809static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001810{
XiaokangQian0fa66432021-11-15 03:33:57 +00001811 int ret;
1812
1813 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1814 if( ret != 0 )
1815 return( ret );
1816
Jerry Yue3d67cb2022-05-19 15:33:10 +08001817 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1818 if( ret != 0 )
1819 {
1820 MBEDTLS_SSL_DEBUG_RET( 1,
1821 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1822 return ( ret );
1823 }
1824
XiaokangQian0fa66432021-11-15 03:33:57 +00001825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1826 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001827}
1828
1829/*
1830 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1831 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001832static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001833{
Jerry Yu378254d2021-10-30 21:44:47 +08001834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001835 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001836 return( 0 );
1837}
1838
1839/*
1840 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1841 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001842static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001843{
Jerry Yu378254d2021-10-30 21:44:47 +08001844
1845 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1846
1847 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1848 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001849}
1850
Jerry Yu92c6b402021-08-27 16:59:09 +08001851int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001852{
Jerry Yu92c6b402021-08-27 16:59:09 +08001853 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001854
Jerry Yu92c6b402021-08-27 16:59:09 +08001855 switch( ssl->state )
1856 {
1857 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001858 * ssl->state is initialized as HELLO_REQUEST. It is the same
1859 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001860 */
1861 case MBEDTLS_SSL_HELLO_REQUEST:
1862 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01001863 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001864 break;
1865
1866 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001867 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001868 break;
1869
1870 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001871 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001872 break;
1873
Jerry Yua93ac112021-10-27 16:31:48 +08001874#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001875 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1876 ret = ssl_tls13_process_certificate_request( ssl );
1877 break;
1878
Jerry Yu687101b2021-09-14 16:03:56 +08001879 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001880 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001881 break;
1882
1883 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001884 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001885 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001886#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001887
1888 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001889 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001890 break;
1891
Jerry Yu566c7812022-01-26 15:41:22 +08001892 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1893 ret = ssl_tls13_write_client_certificate( ssl );
1894 break;
1895
Ronald Cron9df7c802022-03-08 18:38:54 +01001896#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001897 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1898 ret = ssl_tls13_write_client_certificate_verify( ssl );
1899 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08001900#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001901
Jerry Yu687101b2021-09-14 16:03:56 +08001902 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001903 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001904 break;
1905
1906 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001907 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001908 break;
1909
1910 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001911 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001912 break;
1913
Ronald Cron49ad6192021-11-24 16:25:31 +01001914 /*
1915 * Injection of dummy-CCS's for middlebox compatibility
1916 */
1917#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001918 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01001919 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1920 if( ret == 0 )
1921 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1922 break;
1923
1924 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1925 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1926 if( ret == 0 )
1927 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01001928 break;
1929#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1930
Jerry Yu92c6b402021-08-27 16:59:09 +08001931 default:
1932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934 }
1935
1936 return( ret );
1937}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001938
Jerry Yufb4b6472022-01-27 15:03:26 +08001939#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001940
Jerry Yufb4b6472022-01-27 15:03:26 +08001941