blob: 8f955e6bc64dd97277ae38ebab708ca6745290d5 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040052 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040078 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Glenn Strausscd78df62022-04-07 19:07:11 -040083 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010084 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040085 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
88 }
89
90 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020095MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Ronald Cron98473382022-03-30 20:04:10 +0200102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400103 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
104 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
Ronald Cron98473382022-03-30 20:04:10 +0200113 if( &buf[2] != end )
114 {
115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
116 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
117 MBEDTLS_ERR_SSL_DECODE_ERROR );
118 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
119 }
120
Jerry Yue1b9c292021-09-10 10:08:31 +0800121 return( 0 );
122}
123
lhuang0486cacac2022-01-21 07:34:27 -0800124#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
Ronald Cron81a334f2022-05-31 16:04:11 +0200127 const unsigned char *buf, size_t len )
lhuang0486cacac2022-01-21 07:34:27 -0800128{
lhuang0486cacac2022-01-21 07:34:27 -0800129 const unsigned char *p = buf;
130 const unsigned char *end = buf + len;
Ronald Cron81a334f2022-05-31 16:04:11 +0200131 size_t protocol_name_list_len, protocol_name_len;
132 const unsigned char *protocol_name_list_end;
lhuang0486cacac2022-01-21 07:34:27 -0800133
134 /* If we didn't send it, the server shouldn't send it */
135 if( ssl->conf->alpn_list == NULL )
136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
137
138 /*
139 * opaque ProtocolName<1..2^8-1>;
140 *
141 * struct {
142 * ProtocolName protocol_name_list<2..2^16-1>
143 * } ProtocolNameList;
144 *
145 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
146 */
147
Ronald Cron81a334f2022-05-31 16:04:11 +0200148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
149 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
lhuang0486cacac2022-01-21 07:34:27 -0800150 p += 2;
lhuang0486cacac2022-01-21 07:34:27 -0800151
Ronald Cron81a334f2022-05-31 16:04:11 +0200152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
153 protocol_name_list_end = p + protocol_name_list_len;
154
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, 1 );
156 protocol_name_len = *p++;
lhuang0486cacac2022-01-21 07:34:27 -0800157
158 /* Check that the server chosen protocol was in our list and save it */
Ronald Cron81a334f2022-05-31 16:04:11 +0200159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, protocol_name_len );
160 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
lhuang0486cacac2022-01-21 07:34:27 -0800161 {
Ronald Cron81a334f2022-05-31 16:04:11 +0200162 if( protocol_name_len == strlen( *alpn ) &&
163 memcmp( p, *alpn, protocol_name_len ) == 0 )
lhuang0486cacac2022-01-21 07:34:27 -0800164 {
165 ssl->alpn_chosen = *alpn;
166 return( 0 );
167 }
168 }
169
170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
171}
172#endif /* MBEDTLS_SSL_ALPN */
173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200174MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian16acd4b2022-01-14 07:35:47 +0000175static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000176{
177 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100178
XiaokangQian647719a2021-12-07 09:16:29 +0000179 if( group_id == 0 )
180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
181
XiaokangQian355e09a2022-01-20 11:14:50 +0000182#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000183 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000184 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
187
188 /* Destroy generated private key. */
189 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
190 if( status != PSA_SUCCESS )
191 {
192 ret = psa_ssl_status_to_mbedtls( status );
193 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
194 return( ret );
195 }
196
197 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000198 return( 0 );
199 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000200 else
201#endif /* MBEDTLS_ECDH_C */
202 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000203 {
204 /* Do something */
205 }
206
207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
208}
209
210/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211 * Functions for writing key_share extension.
212 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200213MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub60e3cf2021-09-08 16:41:02 +0800214static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
215 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216{
217 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
218
Jerry Yu56fc07f2021-09-01 17:48:49 +0800219
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100221 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800222 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100223 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800224 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
225
Brett Warren14efd332021-10-06 09:32:11 +0100226 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000228 const mbedtls_ecp_curve_info *curve_info;
229 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
230 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100231 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800232 {
Brett Warren14efd332021-10-06 09:32:11 +0100233 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800234 return( 0 );
235 }
236 }
237#else
238 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800239 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240#endif /* MBEDTLS_ECDH_C */
241
242 /*
243 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800244 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 */
246
247 return( ret );
248}
249
250/*
251 * ssl_tls13_write_key_share_ext
252 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800253 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 *
255 * struct {
256 * NamedGroup group;
257 * opaque key_exchange<1..2^16-1>;
258 * } KeyShareEntry;
259 * struct {
260 * KeyShareEntry client_shares<0..2^16-1>;
261 * } KeyShareClientHello;
262 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
265 unsigned char *buf,
266 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268{
269 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000270 unsigned char *client_shares; /* Start of client_shares */
271 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
Xiaofei Baid25fab62021-12-02 06:36:27 +0000275 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276
Jerry Yub60e3cf2021-09-08 16:41:02 +0800277 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 * - extension_type (2 bytes)
279 * - extension_data_length (2 bytes)
280 * - client_shares_length (2 bytes)
281 */
282 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
283 p += 6;
284
285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
286
287 /* HRR could already have requested something else. */
288 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800289 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
290 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293 &group_id ) );
294 }
295
296 /*
297 * Dispatch to type-specific key generation function.
298 *
299 * So far, we're only supporting ECDHE. With the introduction
300 * of PQC KEMs, we'll want to have multiple branches, one per
301 * type of KEM, and dispatch to the corresponding crypto. And
302 * only one key share entry is allowed.
303 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000304 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800306 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800308 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000309 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100311 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312
313 /* Check there is space for header of KeyShareEntry
314 * - group (2 bytes)
315 * - key_exchange_length (2 bytes)
316 */
317 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
318 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800319 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
320 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 p += key_exchange_len;
322 if( ret != 0 )
323 return( ret );
324
325 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000326 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000328 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 }
330 else
331#endif /* MBEDTLS_ECDH_C */
332 if( 0 /* other KEMs? */ )
333 {
334 /* Do something */
335 }
336 else
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
338
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000340 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( client_shares_len == 0)
342 {
343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800345 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346 /* Write extension_type */
347 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
348 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800349 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800351 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 /* Update offered_group_id field */
354 ssl->handshake->offered_group_id = group_id;
355
356 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000357 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358
Xiaofei Baid25fab62021-12-02 06:36:27 +0000359 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800360
361 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
362
363cleanup:
364
365 return( ret );
366}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800367
Jerry Yue1b9c292021-09-10 10:08:31 +0800368
XiaokangQiand59be772022-01-24 10:12:51 +0000369/*
370 * ssl_tls13_parse_hrr_key_share_ext()
371 * Parse key_share extension in Hello Retry Request
372 *
373 * struct {
374 * NamedGroup selected_group;
375 * } KeyShareHelloRetryRequest;
376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand9e068e2022-01-18 06:23:32 +0000378static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000379 const unsigned char *buf,
380 const unsigned char *end )
381{
XiaokangQianb851da82022-01-14 04:03:11 +0000382 const mbedtls_ecp_curve_info *curve_info = NULL;
383 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000384 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000385 int found = 0;
386
387 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
388 if( group_list == NULL )
389 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
390
391 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
392
393 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000394 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000395 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000397
398 /* Upon receipt of this extension in a HelloRetryRequest, the client
399 * MUST first verify that the selected_group field corresponds to a
400 * group which was provided in the "supported_groups" extension in the
401 * original ClientHello.
402 * The supported_group was based on the info in ssl->conf->group_list.
403 *
404 * If the server provided a key share that was not sent in the ClientHello
405 * then the client MUST abort the handshake with an "illegal_parameter" alert.
406 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000407 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000408 {
409 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000410 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000411 continue;
412
413 /* We found a match */
414 found = 1;
415 break;
416 }
417
418 /* Client MUST verify that the selected_group field does not
419 * correspond to a group which was provided in the "key_share"
420 * extension in the original ClientHello. If the server sent an
421 * HRR message with a key share already provided in the
422 * ClientHello then the client MUST abort the handshake with
423 * an "illegal_parameter" alert.
424 */
XiaokangQiand59be772022-01-24 10:12:51 +0000425 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000426 {
427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
428 MBEDTLS_SSL_PEND_FATAL_ALERT(
429 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
430 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
431 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
432 }
433
434 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000435 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000436
437 return( 0 );
438}
439
Jerry Yue1b9c292021-09-10 10:08:31 +0800440/*
Jerry Yub85277e2021-10-13 13:36:05 +0800441 * ssl_tls13_parse_key_share_ext()
442 * Parse key_share extension in Server Hello
443 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 * struct {
445 * KeyShareEntry server_share;
446 * } KeyShareServerHello;
447 * struct {
448 * NamedGroup group;
449 * opaque key_exchange<1..2^16-1>;
450 * } KeyShareEntry;
451 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800453static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800454 const unsigned char *buf,
455 const unsigned char *end )
456{
Jerry Yub85277e2021-10-13 13:36:05 +0800457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800458 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800459 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800460
Jerry Yu4a173382021-10-11 21:45:31 +0800461 /* ...
462 * NamedGroup group; (2 bytes)
463 * ...
464 */
465 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
466 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800467 p += 2;
468
Jerry Yu4a173382021-10-11 21:45:31 +0800469 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800470 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800471 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800472 {
473 MBEDTLS_SSL_DEBUG_MSG( 1,
474 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800475 (unsigned) offered_group, (unsigned) group ) );
476 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
477 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
478 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800479 }
480
481#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800482 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800483 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100484 const mbedtls_ecp_curve_info *curve_info =
485 mbedtls_ecp_curve_info_from_tls_id( group );
486 if( curve_info == NULL )
487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
490 }
491
492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
493
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000494 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 if( ret != 0 )
496 return( ret );
497 }
Jerry Yub85277e2021-10-13 13:36:05 +0800498 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800499#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800500 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800501 {
502 /* Do something */
503 }
504 else
505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
506
507 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
508 return( ret );
509}
510
XiaokangQiand59be772022-01-24 10:12:51 +0000511/*
512 * ssl_tls13_parse_cookie_ext()
513 * Parse cookie extension in Hello Retry Request
514 *
515 * struct {
516 * opaque cookie<1..2^16-1>;
517 * } Cookie;
518 *
519 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
520 * extension to the client (this is an exception to the usual rule that
521 * the only extensions that may be sent are those that appear in the
522 * ClientHello). When sending the new ClientHello, the client MUST copy
523 * the contents of the extension received in the HelloRetryRequest into
524 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
525 * cookies in their initial ClientHello in subsequent connections.
526 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200527MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian43550bd2022-01-21 04:32:58 +0000528static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
529 const unsigned char *buf,
530 const unsigned char *end )
531{
XiaokangQian25c9c902022-02-08 10:49:53 +0000532 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000533 const unsigned char *p = buf;
534 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
535
536 /* Retrieve length field of cookie */
537 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
538 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
539 p += 2;
540
541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
542 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000546 handshake->cookie = mbedtls_calloc( 1, cookie_len );
547 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000548 {
549 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000550 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000551 cookie_len ) );
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553 }
554
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000555 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000556 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000557
558 return( 0 );
559}
XiaokangQian43550bd2022-01-21 04:32:58 +0000560
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200561MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian0b64eed2022-01-27 10:36:51 +0000562static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000563 unsigned char *buf,
564 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000565 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000566{
567 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000568 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
XiaokangQianc02768a2022-02-10 07:31:25 +0000571 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000572 {
573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
574 return( 0 );
575 }
576
577 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000578 handshake->cookie,
579 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
584
XiaokangQian0b64eed2022-01-27 10:36:51 +0000585 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000586 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
587 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000588 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000589
590 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000591 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000592
XiaokangQianc02768a2022-02-10 07:31:25 +0000593 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000594
595 return( 0 );
596}
597
XiaokangQianeb69aee2022-07-05 08:21:43 +0000598#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
599/*
600 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
601 *
602 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
603 *
604 * struct {
605 * PskKeyExchangeMode ke_modes<1..255>;
606 * } PskKeyExchangeModes;
607 */
XiaokangQian86981952022-07-19 09:51:50 +0000608MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianeb69aee2022-07-05 08:21:43 +0000609static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
610 unsigned char *buf,
611 unsigned char *end,
612 size_t *out_len )
613{
XiaokangQianeb69aee2022-07-05 08:21:43 +0000614 unsigned char *p = buf;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000615 int ke_modes_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000616
XiaokangQian008d2bf2022-07-14 07:54:01 +0000617 ((void) ke_modes_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000618 *out_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000619
XiaokangQianeb69aee2022-07-05 08:21:43 +0000620 /* Skip writing extension if no PSK key exchange mode
XiaokangQian7c12d312022-07-20 07:25:43 +0000621 * is enabled in the config.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000622 */
XiaokangQian86981952022-07-19 09:51:50 +0000623 if( !mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000624 {
625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip psk_key_exchange_modes extension" ) );
626 return( 0 );
627 }
628
629 /* Require 7 bytes of data, otherwise fail,
630 * even if extension might be shorter.
631 */
632 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
633 MBEDTLS_SSL_DEBUG_MSG(
634 3, ( "client hello, adding psk_key_exchange_modes extension" ) );
635
XiaokangQianeb69aee2022-07-05 08:21:43 +0000636 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0 );
637
XiaokangQian008d2bf2022-07-14 07:54:01 +0000638 /* Skip extension length (2 bytes) and
639 * ke_modes length (1 byte) for now.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000640 */
641 p += 5;
642
643 if( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) )
644 {
645 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000646 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000647
648 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding pure PSK key exchange mode" ) );
649 }
650
651 if( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) )
652 {
653 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000654 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000655
656 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding PSK-ECDHE key exchange mode" ) );
657 }
658
XiaokangQian008d2bf2022-07-14 07:54:01 +0000659 /* Now write the extension and ke_modes length */
660 MBEDTLS_PUT_UINT16_BE( ke_modes_len + 1, buf, 2 );
661 buf[4] = ke_modes_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662
663 *out_len = p - buf;
664 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
665 return ( 0 );
666}
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800667
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800668/* Check if we have any PSK to offer, returns 0 if a PSK is available. */
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800669MBEDTLS_CHECK_RETURN_CRITICAL
670static int ssl_tls13_get_psk_to_offer(
671 const mbedtls_ssl_context *ssl,
672 int *psk_type,
673 const unsigned char **psk, size_t *psk_len,
674 const unsigned char **psk_identity, size_t *psk_identity_len )
675{
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800676 *psk = NULL;
677 *psk_len = 0;
678 *psk_identity = NULL;
679 *psk_identity_len = 0;
Jerry Yu95db17e2022-09-14 10:30:53 +0800680 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800681
682#if defined(MBEDTLS_SSL_SESSION_TICKETS)
683 /* Check if a ticket has been configured. */
Jerry Yufd310eb2022-09-06 09:16:35 +0800684 if( ssl->session_negotiate != NULL &&
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800685 ssl->session_negotiate->ticket != NULL )
686 {
687#if defined(MBEDTLS_HAVE_TIME)
688 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu95db17e2022-09-14 10:30:53 +0800689 if( ssl->session_negotiate->ticket_received <= now &&
690 (uint64_t)( now - ssl->session_negotiate->ticket_received )
691 <= ssl->session_negotiate->ticket_lifetime )
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800692 {
693 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
694 *psk = ssl->session_negotiate->resumption_key;
695 *psk_len = ssl->session_negotiate->resumption_key_len;
696 *psk_identity = ssl->session_negotiate->ticket;
697 *psk_identity_len = ssl->session_negotiate->ticket_len;
698 return( 0 );
699 }
700#endif /* MBEDTLS_HAVE_TIME */
701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket expired" ) );
702 }
703#endif
704
705 /* Check if an external PSK has been configured. */
706 if( ssl->conf->psk != NULL )
707 {
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800708 *psk = ssl->conf->psk;
709 *psk_len = ssl->conf->psk_len;
710 *psk_identity = ssl->conf->psk_identity;
711 *psk_identity_len = ssl->conf->psk_identity_len;
712 return( 0 );
713 }
714
Jerry Yu95db17e2022-09-14 10:30:53 +0800715 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800716}
XiaokangQianeb69aee2022-07-05 08:21:43 +0000717
718/*
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800719 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
XiaokangQianeb69aee2022-07-05 08:21:43 +0000720 *
721 * struct {
722 * opaque identity<1..2^16-1>;
723 * uint32 obfuscated_ticket_age;
724 * } PskIdentity;
725 *
726 * opaque PskBinderEntry<32..255>;
727 *
728 * struct {
XiaokangQian86981952022-07-19 09:51:50 +0000729 * PskIdentity identities<7..2^16-1>;
730 * PskBinderEntry binders<33..2^16-1>;
731 * } OfferedPsks;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000732 *
XiaokangQian86981952022-07-19 09:51:50 +0000733 * struct {
734 * select (Handshake.msg_type) {
735 * case client_hello: OfferedPsks;
736 * ...
737 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +0000738 * } PreSharedKeyExtension;
739 *
XiaokangQianeb69aee2022-07-05 08:21:43 +0000740 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000741int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000742 mbedtls_ssl_context *ssl,
743 unsigned char *buf, unsigned char *end,
744 size_t *out_len, size_t *binders_len )
745{
746 unsigned char *p = buf;
747 const unsigned char *psk;
748 size_t psk_len;
749 const unsigned char *psk_identity;
750 size_t psk_identity_len;
XiaokangQian86981952022-07-19 09:51:50 +0000751 int psk_type;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000752 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000753 const int *ciphersuites;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000754 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000755 int hash_len = 0;
756 size_t identities_len, l_binders_len;
757 uint32_t obfuscated_ticket_age = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000758
759 *out_len = 0;
760 *binders_len = 0;
761
762 /* Check if we have any PSKs to offer. If so, return the first.
763 *
764 * NOTE: Ultimately, we want to be able to offer multiple PSKs,
765 * in which case we want to iterate over them here.
766 *
767 * As it stands, however, we only ever offer one, chosen
768 * by the following heuristic:
769 * - If a ticket has been configured, offer the corresponding PSK.
770 * - If no ticket has been configured by an external PSK has been
771 * configured, offer that.
772 * - Otherwise, skip the PSK extension.
773 */
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800774 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
775 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000776 {
777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
778 return( 0 );
779 }
780
XiaokangQian86981952022-07-19 09:51:50 +0000781 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000782 {
XiaokangQian7c12d312022-07-20 07:25:43 +0000783 /*
784 * Ciphersuite list
785 */
786 ciphersuites = ssl->conf->ciphersuite_list;
XiaokangQian86981952022-07-19 09:51:50 +0000787 for( int i = 0; ciphersuites[i] != 0; i++ )
788 {
789 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
790 ciphersuites[i] );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000791
XiaokangQian86981952022-07-19 09:51:50 +0000792 if( mbedtls_ssl_validate_ciphersuite(
XiaokangQian008d2bf2022-07-14 07:54:01 +0000793 ssl, ciphersuite_info,
794 MBEDTLS_SSL_VERSION_TLS1_3,
795 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
XiaokangQian86981952022-07-19 09:51:50 +0000796 continue;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000797
XiaokangQian86981952022-07-19 09:51:50 +0000798 /* In this implementation we only add one pre-shared-key
799 * extension.
800 */
801 ssl->session_negotiate->ciphersuite = ciphersuites[i];
802 break;
803 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000804 }
Jerry Yue6527512022-08-03 13:39:05 +0800805 else
806#if defined(MBEDTLS_SSL_SESSION_TICKETS)
807 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
808 {
809#if defined(MBEDTLS_HAVE_TIME)
810 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu95db17e2022-09-14 10:30:53 +0800811
Jerry Yue6527512022-08-03 13:39:05 +0800812 obfuscated_ticket_age =
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800813 ( (uint32_t)( now - ssl->session_negotiate->ticket_received ) * 1000 )
814 + ssl->session_negotiate->ticket_age_add;
Jerry Yue6527512022-08-03 13:39:05 +0800815#endif
816 }
817 else
818#endif /* MBEDTLS_SSL_SESSION_TICKETS */
819 {
820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "write_identities_of_pre_shared_key_ext: "
821 "should never happen" ) );
822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
823 }
824
XiaokangQianeb69aee2022-07-05 08:21:43 +0000825
XiaokangQian008d2bf2022-07-14 07:54:01 +0000826 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
827 ssl->session_negotiate->ciphersuite );
828 /* No suitable ciphersuite for the PSK */
829 if( ciphersuite_info == NULL )
830 return( 0 );
831
832 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
833 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000834 if( hash_len == -1 )
835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
836
837 /* Check if we have space to write the extension, binder included.
838 * - extension_type (2 bytes)
839 * - extension_data_len (2 bytes)
840 * - identities_len (2 bytes)
841 * - identity_len (2 bytes)
842 * - identity (psk_identity_len bytes)
843 * - obfuscated_ticket_age (4 bytes)
844 * - binders_len (2 bytes)
845 * - binder_len (1 byte)
846 * - binder (hash_len bytes)
847 */
848
849 identities_len = 6 + psk_identity_len;
850 l_binders_len = 1 + hash_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000851
852 MBEDTLS_SSL_DEBUG_MSG( 3,
853 ( "client hello, adding pre_shared_key extension, "
854 "omitting PSK binder list" ) );
855
856 /* Extension header */
XiaokangQianbee71452022-07-21 08:19:06 +0000857 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000858 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
859 MBEDTLS_PUT_UINT16_BE( 2 + identities_len + 2 + l_binders_len , p, 2 );
860
861 MBEDTLS_PUT_UINT16_BE( identities_len, p, 4 );
862 MBEDTLS_PUT_UINT16_BE( psk_identity_len, p, 6 );
863 p += 8;
XiaokangQianbee71452022-07-21 08:19:06 +0000864 MBEDTLS_SSL_CHK_BUF_PTR( p, end, psk_identity_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000865 memcpy( p, psk_identity, psk_identity_len );
866 p += psk_identity_len;
867
868 /* add obfuscated ticket age */
XiaokangQianbee71452022-07-21 08:19:06 +0000869 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000870 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 0 );
871 p += 4;
872
XiaokangQianbee71452022-07-21 08:19:06 +0000873 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 + l_binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000874 *out_len = ( p - buf ) + l_binders_len + 2;
875 *binders_len = l_binders_len + 2;
876
877 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
878
879 return( 0 );
880}
881
XiaokangQian86981952022-07-19 09:51:50 +0000882int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000883 mbedtls_ssl_context *ssl,
884 unsigned char *buf, unsigned char *end )
885{
886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
887 unsigned char *p = buf;
XiaokangQianadab9a62022-07-18 07:41:26 +0000888 const unsigned char *psk_identity;
889 size_t psk_identity_len;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000890 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
891 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000892 int hash_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000893 const unsigned char *psk = NULL;
894 size_t psk_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000895 int psk_type;
896 unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
897 size_t transcript_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000898
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800899 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
900 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianadab9a62022-07-18 07:41:26 +0000901 {
902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
903 }
904
XiaokangQian008d2bf2022-07-14 07:54:01 +0000905 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
906 ssl->session_negotiate->ciphersuite );
907 if( ciphersuite_info == NULL )
908 return( 0 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000909
XiaokangQian008d2bf2022-07-14 07:54:01 +0000910 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
911 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000912 if( ( hash_len == -1 ) || ( ( end - buf ) != 3 + hash_len ) )
913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
914
915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list" ) );
916
XiaokangQian008d2bf2022-07-14 07:54:01 +0000917 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 + hash_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000918 /* 2 bytes length field for array of psk binders */
919 MBEDTLS_PUT_UINT16_BE( hash_len + 1, p, 0 );
920 p += 2;
921
922 /* 1 bytes length field for next psk binder */
923 *p++ = MBEDTLS_BYTE_0( hash_len );
924
XiaokangQianeb69aee2022-07-05 08:21:43 +0000925 /* Get current state of handshake transcript. */
XiaokangQian008d2bf2022-07-14 07:54:01 +0000926 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000927 transcript, sizeof( transcript ),
928 &transcript_len );
929 if( ret != 0 )
930 return( ret );
931
932 ret = mbedtls_ssl_tls13_create_psk_binder( ssl,
XiaokangQian008d2bf2022-07-14 07:54:01 +0000933 mbedtls_psa_translate_md( ciphersuite_info->mac ),
XiaokangQianeb69aee2022-07-05 08:21:43 +0000934 psk, psk_len, psk_type,
935 transcript, p );
936 if( ret != 0 )
937 {
938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
939 return( ret );
940 }
941
942 return( 0 );
943}
944#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
945
Ronald Cron3d580bf2022-02-18 17:24:56 +0100946int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
947 unsigned char *buf,
948 unsigned char *end,
949 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100950{
951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
952 unsigned char *p = buf;
953 size_t ext_len;
954
955 *out_len = 0;
956
957 /* Write supported_versions extension
958 *
959 * Supported Versions Extension is mandatory with TLS 1.3.
960 */
961 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
962 if( ret != 0 )
963 return( ret );
964 p += ext_len;
965
966 /* Echo the cookie if the server provided one in its preceding
967 * HelloRetryRequest message.
968 */
969 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
970 if( ret != 0 )
971 return( ret );
972 p += ext_len;
973
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100974 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
975 {
976 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
977 if( ret != 0 )
978 return( ret );
979 p += ext_len;
980 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100981
XiaokangQianeb69aee2022-07-05 08:21:43 +0000982#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
983 /* For PSK-based key exchange we need the pre_shared_key extension
984 * and the psk_key_exchange_modes extension.
985 *
986 * The pre_shared_key extension MUST be the last extension in the
987 * ClientHello. Servers MUST check that it is the last extension and
988 * otherwise fail the handshake with an "illegal_parameter" alert.
989 *
990 * Add the psk_key_exchange_modes extension.
991 */
992 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
993 if( ret != 0 )
994 return( ret );
995 p += ext_len;
996#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
997
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100998 *out_len = p - buf;
999
1000 return( 0 );
1001}
1002
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001003/*
Jerry Yu4a173382021-10-11 21:45:31 +08001004 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001006
Ronald Cronda41b382022-03-30 09:57:11 +02001007/**
1008 * \brief Detect if the ServerHello contains a supported_versions extension
1009 * or not.
1010 *
1011 * \param[in] ssl SSL context
1012 * \param[in] buf Buffer containing the ServerHello message
1013 * \param[in] end End of the buffer containing the ServerHello message
1014 *
1015 * \return 0 if the ServerHello does not contain a supported_versions extension
1016 * \return 1 if the ServerHello contains a supported_versions extension
1017 * \return A negative value if an error occurred while parsing the ServerHello.
1018 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001019MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001020static int ssl_tls13_is_supported_versions_ext_present(
1021 mbedtls_ssl_context *ssl,
1022 const unsigned char *buf,
1023 const unsigned char *end )
1024{
1025 const unsigned char *p = buf;
1026 size_t legacy_session_id_echo_len;
1027 size_t extensions_len;
1028 const unsigned char *extensions_end;
1029
1030 /*
1031 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001032 * length:
1033 * - legacy_version 2 bytes
1034 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1035 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001036 */
1037 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
1038 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1039 legacy_session_id_echo_len = *p;
1040
1041 /*
1042 * Jump to the extensions, jumping over:
1043 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1044 * - cipher_suite 2 bytes
1045 * - legacy_compression_method 1 byte
1046 */
Ronald Cron28271062022-06-10 14:43:55 +02001047 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001048 p += legacy_session_id_echo_len + 4;
1049
1050 /* Case of no extension */
1051 if( p == end )
1052 return( 0 );
1053
1054 /* ...
1055 * Extension extensions<6..2^16-1>;
1056 * ...
1057 * struct {
1058 * ExtensionType extension_type; (2 bytes)
1059 * opaque extension_data<0..2^16-1>;
1060 * } Extension;
1061 */
1062 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1063 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1064 p += 2;
1065
1066 /* Check extensions do not go beyond the buffer of data. */
1067 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1068 extensions_end = p + extensions_len;
1069
1070 while( p < extensions_end )
1071 {
1072 unsigned int extension_type;
1073 size_t extension_data_len;
1074
1075 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1076 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1077 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1078 p += 4;
1079
1080 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1081 return( 1 );
1082
1083 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1084 p += extension_data_len;
1085 }
1086
1087 return( 0 );
1088}
1089
Jerry Yu7a186a02021-10-15 18:46:14 +08001090/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001091 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1092 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1093 * - 0 otherwise
1094 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001095MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001096static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1097 const unsigned char *buf,
1098 const unsigned char *end )
1099{
1100 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1101 static const unsigned char magic_downgrade_string[] =
1102 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1103 const unsigned char *last_eight_bytes_of_random;
1104 unsigned char last_byte_of_random;
1105
1106 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1107 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1108
1109 if( memcmp( last_eight_bytes_of_random,
1110 magic_downgrade_string,
1111 sizeof( magic_downgrade_string ) ) == 0 )
1112 {
1113 last_byte_of_random = last_eight_bytes_of_random[7];
1114 return( last_byte_of_random == 0 ||
1115 last_byte_of_random == 1 );
1116 }
1117
1118 return( 0 );
1119}
1120
1121/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001122 * - SSL_SERVER_HELLO or
1123 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001124 * to indicate which message is expected and to be parsed next.
1125 */
Ronald Cron828aff62022-05-31 12:04:31 +02001126#define SSL_SERVER_HELLO 0
1127#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001128MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001129static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1130 const unsigned char *buf,
1131 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001132{
Jerry Yue1b9c292021-09-10 10:08:31 +08001133
1134 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1135 *
Jerry Yu4a173382021-10-11 21:45:31 +08001136 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001137 * special value of the SHA-256 of "HelloRetryRequest".
1138 *
1139 * struct {
1140 * ProtocolVersion legacy_version = 0x0303;
1141 * Random random;
1142 * opaque legacy_session_id_echo<0..32>;
1143 * CipherSuite cipher_suite;
1144 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001145 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001146 * } ServerHello;
1147 *
1148 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001149 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1150 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001151
Jerry Yu93a13f22022-04-11 23:00:01 +08001152 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1153 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001154 {
Ronald Cron828aff62022-05-31 12:04:31 +02001155 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 }
1157
Ronald Cron828aff62022-05-31 12:04:31 +02001158 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001159}
1160
Ronald Cron828aff62022-05-31 12:04:31 +02001161/*
Jerry Yu745bb612021-10-13 22:01:04 +08001162 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001163 * - SSL_SERVER_HELLO or
1164 * - SSL_SERVER_HELLO_HRR or
1165 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001166 */
Ronald Cron828aff62022-05-31 12:04:31 +02001167#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001168MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001169static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001170 const unsigned char *buf,
1171 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001172{
Jerry Yu4a173382021-10-11 21:45:31 +08001173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001174 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001175
Ronald Cron9f0fba32022-02-10 16:45:15 +01001176 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001177 ssl, buf, end ) );
Jerry Yua66fece2022-07-13 14:30:29 +08001178
Ronald Cron9f0fba32022-02-10 16:45:15 +01001179 if( ret == 0 )
1180 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001181 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001182 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001183
1184 /* If the server is negotiating TLS 1.2 or below and:
1185 * . we did not propose TLS 1.2 or
1186 * . the server responded it is TLS 1.3 capable but negotiating a lower
1187 * version of the protocol and thus we are under downgrade attack
1188 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001189 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001190 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001191 {
1192 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1193 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1194 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1195 }
1196
1197 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001198 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001199 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001200 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001201
1202 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1203 {
1204 ret = ssl_tls13_reset_key_share( ssl );
1205 if( ret != 0 )
1206 return( ret );
1207 }
1208
Ronald Cron828aff62022-05-31 12:04:31 +02001209 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001210 }
1211
Jerry Yua66fece2022-07-13 14:30:29 +08001212#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1213 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1214 ssl->session_negotiate->tls_version = ssl->tls_version;
1215#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1216
Ronald Cron5afb9042022-05-31 12:11:39 +02001217 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1218
Ronald Crondb5dfa12022-05-31 11:44:38 +02001219 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001220 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 {
Ronald Cron828aff62022-05-31 12:04:31 +02001222 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1224 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001225 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001227 /* If a client receives a second
1228 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1229 * was itself in response to a HelloRetryRequest), it MUST abort the
1230 * handshake with an "unexpected_message" alert.
1231 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001232 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001233 {
1234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1235 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1236 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1237 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1238 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001239 /*
1240 * Clients must abort the handshake with an "illegal_parameter"
1241 * alert if the HelloRetryRequest would not result in any change
1242 * in the ClientHello.
1243 * In a PSK only key exchange that what we expect.
1244 */
1245 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1246 {
1247 MBEDTLS_SSL_DEBUG_MSG( 1,
1248 ( "Unexpected HRR in pure PSK key exchange." ) );
1249 MBEDTLS_SSL_PEND_FATAL_ALERT(
1250 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1251 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1252 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1253 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001254
Ronald Cron5afb9042022-05-31 12:11:39 +02001255 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001256
Jerry Yu745bb612021-10-13 22:01:04 +08001257 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001258 }
1259
1260cleanup:
1261
1262 return( ret );
1263}
1264
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001266static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1267 const unsigned char **buf,
1268 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001269{
1270 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001271 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001272
Jerry Yude4fb2c2021-09-19 18:05:08 +08001273 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001274 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001275
Jerry Yu4a173382021-10-11 21:45:31 +08001276 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001277
1278 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001279 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1280 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001281 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001282 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1283 ssl->session_negotiate->id,
1284 ssl->session_negotiate->id_len );
1285 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001286 legacy_session_id_echo_len );
1287
1288 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1289 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1290
Jerry Yue1b9c292021-09-10 10:08:31 +08001291 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1292 }
1293
Jerry Yu4a173382021-10-11 21:45:31 +08001294 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001295 *buf = p;
1296
1297 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001298 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001299 return( 0 );
1300}
1301
XiaokangQianeb69aee2022-07-05 08:21:43 +00001302#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1303/*
1304 * struct {
1305 * opaque identity<1..2^16-1>;
1306 * uint32 obfuscated_ticket_age;
1307 * } PskIdentity;
1308 *
1309 * opaque PskBinderEntry<32..255>;
1310 *
1311 * struct {
XiaokangQian008d2bf2022-07-14 07:54:01 +00001312 *
XiaokangQian86981952022-07-19 09:51:50 +00001313 * select (Handshake.msg_type) {
1314 * ...
1315 * case server_hello: uint16 selected_identity;
1316 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +00001317 *
1318 * } PreSharedKeyExtension;
1319 *
1320 */
1321
XiaokangQian86981952022-07-19 09:51:50 +00001322MBEDTLS_CHECK_RETURN_CRITICAL
1323static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1324 const unsigned char *buf,
1325 const unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001326{
1327 int ret = 0;
1328 size_t selected_identity;
1329
Xiaokang Qian89399302022-09-21 07:16:22 +00001330 int psk_type;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001331 const unsigned char *psk;
1332 size_t psk_len;
1333 const unsigned char *psk_identity;
1334 size_t psk_identity_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001335
1336 /* Check which PSK we've offered.
1337 *
1338 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1339 * case, we need to iterate over them here.
1340 */
Jerry Yudb8c5fa2022-08-03 12:10:13 +08001341 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
1342 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001343 {
1344 /* If we haven't offered a PSK, the server must not send
1345 * a PSK identity extension. */
1346 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1347 }
1348
XiaokangQian008d2bf2022-07-14 07:54:01 +00001349 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001350 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1351
1352 /* We have offered only one PSK, so the only valid choice
1353 * for the server is PSK index 0.
1354 *
1355 * This will change once we support multiple PSKs. */
1356 if( selected_identity > 0 )
1357 {
1358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1359
1360 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1361 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1362 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1363 {
1364 return( ret );
1365 }
1366
1367 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1368 }
1369
1370 /* Set the chosen PSK
1371 *
1372 * TODO: We don't have to do this in case we offered 0-RTT and the
1373 * server accepted it, because in this case we've already
1374 * set the handshake PSK. */
1375 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1376 if( ret != 0 )
1377 {
1378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1379 return( ret );
1380 }
1381
1382 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1383 return( 0 );
1384}
1385
1386#endif
1387
Jerry Yue1b9c292021-09-10 10:08:31 +08001388/* Parse ServerHello message and configure context
1389 *
1390 * struct {
1391 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1392 * Random random;
1393 * opaque legacy_session_id_echo<0..32>;
1394 * CipherSuite cipher_suite;
1395 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001396 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001397 * } ServerHello;
1398 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001399MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001400static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1401 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001402 const unsigned char *end,
1403 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001404{
Jerry Yub85277e2021-10-13 13:36:05 +08001405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001406 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001407 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001408 size_t extensions_len;
1409 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001410 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001411 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001412 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001413
1414 /*
1415 * Check there is space for minimal fields
1416 *
1417 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001418 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001419 * - legacy_session_id_echo ( 1 byte ), minimum size
1420 * - cipher_suite ( 2 bytes)
1421 * - legacy_compression_method ( 1 byte )
1422 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001423 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001424
1425 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001426 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1427
Jerry Yu4a173382021-10-11 21:45:31 +08001428 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001429 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001430 * ...
1431 * with ProtocolVersion defined as:
1432 * uint16 ProtocolVersion;
1433 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001434 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1435 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001436 {
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1438 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1439 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001440 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1441 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001442 }
1443 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001444
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001445 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001446 * Random random;
1447 * ...
1448 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001449 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001450 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001451 if( !is_hrr )
1452 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001453 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001454 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1455 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1456 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1457 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001458 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001459
Jerry Yu4a173382021-10-11 21:45:31 +08001460 /* ...
1461 * opaque legacy_session_id_echo<0..32>;
1462 * ...
1463 */
1464 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001465 {
XiaokangQian52da5582022-01-26 09:49:29 +00001466 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001467 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001468 }
1469
Jerry Yu4a173382021-10-11 21:45:31 +08001470 /* ...
1471 * CipherSuite cipher_suite;
1472 * ...
1473 * with CipherSuite defined as:
1474 * uint8 CipherSuite[2];
1475 */
1476 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001477 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1478 p += 2;
1479
Jerry Yu4a173382021-10-11 21:45:31 +08001480
XiaokangQian355e09a2022-01-20 11:14:50 +00001481 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001482 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001483 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001484 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001485 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1486 ssl->tls_version,
1487 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001488 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001489 {
XiaokangQian52da5582022-01-26 09:49:29 +00001490 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001491 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001492 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001493 * If we received an HRR before and that the proposed selected
1494 * ciphersuite in this server hello is not the same as the one
1495 * proposed in the HRR, we abort the handshake and send an
1496 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001497 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001498 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1499 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001500 {
XiaokangQian52da5582022-01-26 09:49:29 +00001501 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001502 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001503
XiaokangQian52da5582022-01-26 09:49:29 +00001504 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001505 {
1506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1507 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001508 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001509 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001510
Jerry Yu4a173382021-10-11 21:45:31 +08001511 /* Configure ciphersuites */
1512 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1513
XiaokangQian355e09a2022-01-20 11:14:50 +00001514 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001515 ssl->session_negotiate->ciphersuite = cipher_suite;
1516
Jerry Yue1b9c292021-09-10 10:08:31 +08001517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1518 cipher_suite, ciphersuite_info->name ) );
1519
1520#if defined(MBEDTLS_HAVE_TIME)
1521 ssl->session_negotiate->start = time( NULL );
1522#endif /* MBEDTLS_HAVE_TIME */
1523
Jerry Yu4a173382021-10-11 21:45:31 +08001524 /* ...
1525 * uint8 legacy_compression_method = 0;
1526 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001527 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001528 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Thomas Daubney31e03a82022-07-25 15:59:25 +01001529 if( p[0] != MBEDTLS_SSL_COMPRESS_NULL )
Jerry Yue1b9c292021-09-10 10:08:31 +08001530 {
Jerry Yub85277e2021-10-13 13:36:05 +08001531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001532 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001533 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001534 }
1535 p++;
1536
Jerry Yub85277e2021-10-13 13:36:05 +08001537 /* ...
1538 * Extension extensions<6..2^16-1>;
1539 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001540 * struct {
1541 * ExtensionType extension_type; (2 bytes)
1542 * opaque extension_data<0..2^16-1>;
1543 * } Extension;
1544 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001546 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001547 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001548
Jerry Yu4a173382021-10-11 21:45:31 +08001549 /* Check extensions do not go beyond the buffer of data. */
1550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1551 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001552
Jerry Yu4a173382021-10-11 21:45:31 +08001553 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001554
Jerry Yu4a173382021-10-11 21:45:31 +08001555 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001556 {
1557 unsigned int extension_type;
1558 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001559 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001560
Jerry Yu4a173382021-10-11 21:45:31 +08001561 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001562 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1563 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1564 p += 4;
1565
Jerry Yu4a173382021-10-11 21:45:31 +08001566 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001567 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001568
1569 switch( extension_type )
1570 {
XiaokangQianb851da82022-01-14 04:03:11 +00001571 case MBEDTLS_TLS_EXT_COOKIE:
1572
XiaokangQiand9e068e2022-01-18 06:23:32 +00001573 if( !is_hrr )
1574 {
XiaokangQian52da5582022-01-26 09:49:29 +00001575 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001576 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001577 }
1578
XiaokangQian43550bd2022-01-21 04:32:58 +00001579 ret = ssl_tls13_parse_cookie_ext( ssl,
1580 p, extension_data_end );
1581 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001582 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001583 MBEDTLS_SSL_DEBUG_RET( 1,
1584 "ssl_tls13_parse_cookie_ext",
1585 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001586 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001587 }
XiaokangQianb851da82022-01-14 04:03:11 +00001588 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001589
Jerry Yue1b9c292021-09-10 10:08:31 +08001590 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001591 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001592 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001593 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001594 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001595 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001596 break;
1597
XiaokangQianeb69aee2022-07-05 08:21:43 +00001598#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001599 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1601 if( is_hrr )
1602 {
1603 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1604 goto cleanup;
1605 }
Jerry Yu4a173382021-10-11 21:45:31 +08001606
XiaokangQian86981952022-07-19 09:51:50 +00001607 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001608 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001609 {
1610 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001611 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001612 return( ret );
1613 }
1614 break;
1615#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001616
Jerry Yue1b9c292021-09-10 10:08:31 +08001617 case MBEDTLS_TLS_EXT_KEY_SHARE:
1618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001619 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1620 {
XiaokangQian52da5582022-01-26 09:49:29 +00001621 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001622 goto cleanup;
1623 }
1624
XiaokangQian53f20b72022-01-18 10:47:33 +00001625 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001626 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001627 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001628 else
XiaokangQianb851da82022-01-14 04:03:11 +00001629 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001630 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001631 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001632 {
1633 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001634 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001635 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001636 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001637 }
1638 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001639
1640 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001641 MBEDTLS_SSL_DEBUG_MSG(
1642 3,
1643 ( "unknown extension found: %u ( ignoring )",
1644 extension_type ) );
1645
XiaokangQian52da5582022-01-26 09:49:29 +00001646 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001647 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001648 }
1649
1650 p += extension_data_len;
1651 }
1652
XiaokangQiand59be772022-01-24 10:12:51 +00001653cleanup:
1654
XiaokangQian52da5582022-01-26 09:49:29 +00001655 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001656 {
1657 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1658 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1659 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1660 }
XiaokangQian52da5582022-01-26 09:49:29 +00001661 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001662 {
1663 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1664 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1665 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1666 }
1667 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001668}
1669
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001670#if defined(MBEDTLS_DEBUG_C)
1671static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001672{
1673 switch( mode )
1674 {
1675 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1676 return "psk";
1677 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1678 return "ephemeral";
1679 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1680 return "psk_ephemeral";
1681 default:
1682 return "unknown mode";
1683 }
1684}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001685#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001686
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001687MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001688static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001689{
Jerry Yub85277e2021-10-13 13:36:05 +08001690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001691 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001692
Jerry Yub85277e2021-10-13 13:36:05 +08001693 /* Determine the key exchange mode:
1694 * 1) If both the pre_shared_key and key_share extensions were received
1695 * then the key exchange mode is PSK with EPHEMERAL.
1696 * 2) If only the pre_shared_key extension was received then the key
1697 * exchange mode is PSK-only.
1698 * 3) If only the key_share extension was received then the key
1699 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001700 */
Jerry Yub85277e2021-10-13 13:36:05 +08001701 switch( handshake->extensions_present &
1702 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001703 {
Jerry Yu745bb612021-10-13 22:01:04 +08001704 /* Only the pre_shared_key extension was received */
1705 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001706 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001707 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001708
Jerry Yu745bb612021-10-13 22:01:04 +08001709 /* Only the key_share extension was received */
1710 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001711 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001712 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001713
Jerry Yu745bb612021-10-13 22:01:04 +08001714 /* Both the pre_shared_key and key_share extensions were received */
1715 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001716 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001717 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001718
Jerry Yu745bb612021-10-13 22:01:04 +08001719 /* Neither pre_shared_key nor key_share extension was received */
1720 default:
1721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1722 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1723 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001724 }
1725
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001726 if( !mbedtls_ssl_conf_tls13_check_kex_modes( ssl, handshake->key_exchange_mode ) )
1727 {
1728 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1729 MBEDTLS_SSL_DEBUG_MSG( 2,
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001730 ( "Key exchange mode(%s) is not configured supported.",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001731 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001732 goto cleanup;
1733 }
1734
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001735 MBEDTLS_SSL_DEBUG_MSG( 3,
1736 ( "Server selected key exchange mode: %s",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001737 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001738
Jerry Yu0b177842021-09-05 19:41:30 +08001739 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1740 *
1741 * TODO: We don't have to do this in case we offered 0-RTT and the
1742 * server accepted it. In this case, we could skip generating
1743 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001744 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001745 if( ret != 0 )
1746 {
Jerry Yue110d252022-05-05 10:19:22 +08001747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001748 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001749 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001750 }
1751
Jerry Yuf86eb752022-05-06 11:16:55 +08001752 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001753 if( ret != 0 )
1754 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001755 MBEDTLS_SSL_DEBUG_RET( 1,
1756 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001757 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001758 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001759 }
1760
Jerry Yue110d252022-05-05 10:19:22 +08001761 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1763 ssl->session_in = ssl->session_negotiate;
1764
Jerry Yu4a173382021-10-11 21:45:31 +08001765cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001766 if( ret != 0 )
1767 {
Jerry Yu4a173382021-10-11 21:45:31 +08001768 MBEDTLS_SSL_PEND_FATAL_ALERT(
1769 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1770 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1771 }
Jerry Yue110d252022-05-05 10:19:22 +08001772
Jerry Yu4a173382021-10-11 21:45:31 +08001773 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001774}
1775
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001776MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001777static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001778{
1779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1780
XiaokangQian78b1fa72022-01-19 06:56:30 +00001781 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001782
XiaokangQian78b1fa72022-01-19 06:56:30 +00001783 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001784 * We are going to re-generate a shared secret corresponding to the group
1785 * selected by the server, which is different from the group for which we
1786 * generated a shared secret in the first client hello.
1787 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001788 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001789 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001790 if( ret != 0 )
1791 return( ret );
1792
1793 return( 0 );
1794}
1795
Jerry Yue1b9c292021-09-10 10:08:31 +08001796/*
Jerry Yu4a173382021-10-11 21:45:31 +08001797 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001798 * Handler for MBEDTLS_SSL_SERVER_HELLO
1799 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001800MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001801static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001802{
Jerry Yu4a173382021-10-11 21:45:31 +08001803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001804 unsigned char *buf = NULL;
1805 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001806 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001807
1808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1809
Ronald Crondb5dfa12022-05-31 11:44:38 +02001810 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1811 MBEDTLS_SSL_HS_SERVER_HELLO,
1812 &buf, &buf_len ) );
1813
Ronald Cron828aff62022-05-31 12:04:31 +02001814 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001815 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001816 goto cleanup;
1817 else
Ronald Cron828aff62022-05-31 12:04:31 +02001818 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001819
Ronald Cron828aff62022-05-31 12:04:31 +02001820 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001821 {
1822 ret = 0;
1823 goto cleanup;
1824 }
1825
XiaokangQianb851da82022-01-14 04:03:11 +00001826 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001827 buf + buf_len,
1828 is_hrr ) );
1829 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001830 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1831
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001832 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1833 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001834
XiaokangQiand9e068e2022-01-18 06:23:32 +00001835 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001836 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001837 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001838#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1839 /* If not offering early data, the client sends a dummy CCS record
1840 * immediately before its second flight. This may either be before
1841 * its second ClientHello or before its encrypted handshake flight.
1842 */
1843 mbedtls_ssl_handshake_set_state( ssl,
1844 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1845#else
1846 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1847#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1848 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001849 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001850 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001851 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001852 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1853 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001854
1855cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1857 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001858 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001859}
1860
1861/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001862 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001863 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001864 *
1865 * The EncryptedExtensions message contains any extensions which
1866 * should be protected, i.e., any which are not needed to establish
1867 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001868 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001869
XiaokangQian08da26c2021-10-09 10:12:11 +00001870/* Parse EncryptedExtensions message
1871 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001872 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001873 * } EncryptedExtensions;
1874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001875MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001876static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1877 const unsigned char *buf,
1878 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001879{
1880 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001881 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001882 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001883 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001884
XiaokangQian08da26c2021-10-09 10:12:11 +00001885 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001886 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001887 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001888
XiaokangQian97799ac2021-10-11 10:05:54 +00001889 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001890 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001891 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001892
XiaokangQian8db25ff2021-10-13 05:56:18 +00001893 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001894 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001895 unsigned int extension_type;
1896 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001897
XiaokangQian08da26c2021-10-09 10:12:11 +00001898 /*
1899 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001900 * ExtensionType extension_type; (2 bytes)
1901 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001902 * } Extension;
1903 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001904 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001905 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1906 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1907 p += 4;
1908
XiaokangQian8db25ff2021-10-13 05:56:18 +00001909 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001910
XiaokangQian97799ac2021-10-11 10:05:54 +00001911 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001912 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001913 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001914 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001915 switch( extension_type )
1916 {
Jerry Yu661dd942022-08-03 14:50:01 +08001917 case MBEDTLS_TLS_EXT_SERVERNAME:
1918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found server_name extension" ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001919
Jerry Yu661dd942022-08-03 14:50:01 +08001920 /* The server_name extension should be an empty extension */
1921
1922 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00001923 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1925 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001926
lhuang0486cacac2022-01-21 07:34:27 -08001927#if defined(MBEDTLS_SSL_ALPN)
1928 case MBEDTLS_TLS_EXT_ALPN:
1929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1930
1931 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1932 {
1933 return( ret );
1934 }
1935
1936 break;
1937#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001938 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001939 MBEDTLS_SSL_DEBUG_MSG(
1940 3, ( "unsupported extension found: %u ", extension_type) );
1941 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001942 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001943 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1944 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001945 }
1946
XiaokangQian08da26c2021-10-09 10:12:11 +00001947 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001948 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001949
XiaokangQian97799ac2021-10-11 10:05:54 +00001950 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001951 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001952 {
1953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001954 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001955 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001956 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001957 }
1958
1959 return( ret );
1960}
1961
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001962MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001963static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1964{
1965 int ret;
1966 unsigned char *buf;
1967 size_t buf_len;
1968
1969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1970
1971 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1972 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1973 &buf, &buf_len ) );
1974
1975 /* Process the message contents */
1976 MBEDTLS_SSL_PROC_CHK(
1977 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1978
1979 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1980 buf, buf_len );
1981
Ronald Cronfb508b82022-05-31 14:49:55 +02001982#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001983 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001984 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1985 else
1986 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1987#else
1988 ((void) ssl);
1989 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1990#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001991
1992cleanup:
1993
1994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1995 return( ret );
1996
1997}
1998
Jerry Yua93ac112021-10-27 16:31:48 +08001999#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08002000/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002001 * STATE HANDLING: CertificateRequest
2002 *
Jerry Yud2674312021-10-29 10:08:19 +08002003 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002004#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2005#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002006/* Coordination:
2007 * Deals with the ambiguity of not knowing if a CertificateRequest
2008 * will be sent. Returns a negative code on failure, or
2009 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2010 * - SSL_CERTIFICATE_REQUEST_SKIP
2011 * indicating if a Certificate Request is expected or not.
2012 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002013MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002014static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08002015{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08002017
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002018 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08002019 {
2020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2021 return( ret );
2022 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002023 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002024
2025 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
2026 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
2027 {
Ronald Cron19385882022-06-15 16:26:13 +02002028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002029 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08002030 }
2031
Ronald Cron19385882022-06-15 16:26:13 +02002032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
2033
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002034 return( SSL_CERTIFICATE_REQUEST_SKIP );
2035}
2036
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002037/*
2038 * ssl_tls13_parse_certificate_request()
2039 * Parse certificate request
2040 * struct {
2041 * opaque certificate_request_context<0..2^8-1>;
2042 * Extension extensions<2..2^16-1>;
2043 * } CertificateRequest;
2044 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002045MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002046static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
2047 const unsigned char *buf,
2048 const unsigned char *end )
2049{
2050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2051 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002052 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002053 size_t extensions_len = 0;
2054 const unsigned char *extensions_end;
2055 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002056
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002057 /* ...
2058 * opaque certificate_request_context<0..2^8-1>
2059 * ...
2060 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002061 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
2062 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002063 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002064
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002065 if( certificate_request_context_len > 0 )
2066 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002067 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002068 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
2069 p, certificate_request_context_len );
2070
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002071 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002072 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002073 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002074 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002075 {
2076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2077 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2078 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002079 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002080 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002081 p += certificate_request_context_len;
2082 }
2083
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002084 /* ...
2085 * Extension extensions<2..2^16-1>;
2086 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002087 */
2088 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2089 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2090 p += 2;
2091
2092 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2093 extensions_end = p + extensions_len;
2094
2095 while( p < extensions_end )
2096 {
2097 unsigned int extension_type;
2098 size_t extension_data_len;
2099
2100 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2101 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2102 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2103 p += 4;
2104
2105 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2106
2107 switch( extension_type )
2108 {
2109 case MBEDTLS_TLS_EXT_SIG_ALG:
2110 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002111 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002112 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002113 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002114 if( ret != 0 )
2115 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002116 if( ! sig_alg_ext_found )
2117 sig_alg_ext_found = 1;
2118 else
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 3,
2121 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002122 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002123 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002124 break;
2125
2126 default:
2127 MBEDTLS_SSL_DEBUG_MSG(
2128 3,
2129 ( "unknown extension found: %u ( ignoring )",
2130 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002131 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002132 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002133 p += extension_data_len;
2134 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002135 /* Check that we consumed all the message. */
2136 if( p != end )
2137 {
2138 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002139 ( "CertificateRequest misaligned" ) );
2140 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002141 }
2142 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002143 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002144 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002145 MBEDTLS_SSL_DEBUG_MSG( 3,
2146 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002147 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002148 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002149
Jerry Yu7840f812022-01-29 10:26:51 +08002150 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002151 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002152
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002153decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002154 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2155 MBEDTLS_ERR_SSL_DECODE_ERROR );
2156 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002157}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002158
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002159/*
2160 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2161 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002162MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002163static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002164{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002166
2167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2168
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002169 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2170
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002171 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2172 {
2173 unsigned char *buf;
2174 size_t buf_len;
2175
2176 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2177 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2178 &buf, &buf_len ) );
2179
2180 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2181 buf, buf + buf_len ) );
2182
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002183 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2184 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002185 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002186 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002187 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002188 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002189 }
2190 else
2191 {
2192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002193 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2194 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002195 }
2196
Jerry Yud2674312021-10-29 10:08:19 +08002197 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2198
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002199cleanup:
2200
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2202 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002203}
2204
2205/*
Jerry Yu687101b2021-09-14 16:03:56 +08002206 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2207 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002208MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002209static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002210{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002211 int ret;
2212
2213 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002214 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002215 return( ret );
2216
Jerry Yu687101b2021-09-14 16:03:56 +08002217 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2218 return( 0 );
2219}
2220
2221/*
2222 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2223 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002224MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002225static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002226{
Jerry Yu30b071c2021-09-12 20:16:03 +08002227 int ret;
2228
2229 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2230 if( ret != 0 )
2231 return( ret );
2232
Jerry Yu687101b2021-09-14 16:03:56 +08002233 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2234 return( 0 );
2235}
Jerry Yua93ac112021-10-27 16:31:48 +08002236#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002237
Jerry Yu687101b2021-09-14 16:03:56 +08002238/*
2239 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2240 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002241MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002242static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002243{
XiaokangQianac0385c2021-11-03 06:40:11 +00002244 int ret;
2245
XiaokangQianc5c39d52021-11-09 11:55:10 +00002246 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002247 if( ret != 0 )
2248 return( ret );
2249
Jerry Yue3d67cb2022-05-19 15:33:10 +08002250 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2251 if( ret != 0 )
2252 {
2253 MBEDTLS_SSL_PEND_FATAL_ALERT(
2254 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2255 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2256 return( ret );
2257 }
2258
Ronald Cron49ad6192021-11-24 16:25:31 +01002259#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2260 mbedtls_ssl_handshake_set_state(
2261 ssl,
2262 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2263#else
Jerry Yuca133a32022-02-15 14:22:05 +08002264 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002265#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002266
XiaokangQianac0385c2021-11-03 06:40:11 +00002267 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002268}
2269
2270/*
Jerry Yu566c7812022-01-26 15:41:22 +08002271 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2272 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002274static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2275{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002276 int non_empty_certificate_msg = 0;
2277
Jerry Yu5cc35062022-01-28 16:16:08 +08002278 MBEDTLS_SSL_DEBUG_MSG( 1,
2279 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002280 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002281
Ronald Cron9df7c802022-03-08 18:38:54 +01002282#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002283 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002284 {
2285 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2286 if( ret != 0 )
2287 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002288
Ronald Cron7a94aca2022-03-09 07:44:27 +01002289 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2290 non_empty_certificate_msg = 1;
2291 }
2292 else
2293 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002295 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002296#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002297
Ronald Cron7a94aca2022-03-09 07:44:27 +01002298 if( non_empty_certificate_msg )
2299 {
2300 mbedtls_ssl_handshake_set_state( ssl,
2301 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2302 }
2303 else
Ronald Cron19385882022-06-15 16:26:13 +02002304 {
2305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002306 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002307 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002308
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002309 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002310}
2311
Ronald Cron9df7c802022-03-08 18:38:54 +01002312#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002313/*
2314 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2315 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002316MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002317static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2318{
Ronald Crona8b38872022-03-09 07:59:25 +01002319 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2320
2321 if( ret == 0 )
2322 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2323
2324 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002325}
Jerry Yu90f152d2022-01-29 22:12:42 +08002326#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002327
2328/*
Jerry Yu687101b2021-09-14 16:03:56 +08002329 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2330 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002331MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002332static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002333{
XiaokangQian0fa66432021-11-15 03:33:57 +00002334 int ret;
2335
2336 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2337 if( ret != 0 )
2338 return( ret );
2339
Jerry Yu466dda82022-09-13 11:20:20 +08002340 ret = mbedtls_ssl_tls13_compute_resumption_master_secret( ssl );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002341 if( ret != 0 )
2342 {
2343 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu466dda82022-09-13 11:20:20 +08002344 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002345 return ( ret );
2346 }
2347
XiaokangQian0fa66432021-11-15 03:33:57 +00002348 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2349 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002350}
2351
2352/*
2353 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2354 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002355MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002356static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002357{
Jerry Yu378254d2021-10-30 21:44:47 +08002358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002359 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002360 return( 0 );
2361}
2362
2363/*
2364 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2365 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002366MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002367static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002368{
Jerry Yu378254d2021-10-30 21:44:47 +08002369
2370 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2371
2372 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2373 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002374}
2375
Jerry Yuf8a49942022-07-07 11:32:32 +00002376#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2377
Jerry Yua0446a02022-07-13 11:22:55 +08002378MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002379static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2380 const unsigned char *buf,
2381 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002382{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002383 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002384
2385 ((void) ssl);
2386
2387 while( p < end )
2388 {
2389 unsigned int extension_type;
2390 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002391
Jerry Yuf8a49942022-07-07 11:32:32 +00002392 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2393 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2394 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2395 p += 4;
2396
2397 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002398
2399 switch( extension_type )
2400 {
2401 case MBEDTLS_TLS_EXT_EARLY_DATA:
2402 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2403 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002404
Jerry Yuf8a49942022-07-07 11:32:32 +00002405 default:
2406 break;
2407 }
2408 p += extension_data_len;
2409 }
2410
2411 return( 0 );
2412}
2413
2414/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002415 * From RFC8446, page 74
2416 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002417 * struct {
2418 * uint32 ticket_lifetime;
2419 * uint32 ticket_age_add;
2420 * opaque ticket_nonce<0..255>;
2421 * opaque ticket<1..2^16-1>;
2422 * Extension extensions<0..2^16-2>;
2423 * } NewSessionTicket;
2424 *
2425 */
Jerry Yua0446a02022-07-13 11:22:55 +08002426MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002427static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2428 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002429 unsigned char *end,
2430 unsigned char **ticket_nonce,
2431 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002432{
2433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2434 unsigned char *p = buf;
2435 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002436 size_t ticket_len;
2437 unsigned char *ticket;
2438 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002439
Jerry Yucb3b1392022-07-12 06:09:38 +00002440 *ticket_nonce = NULL;
2441 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002442 /*
2443 * ticket_lifetime 4 bytes
2444 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002445 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002446 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002447 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002448
2449 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002450 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002451 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002452 ( unsigned int )session->ticket_lifetime ) );
2453
Jerry Yucb3b1392022-07-12 06:09:38 +00002454 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002455 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002456 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002457 ( unsigned int )session->ticket_age_add ) );
2458
Jerry Yucb3b1392022-07-12 06:09:38 +00002459 *ticket_nonce_len = p[8];
2460 p += 9;
2461
2462 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2463 *ticket_nonce = p;
2464 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2465 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002466
2467 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002468 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002469 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2470 p += 2;
2471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002472 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2473
2474 /* Check if we previously received a ticket already. */
2475 if( session->ticket != NULL || session->ticket_len > 0 )
2476 {
2477 mbedtls_free( session->ticket );
2478 session->ticket = NULL;
2479 session->ticket_len = 0;
2480 }
2481
2482 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2483 {
2484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2486 }
2487 memcpy( ticket, p, ticket_len );
2488 p += ticket_len;
2489 session->ticket = ticket;
2490 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002491
Jerry Yucb3b1392022-07-12 06:09:38 +00002492 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002493 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2494 p += 2;
2495 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2496
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002497 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002498
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002499 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002500 if( ret != 0 )
2501 {
2502 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002503 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002504 ret );
2505 return( ret );
2506 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002507
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002508 /* session has been updated, allow export */
2509 session->exported = 0;
2510
Jerry Yucb3b1392022-07-12 06:09:38 +00002511 return( 0 );
2512}
2513
Jerry Yua0446a02022-07-13 11:22:55 +08002514MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002515static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2516 unsigned char *ticket_nonce,
2517 size_t ticket_nonce_len )
2518{
2519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2520 mbedtls_ssl_session *session = ssl->session;
2521 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2522 psa_algorithm_t psa_hash_alg;
2523 int hash_length;
2524
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002525#if defined(MBEDTLS_HAVE_TIME)
2526 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002527 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002528#endif
2529
Jerry Yuf8a49942022-07-07 11:32:32 +00002530 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2531 if( ciphersuite_info == NULL )
2532 {
2533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2535 }
2536
2537 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2538 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002539 if( hash_length == -1 ||
2540 ( size_t )hash_length > sizeof( session->resumption_key ) )
2541 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002543 }
2544
Jerry Yuf8a49942022-07-07 11:32:32 +00002545
2546 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2547 session->app_secrets.resumption_master_secret,
2548 hash_length );
2549
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002550 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002551 *
2552 * HKDF-Expand-Label( resumption_master_secret,
2553 * "resumption", ticket_nonce, Hash.length )
2554 */
2555 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2556 psa_hash_alg,
2557 session->app_secrets.resumption_master_secret,
2558 hash_length,
2559 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2560 ticket_nonce,
2561 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002562 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002563 hash_length );
2564
2565 if( ret != 0 )
2566 {
2567 MBEDTLS_SSL_DEBUG_RET( 2,
2568 "Creating the ticket-resumed PSK failed",
2569 ret );
2570 return( ret );
2571 }
2572
Jerry Yu0a430c82022-07-20 11:02:48 +08002573 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002574
2575 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002576 session->resumption_key,
2577 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002578
Jerry Yuf8a49942022-07-07 11:32:32 +00002579 return( 0 );
2580}
2581
2582/*
Jerry Yua357cf42022-07-12 05:36:45 +00002583 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002584 */
Jerry Yua0446a02022-07-13 11:22:55 +08002585MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002586static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2587{
2588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2589 unsigned char *buf;
2590 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002591 unsigned char *ticket_nonce;
2592 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002593
2594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2595
2596 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2597 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2598 &buf, &buf_len ) );
2599
Jerry Yucb3b1392022-07-12 06:09:38 +00002600 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2601 ssl, buf, buf + buf_len,
2602 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002603
Jerry Yucb3b1392022-07-12 06:09:38 +00002604 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2605 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002606
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002607 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2608
Jerry Yuf8a49942022-07-07 11:32:32 +00002609cleanup:
2610
2611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2612 return( ret );
2613}
2614#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2615
Jerry Yu92c6b402021-08-27 16:59:09 +08002616int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002617{
Jerry Yu92c6b402021-08-27 16:59:09 +08002618 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002619
Jerry Yu92c6b402021-08-27 16:59:09 +08002620 switch( ssl->state )
2621 {
2622 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002623 * ssl->state is initialized as HELLO_REQUEST. It is the same
2624 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002625 */
2626 case MBEDTLS_SSL_HELLO_REQUEST:
2627 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002628 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002629 break;
2630
2631 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002632 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002633 break;
2634
2635 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002636 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002637 break;
2638
Jerry Yua93ac112021-10-27 16:31:48 +08002639#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002640 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2641 ret = ssl_tls13_process_certificate_request( ssl );
2642 break;
2643
Jerry Yu687101b2021-09-14 16:03:56 +08002644 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002645 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002646 break;
2647
2648 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002649 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002650 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002651#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002652
2653 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002654 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002655 break;
2656
Jerry Yu566c7812022-01-26 15:41:22 +08002657 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2658 ret = ssl_tls13_write_client_certificate( ssl );
2659 break;
2660
Ronald Cron9df7c802022-03-08 18:38:54 +01002661#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002662 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2663 ret = ssl_tls13_write_client_certificate_verify( ssl );
2664 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002665#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002666
Jerry Yu687101b2021-09-14 16:03:56 +08002667 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002668 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002669 break;
2670
2671 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002672 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002673 break;
2674
2675 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002676 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002677 break;
2678
Ronald Cron49ad6192021-11-24 16:25:31 +01002679 /*
2680 * Injection of dummy-CCS's for middlebox compatibility
2681 */
2682#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002683 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002684 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2685 if( ret == 0 )
2686 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2687 break;
2688
2689 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2690 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2691 if( ret == 0 )
2692 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002693 break;
2694#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2695
Jerry Yuf8a49942022-07-07 11:32:32 +00002696#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002697 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002698 ret = ssl_tls13_process_new_session_ticket( ssl );
2699 if( ret != 0 )
2700 break;
2701 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2702 break;
2703#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2704
Jerry Yu92c6b402021-08-27 16:59:09 +08002705 default:
2706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2708 }
2709
2710 return( ret );
2711}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002712
Jerry Yufb4b6472022-01-27 15:03:26 +08002713#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002714
Jerry Yufb4b6472022-01-27 15:03:26 +08002715