blob: 42653a310bf593790bcaebc8fb098f1088d1382b [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
Ronald Cron3d580bf2022-02-18 17:24:56 +0100598int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
599 unsigned char *buf,
600 unsigned char *end,
601 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100602{
603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
604 unsigned char *p = buf;
605 size_t ext_len;
606
607 *out_len = 0;
608
609 /* Write supported_versions extension
610 *
611 * Supported Versions Extension is mandatory with TLS 1.3.
612 */
613 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
614 if( ret != 0 )
615 return( ret );
616 p += ext_len;
617
618 /* Echo the cookie if the server provided one in its preceding
619 * HelloRetryRequest message.
620 */
621 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
622 if( ret != 0 )
623 return( ret );
624 p += ext_len;
625
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100626 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
627 {
628 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
629 if( ret != 0 )
630 return( ret );
631 p += ext_len;
632 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100633
634 *out_len = p - buf;
635
636 return( 0 );
637}
638
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800639/*
Jerry Yu4a173382021-10-11 21:45:31 +0800640 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800641 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200642
Ronald Cronda41b382022-03-30 09:57:11 +0200643/**
644 * \brief Detect if the ServerHello contains a supported_versions extension
645 * or not.
646 *
647 * \param[in] ssl SSL context
648 * \param[in] buf Buffer containing the ServerHello message
649 * \param[in] end End of the buffer containing the ServerHello message
650 *
651 * \return 0 if the ServerHello does not contain a supported_versions extension
652 * \return 1 if the ServerHello contains a supported_versions extension
653 * \return A negative value if an error occurred while parsing the ServerHello.
654 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200655MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +0100656static int ssl_tls13_is_supported_versions_ext_present(
657 mbedtls_ssl_context *ssl,
658 const unsigned char *buf,
659 const unsigned char *end )
660{
661 const unsigned char *p = buf;
662 size_t legacy_session_id_echo_len;
663 size_t extensions_len;
664 const unsigned char *extensions_end;
665
666 /*
667 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200668 * length:
669 * - legacy_version 2 bytes
670 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
671 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100672 */
673 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
674 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
675 legacy_session_id_echo_len = *p;
676
677 /*
678 * Jump to the extensions, jumping over:
679 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
680 * - cipher_suite 2 bytes
681 * - legacy_compression_method 1 byte
682 */
Ronald Cron28271062022-06-10 14:43:55 +0200683 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100684 p += legacy_session_id_echo_len + 4;
685
686 /* Case of no extension */
687 if( p == end )
688 return( 0 );
689
690 /* ...
691 * Extension extensions<6..2^16-1>;
692 * ...
693 * struct {
694 * ExtensionType extension_type; (2 bytes)
695 * opaque extension_data<0..2^16-1>;
696 * } Extension;
697 */
698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
699 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
700 p += 2;
701
702 /* Check extensions do not go beyond the buffer of data. */
703 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
704 extensions_end = p + extensions_len;
705
706 while( p < extensions_end )
707 {
708 unsigned int extension_type;
709 size_t extension_data_len;
710
711 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
712 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
713 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
714 p += 4;
715
716 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
717 return( 1 );
718
719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
720 p += extension_data_len;
721 }
722
723 return( 0 );
724}
725
Jerry Yu7a186a02021-10-15 18:46:14 +0800726/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +0200727 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
728 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
729 * - 0 otherwise
730 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200731MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +0200732static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
733 const unsigned char *buf,
734 const unsigned char *end )
735{
736 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
737 static const unsigned char magic_downgrade_string[] =
738 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
739 const unsigned char *last_eight_bytes_of_random;
740 unsigned char last_byte_of_random;
741
742 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
743 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
744
745 if( memcmp( last_eight_bytes_of_random,
746 magic_downgrade_string,
747 sizeof( magic_downgrade_string ) ) == 0 )
748 {
749 last_byte_of_random = last_eight_bytes_of_random[7];
750 return( last_byte_of_random == 0 ||
751 last_byte_of_random == 1 );
752 }
753
754 return( 0 );
755}
756
757/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200758 * - SSL_SERVER_HELLO or
759 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000760 * to indicate which message is expected and to be parsed next.
761 */
Ronald Cron828aff62022-05-31 12:04:31 +0200762#define SSL_SERVER_HELLO 0
763#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200764MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +0800765static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
766 const unsigned char *buf,
767 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800768{
Jerry Yue1b9c292021-09-10 10:08:31 +0800769
770 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
771 *
Jerry Yu4a173382021-10-11 21:45:31 +0800772 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800773 * special value of the SHA-256 of "HelloRetryRequest".
774 *
775 * struct {
776 * ProtocolVersion legacy_version = 0x0303;
777 * Random random;
778 * opaque legacy_session_id_echo<0..32>;
779 * CipherSuite cipher_suite;
780 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800781 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800782 * } ServerHello;
783 *
784 */
Jerry Yu93a13f22022-04-11 23:00:01 +0800785 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
786 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800787
Jerry Yu93a13f22022-04-11 23:00:01 +0800788 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
789 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800790 {
Ronald Cron828aff62022-05-31 12:04:31 +0200791 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800792 }
793
Ronald Cron828aff62022-05-31 12:04:31 +0200794 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800795}
796
Ronald Cron828aff62022-05-31 12:04:31 +0200797/*
Jerry Yu745bb612021-10-13 22:01:04 +0800798 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +0200799 * - SSL_SERVER_HELLO or
800 * - SSL_SERVER_HELLO_HRR or
801 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +0800802 */
Ronald Cron828aff62022-05-31 12:04:31 +0200803#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200804MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +0200805static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200806 const unsigned char *buf,
807 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800808{
Jerry Yu4a173382021-10-11 21:45:31 +0800809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +0200810 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +0800811
Ronald Cron9f0fba32022-02-10 16:45:15 +0100812 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200813 ssl, buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100814 if( ret == 0 )
815 {
Ronald Cronfd6193c2022-04-05 11:04:20 +0200816 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +0200817 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +0200818
819 /* If the server is negotiating TLS 1.2 or below and:
820 * . we did not propose TLS 1.2 or
821 * . the server responded it is TLS 1.3 capable but negotiating a lower
822 * version of the protocol and thus we are under downgrade attack
823 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +0100824 */
Ronald Cron5afb9042022-05-31 12:11:39 +0200825 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +0100826 {
827 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
828 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
829 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
830 }
831
832 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -0400833 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +0100834 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +0200835 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100836
837 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
838 {
839 ret = ssl_tls13_reset_key_share( ssl );
840 if( ret != 0 )
841 return( ret );
842 }
843
Ronald Cron828aff62022-05-31 12:04:31 +0200844 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100845 }
846
Ronald Cron5afb9042022-05-31 12:11:39 +0200847 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
848
Ronald Crondb5dfa12022-05-31 11:44:38 +0200849 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +0800850 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800851 {
Ronald Cron828aff62022-05-31 12:04:31 +0200852 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +0800853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
854 break;
Ronald Cron828aff62022-05-31 12:04:31 +0200855 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +0800856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +0000857 /* If a client receives a second
858 * HelloRetryRequest in the same connection (i.e., where the ClientHello
859 * was itself in response to a HelloRetryRequest), it MUST abort the
860 * handshake with an "unexpected_message" alert.
861 */
Ronald Cron5afb9042022-05-31 12:11:39 +0200862 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +0000863 {
864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
865 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
866 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
868 }
XiaokangQian2b01dc32022-01-21 02:53:13 +0000869 /*
870 * Clients must abort the handshake with an "illegal_parameter"
871 * alert if the HelloRetryRequest would not result in any change
872 * in the ClientHello.
873 * In a PSK only key exchange that what we expect.
874 */
875 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
876 {
877 MBEDTLS_SSL_DEBUG_MSG( 1,
878 ( "Unexpected HRR in pure PSK key exchange." ) );
879 MBEDTLS_SSL_PEND_FATAL_ALERT(
880 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
881 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
882 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
883 }
XiaokangQian16acd4b2022-01-14 07:35:47 +0000884
Ronald Cron5afb9042022-05-31 12:11:39 +0200885 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +0000886
Jerry Yu745bb612021-10-13 22:01:04 +0800887 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 }
889
890cleanup:
891
892 return( ret );
893}
894
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200895MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800896static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
897 const unsigned char **buf,
898 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800899{
900 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800901 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800902
Jerry Yude4fb2c2021-09-19 18:05:08 +0800903 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800904 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800905
Jerry Yu4a173382021-10-11 21:45:31 +0800906 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800907
908 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800909 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
910 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800911 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800912 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
913 ssl->session_negotiate->id,
914 ssl->session_negotiate->id_len );
915 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800916 legacy_session_id_echo_len );
917
918 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
919 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
920
Jerry Yue1b9c292021-09-10 10:08:31 +0800921 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
922 }
923
Jerry Yu4a173382021-10-11 21:45:31 +0800924 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 *buf = p;
926
927 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800928 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800929 return( 0 );
930}
931
Jerry Yue1b9c292021-09-10 10:08:31 +0800932/* Parse ServerHello message and configure context
933 *
934 * struct {
935 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
936 * Random random;
937 * opaque legacy_session_id_echo<0..32>;
938 * CipherSuite cipher_suite;
939 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800940 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 * } ServerHello;
942 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +0800944static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
945 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +0000946 const unsigned char *end,
947 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +0800948{
Jerry Yub85277e2021-10-13 13:36:05 +0800949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800950 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +0000951 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +0800952 size_t extensions_len;
953 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800954 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800955 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +0000956 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800957
958 /*
959 * Check there is space for minimal fields
960 *
961 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800962 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800963 * - legacy_session_id_echo ( 1 byte ), minimum size
964 * - cipher_suite ( 2 bytes)
965 * - legacy_compression_method ( 1 byte )
966 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800967 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800968
969 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800970 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
971
Jerry Yu4a173382021-10-11 21:45:31 +0800972 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800973 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800974 * ...
975 * with ProtocolVersion defined as:
976 * uint16 ProtocolVersion;
977 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400978 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
979 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800980 {
981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
982 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
983 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +0000984 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
985 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +0800986 }
987 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800989 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800990 * Random random;
991 * ...
992 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800993 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800994 */
XiaokangQian53f20b72022-01-18 10:47:33 +0000995 if( !is_hrr )
996 {
XiaokangQian355e09a2022-01-20 11:14:50 +0000997 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +0000998 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
999 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1000 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1001 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001002 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001003
Jerry Yu4a173382021-10-11 21:45:31 +08001004 /* ...
1005 * opaque legacy_session_id_echo<0..32>;
1006 * ...
1007 */
1008 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001009 {
XiaokangQian52da5582022-01-26 09:49:29 +00001010 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001011 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001012 }
1013
Jerry Yu4a173382021-10-11 21:45:31 +08001014 /* ...
1015 * CipherSuite cipher_suite;
1016 * ...
1017 * with CipherSuite defined as:
1018 * uint8 CipherSuite[2];
1019 */
1020 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1022 p += 2;
1023
Jerry Yu4a173382021-10-11 21:45:31 +08001024
XiaokangQian355e09a2022-01-20 11:14:50 +00001025 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001026 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001027 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001028 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001029 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1030 ssl->tls_version,
1031 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001032 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001033 {
XiaokangQian52da5582022-01-26 09:49:29 +00001034 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001036 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001037 * If we received an HRR before and that the proposed selected
1038 * ciphersuite in this server hello is not the same as the one
1039 * proposed in the HRR, we abort the handshake and send an
1040 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001041 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001042 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1043 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001044 {
XiaokangQian52da5582022-01-26 09:49:29 +00001045 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001046 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001047
XiaokangQian52da5582022-01-26 09:49:29 +00001048 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001049 {
1050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1051 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001052 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001053 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001054
Jerry Yu4a173382021-10-11 21:45:31 +08001055 /* Configure ciphersuites */
1056 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1057
XiaokangQian355e09a2022-01-20 11:14:50 +00001058 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001059 ssl->session_negotiate->ciphersuite = cipher_suite;
1060
Jerry Yue1b9c292021-09-10 10:08:31 +08001061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1062 cipher_suite, ciphersuite_info->name ) );
1063
1064#if defined(MBEDTLS_HAVE_TIME)
1065 ssl->session_negotiate->start = time( NULL );
1066#endif /* MBEDTLS_HAVE_TIME */
1067
Jerry Yu4a173382021-10-11 21:45:31 +08001068 /* ...
1069 * uint8 legacy_compression_method = 0;
1070 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001071 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001073 if( p[0] != 0 )
1074 {
Jerry Yub85277e2021-10-13 13:36:05 +08001075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001076 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001077 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 }
1079 p++;
1080
Jerry Yub85277e2021-10-13 13:36:05 +08001081 /* ...
1082 * Extension extensions<6..2^16-1>;
1083 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001084 * struct {
1085 * ExtensionType extension_type; (2 bytes)
1086 * opaque extension_data<0..2^16-1>;
1087 * } Extension;
1088 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001089 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001090 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001092
Jerry Yu4a173382021-10-11 21:45:31 +08001093 /* Check extensions do not go beyond the buffer of data. */
1094 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1095 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001096
Jerry Yu4a173382021-10-11 21:45:31 +08001097 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001098
Jerry Yu4a173382021-10-11 21:45:31 +08001099 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001100 {
1101 unsigned int extension_type;
1102 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001103 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001104
Jerry Yu4a173382021-10-11 21:45:31 +08001105 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001106 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1107 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1108 p += 4;
1109
Jerry Yu4a173382021-10-11 21:45:31 +08001110 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001111 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001112
1113 switch( extension_type )
1114 {
XiaokangQianb851da82022-01-14 04:03:11 +00001115 case MBEDTLS_TLS_EXT_COOKIE:
1116
XiaokangQiand9e068e2022-01-18 06:23:32 +00001117 if( !is_hrr )
1118 {
XiaokangQian52da5582022-01-26 09:49:29 +00001119 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001120 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001121 }
1122
XiaokangQian43550bd2022-01-21 04:32:58 +00001123 ret = ssl_tls13_parse_cookie_ext( ssl,
1124 p, extension_data_end );
1125 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001126 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001127 MBEDTLS_SSL_DEBUG_RET( 1,
1128 "ssl_tls13_parse_cookie_ext",
1129 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001130 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001131 }
XiaokangQianb851da82022-01-14 04:03:11 +00001132 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001133
Jerry Yue1b9c292021-09-10 10:08:31 +08001134 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001135 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001136 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001137 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001138 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001139 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001140 break;
1141
1142 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001145
XiaokangQian52da5582022-01-26 09:49:29 +00001146 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001147 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001148
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 case MBEDTLS_TLS_EXT_KEY_SHARE:
1150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001151 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1152 {
XiaokangQian52da5582022-01-26 09:49:29 +00001153 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001154 goto cleanup;
1155 }
1156
XiaokangQian53f20b72022-01-18 10:47:33 +00001157 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001158 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001159 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001160 else
XiaokangQianb851da82022-01-14 04:03:11 +00001161 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001162 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001163 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 {
1165 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001166 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001167 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001168 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001169 }
1170 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001171
1172 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001173 MBEDTLS_SSL_DEBUG_MSG(
1174 3,
1175 ( "unknown extension found: %u ( ignoring )",
1176 extension_type ) );
1177
XiaokangQian52da5582022-01-26 09:49:29 +00001178 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001179 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001180 }
1181
1182 p += extension_data_len;
1183 }
1184
XiaokangQiand59be772022-01-24 10:12:51 +00001185cleanup:
1186
XiaokangQian52da5582022-01-26 09:49:29 +00001187 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001188 {
1189 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1190 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1191 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1192 }
XiaokangQian52da5582022-01-26 09:49:29 +00001193 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001194 {
1195 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1196 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1197 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1198 }
1199 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001200}
1201
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001202MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001203static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001204{
Jerry Yub85277e2021-10-13 13:36:05 +08001205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001206 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001207
Jerry Yub85277e2021-10-13 13:36:05 +08001208 /* Determine the key exchange mode:
1209 * 1) If both the pre_shared_key and key_share extensions were received
1210 * then the key exchange mode is PSK with EPHEMERAL.
1211 * 2) If only the pre_shared_key extension was received then the key
1212 * exchange mode is PSK-only.
1213 * 3) If only the key_share extension was received then the key
1214 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001215 */
Jerry Yub85277e2021-10-13 13:36:05 +08001216 switch( handshake->extensions_present &
1217 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001218 {
Jerry Yu745bb612021-10-13 22:01:04 +08001219 /* Only the pre_shared_key extension was received */
1220 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001221 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001222 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001223
Jerry Yu745bb612021-10-13 22:01:04 +08001224 /* Only the key_share extension was received */
1225 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001226 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001227 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001228
Jerry Yu745bb612021-10-13 22:01:04 +08001229 /* Both the pre_shared_key and key_share extensions were received */
1230 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001231 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001232 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001233
Jerry Yu745bb612021-10-13 22:01:04 +08001234 /* Neither pre_shared_key nor key_share extension was received */
1235 default:
1236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1237 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1238 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001239 }
1240
1241 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1242 *
1243 * TODO: We don't have to do this in case we offered 0-RTT and the
1244 * server accepted it. In this case, we could skip generating
1245 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001246 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001247 if( ret != 0 )
1248 {
Jerry Yue110d252022-05-05 10:19:22 +08001249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001250 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001251 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001252 }
1253
Jerry Yuf86eb752022-05-06 11:16:55 +08001254 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001255 if( ret != 0 )
1256 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001257 MBEDTLS_SSL_DEBUG_RET( 1,
1258 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001259 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001260 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001261 }
1262
Jerry Yue110d252022-05-05 10:19:22 +08001263 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1265 ssl->session_in = ssl->session_negotiate;
1266
Jerry Yu4a173382021-10-11 21:45:31 +08001267cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001268 if( ret != 0 )
1269 {
Jerry Yu4a173382021-10-11 21:45:31 +08001270 MBEDTLS_SSL_PEND_FATAL_ALERT(
1271 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1272 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1273 }
Jerry Yue110d252022-05-05 10:19:22 +08001274
Jerry Yu4a173382021-10-11 21:45:31 +08001275 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001276}
1277
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001278MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001279static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001280{
1281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1282
XiaokangQian78b1fa72022-01-19 06:56:30 +00001283 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001284
XiaokangQian78b1fa72022-01-19 06:56:30 +00001285 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001286 * We are going to re-generate a shared secret corresponding to the group
1287 * selected by the server, which is different from the group for which we
1288 * generated a shared secret in the first client hello.
1289 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001290 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001291 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001292 if( ret != 0 )
1293 return( ret );
1294
1295 return( 0 );
1296}
1297
Jerry Yue1b9c292021-09-10 10:08:31 +08001298/*
Jerry Yu4a173382021-10-11 21:45:31 +08001299 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001300 * Handler for MBEDTLS_SSL_SERVER_HELLO
1301 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001302MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001303static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001304{
Jerry Yu4a173382021-10-11 21:45:31 +08001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001306 unsigned char *buf = NULL;
1307 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001308 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001309
1310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1311
Ronald Crondb5dfa12022-05-31 11:44:38 +02001312 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1313 MBEDTLS_SSL_HS_SERVER_HELLO,
1314 &buf, &buf_len ) );
1315
Ronald Cron828aff62022-05-31 12:04:31 +02001316 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001317 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001318 goto cleanup;
1319 else
Ronald Cron828aff62022-05-31 12:04:31 +02001320 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001321
Ronald Cron828aff62022-05-31 12:04:31 +02001322 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001323 {
1324 ret = 0;
1325 goto cleanup;
1326 }
1327
XiaokangQianb851da82022-01-14 04:03:11 +00001328 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001329 buf + buf_len,
1330 is_hrr ) );
1331 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001332 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1333
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001334 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1335 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001336
XiaokangQiand9e068e2022-01-18 06:23:32 +00001337 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001338 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001339 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001340#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1341 /* If not offering early data, the client sends a dummy CCS record
1342 * immediately before its second flight. This may either be before
1343 * its second ClientHello or before its encrypted handshake flight.
1344 */
1345 mbedtls_ssl_handshake_set_state( ssl,
1346 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1347#else
1348 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1349#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1350 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001351 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001352 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001353 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001354 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1355 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001356
1357cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1359 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001360 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001361}
1362
1363/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001364 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001365 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001366 *
1367 * The EncryptedExtensions message contains any extensions which
1368 * should be protected, i.e., any which are not needed to establish
1369 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001370 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001371
XiaokangQian08da26c2021-10-09 10:12:11 +00001372/* Parse EncryptedExtensions message
1373 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001374 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001375 * } EncryptedExtensions;
1376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001377MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001378static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1379 const unsigned char *buf,
1380 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001381{
1382 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001383 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001384 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001385 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001386
XiaokangQian08da26c2021-10-09 10:12:11 +00001387 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001388 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001389 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001390
XiaokangQian97799ac2021-10-11 10:05:54 +00001391 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001392 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001393 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001394
XiaokangQian8db25ff2021-10-13 05:56:18 +00001395 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001396 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001397 unsigned int extension_type;
1398 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001399
XiaokangQian08da26c2021-10-09 10:12:11 +00001400 /*
1401 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001402 * ExtensionType extension_type; (2 bytes)
1403 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001404 * } Extension;
1405 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001407 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1408 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1409 p += 4;
1410
XiaokangQian8db25ff2021-10-13 05:56:18 +00001411 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001412
XiaokangQian97799ac2021-10-11 10:05:54 +00001413 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001414 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001415 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001416 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001417 switch( extension_type )
1418 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001419
XiaokangQian08da26c2021-10-09 10:12:11 +00001420 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1422 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001423
lhuang0486cacac2022-01-21 07:34:27 -08001424#if defined(MBEDTLS_SSL_ALPN)
1425 case MBEDTLS_TLS_EXT_ALPN:
1426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1427
1428 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1429 {
1430 return( ret );
1431 }
1432
1433 break;
1434#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001435 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001436 MBEDTLS_SSL_DEBUG_MSG(
1437 3, ( "unsupported extension found: %u ", extension_type) );
1438 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001439 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001440 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1441 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001442 }
1443
XiaokangQian08da26c2021-10-09 10:12:11 +00001444 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001445 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001446
XiaokangQian97799ac2021-10-11 10:05:54 +00001447 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001448 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001449 {
1450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001451 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001452 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001453 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001454 }
1455
1456 return( ret );
1457}
1458
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001459MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001460static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1461{
1462 int ret;
1463 unsigned char *buf;
1464 size_t buf_len;
1465
1466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1467
1468 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1469 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1470 &buf, &buf_len ) );
1471
1472 /* Process the message contents */
1473 MBEDTLS_SSL_PROC_CHK(
1474 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1475
1476 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1477 buf, buf_len );
1478
Ronald Cronfb508b82022-05-31 14:49:55 +02001479#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1480 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1481 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1482 else
1483 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1484#else
1485 ((void) ssl);
1486 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1487#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001488
1489cleanup:
1490
1491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1492 return( ret );
1493
1494}
1495
Jerry Yua93ac112021-10-27 16:31:48 +08001496#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001497/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001498 * STATE HANDLING: CertificateRequest
1499 *
Jerry Yud2674312021-10-29 10:08:19 +08001500 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001501#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1502#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001503/* Coordination:
1504 * Deals with the ambiguity of not knowing if a CertificateRequest
1505 * will be sent. Returns a negative code on failure, or
1506 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1507 * - SSL_CERTIFICATE_REQUEST_SKIP
1508 * indicating if a Certificate Request is expected or not.
1509 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001510MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001511static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001512{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001514
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001515 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001516 {
1517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1518 return( SSL_CERTIFICATE_REQUEST_SKIP );
1519 }
1520
1521 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001522 {
1523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1524 return( ret );
1525 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001526 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001527
1528 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1529 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1530 {
Ronald Cron19385882022-06-15 16:26:13 +02001531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001532 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001533 }
1534
Ronald Cron19385882022-06-15 16:26:13 +02001535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1536
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001537 return( SSL_CERTIFICATE_REQUEST_SKIP );
1538}
1539
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001540/*
1541 * ssl_tls13_parse_certificate_request()
1542 * Parse certificate request
1543 * struct {
1544 * opaque certificate_request_context<0..2^8-1>;
1545 * Extension extensions<2..2^16-1>;
1546 * } CertificateRequest;
1547 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001548MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001549static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1550 const unsigned char *buf,
1551 const unsigned char *end )
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001555 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001556 size_t extensions_len = 0;
1557 const unsigned char *extensions_end;
1558 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001559
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001560 /* ...
1561 * opaque certificate_request_context<0..2^8-1>
1562 * ...
1563 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001564 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1565 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001566 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001567
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001568 if( certificate_request_context_len > 0 )
1569 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001570 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001571 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1572 p, certificate_request_context_len );
1573
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001574 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001575 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001576 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001577 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001578 {
1579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1580 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1581 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001582 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001583 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001584 p += certificate_request_context_len;
1585 }
1586
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001587 /* ...
1588 * Extension extensions<2..2^16-1>;
1589 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001590 */
1591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1592 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1593 p += 2;
1594
1595 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1596 extensions_end = p + extensions_len;
1597
1598 while( p < extensions_end )
1599 {
1600 unsigned int extension_type;
1601 size_t extension_data_len;
1602
1603 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1604 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1605 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1606 p += 4;
1607
1608 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1609
1610 switch( extension_type )
1611 {
1612 case MBEDTLS_TLS_EXT_SIG_ALG:
1613 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001614 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02001615 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02001616 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001617 if( ret != 0 )
1618 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001619 if( ! sig_alg_ext_found )
1620 sig_alg_ext_found = 1;
1621 else
1622 {
1623 MBEDTLS_SSL_DEBUG_MSG( 3,
1624 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001625 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001626 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001627 break;
1628
1629 default:
1630 MBEDTLS_SSL_DEBUG_MSG(
1631 3,
1632 ( "unknown extension found: %u ( ignoring )",
1633 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001634 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001635 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001636 p += extension_data_len;
1637 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001638 /* Check that we consumed all the message. */
1639 if( p != end )
1640 {
1641 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001642 ( "CertificateRequest misaligned" ) );
1643 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001644 }
1645 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001646 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001647 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001648 MBEDTLS_SSL_DEBUG_MSG( 3,
1649 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001650 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001651 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001652
Jerry Yu7840f812022-01-29 10:26:51 +08001653 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001654 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001655
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001656decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001657 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1658 MBEDTLS_ERR_SSL_DECODE_ERROR );
1659 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001660}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001661
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001662/*
1663 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1664 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001665MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001666static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001667{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001669
1670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1671
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001672 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1673
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001674 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1675 {
1676 unsigned char *buf;
1677 size_t buf_len;
1678
1679 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1680 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1681 &buf, &buf_len ) );
1682
1683 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1684 buf, buf + buf_len ) );
1685
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001686 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1687 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001688 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001689 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001690 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00001691 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001692 }
1693 else
1694 {
1695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001696 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1697 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001698 }
1699
Jerry Yud2674312021-10-29 10:08:19 +08001700 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1701
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001702cleanup:
1703
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1705 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08001706}
1707
1708/*
Jerry Yu687101b2021-09-14 16:03:56 +08001709 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1710 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001711MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001712static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001713{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001714 int ret;
1715
1716 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001717 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001718 return( ret );
1719
Jerry Yu687101b2021-09-14 16:03:56 +08001720 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1721 return( 0 );
1722}
1723
1724/*
1725 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1726 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001727MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001728static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001729{
Jerry Yu30b071c2021-09-12 20:16:03 +08001730 int ret;
1731
1732 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1733 if( ret != 0 )
1734 return( ret );
1735
Jerry Yu687101b2021-09-14 16:03:56 +08001736 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1737 return( 0 );
1738}
Jerry Yua93ac112021-10-27 16:31:48 +08001739#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001740
Jerry Yu687101b2021-09-14 16:03:56 +08001741/*
1742 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1743 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001744MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001745static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001746{
XiaokangQianac0385c2021-11-03 06:40:11 +00001747 int ret;
1748
XiaokangQianc5c39d52021-11-09 11:55:10 +00001749 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001750 if( ret != 0 )
1751 return( ret );
1752
Jerry Yue3d67cb2022-05-19 15:33:10 +08001753 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1754 if( ret != 0 )
1755 {
1756 MBEDTLS_SSL_PEND_FATAL_ALERT(
1757 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1758 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1759 return( ret );
1760 }
1761
Ronald Cron49ad6192021-11-24 16:25:31 +01001762#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1763 mbedtls_ssl_handshake_set_state(
1764 ssl,
1765 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1766#else
Jerry Yuca133a32022-02-15 14:22:05 +08001767 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08001768#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01001769
XiaokangQianac0385c2021-11-03 06:40:11 +00001770 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001771}
1772
1773/*
Jerry Yu566c7812022-01-26 15:41:22 +08001774 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1775 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001776MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08001777static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
1778{
Ronald Cron7a94aca2022-03-09 07:44:27 +01001779 int non_empty_certificate_msg = 0;
1780
Jerry Yu5cc35062022-01-28 16:16:08 +08001781 MBEDTLS_SSL_DEBUG_MSG( 1,
1782 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08001783 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08001784
Ronald Cron9df7c802022-03-08 18:38:54 +01001785#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001786 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01001787 {
1788 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
1789 if( ret != 0 )
1790 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001791
Ronald Cron7a94aca2022-03-09 07:44:27 +01001792 if( mbedtls_ssl_own_cert( ssl ) != NULL )
1793 non_empty_certificate_msg = 1;
1794 }
1795 else
1796 {
XiaokangQian23c5be62022-06-07 02:04:34 +00001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001798 }
Ronald Cron9df7c802022-03-08 18:38:54 +01001799#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001800
Ronald Cron7a94aca2022-03-09 07:44:27 +01001801 if( non_empty_certificate_msg )
1802 {
1803 mbedtls_ssl_handshake_set_state( ssl,
1804 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1805 }
1806 else
Ronald Cron19385882022-06-15 16:26:13 +02001807 {
1808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01001809 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001810 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01001811
Ronald Cron5bb8fc82022-03-09 07:00:13 +01001812 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08001813}
1814
Ronald Cron9df7c802022-03-08 18:38:54 +01001815#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08001816/*
1817 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1818 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001819MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08001820static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1821{
Ronald Crona8b38872022-03-09 07:59:25 +01001822 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
1823
1824 if( ret == 0 )
1825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1826
1827 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08001828}
Jerry Yu90f152d2022-01-29 22:12:42 +08001829#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08001830
1831/*
Jerry Yu687101b2021-09-14 16:03:56 +08001832 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1833 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001834MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00001835static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001836{
XiaokangQian0fa66432021-11-15 03:33:57 +00001837 int ret;
1838
1839 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1840 if( ret != 0 )
1841 return( ret );
1842
Jerry Yue3d67cb2022-05-19 15:33:10 +08001843 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1844 if( ret != 0 )
1845 {
1846 MBEDTLS_SSL_DEBUG_RET( 1,
1847 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1848 return ( ret );
1849 }
1850
XiaokangQian0fa66432021-11-15 03:33:57 +00001851 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1852 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001853}
1854
1855/*
1856 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1857 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001858MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001859static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001860{
Jerry Yu378254d2021-10-30 21:44:47 +08001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001862 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001863 return( 0 );
1864}
1865
1866/*
1867 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1868 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001869MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001870static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001871{
Jerry Yu378254d2021-10-30 21:44:47 +08001872
1873 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1874
1875 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1876 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001877}
1878
Jerry Yuf8a49942022-07-07 11:32:32 +00001879#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1880
1881static int ssl_tls13_parse_new_session_ticket_extensions(
1882 mbedtls_ssl_context *ssl,
1883 const unsigned char *buf,
1884 const unsigned char *end )
1885{
1886 unsigned char *p = ( unsigned char * )buf;
1887
1888 ((void) ssl);
1889
1890 while( p < end )
1891 {
1892 unsigned int extension_type;
1893 size_t extension_data_len;
1894 const unsigned char *extension_data_end;
1895
1896 ((void) extension_data_end);
1897 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
1898 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1899 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1900 p += 4;
1901
1902 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
1903 extension_data_end = p + extension_data_len;
1904
1905 switch( extension_type )
1906 {
1907 case MBEDTLS_TLS_EXT_EARLY_DATA:
1908 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
1909 break;
1910 default:
1911 break;
1912 }
1913 p += extension_data_len;
1914 }
1915
1916 return( 0 );
1917}
1918
1919/*
1920 * struct {
1921 * uint32 ticket_lifetime;
1922 * uint32 ticket_age_add;
1923 * opaque ticket_nonce<0..255>;
1924 * opaque ticket<1..2^16-1>;
1925 * Extension extensions<0..2^16-2>;
1926 * } NewSessionTicket;
1927 *
1928 */
1929static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
1930 unsigned char *buf,
1931 unsigned char *end )
1932{
1933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1934 unsigned char *p = buf;
1935 mbedtls_ssl_session *session = ssl->session;
1936 size_t ticket_nonce_len;
1937 unsigned char *ticket_nonce;
1938 size_t ticket_len;
1939 unsigned char *ticket;
1940 size_t extensions_len;
1941 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1942 psa_algorithm_t psa_hash_alg;
1943 int hash_length;
1944
1945 /*
1946 * ticket_lifetime 4 bytes
1947 * ticket_age_add 4 bytes
1948 * ticket_nonce >=1 byte
1949 * ticket >=2 bytes
1950 * extensions >=2 bytes
1951 */
1952 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 13);
1953
1954 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
1955 p += 4;
1956 MBEDTLS_SSL_DEBUG_MSG( 3,
1957 ( "ticket->lifetime: %u",
1958 ( unsigned int )session->ticket_lifetime ) );
1959
1960 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 0 );
1961 p += 4;
1962 MBEDTLS_SSL_DEBUG_MSG( 3,
1963 ( "ticket->ticket_age_add: %u",
1964 ( unsigned int )session->ticket_age_add ) );
1965
1966 ticket_nonce_len = *p++;
1967 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_nonce_len );
1968 ticket_nonce = p;
1969 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", ticket_nonce, ticket_nonce_len );
1970 p += ticket_nonce_len;
1971
1972 /* Ticket */
1973 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1974 p += 2;
1975 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
1976
1977 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
1978
1979 /* Check if we previously received a ticket already. */
1980 if( session->ticket != NULL || session->ticket_len > 0 )
1981 {
1982 mbedtls_free( session->ticket );
1983 session->ticket = NULL;
1984 session->ticket_len = 0;
1985 }
1986
1987 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
1988 {
1989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
1990 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1991 }
1992 memcpy( ticket, p, ticket_len );
1993 p += ticket_len;
1994 session->ticket = ticket;
1995 session->ticket_len = ticket_len;
1996 MBEDTLS_SSL_DEBUG_BUF( 4, "stored ticket", ticket, ticket_len );
1997
1998 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1999 p += 2;
2000 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2001
2002 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket->extension", p, extensions_len );
2003
2004 ret = ssl_tls13_parse_new_session_ticket_extensions( ssl, p, end );
2005 if( ret != 0 )
2006 {
2007 MBEDTLS_SSL_DEBUG_RET( 1,
2008 "ssl_tls13_parse_new_session_ticket_extensions",
2009 ret );
2010 return( ret );
2011 }
2012 p += extensions_len;
2013
2014 /* Compute PSK based on received nonce and resumption_master_secret
2015 * in the following style:
2016 *
2017 * HKDF-Expand-Label( resumption_master_secret,
2018 * "resumption", ticket_nonce, Hash.length )
2019 */
2020 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2021 if( ciphersuite_info == NULL )
2022 {
2023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2025 }
2026
2027 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2028 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
2029 if( hash_length == -1 )
2030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2031
2032 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2033 session->app_secrets.resumption_master_secret,
2034 hash_length );
2035
2036 /* Computer resumption key
2037 *
2038 * HKDF-Expand-Label( resumption_master_secret,
2039 * "resumption", ticket_nonce, Hash.length )
2040 */
2041 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2042 psa_hash_alg,
2043 session->app_secrets.resumption_master_secret,
2044 hash_length,
2045 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2046 ticket_nonce,
2047 ticket_nonce_len,
2048 session->key,
2049 hash_length );
2050
2051 if( ret != 0 )
2052 {
2053 MBEDTLS_SSL_DEBUG_RET( 2,
2054 "Creating the ticket-resumed PSK failed",
2055 ret );
2056 return( ret );
2057 }
2058
2059 session->key_len = hash_length;
2060
2061 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
2062 session->key,
2063 session->key_len );
2064
2065#if defined(MBEDTLS_HAVE_TIME)
2066 /* Store ticket creation time */
2067 session->ticket_received = time( NULL );
2068#endif
2069
2070 return( 0 );
2071}
2072
2073static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl )
2074{
2075 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2076 return( 0 );
2077}
2078
2079/*
Jerry Yua357cf42022-07-12 05:36:45 +00002080 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002081 */
2082static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2083{
2084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2085 unsigned char *buf;
2086 size_t buf_len;
2087
2088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2089
2090 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2091 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2092 &buf, &buf_len ) );
2093
2094 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket( ssl,
2095 buf,
2096 buf + buf_len ) );
2097
2098 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket( ssl ) );
2099
2100cleanup:
2101
2102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2103 return( ret );
2104}
2105#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2106
Jerry Yu92c6b402021-08-27 16:59:09 +08002107int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002108{
Jerry Yu92c6b402021-08-27 16:59:09 +08002109 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002110
Jerry Yu92c6b402021-08-27 16:59:09 +08002111 switch( ssl->state )
2112 {
2113 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002114 * ssl->state is initialized as HELLO_REQUEST. It is the same
2115 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002116 */
2117 case MBEDTLS_SSL_HELLO_REQUEST:
2118 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002119 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002120 break;
2121
2122 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002123 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002124 break;
2125
2126 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002127 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002128 break;
2129
Jerry Yua93ac112021-10-27 16:31:48 +08002130#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002131 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2132 ret = ssl_tls13_process_certificate_request( ssl );
2133 break;
2134
Jerry Yu687101b2021-09-14 16:03:56 +08002135 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002136 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002137 break;
2138
2139 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002140 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002141 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002142#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002143
2144 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002145 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002146 break;
2147
Jerry Yu566c7812022-01-26 15:41:22 +08002148 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2149 ret = ssl_tls13_write_client_certificate( ssl );
2150 break;
2151
Ronald Cron9df7c802022-03-08 18:38:54 +01002152#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002153 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2154 ret = ssl_tls13_write_client_certificate_verify( ssl );
2155 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002156#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002157
Jerry Yu687101b2021-09-14 16:03:56 +08002158 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002159 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002160 break;
2161
2162 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002163 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002164 break;
2165
2166 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002167 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002168 break;
2169
Ronald Cron49ad6192021-11-24 16:25:31 +01002170 /*
2171 * Injection of dummy-CCS's for middlebox compatibility
2172 */
2173#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002174 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002175 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2176 if( ret == 0 )
2177 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2178 break;
2179
2180 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2181 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2182 if( ret == 0 )
2183 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002184 break;
2185#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2186
Jerry Yuf8a49942022-07-07 11:32:32 +00002187#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002188 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002189 ret = ssl_tls13_process_new_session_ticket( ssl );
2190 if( ret != 0 )
2191 break;
2192 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2193 break;
2194#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2195
Jerry Yu92c6b402021-08-27 16:59:09 +08002196 default:
2197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2199 }
2200
2201 return( ret );
2202}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002203
Jerry Yufb4b6472022-01-27 15:03:26 +08002204#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002205
Jerry Yufb4b6472022-01-27 15:03:26 +08002206