blob: 4571ada0deede47aaf6975747a42af6483278d9c [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;
XiaokangQian335cfaa2022-08-11 03:18:07 +00001329 int psk_type;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001330
1331 const unsigned char *psk;
1332 size_t psk_len;
1333 const unsigned char *psk_identity;
1334 size_t psk_identity_len;
Jerry Yudb8c5fa2022-08-03 12:10:13 +08001335 int psk_type;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001336
1337 /* Check which PSK we've offered.
1338 *
1339 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1340 * case, we need to iterate over them here.
1341 */
Jerry Yudb8c5fa2022-08-03 12:10:13 +08001342 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
1343 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001344 {
1345 /* If we haven't offered a PSK, the server must not send
1346 * a PSK identity extension. */
1347 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1348 }
1349
XiaokangQian008d2bf2022-07-14 07:54:01 +00001350 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001351 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1352
1353 /* We have offered only one PSK, so the only valid choice
1354 * for the server is PSK index 0.
1355 *
1356 * This will change once we support multiple PSKs. */
1357 if( selected_identity > 0 )
1358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1360
1361 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1362 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1363 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1364 {
1365 return( ret );
1366 }
1367
1368 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1369 }
1370
1371 /* Set the chosen PSK
1372 *
1373 * TODO: We don't have to do this in case we offered 0-RTT and the
1374 * server accepted it, because in this case we've already
1375 * set the handshake PSK. */
1376 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1377 if( ret != 0 )
1378 {
1379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1380 return( ret );
1381 }
1382
1383 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1384 return( 0 );
1385}
1386
1387#endif
1388
Jerry Yue1b9c292021-09-10 10:08:31 +08001389/* Parse ServerHello message and configure context
1390 *
1391 * struct {
1392 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1393 * Random random;
1394 * opaque legacy_session_id_echo<0..32>;
1395 * CipherSuite cipher_suite;
1396 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001397 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001398 * } ServerHello;
1399 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001400MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001401static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1402 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001403 const unsigned char *end,
1404 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001405{
Jerry Yub85277e2021-10-13 13:36:05 +08001406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001407 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001408 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001409 size_t extensions_len;
1410 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001411 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001412 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001413 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001414
1415 /*
1416 * Check there is space for minimal fields
1417 *
1418 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001419 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001420 * - legacy_session_id_echo ( 1 byte ), minimum size
1421 * - cipher_suite ( 2 bytes)
1422 * - legacy_compression_method ( 1 byte )
1423 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001424 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001425
1426 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001427 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1428
Jerry Yu4a173382021-10-11 21:45:31 +08001429 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001430 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001431 * ...
1432 * with ProtocolVersion defined as:
1433 * uint16 ProtocolVersion;
1434 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001435 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1436 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001437 {
1438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1439 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1440 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001441 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1442 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001443 }
1444 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001445
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001446 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001447 * Random random;
1448 * ...
1449 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001450 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001451 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001452 if( !is_hrr )
1453 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001454 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001455 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1456 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1457 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1458 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001459 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001460
Jerry Yu4a173382021-10-11 21:45:31 +08001461 /* ...
1462 * opaque legacy_session_id_echo<0..32>;
1463 * ...
1464 */
1465 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001466 {
XiaokangQian52da5582022-01-26 09:49:29 +00001467 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001468 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001469 }
1470
Jerry Yu4a173382021-10-11 21:45:31 +08001471 /* ...
1472 * CipherSuite cipher_suite;
1473 * ...
1474 * with CipherSuite defined as:
1475 * uint8 CipherSuite[2];
1476 */
1477 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001478 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1479 p += 2;
1480
Jerry Yu4a173382021-10-11 21:45:31 +08001481
XiaokangQian355e09a2022-01-20 11:14:50 +00001482 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001483 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001484 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001485 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001486 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1487 ssl->tls_version,
1488 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001489 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001490 {
XiaokangQian52da5582022-01-26 09:49:29 +00001491 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001492 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001493 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001494 * If we received an HRR before and that the proposed selected
1495 * ciphersuite in this server hello is not the same as the one
1496 * proposed in the HRR, we abort the handshake and send an
1497 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001498 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001499 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1500 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001501 {
XiaokangQian52da5582022-01-26 09:49:29 +00001502 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001503 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001504
XiaokangQian52da5582022-01-26 09:49:29 +00001505 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001506 {
1507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1508 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001509 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001510 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001511
Jerry Yu4a173382021-10-11 21:45:31 +08001512 /* Configure ciphersuites */
1513 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1514
XiaokangQian355e09a2022-01-20 11:14:50 +00001515 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001516 ssl->session_negotiate->ciphersuite = cipher_suite;
1517
Jerry Yue1b9c292021-09-10 10:08:31 +08001518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1519 cipher_suite, ciphersuite_info->name ) );
1520
1521#if defined(MBEDTLS_HAVE_TIME)
1522 ssl->session_negotiate->start = time( NULL );
1523#endif /* MBEDTLS_HAVE_TIME */
1524
Jerry Yu4a173382021-10-11 21:45:31 +08001525 /* ...
1526 * uint8 legacy_compression_method = 0;
1527 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001528 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001529 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Thomas Daubney31e03a82022-07-25 15:59:25 +01001530 if( p[0] != MBEDTLS_SSL_COMPRESS_NULL )
Jerry Yue1b9c292021-09-10 10:08:31 +08001531 {
Jerry Yub85277e2021-10-13 13:36:05 +08001532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001533 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001534 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001535 }
1536 p++;
1537
Jerry Yub85277e2021-10-13 13:36:05 +08001538 /* ...
1539 * Extension extensions<6..2^16-1>;
1540 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001541 * struct {
1542 * ExtensionType extension_type; (2 bytes)
1543 * opaque extension_data<0..2^16-1>;
1544 * } Extension;
1545 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001546 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001547 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001548 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001549
Jerry Yu4a173382021-10-11 21:45:31 +08001550 /* Check extensions do not go beyond the buffer of data. */
1551 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1552 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001553
Jerry Yu4a173382021-10-11 21:45:31 +08001554 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001555
Jerry Yu4a173382021-10-11 21:45:31 +08001556 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001557 {
1558 unsigned int extension_type;
1559 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001560 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001561
Jerry Yu4a173382021-10-11 21:45:31 +08001562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001563 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1564 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1565 p += 4;
1566
Jerry Yu4a173382021-10-11 21:45:31 +08001567 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001568 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001569
1570 switch( extension_type )
1571 {
XiaokangQianb851da82022-01-14 04:03:11 +00001572 case MBEDTLS_TLS_EXT_COOKIE:
1573
XiaokangQiand9e068e2022-01-18 06:23:32 +00001574 if( !is_hrr )
1575 {
XiaokangQian52da5582022-01-26 09:49:29 +00001576 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001577 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001578 }
1579
XiaokangQian43550bd2022-01-21 04:32:58 +00001580 ret = ssl_tls13_parse_cookie_ext( ssl,
1581 p, extension_data_end );
1582 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001583 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001584 MBEDTLS_SSL_DEBUG_RET( 1,
1585 "ssl_tls13_parse_cookie_ext",
1586 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001587 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001588 }
XiaokangQianb851da82022-01-14 04:03:11 +00001589 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001590
Jerry Yue1b9c292021-09-10 10:08:31 +08001591 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001592 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001593 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001594 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001595 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001596 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001597 break;
1598
XiaokangQianeb69aee2022-07-05 08:21:43 +00001599#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001600 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1602 if( is_hrr )
1603 {
1604 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1605 goto cleanup;
1606 }
Jerry Yu4a173382021-10-11 21:45:31 +08001607
XiaokangQian86981952022-07-19 09:51:50 +00001608 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001609 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001610 {
1611 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001612 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001613 return( ret );
1614 }
1615 break;
1616#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001617
Jerry Yue1b9c292021-09-10 10:08:31 +08001618 case MBEDTLS_TLS_EXT_KEY_SHARE:
1619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001620 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1621 {
XiaokangQian52da5582022-01-26 09:49:29 +00001622 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001623 goto cleanup;
1624 }
1625
XiaokangQian53f20b72022-01-18 10:47:33 +00001626 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001627 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001628 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001629 else
XiaokangQianb851da82022-01-14 04:03:11 +00001630 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001631 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001632 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001633 {
1634 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001635 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001636 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001637 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001638 }
1639 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001640
1641 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001642 MBEDTLS_SSL_DEBUG_MSG(
1643 3,
1644 ( "unknown extension found: %u ( ignoring )",
1645 extension_type ) );
1646
XiaokangQian52da5582022-01-26 09:49:29 +00001647 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001648 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001649 }
1650
1651 p += extension_data_len;
1652 }
1653
XiaokangQiand59be772022-01-24 10:12:51 +00001654cleanup:
1655
XiaokangQian52da5582022-01-26 09:49:29 +00001656 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001657 {
1658 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1659 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1660 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1661 }
XiaokangQian52da5582022-01-26 09:49:29 +00001662 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001663 {
1664 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1665 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1666 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1667 }
1668 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001669}
1670
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001671MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001672static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001673{
Jerry Yub85277e2021-10-13 13:36:05 +08001674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001675 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001676
Jerry Yub85277e2021-10-13 13:36:05 +08001677 /* Determine the key exchange mode:
1678 * 1) If both the pre_shared_key and key_share extensions were received
1679 * then the key exchange mode is PSK with EPHEMERAL.
1680 * 2) If only the pre_shared_key extension was received then the key
1681 * exchange mode is PSK-only.
1682 * 3) If only the key_share extension was received then the key
1683 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001684 */
Jerry Yub85277e2021-10-13 13:36:05 +08001685 switch( handshake->extensions_present &
1686 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001687 {
Jerry Yu745bb612021-10-13 22:01:04 +08001688 /* Only the pre_shared_key extension was received */
1689 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001690 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yue9764922022-08-03 14:34:24 +08001691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
Jerry Yu745bb612021-10-13 22:01:04 +08001692 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001693
Jerry Yu745bb612021-10-13 22:01:04 +08001694 /* Only the key_share extension was received */
1695 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001696 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yue9764922022-08-03 14:34:24 +08001697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
Jerry Yu745bb612021-10-13 22:01:04 +08001698 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001699
Jerry Yu745bb612021-10-13 22:01:04 +08001700 /* Both the pre_shared_key and key_share extensions were received */
1701 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001702 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yue9764922022-08-03 14:34:24 +08001703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
Jerry Yu745bb612021-10-13 22:01:04 +08001704 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001705
Jerry Yu745bb612021-10-13 22:01:04 +08001706 /* Neither pre_shared_key nor key_share extension was received */
1707 default:
1708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1709 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1710 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001711 }
1712
Xiaokang Qian5001bfc2022-09-07 09:30:03 +00001713 MBEDTLS_SSL_DEBUG_MSG( 3,
1714 ( "Server selected key exchange mode: %s",
1715 handshake->key_exchange_mode == 1 ? "psk" :
1716 (handshake->key_exchange_mode == 2 ? "ephemeral" :
1717 "psk_ephemeral")) );
1718
Jerry Yu0b177842021-09-05 19:41:30 +08001719 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1720 *
1721 * TODO: We don't have to do this in case we offered 0-RTT and the
1722 * server accepted it. In this case, we could skip generating
1723 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001724 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001725 if( ret != 0 )
1726 {
Jerry Yue110d252022-05-05 10:19:22 +08001727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001728 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001729 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001730 }
1731
Jerry Yuf86eb752022-05-06 11:16:55 +08001732 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001733 if( ret != 0 )
1734 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001735 MBEDTLS_SSL_DEBUG_RET( 1,
1736 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001737 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001738 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001739 }
1740
Jerry Yue110d252022-05-05 10:19:22 +08001741 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1743 ssl->session_in = ssl->session_negotiate;
1744
Jerry Yu4a173382021-10-11 21:45:31 +08001745cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001746 if( ret != 0 )
1747 {
Jerry Yu4a173382021-10-11 21:45:31 +08001748 MBEDTLS_SSL_PEND_FATAL_ALERT(
1749 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1750 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1751 }
Jerry Yue110d252022-05-05 10:19:22 +08001752
Jerry Yu4a173382021-10-11 21:45:31 +08001753 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001754}
1755
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001756MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001757static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001758{
1759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1760
XiaokangQian78b1fa72022-01-19 06:56:30 +00001761 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001762
XiaokangQian78b1fa72022-01-19 06:56:30 +00001763 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001764 * We are going to re-generate a shared secret corresponding to the group
1765 * selected by the server, which is different from the group for which we
1766 * generated a shared secret in the first client hello.
1767 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001768 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001769 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001770 if( ret != 0 )
1771 return( ret );
1772
1773 return( 0 );
1774}
1775
Jerry Yue1b9c292021-09-10 10:08:31 +08001776/*
Jerry Yu4a173382021-10-11 21:45:31 +08001777 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001778 * Handler for MBEDTLS_SSL_SERVER_HELLO
1779 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001780MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001781static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001782{
Jerry Yu4a173382021-10-11 21:45:31 +08001783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001784 unsigned char *buf = NULL;
1785 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001786 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001787
1788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1789
Ronald Crondb5dfa12022-05-31 11:44:38 +02001790 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1791 MBEDTLS_SSL_HS_SERVER_HELLO,
1792 &buf, &buf_len ) );
1793
Ronald Cron828aff62022-05-31 12:04:31 +02001794 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001795 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001796 goto cleanup;
1797 else
Ronald Cron828aff62022-05-31 12:04:31 +02001798 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001799
Ronald Cron828aff62022-05-31 12:04:31 +02001800 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001801 {
1802 ret = 0;
1803 goto cleanup;
1804 }
1805
XiaokangQianb851da82022-01-14 04:03:11 +00001806 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001807 buf + buf_len,
1808 is_hrr ) );
1809 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001810 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1811
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001812 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1813 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001814
XiaokangQiand9e068e2022-01-18 06:23:32 +00001815 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001816 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001817 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001818#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1819 /* If not offering early data, the client sends a dummy CCS record
1820 * immediately before its second flight. This may either be before
1821 * its second ClientHello or before its encrypted handshake flight.
1822 */
1823 mbedtls_ssl_handshake_set_state( ssl,
1824 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1825#else
1826 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1827#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1828 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001829 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001830 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001831 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001832 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1833 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001834
1835cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1837 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001838 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001839}
1840
1841/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001842 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001843 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001844 *
1845 * The EncryptedExtensions message contains any extensions which
1846 * should be protected, i.e., any which are not needed to establish
1847 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001848 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001849
XiaokangQian08da26c2021-10-09 10:12:11 +00001850/* Parse EncryptedExtensions message
1851 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001852 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001853 * } EncryptedExtensions;
1854 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001855MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001856static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1857 const unsigned char *buf,
1858 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001859{
1860 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001861 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001862 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001863 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001864
XiaokangQian08da26c2021-10-09 10:12:11 +00001865 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001866 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001867 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001868
XiaokangQian97799ac2021-10-11 10:05:54 +00001869 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001870 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001871 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001872
XiaokangQian8db25ff2021-10-13 05:56:18 +00001873 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001874 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001875 unsigned int extension_type;
1876 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001877
XiaokangQian08da26c2021-10-09 10:12:11 +00001878 /*
1879 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001880 * ExtensionType extension_type; (2 bytes)
1881 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001882 * } Extension;
1883 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001884 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001885 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1886 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1887 p += 4;
1888
XiaokangQian8db25ff2021-10-13 05:56:18 +00001889 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001890
XiaokangQian97799ac2021-10-11 10:05:54 +00001891 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001892 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001893 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001894 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001895 switch( extension_type )
1896 {
Jerry Yu661dd942022-08-03 14:50:01 +08001897 case MBEDTLS_TLS_EXT_SERVERNAME:
1898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found server_name extension" ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001899
Jerry Yu661dd942022-08-03 14:50:01 +08001900 /* The server_name extension should be an empty extension */
1901
1902 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00001903 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1905 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001906
lhuang0486cacac2022-01-21 07:34:27 -08001907#if defined(MBEDTLS_SSL_ALPN)
1908 case MBEDTLS_TLS_EXT_ALPN:
1909 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1910
1911 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1912 {
1913 return( ret );
1914 }
1915
1916 break;
1917#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001918 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001919 MBEDTLS_SSL_DEBUG_MSG(
1920 3, ( "unsupported extension found: %u ", extension_type) );
1921 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001922 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001923 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1924 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001925 }
1926
XiaokangQian08da26c2021-10-09 10:12:11 +00001927 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001928 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001929
XiaokangQian97799ac2021-10-11 10:05:54 +00001930 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001931 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001932 {
1933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001934 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001935 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001936 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001937 }
1938
1939 return( ret );
1940}
1941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001942MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001943static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1944{
1945 int ret;
1946 unsigned char *buf;
1947 size_t buf_len;
1948
1949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1950
1951 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1952 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1953 &buf, &buf_len ) );
1954
1955 /* Process the message contents */
1956 MBEDTLS_SSL_PROC_CHK(
1957 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1958
1959 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1960 buf, buf_len );
1961
Ronald Cronfb508b82022-05-31 14:49:55 +02001962#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001963 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001964 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1965 else
1966 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1967#else
1968 ((void) ssl);
1969 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1970#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001971
1972cleanup:
1973
1974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1975 return( ret );
1976
1977}
1978
Jerry Yua93ac112021-10-27 16:31:48 +08001979#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001980/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001981 * STATE HANDLING: CertificateRequest
1982 *
Jerry Yud2674312021-10-29 10:08:19 +08001983 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001984#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1985#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001986/* Coordination:
1987 * Deals with the ambiguity of not knowing if a CertificateRequest
1988 * will be sent. Returns a negative code on failure, or
1989 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1990 * - SSL_CERTIFICATE_REQUEST_SKIP
1991 * indicating if a Certificate Request is expected or not.
1992 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001993MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001994static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001995{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001997
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001998 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001999 {
2000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2001 return( ret );
2002 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002003 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002004
2005 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
2006 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
2007 {
Ronald Cron19385882022-06-15 16:26:13 +02002008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002009 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08002010 }
2011
Ronald Cron19385882022-06-15 16:26:13 +02002012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
2013
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002014 return( SSL_CERTIFICATE_REQUEST_SKIP );
2015}
2016
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002017/*
2018 * ssl_tls13_parse_certificate_request()
2019 * Parse certificate request
2020 * struct {
2021 * opaque certificate_request_context<0..2^8-1>;
2022 * Extension extensions<2..2^16-1>;
2023 * } CertificateRequest;
2024 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002025MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002026static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
2027 const unsigned char *buf,
2028 const unsigned char *end )
2029{
2030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2031 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002032 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002033 size_t extensions_len = 0;
2034 const unsigned char *extensions_end;
2035 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002036
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002037 /* ...
2038 * opaque certificate_request_context<0..2^8-1>
2039 * ...
2040 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002041 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
2042 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002043 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002044
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 if( certificate_request_context_len > 0 )
2046 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002047 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002048 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
2049 p, certificate_request_context_len );
2050
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002051 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002052 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002053 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002054 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002055 {
2056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2057 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2058 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002059 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002060 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002061 p += certificate_request_context_len;
2062 }
2063
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002064 /* ...
2065 * Extension extensions<2..2^16-1>;
2066 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002067 */
2068 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2069 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2070 p += 2;
2071
2072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2073 extensions_end = p + extensions_len;
2074
2075 while( p < extensions_end )
2076 {
2077 unsigned int extension_type;
2078 size_t extension_data_len;
2079
2080 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2081 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2082 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2083 p += 4;
2084
2085 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2086
2087 switch( extension_type )
2088 {
2089 case MBEDTLS_TLS_EXT_SIG_ALG:
2090 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002091 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002092 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002093 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002094 if( ret != 0 )
2095 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002096 if( ! sig_alg_ext_found )
2097 sig_alg_ext_found = 1;
2098 else
2099 {
2100 MBEDTLS_SSL_DEBUG_MSG( 3,
2101 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002102 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002103 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002104 break;
2105
2106 default:
2107 MBEDTLS_SSL_DEBUG_MSG(
2108 3,
2109 ( "unknown extension found: %u ( ignoring )",
2110 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002111 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002112 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002113 p += extension_data_len;
2114 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002115 /* Check that we consumed all the message. */
2116 if( p != end )
2117 {
2118 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002119 ( "CertificateRequest misaligned" ) );
2120 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002121 }
2122 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002123 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002124 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002125 MBEDTLS_SSL_DEBUG_MSG( 3,
2126 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002127 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002128 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002129
Jerry Yu7840f812022-01-29 10:26:51 +08002130 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002131 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002132
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002133decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002134 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2135 MBEDTLS_ERR_SSL_DECODE_ERROR );
2136 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002137}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002138
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002139/*
2140 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2141 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002142MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002143static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002144{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002146
2147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2148
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002149 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2150
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002151 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2152 {
2153 unsigned char *buf;
2154 size_t buf_len;
2155
2156 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2157 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2158 &buf, &buf_len ) );
2159
2160 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2161 buf, buf + buf_len ) );
2162
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002163 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2164 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002165 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002166 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002167 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002168 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002169 }
2170 else
2171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002173 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2174 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002175 }
2176
Jerry Yud2674312021-10-29 10:08:19 +08002177 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2178
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002179cleanup:
2180
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2182 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002183}
2184
2185/*
Jerry Yu687101b2021-09-14 16:03:56 +08002186 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2187 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002188MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002189static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002190{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002191 int ret;
2192
2193 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002194 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002195 return( ret );
2196
Jerry Yu687101b2021-09-14 16:03:56 +08002197 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2198 return( 0 );
2199}
2200
2201/*
2202 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2203 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002204MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002205static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002206{
Jerry Yu30b071c2021-09-12 20:16:03 +08002207 int ret;
2208
2209 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2210 if( ret != 0 )
2211 return( ret );
2212
Jerry Yu687101b2021-09-14 16:03:56 +08002213 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2214 return( 0 );
2215}
Jerry Yua93ac112021-10-27 16:31:48 +08002216#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002217
Jerry Yu687101b2021-09-14 16:03:56 +08002218/*
2219 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2220 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002221MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002222static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002223{
XiaokangQianac0385c2021-11-03 06:40:11 +00002224 int ret;
2225
XiaokangQianc5c39d52021-11-09 11:55:10 +00002226 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002227 if( ret != 0 )
2228 return( ret );
2229
Jerry Yue3d67cb2022-05-19 15:33:10 +08002230 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2231 if( ret != 0 )
2232 {
2233 MBEDTLS_SSL_PEND_FATAL_ALERT(
2234 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2235 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2236 return( ret );
2237 }
2238
Ronald Cron49ad6192021-11-24 16:25:31 +01002239#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2240 mbedtls_ssl_handshake_set_state(
2241 ssl,
2242 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2243#else
Jerry Yuca133a32022-02-15 14:22:05 +08002244 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002245#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002246
XiaokangQianac0385c2021-11-03 06:40:11 +00002247 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002248}
2249
2250/*
Jerry Yu566c7812022-01-26 15:41:22 +08002251 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2252 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002253MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002254static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2255{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002256 int non_empty_certificate_msg = 0;
2257
Jerry Yu5cc35062022-01-28 16:16:08 +08002258 MBEDTLS_SSL_DEBUG_MSG( 1,
2259 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002260 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002261
Ronald Cron9df7c802022-03-08 18:38:54 +01002262#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002263 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002264 {
2265 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2266 if( ret != 0 )
2267 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002268
Ronald Cron7a94aca2022-03-09 07:44:27 +01002269 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2270 non_empty_certificate_msg = 1;
2271 }
2272 else
2273 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002275 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002276#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002277
Ronald Cron7a94aca2022-03-09 07:44:27 +01002278 if( non_empty_certificate_msg )
2279 {
2280 mbedtls_ssl_handshake_set_state( ssl,
2281 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2282 }
2283 else
Ronald Cron19385882022-06-15 16:26:13 +02002284 {
2285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002286 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002287 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002288
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002289 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002290}
2291
Ronald Cron9df7c802022-03-08 18:38:54 +01002292#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002293/*
2294 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2295 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002297static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2298{
Ronald Crona8b38872022-03-09 07:59:25 +01002299 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2300
2301 if( ret == 0 )
2302 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2303
2304 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002305}
Jerry Yu90f152d2022-01-29 22:12:42 +08002306#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002307
2308/*
Jerry Yu687101b2021-09-14 16:03:56 +08002309 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2310 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002311MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002312static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002313{
XiaokangQian0fa66432021-11-15 03:33:57 +00002314 int ret;
2315
2316 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2317 if( ret != 0 )
2318 return( ret );
2319
Jerry Yu466dda82022-09-13 11:20:20 +08002320 ret = mbedtls_ssl_tls13_compute_resumption_master_secret( ssl );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002321 if( ret != 0 )
2322 {
2323 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu466dda82022-09-13 11:20:20 +08002324 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002325 return ( ret );
2326 }
2327
XiaokangQian0fa66432021-11-15 03:33:57 +00002328 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2329 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002330}
2331
2332/*
2333 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2334 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002335MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002336static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002337{
Jerry Yu378254d2021-10-30 21:44:47 +08002338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002339 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002340 return( 0 );
2341}
2342
2343/*
2344 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2345 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002346MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002347static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002348{
Jerry Yu378254d2021-10-30 21:44:47 +08002349
2350 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2351
2352 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2353 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002354}
2355
Jerry Yuf8a49942022-07-07 11:32:32 +00002356#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2357
Jerry Yua0446a02022-07-13 11:22:55 +08002358MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002359static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2360 const unsigned char *buf,
2361 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002362{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002363 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002364
2365 ((void) ssl);
2366
2367 while( p < end )
2368 {
2369 unsigned int extension_type;
2370 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002371
Jerry Yuf8a49942022-07-07 11:32:32 +00002372 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2373 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2374 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2375 p += 4;
2376
2377 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002378
2379 switch( extension_type )
2380 {
2381 case MBEDTLS_TLS_EXT_EARLY_DATA:
2382 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2383 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002384
Jerry Yuf8a49942022-07-07 11:32:32 +00002385 default:
2386 break;
2387 }
2388 p += extension_data_len;
2389 }
2390
2391 return( 0 );
2392}
2393
2394/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002395 * From RFC8446, page 74
2396 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002397 * struct {
2398 * uint32 ticket_lifetime;
2399 * uint32 ticket_age_add;
2400 * opaque ticket_nonce<0..255>;
2401 * opaque ticket<1..2^16-1>;
2402 * Extension extensions<0..2^16-2>;
2403 * } NewSessionTicket;
2404 *
2405 */
Jerry Yua0446a02022-07-13 11:22:55 +08002406MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002407static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2408 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002409 unsigned char *end,
2410 unsigned char **ticket_nonce,
2411 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002412{
2413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2414 unsigned char *p = buf;
2415 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002416 size_t ticket_len;
2417 unsigned char *ticket;
2418 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002419
Jerry Yucb3b1392022-07-12 06:09:38 +00002420 *ticket_nonce = NULL;
2421 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002422 /*
2423 * ticket_lifetime 4 bytes
2424 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002425 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002426 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002427 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002428
2429 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002430 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002431 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002432 ( unsigned int )session->ticket_lifetime ) );
2433
Jerry Yucb3b1392022-07-12 06:09:38 +00002434 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002435 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002436 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002437 ( unsigned int )session->ticket_age_add ) );
2438
Jerry Yucb3b1392022-07-12 06:09:38 +00002439 *ticket_nonce_len = p[8];
2440 p += 9;
2441
2442 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2443 *ticket_nonce = p;
2444 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2445 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002446
2447 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002448 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002449 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2450 p += 2;
2451 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002452 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2453
2454 /* Check if we previously received a ticket already. */
2455 if( session->ticket != NULL || session->ticket_len > 0 )
2456 {
2457 mbedtls_free( session->ticket );
2458 session->ticket = NULL;
2459 session->ticket_len = 0;
2460 }
2461
2462 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2463 {
2464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2466 }
2467 memcpy( ticket, p, ticket_len );
2468 p += ticket_len;
2469 session->ticket = ticket;
2470 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002471
Jerry Yucb3b1392022-07-12 06:09:38 +00002472 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002473 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2474 p += 2;
2475 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2476
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002477 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002478
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002479 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002480 if( ret != 0 )
2481 {
2482 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002483 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002484 ret );
2485 return( ret );
2486 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002487
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002488 /* session has been updated, allow export */
2489 session->exported = 0;
2490
Jerry Yucb3b1392022-07-12 06:09:38 +00002491 return( 0 );
2492}
2493
Jerry Yua0446a02022-07-13 11:22:55 +08002494MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002495static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2496 unsigned char *ticket_nonce,
2497 size_t ticket_nonce_len )
2498{
2499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2500 mbedtls_ssl_session *session = ssl->session;
2501 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2502 psa_algorithm_t psa_hash_alg;
2503 int hash_length;
2504
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002505#if defined(MBEDTLS_HAVE_TIME)
2506 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002507 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002508#endif
2509
Jerry Yuf8a49942022-07-07 11:32:32 +00002510 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2511 if( ciphersuite_info == NULL )
2512 {
2513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2515 }
2516
2517 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2518 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002519 if( hash_length == -1 ||
2520 ( size_t )hash_length > sizeof( session->resumption_key ) )
2521 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002523 }
2524
Jerry Yuf8a49942022-07-07 11:32:32 +00002525
2526 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2527 session->app_secrets.resumption_master_secret,
2528 hash_length );
2529
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002530 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002531 *
2532 * HKDF-Expand-Label( resumption_master_secret,
2533 * "resumption", ticket_nonce, Hash.length )
2534 */
2535 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2536 psa_hash_alg,
2537 session->app_secrets.resumption_master_secret,
2538 hash_length,
2539 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2540 ticket_nonce,
2541 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002542 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002543 hash_length );
2544
2545 if( ret != 0 )
2546 {
2547 MBEDTLS_SSL_DEBUG_RET( 2,
2548 "Creating the ticket-resumed PSK failed",
2549 ret );
2550 return( ret );
2551 }
2552
Jerry Yu0a430c82022-07-20 11:02:48 +08002553 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002554
2555 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002556 session->resumption_key,
2557 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002558
Jerry Yuf8a49942022-07-07 11:32:32 +00002559 return( 0 );
2560}
2561
2562/*
Jerry Yua357cf42022-07-12 05:36:45 +00002563 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002564 */
Jerry Yua0446a02022-07-13 11:22:55 +08002565MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002566static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2567{
2568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2569 unsigned char *buf;
2570 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002571 unsigned char *ticket_nonce;
2572 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002573
2574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2575
2576 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2577 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2578 &buf, &buf_len ) );
2579
Jerry Yucb3b1392022-07-12 06:09:38 +00002580 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2581 ssl, buf, buf + buf_len,
2582 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002583
Jerry Yucb3b1392022-07-12 06:09:38 +00002584 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2585 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002586
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002587 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2588
Jerry Yuf8a49942022-07-07 11:32:32 +00002589cleanup:
2590
2591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2592 return( ret );
2593}
2594#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2595
Jerry Yu92c6b402021-08-27 16:59:09 +08002596int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002597{
Jerry Yu92c6b402021-08-27 16:59:09 +08002598 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002599
Jerry Yu92c6b402021-08-27 16:59:09 +08002600 switch( ssl->state )
2601 {
2602 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002603 * ssl->state is initialized as HELLO_REQUEST. It is the same
2604 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002605 */
2606 case MBEDTLS_SSL_HELLO_REQUEST:
2607 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002608 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002609 break;
2610
2611 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002612 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002613 break;
2614
2615 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002616 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002617 break;
2618
Jerry Yua93ac112021-10-27 16:31:48 +08002619#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002620 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2621 ret = ssl_tls13_process_certificate_request( ssl );
2622 break;
2623
Jerry Yu687101b2021-09-14 16:03:56 +08002624 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002625 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002626 break;
2627
2628 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002629 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002630 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002631#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002632
2633 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002634 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002635 break;
2636
Jerry Yu566c7812022-01-26 15:41:22 +08002637 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2638 ret = ssl_tls13_write_client_certificate( ssl );
2639 break;
2640
Ronald Cron9df7c802022-03-08 18:38:54 +01002641#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002642 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2643 ret = ssl_tls13_write_client_certificate_verify( ssl );
2644 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002645#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002646
Jerry Yu687101b2021-09-14 16:03:56 +08002647 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002648 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002649 break;
2650
2651 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002652 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002653 break;
2654
2655 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002656 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002657 break;
2658
Ronald Cron49ad6192021-11-24 16:25:31 +01002659 /*
2660 * Injection of dummy-CCS's for middlebox compatibility
2661 */
2662#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002663 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002664 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2665 if( ret == 0 )
2666 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2667 break;
2668
2669 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2670 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2671 if( ret == 0 )
2672 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002673 break;
2674#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2675
Jerry Yuf8a49942022-07-07 11:32:32 +00002676#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002677 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002678 ret = ssl_tls13_process_new_session_ticket( ssl );
2679 if( ret != 0 )
2680 break;
2681 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2682 break;
2683#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2684
Jerry Yu92c6b402021-08-27 16:59:09 +08002685 default:
2686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2688 }
2689
2690 return( ret );
2691}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002692
Jerry Yufb4b6472022-01-27 15:03:26 +08002693#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002694
Jerry Yufb4b6472022-01-27 15:03:26 +08002695