blob: 547651d9715f3f3c8f869a140162fd6d4c7fc8d3 [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 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040052 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040078 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Glenn Strausscd78df62022-04-07 19:07:11 -040083 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010084 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040085 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
88 }
89
90 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020095MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Ronald Cron98473382022-03-30 20:04:10 +0200102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400103 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
104 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
Ronald Cron98473382022-03-30 20:04:10 +0200113 if( &buf[2] != end )
114 {
115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
116 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
117 MBEDTLS_ERR_SSL_DECODE_ERROR );
118 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
119 }
120
Jerry Yue1b9c292021-09-10 10:08:31 +0800121 return( 0 );
122}
123
lhuang0486cacac2022-01-21 07:34:27 -0800124#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
Ronald Cron81a334f2022-05-31 16:04:11 +0200127 const unsigned char *buf, size_t len )
lhuang0486cacac2022-01-21 07:34:27 -0800128{
lhuang0486cacac2022-01-21 07:34:27 -0800129 const unsigned char *p = buf;
130 const unsigned char *end = buf + len;
Ronald Cron81a334f2022-05-31 16:04:11 +0200131 size_t protocol_name_list_len, protocol_name_len;
132 const unsigned char *protocol_name_list_end;
lhuang0486cacac2022-01-21 07:34:27 -0800133
134 /* If we didn't send it, the server shouldn't send it */
135 if( ssl->conf->alpn_list == NULL )
136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
137
138 /*
139 * opaque ProtocolName<1..2^8-1>;
140 *
141 * struct {
142 * ProtocolName protocol_name_list<2..2^16-1>
143 * } ProtocolNameList;
144 *
145 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
146 */
147
Ronald Cron81a334f2022-05-31 16:04:11 +0200148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
149 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
lhuang0486cacac2022-01-21 07:34:27 -0800150 p += 2;
lhuang0486cacac2022-01-21 07:34:27 -0800151
Ronald Cron81a334f2022-05-31 16:04:11 +0200152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
153 protocol_name_list_end = p + protocol_name_list_len;
154
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, 1 );
156 protocol_name_len = *p++;
lhuang0486cacac2022-01-21 07:34:27 -0800157
158 /* Check that the server chosen protocol was in our list and save it */
Ronald Cron81a334f2022-05-31 16:04:11 +0200159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, protocol_name_len );
160 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
lhuang0486cacac2022-01-21 07:34:27 -0800161 {
Ronald Cron81a334f2022-05-31 16:04:11 +0200162 if( protocol_name_len == strlen( *alpn ) &&
163 memcmp( p, *alpn, protocol_name_len ) == 0 )
lhuang0486cacac2022-01-21 07:34:27 -0800164 {
165 ssl->alpn_chosen = *alpn;
166 return( 0 );
167 }
168 }
169
170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
171}
172#endif /* MBEDTLS_SSL_ALPN */
173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200174MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian16acd4b2022-01-14 07:35:47 +0000175static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000176{
177 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100178
XiaokangQian647719a2021-12-07 09:16:29 +0000179 if( group_id == 0 )
180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
181
XiaokangQian355e09a2022-01-20 11:14:50 +0000182#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000183 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000184 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
187
188 /* Destroy generated private key. */
189 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
190 if( status != PSA_SUCCESS )
191 {
192 ret = psa_ssl_status_to_mbedtls( status );
193 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
194 return( ret );
195 }
196
197 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000198 return( 0 );
199 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000200 else
201#endif /* MBEDTLS_ECDH_C */
202 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000203 {
204 /* Do something */
205 }
206
207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
208}
209
210/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211 * Functions for writing key_share extension.
212 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200213MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub60e3cf2021-09-08 16:41:02 +0800214static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
215 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216{
217 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
218
Jerry Yu56fc07f2021-09-01 17:48:49 +0800219
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100221 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800222 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100223 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800224 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
225
Brett Warren14efd332021-10-06 09:32:11 +0100226 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000228 const mbedtls_ecp_curve_info *curve_info;
229 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
230 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100231 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800232 {
Brett Warren14efd332021-10-06 09:32:11 +0100233 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800234 return( 0 );
235 }
236 }
237#else
238 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800239 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240#endif /* MBEDTLS_ECDH_C */
241
242 /*
243 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800244 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 */
246
247 return( ret );
248}
249
250/*
251 * ssl_tls13_write_key_share_ext
252 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800253 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 *
255 * struct {
256 * NamedGroup group;
257 * opaque key_exchange<1..2^16-1>;
258 * } KeyShareEntry;
259 * struct {
260 * KeyShareEntry client_shares<0..2^16-1>;
261 * } KeyShareClientHello;
262 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
265 unsigned char *buf,
266 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268{
269 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000270 unsigned char *client_shares; /* Start of client_shares */
271 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
Xiaofei Baid25fab62021-12-02 06:36:27 +0000275 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276
Jerry Yub60e3cf2021-09-08 16:41:02 +0800277 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 * - extension_type (2 bytes)
279 * - extension_data_length (2 bytes)
280 * - client_shares_length (2 bytes)
281 */
282 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
283 p += 6;
284
285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
286
287 /* HRR could already have requested something else. */
288 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800289 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
290 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293 &group_id ) );
294 }
295
296 /*
297 * Dispatch to type-specific key generation function.
298 *
299 * So far, we're only supporting ECDHE. With the introduction
300 * of PQC KEMs, we'll want to have multiple branches, one per
301 * type of KEM, and dispatch to the corresponding crypto. And
302 * only one key share entry is allowed.
303 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000304 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800306 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800308 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000309 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100311 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312
313 /* Check there is space for header of KeyShareEntry
314 * - group (2 bytes)
315 * - key_exchange_length (2 bytes)
316 */
317 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
318 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800319 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
320 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 p += key_exchange_len;
322 if( ret != 0 )
323 return( ret );
324
325 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000326 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000328 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 }
330 else
331#endif /* MBEDTLS_ECDH_C */
332 if( 0 /* other KEMs? */ )
333 {
334 /* Do something */
335 }
336 else
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
338
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000340 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( client_shares_len == 0)
342 {
343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800345 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346 /* Write extension_type */
347 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
348 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800349 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800351 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 /* Update offered_group_id field */
354 ssl->handshake->offered_group_id = group_id;
355
356 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000357 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358
Xiaofei Baid25fab62021-12-02 06:36:27 +0000359 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800360
361 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
362
363cleanup:
364
365 return( ret );
366}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800367
Jerry Yue1b9c292021-09-10 10:08:31 +0800368
XiaokangQiand59be772022-01-24 10:12:51 +0000369/*
370 * ssl_tls13_parse_hrr_key_share_ext()
371 * Parse key_share extension in Hello Retry Request
372 *
373 * struct {
374 * NamedGroup selected_group;
375 * } KeyShareHelloRetryRequest;
376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand9e068e2022-01-18 06:23:32 +0000378static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000379 const unsigned char *buf,
380 const unsigned char *end )
381{
XiaokangQianb851da82022-01-14 04:03:11 +0000382 const mbedtls_ecp_curve_info *curve_info = NULL;
383 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000384 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000385 int found = 0;
386
387 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
388 if( group_list == NULL )
389 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
390
391 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
392
393 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000394 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000395 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000397
398 /* Upon receipt of this extension in a HelloRetryRequest, the client
399 * MUST first verify that the selected_group field corresponds to a
400 * group which was provided in the "supported_groups" extension in the
401 * original ClientHello.
402 * The supported_group was based on the info in ssl->conf->group_list.
403 *
404 * If the server provided a key share that was not sent in the ClientHello
405 * then the client MUST abort the handshake with an "illegal_parameter" alert.
406 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000407 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000408 {
409 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000410 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000411 continue;
412
413 /* We found a match */
414 found = 1;
415 break;
416 }
417
418 /* Client MUST verify that the selected_group field does not
419 * correspond to a group which was provided in the "key_share"
420 * extension in the original ClientHello. If the server sent an
421 * HRR message with a key share already provided in the
422 * ClientHello then the client MUST abort the handshake with
423 * an "illegal_parameter" alert.
424 */
XiaokangQiand59be772022-01-24 10:12:51 +0000425 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000426 {
427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
428 MBEDTLS_SSL_PEND_FATAL_ALERT(
429 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
430 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
431 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
432 }
433
434 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000435 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000436
437 return( 0 );
438}
439
Jerry Yue1b9c292021-09-10 10:08:31 +0800440/*
Jerry Yub85277e2021-10-13 13:36:05 +0800441 * ssl_tls13_parse_key_share_ext()
442 * Parse key_share extension in Server Hello
443 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 * struct {
445 * KeyShareEntry server_share;
446 * } KeyShareServerHello;
447 * struct {
448 * NamedGroup group;
449 * opaque key_exchange<1..2^16-1>;
450 * } KeyShareEntry;
451 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800453static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800454 const unsigned char *buf,
455 const unsigned char *end )
456{
Jerry Yub85277e2021-10-13 13:36:05 +0800457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800458 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800459 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800460
Jerry Yu4a173382021-10-11 21:45:31 +0800461 /* ...
462 * NamedGroup group; (2 bytes)
463 * ...
464 */
465 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
466 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800467 p += 2;
468
Jerry Yu4a173382021-10-11 21:45:31 +0800469 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800470 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800471 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800472 {
473 MBEDTLS_SSL_DEBUG_MSG( 1,
474 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800475 (unsigned) offered_group, (unsigned) group ) );
476 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
477 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
478 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800479 }
480
481#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800482 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800483 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100484 const mbedtls_ecp_curve_info *curve_info =
485 mbedtls_ecp_curve_info_from_tls_id( group );
486 if( curve_info == NULL )
487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
490 }
491
492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
493
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000494 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 if( ret != 0 )
496 return( ret );
497 }
Jerry Yub85277e2021-10-13 13:36:05 +0800498 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800499#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800500 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800501 {
502 /* Do something */
503 }
504 else
505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
506
507 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
508 return( ret );
509}
510
XiaokangQiand59be772022-01-24 10:12:51 +0000511/*
512 * ssl_tls13_parse_cookie_ext()
513 * Parse cookie extension in Hello Retry Request
514 *
515 * struct {
516 * opaque cookie<1..2^16-1>;
517 * } Cookie;
518 *
519 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
520 * extension to the client (this is an exception to the usual rule that
521 * the only extensions that may be sent are those that appear in the
522 * ClientHello). When sending the new ClientHello, the client MUST copy
523 * the contents of the extension received in the HelloRetryRequest into
524 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
525 * cookies in their initial ClientHello in subsequent connections.
526 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200527MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian43550bd2022-01-21 04:32:58 +0000528static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
529 const unsigned char *buf,
530 const unsigned char *end )
531{
XiaokangQian25c9c902022-02-08 10:49:53 +0000532 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000533 const unsigned char *p = buf;
534 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
535
536 /* Retrieve length field of cookie */
537 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
538 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
539 p += 2;
540
541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
542 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000546 handshake->cookie = mbedtls_calloc( 1, cookie_len );
547 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000548 {
549 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000550 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000551 cookie_len ) );
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553 }
554
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000555 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000556 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000557
558 return( 0 );
559}
XiaokangQian43550bd2022-01-21 04:32:58 +0000560
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200561MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian0b64eed2022-01-27 10:36:51 +0000562static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000563 unsigned char *buf,
564 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000565 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000566{
567 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000568 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
XiaokangQianc02768a2022-02-10 07:31:25 +0000571 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000572 {
573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
574 return( 0 );
575 }
576
577 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000578 handshake->cookie,
579 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
584
XiaokangQian0b64eed2022-01-27 10:36:51 +0000585 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000586 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
587 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000588 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000589
590 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000591 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000592
XiaokangQianc02768a2022-02-10 07:31:25 +0000593 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000594
595 return( 0 );
596}
597
XiaokangQianeb69aee2022-07-05 08:21:43 +0000598#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
599/*
600 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
601 *
602 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
603 *
604 * struct {
605 * PskKeyExchangeMode ke_modes<1..255>;
606 * } PskKeyExchangeModes;
607 */
XiaokangQian86981952022-07-19 09:51:50 +0000608MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianeb69aee2022-07-05 08:21:43 +0000609static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
610 unsigned char *buf,
611 unsigned char *end,
612 size_t *out_len )
613{
XiaokangQianeb69aee2022-07-05 08:21:43 +0000614 unsigned char *p = buf;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000615 int ke_modes_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000616
XiaokangQian008d2bf2022-07-14 07:54:01 +0000617 ((void) ke_modes_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000618 *out_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000619
XiaokangQianeb69aee2022-07-05 08:21:43 +0000620 /* Skip writing extension if no PSK key exchange mode
XiaokangQian7c12d312022-07-20 07:25:43 +0000621 * is enabled in the config.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000622 */
XiaokangQian86981952022-07-19 09:51:50 +0000623 if( !mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000624 {
625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip psk_key_exchange_modes extension" ) );
626 return( 0 );
627 }
628
629 /* Require 7 bytes of data, otherwise fail,
630 * even if extension might be shorter.
631 */
632 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
633 MBEDTLS_SSL_DEBUG_MSG(
634 3, ( "client hello, adding psk_key_exchange_modes extension" ) );
635
XiaokangQianeb69aee2022-07-05 08:21:43 +0000636 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0 );
637
XiaokangQian008d2bf2022-07-14 07:54:01 +0000638 /* Skip extension length (2 bytes) and
639 * ke_modes length (1 byte) for now.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000640 */
641 p += 5;
642
643 if( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) )
644 {
645 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000646 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000647
648 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding pure PSK key exchange mode" ) );
649 }
650
651 if( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) )
652 {
653 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000654 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000655
656 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding PSK-ECDHE key exchange mode" ) );
657 }
658
XiaokangQian008d2bf2022-07-14 07:54:01 +0000659 /* Now write the extension and ke_modes length */
660 MBEDTLS_PUT_UINT16_BE( ke_modes_len + 1, buf, 2 );
661 buf[4] = ke_modes_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662
663 *out_len = p - buf;
664 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
665 return ( 0 );
666}
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800667
Jerry Yuf7c12592022-09-28 22:09:38 +0800668static psa_algorithm_t ssl_tls13_ciphersuite_to_alg( mbedtls_ssl_context *ssl,
669 int ciphersuite )
670{
671 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
672 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
673
674 if( mbedtls_ssl_validate_ciphersuite(
675 ssl, ciphersuite_info,
676 MBEDTLS_SSL_VERSION_TLS1_3,
677 MBEDTLS_SSL_VERSION_TLS1_3 ) == 0 )
678 {
679 return( mbedtls_psa_translate_md( ciphersuite_info->mac ) );
680 }
681 return( PSA_ALG_NONE );
682}
683
684static int ssl_tls13_has_configured_psk( mbedtls_ssl_context *ssl )
685{
686 return( ssl->conf->psk != NULL &&
687 ssl->conf->psk_len != 0 &&
688 ssl->conf->psk_identity != NULL &&
689 ssl->conf->psk_identity_len != 0 );
690}
691
692static int ssl_tls13_has_configured_ticket( mbedtls_ssl_context *ssl )
693{
694
695#if defined(MBEDTLS_SSL_SESSION_TICKETS)
696 mbedtls_ssl_session *session = ssl->session_negotiate;
697 return( session != NULL &&
698 session->ticket != NULL &&
699 ssl_tls13_ciphersuite_to_alg( ssl,
700 ssl->session_negotiate->ciphersuite ) != PSA_ALG_NONE );
701#else
702 ((void) ssl);
703 return( 0 );
704#endif
705}
706
707#if defined(MBEDTLS_SSL_SESSION_TICKETS)
708
709MBEDTLS_CHECK_RETURN_CRITICAL
710static int ssl_tls13_session_tickets_get_identity(
711 mbedtls_ssl_context *ssl, psa_algorithm_t *psa_alg,
712 const unsigned char **identity, size_t *identity_len )
713{
714 mbedtls_ssl_session *session = ssl->session_negotiate;
715 *psa_alg = ssl_tls13_ciphersuite_to_alg( ssl, session->ciphersuite );
716 *identity = session->ticket;
717 *identity_len = session->ticket_len;
718 return( 0 );
719}
720
721#endif /* MBEDTLS_SSL_SESSION_TICKETS */
722
723MBEDTLS_CHECK_RETURN_CRITICAL
724static int ssl_tls13_psk_get_identity( mbedtls_ssl_context *ssl,
725 psa_algorithm_t *psa_alg,
726 const unsigned char **identity,
727 size_t *identity_len )
728{
729
730 *psa_alg = PSA_ALG_SHA_256;
731 *identity = ssl->conf->psk_identity;
732 *identity_len = ssl->conf->psk_identity_len;
733 return( 0 );
734}
735
XiaokangQianeb69aee2022-07-05 08:21:43 +0000736/*
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800737 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
XiaokangQianeb69aee2022-07-05 08:21:43 +0000738 *
739 * struct {
740 * opaque identity<1..2^16-1>;
741 * uint32 obfuscated_ticket_age;
742 * } PskIdentity;
743 *
744 * opaque PskBinderEntry<32..255>;
745 *
746 * struct {
XiaokangQian86981952022-07-19 09:51:50 +0000747 * PskIdentity identities<7..2^16-1>;
748 * PskBinderEntry binders<33..2^16-1>;
749 * } OfferedPsks;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000750 *
XiaokangQian86981952022-07-19 09:51:50 +0000751 * struct {
752 * select (Handshake.msg_type) {
753 * case client_hello: OfferedPsks;
754 * ...
755 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +0000756 * } PreSharedKeyExtension;
757 *
XiaokangQianeb69aee2022-07-05 08:21:43 +0000758 */
Jerry Yuf7c12592022-09-28 22:09:38 +0800759MBEDTLS_CHECK_RETURN_CRITICAL
760static int ssl_tls13_write_identity( mbedtls_ssl_context *ssl,
761 unsigned char *buf,
762 unsigned char *end,
763 int psk_type,
764 size_t *out_len,
765 size_t *binder_len )
766{
767 unsigned char *p = buf;
768 psa_algorithm_t psa_alg;
769 const unsigned char *identity;
770 size_t identity_len;
771 uint32_t obfuscated_ticket_age = 0;
772 int hash_len;
773
774 *out_len = 0;
775 *binder_len = 0;
776
777 switch( psk_type )
778 {
779#if defined(MBEDTLS_SSL_SESSION_TICKETS)
780 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
781 if( ssl_tls13_session_tickets_get_identity(
782 ssl, &psa_alg, &identity, &identity_len ) == 0 )
783 {
784#if defined(MBEDTLS_HAVE_TIME)
785 mbedtls_time_t now = mbedtls_time( NULL );
786 mbedtls_ssl_session *session = ssl->session_negotiate;
787 obfuscated_ticket_age =
788 (uint32_t)( now - session->ticket_received );
789 obfuscated_ticket_age *= 1000;
790 obfuscated_ticket_age += session->ticket_age_add ;
791#endif /* MBEDTLS_HAVE_TIME */
792 }
793 else
794 {
795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
796 }
797 break;
798#endif /* MBEDTLS_SSL_SESSION_TICKETS */
799 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
800 if( ssl_tls13_psk_get_identity(
801 ssl, &psa_alg, &identity, &identity_len ) != 0 )
802 {
803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
804 }
805 break;
806 default:
807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
808 }
809
810 hash_len = PSA_HASH_LENGTH( psa_alg );
811 if( hash_len == -1 )
812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
813
814 /*
815 * - identity_len (2 bytes)
816 * - identity (psk_identity_len bytes)
817 * - obfuscated_ticket_age (4 bytes)
818 */
819 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + identity_len );
820
821 MBEDTLS_PUT_UINT16_BE( identity_len, p, 0 );
822 memcpy( p + 2, identity, identity_len );
823 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 2 + identity_len );
824
825 MBEDTLS_SSL_DEBUG_BUF( 4, "write identity", p, 6 + identity_len );
826
827 *out_len = 6 + identity_len;
828 *binder_len = 1 + hash_len;
829
830 return( 0 );
831}
832
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000833int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000834 mbedtls_ssl_context *ssl,
835 unsigned char *buf, unsigned char *end,
836 size_t *out_len, size_t *binders_len )
837{
Jerry Yuf7c12592022-09-28 22:09:38 +0800838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
839 unsigned char *p = buf;
840 size_t l_binders_len = 0;
841
842 *out_len = 0;
843 *binders_len = 0;
844
845 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
846 if( !ssl_tls13_has_configured_psk( ssl ) &&
847 !ssl_tls13_has_configured_ticket( ssl ) )
848 {
849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
850 return( 0 );
851 }
852
853 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Pre-configured PSK number = %d",
854 ssl_tls13_has_configured_psk( ssl ) +
855 ssl_tls13_has_configured_ticket( ssl ) ) );
856 /* Check if we have space to write the extension, binders included.
857 * - extension_type (2 bytes)
858 * - extension_data_len (2 bytes)
859 * - identities_len (2 bytes)
860 */
861 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
862 p += 6;
863
864 if( ssl_tls13_has_configured_ticket( ssl ) )
865 {
866 size_t output_len, binder_len;
867 ret = ssl_tls13_write_identity( ssl, p, end,
868 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
869 &output_len, &binder_len );
870 if( ret != 0 )
871 return( ret );
872 p += output_len;
873 l_binders_len += binder_len;
874 }
875
876 if( ssl_tls13_has_configured_psk( ssl ) )
877 {
878 size_t output_len, binder_len;
879 ret = ssl_tls13_write_identity( ssl, p, end,
880 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
881 &output_len, &binder_len );
882 if( ret != 0 )
883 return( ret );
884 p += output_len;
885 l_binders_len += binder_len;
886 }
887
888 MBEDTLS_SSL_DEBUG_MSG( 3,
889 ( "client hello, adding pre_shared_key extension, "
890 "omitting PSK binder list" ) );
891 /*
892 * - extension_type (2 bytes)
893 * - extension_data_len (2 bytes)
894 * - identities_len (2 bytes)
895 */
896 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0 );
897 MBEDTLS_PUT_UINT16_BE( p - buf - 4 + 2 + l_binders_len , buf, 2 );
898 MBEDTLS_PUT_UINT16_BE( p - buf - 6 , buf, 4 );
899
900 /* Check if there are enough space for binders */
901 MBEDTLS_SSL_CHK_BUF_PTR( p, end, l_binders_len + 2 );
902
903 *out_len = ( p - buf ) + l_binders_len + 2;
904 *binders_len = l_binders_len + 2;
905
906 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key identities", buf, p - buf );
907
908 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
909
XiaokangQianeb69aee2022-07-05 08:21:43 +0000910 return( 0 );
911}
912
XiaokangQian86981952022-07-19 09:51:50 +0000913int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Jerry Yu0c6105b2022-08-12 17:26:40 +0800914 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000915{
Jerry Yu0c6105b2022-08-12 17:26:40 +0800916 ((void) ssl);
917 ((void) buf);
918 ((void) end);
XiaokangQianeb69aee2022-07-05 08:21:43 +0000919
920 return( 0 );
921}
Jerry Yu0c6105b2022-08-12 17:26:40 +0800922
923/*
924 * struct {
925 * opaque identity<1..2^16-1>;
926 * uint32 obfuscated_ticket_age;
927 * } PskIdentity;
928 *
929 * opaque PskBinderEntry<32..255>;
930 *
931 * struct {
932 *
933 * select (Handshake.msg_type) {
934 * ...
935 * case server_hello: uint16 selected_identity;
936 * };
937 *
938 * } PreSharedKeyExtension;
939 *
940 */
941MBEDTLS_CHECK_RETURN_CRITICAL
942static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
943 const unsigned char *buf,
944 const unsigned char *end )
945{
946 ((void) ssl);
947 ((void) buf);
948 ((void) end);
949 return( 0 );
950}
951
XiaokangQianeb69aee2022-07-05 08:21:43 +0000952#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
953
Ronald Cron3d580bf2022-02-18 17:24:56 +0100954int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
955 unsigned char *buf,
956 unsigned char *end,
957 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100958{
959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
960 unsigned char *p = buf;
961 size_t ext_len;
962
963 *out_len = 0;
964
965 /* Write supported_versions extension
966 *
967 * Supported Versions Extension is mandatory with TLS 1.3.
968 */
969 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
970 if( ret != 0 )
971 return( ret );
972 p += ext_len;
973
974 /* Echo the cookie if the server provided one in its preceding
975 * HelloRetryRequest message.
976 */
977 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
978 if( ret != 0 )
979 return( ret );
980 p += ext_len;
981
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100982 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
983 {
984 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
985 if( ret != 0 )
986 return( ret );
987 p += ext_len;
988 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100989
XiaokangQianeb69aee2022-07-05 08:21:43 +0000990#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
991 /* For PSK-based key exchange we need the pre_shared_key extension
992 * and the psk_key_exchange_modes extension.
993 *
994 * The pre_shared_key extension MUST be the last extension in the
995 * ClientHello. Servers MUST check that it is the last extension and
996 * otherwise fail the handshake with an "illegal_parameter" alert.
997 *
998 * Add the psk_key_exchange_modes extension.
999 */
1000 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
1001 if( ret != 0 )
1002 return( ret );
1003 p += ext_len;
1004#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1005
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001006 *out_len = p - buf;
1007
1008 return( 0 );
1009}
1010
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001011/*
Jerry Yu4a173382021-10-11 21:45:31 +08001012 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001013 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001014
Ronald Cronda41b382022-03-30 09:57:11 +02001015/**
1016 * \brief Detect if the ServerHello contains a supported_versions extension
1017 * or not.
1018 *
1019 * \param[in] ssl SSL context
1020 * \param[in] buf Buffer containing the ServerHello message
1021 * \param[in] end End of the buffer containing the ServerHello message
1022 *
1023 * \return 0 if the ServerHello does not contain a supported_versions extension
1024 * \return 1 if the ServerHello contains a supported_versions extension
1025 * \return A negative value if an error occurred while parsing the ServerHello.
1026 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001027MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001028static int ssl_tls13_is_supported_versions_ext_present(
1029 mbedtls_ssl_context *ssl,
1030 const unsigned char *buf,
1031 const unsigned char *end )
1032{
1033 const unsigned char *p = buf;
1034 size_t legacy_session_id_echo_len;
1035 size_t extensions_len;
1036 const unsigned char *extensions_end;
1037
1038 /*
1039 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001040 * length:
1041 * - legacy_version 2 bytes
1042 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1043 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001044 */
1045 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
1046 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1047 legacy_session_id_echo_len = *p;
1048
1049 /*
1050 * Jump to the extensions, jumping over:
1051 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1052 * - cipher_suite 2 bytes
1053 * - legacy_compression_method 1 byte
1054 */
Ronald Cron28271062022-06-10 14:43:55 +02001055 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001056 p += legacy_session_id_echo_len + 4;
1057
1058 /* Case of no extension */
1059 if( p == end )
1060 return( 0 );
1061
1062 /* ...
1063 * Extension extensions<6..2^16-1>;
1064 * ...
1065 * struct {
1066 * ExtensionType extension_type; (2 bytes)
1067 * opaque extension_data<0..2^16-1>;
1068 * } Extension;
1069 */
1070 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1071 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1072 p += 2;
1073
1074 /* Check extensions do not go beyond the buffer of data. */
1075 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1076 extensions_end = p + extensions_len;
1077
1078 while( p < extensions_end )
1079 {
1080 unsigned int extension_type;
1081 size_t extension_data_len;
1082
1083 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1084 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1085 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1086 p += 4;
1087
1088 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1089 return( 1 );
1090
1091 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1092 p += extension_data_len;
1093 }
1094
1095 return( 0 );
1096}
1097
Jerry Yu7a186a02021-10-15 18:46:14 +08001098/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001099 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1100 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1101 * - 0 otherwise
1102 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001103MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001104static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1105 const unsigned char *buf,
1106 const unsigned char *end )
1107{
1108 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1109 static const unsigned char magic_downgrade_string[] =
1110 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1111 const unsigned char *last_eight_bytes_of_random;
1112 unsigned char last_byte_of_random;
1113
1114 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1115 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1116
1117 if( memcmp( last_eight_bytes_of_random,
1118 magic_downgrade_string,
1119 sizeof( magic_downgrade_string ) ) == 0 )
1120 {
1121 last_byte_of_random = last_eight_bytes_of_random[7];
1122 return( last_byte_of_random == 0 ||
1123 last_byte_of_random == 1 );
1124 }
1125
1126 return( 0 );
1127}
1128
1129/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001130 * - SSL_SERVER_HELLO or
1131 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001132 * to indicate which message is expected and to be parsed next.
1133 */
Ronald Cron828aff62022-05-31 12:04:31 +02001134#define SSL_SERVER_HELLO 0
1135#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001137static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1138 const unsigned char *buf,
1139 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001140{
Jerry Yue1b9c292021-09-10 10:08:31 +08001141
1142 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1143 *
Jerry Yu4a173382021-10-11 21:45:31 +08001144 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001145 * special value of the SHA-256 of "HelloRetryRequest".
1146 *
1147 * struct {
1148 * ProtocolVersion legacy_version = 0x0303;
1149 * Random random;
1150 * opaque legacy_session_id_echo<0..32>;
1151 * CipherSuite cipher_suite;
1152 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001153 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 * } ServerHello;
1155 *
1156 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001157 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1158 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001159
Jerry Yu93a13f22022-04-11 23:00:01 +08001160 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1161 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001162 {
Ronald Cron828aff62022-05-31 12:04:31 +02001163 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 }
1165
Ronald Cron828aff62022-05-31 12:04:31 +02001166 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001167}
1168
Ronald Cron828aff62022-05-31 12:04:31 +02001169/*
Jerry Yu745bb612021-10-13 22:01:04 +08001170 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001171 * - SSL_SERVER_HELLO or
1172 * - SSL_SERVER_HELLO_HRR or
1173 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001174 */
Ronald Cron828aff62022-05-31 12:04:31 +02001175#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001177static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001178 const unsigned char *buf,
1179 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001180{
Jerry Yu4a173382021-10-11 21:45:31 +08001181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001182 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001183
Ronald Cron9f0fba32022-02-10 16:45:15 +01001184 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001185 ssl, buf, end ) );
Jerry Yua66fece2022-07-13 14:30:29 +08001186
Ronald Cron9f0fba32022-02-10 16:45:15 +01001187 if( ret == 0 )
1188 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001189 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001190 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001191
1192 /* If the server is negotiating TLS 1.2 or below and:
1193 * . we did not propose TLS 1.2 or
1194 * . the server responded it is TLS 1.3 capable but negotiating a lower
1195 * version of the protocol and thus we are under downgrade attack
1196 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001197 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001198 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001199 {
1200 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1201 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1202 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1203 }
1204
1205 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001206 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001207 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001208 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001209
1210 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1211 {
1212 ret = ssl_tls13_reset_key_share( ssl );
1213 if( ret != 0 )
1214 return( ret );
1215 }
1216
Ronald Cron828aff62022-05-31 12:04:31 +02001217 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001218 }
1219
Jerry Yua66fece2022-07-13 14:30:29 +08001220#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1221 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1222 ssl->session_negotiate->tls_version = ssl->tls_version;
1223#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1224
Ronald Cron5afb9042022-05-31 12:11:39 +02001225 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1226
Ronald Crondb5dfa12022-05-31 11:44:38 +02001227 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001228 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001229 {
Ronald Cron828aff62022-05-31 12:04:31 +02001230 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1232 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001233 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001235 /* If a client receives a second
1236 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1237 * was itself in response to a HelloRetryRequest), it MUST abort the
1238 * handshake with an "unexpected_message" alert.
1239 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001240 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001241 {
1242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1243 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1244 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1245 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1246 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001247 /*
1248 * Clients must abort the handshake with an "illegal_parameter"
1249 * alert if the HelloRetryRequest would not result in any change
1250 * in the ClientHello.
1251 * In a PSK only key exchange that what we expect.
1252 */
1253 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1254 {
1255 MBEDTLS_SSL_DEBUG_MSG( 1,
1256 ( "Unexpected HRR in pure PSK key exchange." ) );
1257 MBEDTLS_SSL_PEND_FATAL_ALERT(
1258 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1259 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1260 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1261 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001262
Ronald Cron5afb9042022-05-31 12:11:39 +02001263 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001264
Jerry Yu745bb612021-10-13 22:01:04 +08001265 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001266 }
1267
1268cleanup:
1269
1270 return( ret );
1271}
1272
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001274static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1275 const unsigned char **buf,
1276 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001277{
1278 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001279 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001280
Jerry Yude4fb2c2021-09-19 18:05:08 +08001281 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001282 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001283
Jerry Yu4a173382021-10-11 21:45:31 +08001284 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001285
1286 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001287 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1288 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001289 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001290 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1291 ssl->session_negotiate->id,
1292 ssl->session_negotiate->id_len );
1293 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001294 legacy_session_id_echo_len );
1295
1296 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1297 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1298
Jerry Yue1b9c292021-09-10 10:08:31 +08001299 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1300 }
1301
Jerry Yu4a173382021-10-11 21:45:31 +08001302 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001303 *buf = p;
1304
1305 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001306 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001307 return( 0 );
1308}
1309
Jerry Yue1b9c292021-09-10 10:08:31 +08001310/* Parse ServerHello message and configure context
1311 *
1312 * struct {
1313 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1314 * Random random;
1315 * opaque legacy_session_id_echo<0..32>;
1316 * CipherSuite cipher_suite;
1317 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001318 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001319 * } ServerHello;
1320 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001321MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001322static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1323 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001324 const unsigned char *end,
1325 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001326{
Jerry Yub85277e2021-10-13 13:36:05 +08001327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001328 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001329 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001330 size_t extensions_len;
1331 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001333 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001334 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001335
1336 /*
1337 * Check there is space for minimal fields
1338 *
1339 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001340 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001341 * - legacy_session_id_echo ( 1 byte ), minimum size
1342 * - cipher_suite ( 2 bytes)
1343 * - legacy_compression_method ( 1 byte )
1344 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001345 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001346
1347 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001348 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1349
Jerry Yu4a173382021-10-11 21:45:31 +08001350 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001351 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001352 * ...
1353 * with ProtocolVersion defined as:
1354 * uint16 ProtocolVersion;
1355 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001356 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1357 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1360 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1361 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001362 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1363 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001364 }
1365 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001366
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001367 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001368 * Random random;
1369 * ...
1370 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001371 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001372 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001373 if( !is_hrr )
1374 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001375 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001376 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1377 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1378 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1379 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001380 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001381
Jerry Yu4a173382021-10-11 21:45:31 +08001382 /* ...
1383 * opaque legacy_session_id_echo<0..32>;
1384 * ...
1385 */
1386 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001387 {
XiaokangQian52da5582022-01-26 09:49:29 +00001388 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001389 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001390 }
1391
Jerry Yu4a173382021-10-11 21:45:31 +08001392 /* ...
1393 * CipherSuite cipher_suite;
1394 * ...
1395 * with CipherSuite defined as:
1396 * uint8 CipherSuite[2];
1397 */
1398 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001399 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1400 p += 2;
1401
Jerry Yu4a173382021-10-11 21:45:31 +08001402
XiaokangQian355e09a2022-01-20 11:14:50 +00001403 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001404 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001405 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001406 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001407 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1408 ssl->tls_version,
1409 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001410 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001411 {
XiaokangQian52da5582022-01-26 09:49:29 +00001412 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001413 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001414 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001415 * If we received an HRR before and that the proposed selected
1416 * ciphersuite in this server hello is not the same as the one
1417 * proposed in the HRR, we abort the handshake and send an
1418 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001419 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001420 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1421 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001422 {
XiaokangQian52da5582022-01-26 09:49:29 +00001423 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001424 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001425
XiaokangQian52da5582022-01-26 09:49:29 +00001426 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001427 {
1428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1429 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001430 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001431 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001432
Jerry Yu4a173382021-10-11 21:45:31 +08001433 /* Configure ciphersuites */
1434 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1435
XiaokangQian355e09a2022-01-20 11:14:50 +00001436 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001437 ssl->session_negotiate->ciphersuite = cipher_suite;
1438
Jerry Yue1b9c292021-09-10 10:08:31 +08001439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1440 cipher_suite, ciphersuite_info->name ) );
1441
1442#if defined(MBEDTLS_HAVE_TIME)
1443 ssl->session_negotiate->start = time( NULL );
1444#endif /* MBEDTLS_HAVE_TIME */
1445
Jerry Yu4a173382021-10-11 21:45:31 +08001446 /* ...
1447 * uint8 legacy_compression_method = 0;
1448 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001449 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001450 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Thomas Daubney31e03a82022-07-25 15:59:25 +01001451 if( p[0] != MBEDTLS_SSL_COMPRESS_NULL )
Jerry Yue1b9c292021-09-10 10:08:31 +08001452 {
Jerry Yub85277e2021-10-13 13:36:05 +08001453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001454 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001455 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 }
1457 p++;
1458
Jerry Yub85277e2021-10-13 13:36:05 +08001459 /* ...
1460 * Extension extensions<6..2^16-1>;
1461 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001462 * struct {
1463 * ExtensionType extension_type; (2 bytes)
1464 * opaque extension_data<0..2^16-1>;
1465 * } Extension;
1466 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001467 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001468 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001469 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001470
Jerry Yu4a173382021-10-11 21:45:31 +08001471 /* Check extensions do not go beyond the buffer of data. */
1472 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1473 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001474
Jerry Yu4a173382021-10-11 21:45:31 +08001475 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001476
Jerry Yu4a173382021-10-11 21:45:31 +08001477 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001478 {
1479 unsigned int extension_type;
1480 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001481 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001482
Jerry Yu4a173382021-10-11 21:45:31 +08001483 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001484 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1485 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1486 p += 4;
1487
Jerry Yu4a173382021-10-11 21:45:31 +08001488 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001489 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001490
1491 switch( extension_type )
1492 {
XiaokangQianb851da82022-01-14 04:03:11 +00001493 case MBEDTLS_TLS_EXT_COOKIE:
1494
XiaokangQiand9e068e2022-01-18 06:23:32 +00001495 if( !is_hrr )
1496 {
XiaokangQian52da5582022-01-26 09:49:29 +00001497 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001498 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001499 }
1500
XiaokangQian43550bd2022-01-21 04:32:58 +00001501 ret = ssl_tls13_parse_cookie_ext( ssl,
1502 p, extension_data_end );
1503 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001504 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001505 MBEDTLS_SSL_DEBUG_RET( 1,
1506 "ssl_tls13_parse_cookie_ext",
1507 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001508 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001509 }
XiaokangQianb851da82022-01-14 04:03:11 +00001510 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001511
Jerry Yue1b9c292021-09-10 10:08:31 +08001512 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001513 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001514 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001515 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001516 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001517 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001518 break;
1519
XiaokangQianeb69aee2022-07-05 08:21:43 +00001520#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001521 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1523 if( is_hrr )
1524 {
1525 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1526 goto cleanup;
1527 }
Jerry Yu4a173382021-10-11 21:45:31 +08001528
XiaokangQian86981952022-07-19 09:51:50 +00001529 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001530 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001531 {
1532 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001533 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001534 return( ret );
1535 }
1536 break;
1537#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001538
Jerry Yue1b9c292021-09-10 10:08:31 +08001539 case MBEDTLS_TLS_EXT_KEY_SHARE:
1540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001541 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1542 {
XiaokangQian52da5582022-01-26 09:49:29 +00001543 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001544 goto cleanup;
1545 }
1546
XiaokangQian53f20b72022-01-18 10:47:33 +00001547 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001548 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001549 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001550 else
XiaokangQianb851da82022-01-14 04:03:11 +00001551 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001552 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001553 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001554 {
1555 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001556 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001557 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001558 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001559 }
1560 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001561
1562 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001563 MBEDTLS_SSL_DEBUG_MSG(
1564 3,
1565 ( "unknown extension found: %u ( ignoring )",
1566 extension_type ) );
1567
XiaokangQian52da5582022-01-26 09:49:29 +00001568 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001569 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001570 }
1571
1572 p += extension_data_len;
1573 }
1574
XiaokangQiand59be772022-01-24 10:12:51 +00001575cleanup:
1576
XiaokangQian52da5582022-01-26 09:49:29 +00001577 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001578 {
1579 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1580 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1581 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1582 }
XiaokangQian52da5582022-01-26 09:49:29 +00001583 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001584 {
1585 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1586 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1587 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1588 }
1589 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001590}
1591
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001592#if defined(MBEDTLS_DEBUG_C)
1593static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001594{
1595 switch( mode )
1596 {
1597 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1598 return "psk";
1599 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1600 return "ephemeral";
1601 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1602 return "psk_ephemeral";
1603 default:
1604 return "unknown mode";
1605 }
1606}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001607#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001608
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001609MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001610static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001611{
Jerry Yub85277e2021-10-13 13:36:05 +08001612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001613 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001614
Jerry Yub85277e2021-10-13 13:36:05 +08001615 /* Determine the key exchange mode:
1616 * 1) If both the pre_shared_key and key_share extensions were received
1617 * then the key exchange mode is PSK with EPHEMERAL.
1618 * 2) If only the pre_shared_key extension was received then the key
1619 * exchange mode is PSK-only.
1620 * 3) If only the key_share extension was received then the key
1621 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001622 */
Jerry Yub85277e2021-10-13 13:36:05 +08001623 switch( handshake->extensions_present &
1624 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001625 {
Jerry Yu745bb612021-10-13 22:01:04 +08001626 /* Only the pre_shared_key extension was received */
1627 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001628 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001629 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001630
Jerry Yu745bb612021-10-13 22:01:04 +08001631 /* Only the key_share extension was received */
1632 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001633 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001634 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001635
Jerry Yu745bb612021-10-13 22:01:04 +08001636 /* Both the pre_shared_key and key_share extensions were received */
1637 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001638 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001639 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001640
Jerry Yu745bb612021-10-13 22:01:04 +08001641 /* Neither pre_shared_key nor key_share extension was received */
1642 default:
1643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1644 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1645 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001646 }
1647
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001648 if( !mbedtls_ssl_conf_tls13_check_kex_modes( ssl, handshake->key_exchange_mode ) )
1649 {
1650 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1651 MBEDTLS_SSL_DEBUG_MSG( 2,
Xiaokang Qianca343ae2022-09-28 02:07:54 +00001652 ( "Key exchange mode(%s) is not supported.",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001653 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001654 goto cleanup;
1655 }
1656
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001657 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaokang Qianca343ae2022-09-28 02:07:54 +00001658 ( "Selected key exchange mode: %s",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001659 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001660
Jerry Yu0b177842021-09-05 19:41:30 +08001661 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1662 *
1663 * TODO: We don't have to do this in case we offered 0-RTT and the
1664 * server accepted it. In this case, we could skip generating
1665 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001666 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001667 if( ret != 0 )
1668 {
Jerry Yue110d252022-05-05 10:19:22 +08001669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001670 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001671 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001672 }
1673
Jerry Yuf86eb752022-05-06 11:16:55 +08001674 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001675 if( ret != 0 )
1676 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001677 MBEDTLS_SSL_DEBUG_RET( 1,
1678 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001679 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001680 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001681 }
1682
Jerry Yue110d252022-05-05 10:19:22 +08001683 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1685 ssl->session_in = ssl->session_negotiate;
1686
Jerry Yu4a173382021-10-11 21:45:31 +08001687cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001688 if( ret != 0 )
1689 {
Jerry Yu4a173382021-10-11 21:45:31 +08001690 MBEDTLS_SSL_PEND_FATAL_ALERT(
1691 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1692 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1693 }
Jerry Yue110d252022-05-05 10:19:22 +08001694
Jerry Yu4a173382021-10-11 21:45:31 +08001695 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001696}
1697
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001698MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001699static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001700{
1701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1702
XiaokangQian78b1fa72022-01-19 06:56:30 +00001703 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001704
XiaokangQian78b1fa72022-01-19 06:56:30 +00001705 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001706 * We are going to re-generate a shared secret corresponding to the group
1707 * selected by the server, which is different from the group for which we
1708 * generated a shared secret in the first client hello.
1709 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001710 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001711 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001712 if( ret != 0 )
1713 return( ret );
1714
1715 return( 0 );
1716}
1717
Jerry Yue1b9c292021-09-10 10:08:31 +08001718/*
Jerry Yu4a173382021-10-11 21:45:31 +08001719 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001720 * Handler for MBEDTLS_SSL_SERVER_HELLO
1721 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001722MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001723static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001724{
Jerry Yu4a173382021-10-11 21:45:31 +08001725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001726 unsigned char *buf = NULL;
1727 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001728 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001729
1730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1731
Ronald Crondb5dfa12022-05-31 11:44:38 +02001732 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1733 MBEDTLS_SSL_HS_SERVER_HELLO,
1734 &buf, &buf_len ) );
1735
Ronald Cron828aff62022-05-31 12:04:31 +02001736 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001737 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001738 goto cleanup;
1739 else
Ronald Cron828aff62022-05-31 12:04:31 +02001740 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001741
Ronald Cron828aff62022-05-31 12:04:31 +02001742 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001743 {
1744 ret = 0;
1745 goto cleanup;
1746 }
1747
XiaokangQianb851da82022-01-14 04:03:11 +00001748 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001749 buf + buf_len,
1750 is_hrr ) );
1751 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001752 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1753
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001754 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1755 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001756
XiaokangQiand9e068e2022-01-18 06:23:32 +00001757 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001758 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001759 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001760#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1761 /* If not offering early data, the client sends a dummy CCS record
1762 * immediately before its second flight. This may either be before
1763 * its second ClientHello or before its encrypted handshake flight.
1764 */
1765 mbedtls_ssl_handshake_set_state( ssl,
1766 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1767#else
1768 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1769#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1770 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001771 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001772 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001773 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001774 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1775 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001776
1777cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1779 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001780 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001781}
1782
1783/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001785 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001786 *
1787 * The EncryptedExtensions message contains any extensions which
1788 * should be protected, i.e., any which are not needed to establish
1789 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001790 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001791
XiaokangQian08da26c2021-10-09 10:12:11 +00001792/* Parse EncryptedExtensions message
1793 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001794 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001795 * } EncryptedExtensions;
1796 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001797MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001798static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1799 const unsigned char *buf,
1800 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001801{
1802 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001803 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001804 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001805 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001806
XiaokangQian08da26c2021-10-09 10:12:11 +00001807 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001808 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001809 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001810
XiaokangQian97799ac2021-10-11 10:05:54 +00001811 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001812 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001813 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001814
XiaokangQian8db25ff2021-10-13 05:56:18 +00001815 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001816 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001817 unsigned int extension_type;
1818 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001819
XiaokangQian08da26c2021-10-09 10:12:11 +00001820 /*
1821 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001822 * ExtensionType extension_type; (2 bytes)
1823 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001824 * } Extension;
1825 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001826 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001827 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1828 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1829 p += 4;
1830
XiaokangQian8db25ff2021-10-13 05:56:18 +00001831 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001832
XiaokangQian97799ac2021-10-11 10:05:54 +00001833 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001834 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001835 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001836 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001837 switch( extension_type )
1838 {
Jerry Yu661dd942022-08-03 14:50:01 +08001839 case MBEDTLS_TLS_EXT_SERVERNAME:
1840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found server_name extension" ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001841
Jerry Yu661dd942022-08-03 14:50:01 +08001842 /* The server_name extension should be an empty extension */
1843
1844 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00001845 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1847 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001848
lhuang0486cacac2022-01-21 07:34:27 -08001849#if defined(MBEDTLS_SSL_ALPN)
1850 case MBEDTLS_TLS_EXT_ALPN:
1851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1852
1853 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1854 {
1855 return( ret );
1856 }
1857
1858 break;
1859#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001860 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001861 MBEDTLS_SSL_DEBUG_MSG(
1862 3, ( "unsupported extension found: %u ", extension_type) );
1863 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001864 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001865 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1866 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001867 }
1868
XiaokangQian08da26c2021-10-09 10:12:11 +00001869 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001870 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001871
XiaokangQian97799ac2021-10-11 10:05:54 +00001872 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001873 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001874 {
1875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001876 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001877 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001878 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001879 }
1880
1881 return( ret );
1882}
1883
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001884MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001885static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1886{
1887 int ret;
1888 unsigned char *buf;
1889 size_t buf_len;
1890
1891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1892
1893 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1894 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1895 &buf, &buf_len ) );
1896
1897 /* Process the message contents */
1898 MBEDTLS_SSL_PROC_CHK(
1899 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1900
1901 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1902 buf, buf_len );
1903
Ronald Cronfb508b82022-05-31 14:49:55 +02001904#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001905 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001906 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1907 else
1908 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1909#else
1910 ((void) ssl);
1911 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1912#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001913
1914cleanup:
1915
1916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1917 return( ret );
1918
1919}
1920
Jerry Yua93ac112021-10-27 16:31:48 +08001921#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001922/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001923 * STATE HANDLING: CertificateRequest
1924 *
Jerry Yud2674312021-10-29 10:08:19 +08001925 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001926#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1927#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001928/* Coordination:
1929 * Deals with the ambiguity of not knowing if a CertificateRequest
1930 * will be sent. Returns a negative code on failure, or
1931 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1932 * - SSL_CERTIFICATE_REQUEST_SKIP
1933 * indicating if a Certificate Request is expected or not.
1934 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001935MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001936static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001937{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001939
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001940 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001941 {
1942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1943 return( ret );
1944 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001945 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001946
1947 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1948 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1949 {
Ronald Cron19385882022-06-15 16:26:13 +02001950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001951 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001952 }
1953
Ronald Cron19385882022-06-15 16:26:13 +02001954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1955
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001956 return( SSL_CERTIFICATE_REQUEST_SKIP );
1957}
1958
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001959/*
1960 * ssl_tls13_parse_certificate_request()
1961 * Parse certificate request
1962 * struct {
1963 * opaque certificate_request_context<0..2^8-1>;
1964 * Extension extensions<2..2^16-1>;
1965 * } CertificateRequest;
1966 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001967MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001968static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1969 const unsigned char *buf,
1970 const unsigned char *end )
1971{
1972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1973 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001974 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001975 size_t extensions_len = 0;
1976 const unsigned char *extensions_end;
1977 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001978
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001979 /* ...
1980 * opaque certificate_request_context<0..2^8-1>
1981 * ...
1982 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001983 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1984 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001985 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001986
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 if( certificate_request_context_len > 0 )
1988 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001989 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001990 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1991 p, certificate_request_context_len );
1992
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001993 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001994 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001995 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001996 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001997 {
1998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1999 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2000 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002001 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002002 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002003 p += certificate_request_context_len;
2004 }
2005
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002006 /* ...
2007 * Extension extensions<2..2^16-1>;
2008 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002009 */
2010 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2011 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2012 p += 2;
2013
2014 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2015 extensions_end = p + extensions_len;
2016
2017 while( p < extensions_end )
2018 {
2019 unsigned int extension_type;
2020 size_t extension_data_len;
2021
2022 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2023 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2024 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2025 p += 4;
2026
2027 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2028
2029 switch( extension_type )
2030 {
2031 case MBEDTLS_TLS_EXT_SIG_ALG:
2032 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002033 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002034 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002035 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002036 if( ret != 0 )
2037 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002038 if( ! sig_alg_ext_found )
2039 sig_alg_ext_found = 1;
2040 else
2041 {
2042 MBEDTLS_SSL_DEBUG_MSG( 3,
2043 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002044 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002045 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002046 break;
2047
2048 default:
2049 MBEDTLS_SSL_DEBUG_MSG(
2050 3,
2051 ( "unknown extension found: %u ( ignoring )",
2052 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002053 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002054 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002055 p += extension_data_len;
2056 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002057 /* Check that we consumed all the message. */
2058 if( p != end )
2059 {
2060 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002061 ( "CertificateRequest misaligned" ) );
2062 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002063 }
2064 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002065 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002066 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002067 MBEDTLS_SSL_DEBUG_MSG( 3,
2068 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002069 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002070 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002071
Jerry Yu7840f812022-01-29 10:26:51 +08002072 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002073 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002074
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002075decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002076 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2077 MBEDTLS_ERR_SSL_DECODE_ERROR );
2078 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002079}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002080
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002081/*
2082 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2083 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002084MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002085static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002086{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002088
2089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2090
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002091 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2092
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002093 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2094 {
2095 unsigned char *buf;
2096 size_t buf_len;
2097
2098 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2099 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2100 &buf, &buf_len ) );
2101
2102 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2103 buf, buf + buf_len ) );
2104
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002105 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2106 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002107 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002108 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002109 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002110 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002111 }
2112 else
2113 {
2114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002115 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2116 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002117 }
2118
Jerry Yud2674312021-10-29 10:08:19 +08002119 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2120
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002121cleanup:
2122
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2124 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002125}
2126
2127/*
Jerry Yu687101b2021-09-14 16:03:56 +08002128 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2129 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002130MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002131static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002132{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002133 int ret;
2134
2135 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002136 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002137 return( ret );
2138
Jerry Yu687101b2021-09-14 16:03:56 +08002139 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2140 return( 0 );
2141}
2142
2143/*
2144 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2145 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002146MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002147static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002148{
Jerry Yu30b071c2021-09-12 20:16:03 +08002149 int ret;
2150
2151 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2152 if( ret != 0 )
2153 return( ret );
2154
Jerry Yu687101b2021-09-14 16:03:56 +08002155 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2156 return( 0 );
2157}
Jerry Yua93ac112021-10-27 16:31:48 +08002158#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002159
Jerry Yu687101b2021-09-14 16:03:56 +08002160/*
2161 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2162 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002163MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002164static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002165{
XiaokangQianac0385c2021-11-03 06:40:11 +00002166 int ret;
2167
XiaokangQianc5c39d52021-11-09 11:55:10 +00002168 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002169 if( ret != 0 )
2170 return( ret );
2171
Jerry Yue3d67cb2022-05-19 15:33:10 +08002172 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2173 if( ret != 0 )
2174 {
2175 MBEDTLS_SSL_PEND_FATAL_ALERT(
2176 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2177 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2178 return( ret );
2179 }
2180
Ronald Cron49ad6192021-11-24 16:25:31 +01002181#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2182 mbedtls_ssl_handshake_set_state(
2183 ssl,
2184 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2185#else
Jerry Yuca133a32022-02-15 14:22:05 +08002186 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002187#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002188
XiaokangQianac0385c2021-11-03 06:40:11 +00002189 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002190}
2191
2192/*
Jerry Yu566c7812022-01-26 15:41:22 +08002193 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2194 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002195MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002196static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2197{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002198 int non_empty_certificate_msg = 0;
2199
Jerry Yu5cc35062022-01-28 16:16:08 +08002200 MBEDTLS_SSL_DEBUG_MSG( 1,
2201 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002202 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002203
Ronald Cron9df7c802022-03-08 18:38:54 +01002204#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002205 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002206 {
2207 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2208 if( ret != 0 )
2209 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002210
Ronald Cron7a94aca2022-03-09 07:44:27 +01002211 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2212 non_empty_certificate_msg = 1;
2213 }
2214 else
2215 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002217 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002218#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002219
Ronald Cron7a94aca2022-03-09 07:44:27 +01002220 if( non_empty_certificate_msg )
2221 {
2222 mbedtls_ssl_handshake_set_state( ssl,
2223 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2224 }
2225 else
Ronald Cron19385882022-06-15 16:26:13 +02002226 {
2227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002228 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002229 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002230
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002231 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002232}
2233
Ronald Cron9df7c802022-03-08 18:38:54 +01002234#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002235/*
2236 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2237 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002238MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002239static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2240{
Ronald Crona8b38872022-03-09 07:59:25 +01002241 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2242
2243 if( ret == 0 )
2244 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2245
2246 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002247}
Jerry Yu90f152d2022-01-29 22:12:42 +08002248#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002249
2250/*
Jerry Yu687101b2021-09-14 16:03:56 +08002251 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2252 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002253MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002254static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002255{
XiaokangQian0fa66432021-11-15 03:33:57 +00002256 int ret;
2257
2258 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2259 if( ret != 0 )
2260 return( ret );
2261
Jerry Yu466dda82022-09-13 11:20:20 +08002262 ret = mbedtls_ssl_tls13_compute_resumption_master_secret( ssl );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002263 if( ret != 0 )
2264 {
2265 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu466dda82022-09-13 11:20:20 +08002266 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002267 return ( ret );
2268 }
2269
XiaokangQian0fa66432021-11-15 03:33:57 +00002270 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2271 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002272}
2273
2274/*
2275 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2276 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002277MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002278static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002279{
Jerry Yu378254d2021-10-30 21:44:47 +08002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002281 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002282 return( 0 );
2283}
2284
2285/*
2286 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2287 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002288MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002289static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002290{
Jerry Yu378254d2021-10-30 21:44:47 +08002291
2292 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2293
2294 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2295 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002296}
2297
Jerry Yuf8a49942022-07-07 11:32:32 +00002298#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2299
Jerry Yua0446a02022-07-13 11:22:55 +08002300MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002301static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2302 const unsigned char *buf,
2303 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002304{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002305 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002306
2307 ((void) ssl);
2308
2309 while( p < end )
2310 {
2311 unsigned int extension_type;
2312 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002313
Jerry Yuf8a49942022-07-07 11:32:32 +00002314 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2315 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2316 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2317 p += 4;
2318
2319 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002320
2321 switch( extension_type )
2322 {
2323 case MBEDTLS_TLS_EXT_EARLY_DATA:
2324 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2325 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002326
Jerry Yuf8a49942022-07-07 11:32:32 +00002327 default:
2328 break;
2329 }
2330 p += extension_data_len;
2331 }
2332
2333 return( 0 );
2334}
2335
2336/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002337 * From RFC8446, page 74
2338 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002339 * struct {
2340 * uint32 ticket_lifetime;
2341 * uint32 ticket_age_add;
2342 * opaque ticket_nonce<0..255>;
2343 * opaque ticket<1..2^16-1>;
2344 * Extension extensions<0..2^16-2>;
2345 * } NewSessionTicket;
2346 *
2347 */
Jerry Yua0446a02022-07-13 11:22:55 +08002348MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002349static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2350 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002351 unsigned char *end,
2352 unsigned char **ticket_nonce,
2353 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002354{
2355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2356 unsigned char *p = buf;
2357 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002358 size_t ticket_len;
2359 unsigned char *ticket;
2360 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002361
Jerry Yucb3b1392022-07-12 06:09:38 +00002362 *ticket_nonce = NULL;
2363 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002364 /*
2365 * ticket_lifetime 4 bytes
2366 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002367 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002368 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002369 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002370
2371 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002372 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002373 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002374 ( unsigned int )session->ticket_lifetime ) );
2375
Jerry Yucb3b1392022-07-12 06:09:38 +00002376 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002377 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002378 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002379 ( unsigned int )session->ticket_age_add ) );
2380
Jerry Yucb3b1392022-07-12 06:09:38 +00002381 *ticket_nonce_len = p[8];
2382 p += 9;
2383
2384 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2385 *ticket_nonce = p;
2386 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2387 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002388
2389 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002390 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002391 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2392 p += 2;
2393 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002394 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2395
2396 /* Check if we previously received a ticket already. */
2397 if( session->ticket != NULL || session->ticket_len > 0 )
2398 {
2399 mbedtls_free( session->ticket );
2400 session->ticket = NULL;
2401 session->ticket_len = 0;
2402 }
2403
2404 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2405 {
2406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2407 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2408 }
2409 memcpy( ticket, p, ticket_len );
2410 p += ticket_len;
2411 session->ticket = ticket;
2412 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002413
Jerry Yucb3b1392022-07-12 06:09:38 +00002414 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002415 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2416 p += 2;
2417 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2418
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002419 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002420
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002421 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002422 if( ret != 0 )
2423 {
2424 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002425 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002426 ret );
2427 return( ret );
2428 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002429
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002430 /* session has been updated, allow export */
2431 session->exported = 0;
2432
Jerry Yucb3b1392022-07-12 06:09:38 +00002433 return( 0 );
2434}
2435
Jerry Yua0446a02022-07-13 11:22:55 +08002436MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002437static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2438 unsigned char *ticket_nonce,
2439 size_t ticket_nonce_len )
2440{
2441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2442 mbedtls_ssl_session *session = ssl->session;
2443 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2444 psa_algorithm_t psa_hash_alg;
2445 int hash_length;
2446
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002447#if defined(MBEDTLS_HAVE_TIME)
2448 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002449 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002450#endif
2451
Jerry Yuf8a49942022-07-07 11:32:32 +00002452 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2453 if( ciphersuite_info == NULL )
2454 {
2455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2457 }
2458
2459 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2460 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002461 if( hash_length == -1 ||
2462 ( size_t )hash_length > sizeof( session->resumption_key ) )
2463 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002465 }
2466
Jerry Yuf8a49942022-07-07 11:32:32 +00002467
2468 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2469 session->app_secrets.resumption_master_secret,
2470 hash_length );
2471
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002472 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002473 *
2474 * HKDF-Expand-Label( resumption_master_secret,
2475 * "resumption", ticket_nonce, Hash.length )
2476 */
2477 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2478 psa_hash_alg,
2479 session->app_secrets.resumption_master_secret,
2480 hash_length,
2481 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2482 ticket_nonce,
2483 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002484 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002485 hash_length );
2486
2487 if( ret != 0 )
2488 {
2489 MBEDTLS_SSL_DEBUG_RET( 2,
2490 "Creating the ticket-resumed PSK failed",
2491 ret );
2492 return( ret );
2493 }
2494
Jerry Yu0a430c82022-07-20 11:02:48 +08002495 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002496
2497 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002498 session->resumption_key,
2499 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002500
Jerry Yuf8a49942022-07-07 11:32:32 +00002501 return( 0 );
2502}
2503
2504/*
Jerry Yua357cf42022-07-12 05:36:45 +00002505 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002506 */
Jerry Yua0446a02022-07-13 11:22:55 +08002507MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002508static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2509{
2510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2511 unsigned char *buf;
2512 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002513 unsigned char *ticket_nonce;
2514 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002515
2516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2517
2518 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2519 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2520 &buf, &buf_len ) );
2521
Jerry Yucb3b1392022-07-12 06:09:38 +00002522 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2523 ssl, buf, buf + buf_len,
2524 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002525
Jerry Yucb3b1392022-07-12 06:09:38 +00002526 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2527 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002528
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002529 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2530
Jerry Yuf8a49942022-07-07 11:32:32 +00002531cleanup:
2532
2533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2534 return( ret );
2535}
2536#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2537
Jerry Yu92c6b402021-08-27 16:59:09 +08002538int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002539{
Jerry Yu92c6b402021-08-27 16:59:09 +08002540 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002541
Jerry Yu92c6b402021-08-27 16:59:09 +08002542 switch( ssl->state )
2543 {
2544 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002545 * ssl->state is initialized as HELLO_REQUEST. It is the same
2546 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002547 */
2548 case MBEDTLS_SSL_HELLO_REQUEST:
2549 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002550 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002551 break;
2552
2553 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002554 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002555 break;
2556
2557 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002558 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002559 break;
2560
Jerry Yua93ac112021-10-27 16:31:48 +08002561#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002562 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2563 ret = ssl_tls13_process_certificate_request( ssl );
2564 break;
2565
Jerry Yu687101b2021-09-14 16:03:56 +08002566 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002567 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002568 break;
2569
2570 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002571 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002572 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002573#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002574
2575 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002576 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002577 break;
2578
Jerry Yu566c7812022-01-26 15:41:22 +08002579 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2580 ret = ssl_tls13_write_client_certificate( ssl );
2581 break;
2582
Ronald Cron9df7c802022-03-08 18:38:54 +01002583#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002584 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2585 ret = ssl_tls13_write_client_certificate_verify( ssl );
2586 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002587#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002588
Jerry Yu687101b2021-09-14 16:03:56 +08002589 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002590 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002591 break;
2592
2593 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002594 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002595 break;
2596
2597 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002598 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002599 break;
2600
Ronald Cron49ad6192021-11-24 16:25:31 +01002601 /*
2602 * Injection of dummy-CCS's for middlebox compatibility
2603 */
2604#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002605 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002606 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2607 if( ret == 0 )
2608 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2609 break;
2610
2611 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2612 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2613 if( ret == 0 )
2614 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002615 break;
2616#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2617
Jerry Yuf8a49942022-07-07 11:32:32 +00002618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002619 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002620 ret = ssl_tls13_process_new_session_ticket( ssl );
2621 if( ret != 0 )
2622 break;
2623 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2624 break;
2625#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2626
Jerry Yu92c6b402021-08-27 16:59:09 +08002627 default:
2628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2630 }
2631
2632 return( ret );
2633}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002634
Jerry Yufb4b6472022-01-27 15:03:26 +08002635#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002636
Jerry Yufb4b6472022-01-27 15:03:26 +08002637