blob: 6d87cffc1215fcecb35315b2ef28acdd0ee1e66c [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
668/* Check if we have any PSK to offer, returns 0 if PSK is available.
669 * Assign the psk and ticket if pointers are present.
670 */
671MBEDTLS_CHECK_RETURN_CRITICAL
672static int ssl_tls13_get_psk_to_offer(
673 const mbedtls_ssl_context *ssl,
674 int *psk_type,
675 const unsigned char **psk, size_t *psk_len,
676 const unsigned char **psk_identity, size_t *psk_identity_len )
677{
678 if( psk_type == NULL ||
679 psk == NULL || psk_len == NULL ||
680 psk_identity == NULL || psk_identity_len == NULL )
681 {
682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
683 }
684
685 *psk = NULL;
686 *psk_len = 0;
687 *psk_identity = NULL;
688 *psk_identity_len = 0;
689
690#if defined(MBEDTLS_SSL_SESSION_TICKETS)
691 /* Check if a ticket has been configured. */
692 if( ssl->session_negotiate != NULL &&
693 ssl->session_negotiate->ticket != NULL )
694 {
695#if defined(MBEDTLS_HAVE_TIME)
696 mbedtls_time_t now = mbedtls_time( NULL );
697
698 if( ( ssl->session_negotiate->ticket_received <= now &&
699 now - ssl->session_negotiate->ticket_received < 7 * 86400 * 1000 ) )
700 {
701 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
702 *psk = ssl->session_negotiate->resumption_key;
703 *psk_len = ssl->session_negotiate->resumption_key_len;
704 *psk_identity = ssl->session_negotiate->ticket;
705 *psk_identity_len = ssl->session_negotiate->ticket_len;
706 return( 0 );
707 }
708#endif /* MBEDTLS_HAVE_TIME */
709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket expired" ) );
710 }
711#endif
712
713 /* Check if an external PSK has been configured. */
714 if( ssl->conf->psk != NULL )
715 {
716 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
717 *psk = ssl->conf->psk;
718 *psk_len = ssl->conf->psk_len;
719 *psk_identity = ssl->conf->psk_identity;
720 *psk_identity_len = ssl->conf->psk_identity_len;
721 return( 0 );
722 }
723
724 return( 1 );
725}
XiaokangQianeb69aee2022-07-05 08:21:43 +0000726
727/*
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800728 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
XiaokangQianeb69aee2022-07-05 08:21:43 +0000729 *
730 * struct {
731 * opaque identity<1..2^16-1>;
732 * uint32 obfuscated_ticket_age;
733 * } PskIdentity;
734 *
735 * opaque PskBinderEntry<32..255>;
736 *
737 * struct {
XiaokangQian86981952022-07-19 09:51:50 +0000738 * PskIdentity identities<7..2^16-1>;
739 * PskBinderEntry binders<33..2^16-1>;
740 * } OfferedPsks;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000741 *
XiaokangQian86981952022-07-19 09:51:50 +0000742 * struct {
743 * select (Handshake.msg_type) {
744 * case client_hello: OfferedPsks;
745 * ...
746 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +0000747 * } PreSharedKeyExtension;
748 *
XiaokangQianeb69aee2022-07-05 08:21:43 +0000749 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000750int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000751 mbedtls_ssl_context *ssl,
752 unsigned char *buf, unsigned char *end,
753 size_t *out_len, size_t *binders_len )
754{
755 unsigned char *p = buf;
756 const unsigned char *psk;
757 size_t psk_len;
758 const unsigned char *psk_identity;
759 size_t psk_identity_len;
XiaokangQian86981952022-07-19 09:51:50 +0000760 int psk_type;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000761 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000762 const int *ciphersuites;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000763 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000764 int hash_len = 0;
765 size_t identities_len, l_binders_len;
766 uint32_t obfuscated_ticket_age = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000767
768 *out_len = 0;
769 *binders_len = 0;
770
771 /* Check if we have any PSKs to offer. If so, return the first.
772 *
773 * NOTE: Ultimately, we want to be able to offer multiple PSKs,
774 * in which case we want to iterate over them here.
775 *
776 * As it stands, however, we only ever offer one, chosen
777 * by the following heuristic:
778 * - If a ticket has been configured, offer the corresponding PSK.
779 * - If no ticket has been configured by an external PSK has been
780 * configured, offer that.
781 * - Otherwise, skip the PSK extension.
782 */
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800783 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
784 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000785 {
786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
787 return( 0 );
788 }
789
XiaokangQian86981952022-07-19 09:51:50 +0000790 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000791 {
XiaokangQian7c12d312022-07-20 07:25:43 +0000792 /*
793 * Ciphersuite list
794 */
795 ciphersuites = ssl->conf->ciphersuite_list;
XiaokangQian86981952022-07-19 09:51:50 +0000796 for( int i = 0; ciphersuites[i] != 0; i++ )
797 {
798 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
799 ciphersuites[i] );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000800
XiaokangQian86981952022-07-19 09:51:50 +0000801 if( mbedtls_ssl_validate_ciphersuite(
XiaokangQian008d2bf2022-07-14 07:54:01 +0000802 ssl, ciphersuite_info,
803 MBEDTLS_SSL_VERSION_TLS1_3,
804 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
XiaokangQian86981952022-07-19 09:51:50 +0000805 continue;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000806
XiaokangQian86981952022-07-19 09:51:50 +0000807 /* In this implementation we only add one pre-shared-key
808 * extension.
809 */
810 ssl->session_negotiate->ciphersuite = ciphersuites[i];
811 break;
812 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000813 }
814
XiaokangQian008d2bf2022-07-14 07:54:01 +0000815 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
816 ssl->session_negotiate->ciphersuite );
817 /* No suitable ciphersuite for the PSK */
818 if( ciphersuite_info == NULL )
819 return( 0 );
820
821 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
822 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000823 if( hash_len == -1 )
824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
825
826 /* Check if we have space to write the extension, binder included.
827 * - extension_type (2 bytes)
828 * - extension_data_len (2 bytes)
829 * - identities_len (2 bytes)
830 * - identity_len (2 bytes)
831 * - identity (psk_identity_len bytes)
832 * - obfuscated_ticket_age (4 bytes)
833 * - binders_len (2 bytes)
834 * - binder_len (1 byte)
835 * - binder (hash_len bytes)
836 */
837
838 identities_len = 6 + psk_identity_len;
839 l_binders_len = 1 + hash_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000840
841 MBEDTLS_SSL_DEBUG_MSG( 3,
842 ( "client hello, adding pre_shared_key extension, "
843 "omitting PSK binder list" ) );
844
845 /* Extension header */
XiaokangQianbee71452022-07-21 08:19:06 +0000846 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000847 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
848 MBEDTLS_PUT_UINT16_BE( 2 + identities_len + 2 + l_binders_len , p, 2 );
849
850 MBEDTLS_PUT_UINT16_BE( identities_len, p, 4 );
851 MBEDTLS_PUT_UINT16_BE( psk_identity_len, p, 6 );
852 p += 8;
XiaokangQianbee71452022-07-21 08:19:06 +0000853 MBEDTLS_SSL_CHK_BUF_PTR( p, end, psk_identity_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000854 memcpy( p, psk_identity, psk_identity_len );
855 p += psk_identity_len;
856
857 /* add obfuscated ticket age */
XiaokangQianbee71452022-07-21 08:19:06 +0000858 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000859 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 0 );
860 p += 4;
861
XiaokangQianbee71452022-07-21 08:19:06 +0000862 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 + l_binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000863 *out_len = ( p - buf ) + l_binders_len + 2;
864 *binders_len = l_binders_len + 2;
865
866 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
867
868 return( 0 );
869}
870
XiaokangQian86981952022-07-19 09:51:50 +0000871int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000872 mbedtls_ssl_context *ssl,
873 unsigned char *buf, unsigned char *end )
874{
875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
876 unsigned char *p = buf;
XiaokangQianadab9a62022-07-18 07:41:26 +0000877 const unsigned char *psk_identity;
878 size_t psk_identity_len;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000879 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
880 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000881 int hash_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000882 const unsigned char *psk = NULL;
883 size_t psk_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000884 int psk_type;
885 unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
886 size_t transcript_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000887
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800888 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
889 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianadab9a62022-07-18 07:41:26 +0000890 {
891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
892 }
893
XiaokangQian008d2bf2022-07-14 07:54:01 +0000894 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
895 ssl->session_negotiate->ciphersuite );
896 if( ciphersuite_info == NULL )
897 return( 0 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000898
XiaokangQian008d2bf2022-07-14 07:54:01 +0000899 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
900 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000901 if( ( hash_len == -1 ) || ( ( end - buf ) != 3 + hash_len ) )
902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
903
904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list" ) );
905
XiaokangQian008d2bf2022-07-14 07:54:01 +0000906 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 + hash_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000907 /* 2 bytes length field for array of psk binders */
908 MBEDTLS_PUT_UINT16_BE( hash_len + 1, p, 0 );
909 p += 2;
910
911 /* 1 bytes length field for next psk binder */
912 *p++ = MBEDTLS_BYTE_0( hash_len );
913
XiaokangQianeb69aee2022-07-05 08:21:43 +0000914 /* Get current state of handshake transcript. */
XiaokangQian008d2bf2022-07-14 07:54:01 +0000915 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000916 transcript, sizeof( transcript ),
917 &transcript_len );
918 if( ret != 0 )
919 return( ret );
920
921 ret = mbedtls_ssl_tls13_create_psk_binder( ssl,
XiaokangQian008d2bf2022-07-14 07:54:01 +0000922 mbedtls_psa_translate_md( ciphersuite_info->mac ),
XiaokangQianeb69aee2022-07-05 08:21:43 +0000923 psk, psk_len, psk_type,
924 transcript, p );
925 if( ret != 0 )
926 {
927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
928 return( ret );
929 }
930
931 return( 0 );
932}
933#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
934
Ronald Cron3d580bf2022-02-18 17:24:56 +0100935int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
936 unsigned char *buf,
937 unsigned char *end,
938 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100939{
940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
941 unsigned char *p = buf;
942 size_t ext_len;
943
944 *out_len = 0;
945
946 /* Write supported_versions extension
947 *
948 * Supported Versions Extension is mandatory with TLS 1.3.
949 */
950 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
951 if( ret != 0 )
952 return( ret );
953 p += ext_len;
954
955 /* Echo the cookie if the server provided one in its preceding
956 * HelloRetryRequest message.
957 */
958 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
959 if( ret != 0 )
960 return( ret );
961 p += ext_len;
962
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100963 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
964 {
965 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
966 if( ret != 0 )
967 return( ret );
968 p += ext_len;
969 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100970
XiaokangQianeb69aee2022-07-05 08:21:43 +0000971#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
972 /* For PSK-based key exchange we need the pre_shared_key extension
973 * and the psk_key_exchange_modes extension.
974 *
975 * The pre_shared_key extension MUST be the last extension in the
976 * ClientHello. Servers MUST check that it is the last extension and
977 * otherwise fail the handshake with an "illegal_parameter" alert.
978 *
979 * Add the psk_key_exchange_modes extension.
980 */
981 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
982 if( ret != 0 )
983 return( ret );
984 p += ext_len;
985#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
986
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100987 *out_len = p - buf;
988
989 return( 0 );
990}
991
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800992/*
Jerry Yu4a173382021-10-11 21:45:31 +0800993 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200995
Ronald Cronda41b382022-03-30 09:57:11 +0200996/**
997 * \brief Detect if the ServerHello contains a supported_versions extension
998 * or not.
999 *
1000 * \param[in] ssl SSL context
1001 * \param[in] buf Buffer containing the ServerHello message
1002 * \param[in] end End of the buffer containing the ServerHello message
1003 *
1004 * \return 0 if the ServerHello does not contain a supported_versions extension
1005 * \return 1 if the ServerHello contains a supported_versions extension
1006 * \return A negative value if an error occurred while parsing the ServerHello.
1007 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001008MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001009static int ssl_tls13_is_supported_versions_ext_present(
1010 mbedtls_ssl_context *ssl,
1011 const unsigned char *buf,
1012 const unsigned char *end )
1013{
1014 const unsigned char *p = buf;
1015 size_t legacy_session_id_echo_len;
1016 size_t extensions_len;
1017 const unsigned char *extensions_end;
1018
1019 /*
1020 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001021 * length:
1022 * - legacy_version 2 bytes
1023 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1024 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001025 */
1026 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
1027 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1028 legacy_session_id_echo_len = *p;
1029
1030 /*
1031 * Jump to the extensions, jumping over:
1032 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1033 * - cipher_suite 2 bytes
1034 * - legacy_compression_method 1 byte
1035 */
Ronald Cron28271062022-06-10 14:43:55 +02001036 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001037 p += legacy_session_id_echo_len + 4;
1038
1039 /* Case of no extension */
1040 if( p == end )
1041 return( 0 );
1042
1043 /* ...
1044 * Extension extensions<6..2^16-1>;
1045 * ...
1046 * struct {
1047 * ExtensionType extension_type; (2 bytes)
1048 * opaque extension_data<0..2^16-1>;
1049 * } Extension;
1050 */
1051 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1052 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1053 p += 2;
1054
1055 /* Check extensions do not go beyond the buffer of data. */
1056 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1057 extensions_end = p + extensions_len;
1058
1059 while( p < extensions_end )
1060 {
1061 unsigned int extension_type;
1062 size_t extension_data_len;
1063
1064 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1065 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1066 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1067 p += 4;
1068
1069 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1070 return( 1 );
1071
1072 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1073 p += extension_data_len;
1074 }
1075
1076 return( 0 );
1077}
1078
Jerry Yu7a186a02021-10-15 18:46:14 +08001079/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001080 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1081 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1082 * - 0 otherwise
1083 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001084MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001085static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1086 const unsigned char *buf,
1087 const unsigned char *end )
1088{
1089 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1090 static const unsigned char magic_downgrade_string[] =
1091 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1092 const unsigned char *last_eight_bytes_of_random;
1093 unsigned char last_byte_of_random;
1094
1095 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1096 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1097
1098 if( memcmp( last_eight_bytes_of_random,
1099 magic_downgrade_string,
1100 sizeof( magic_downgrade_string ) ) == 0 )
1101 {
1102 last_byte_of_random = last_eight_bytes_of_random[7];
1103 return( last_byte_of_random == 0 ||
1104 last_byte_of_random == 1 );
1105 }
1106
1107 return( 0 );
1108}
1109
1110/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001111 * - SSL_SERVER_HELLO or
1112 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001113 * to indicate which message is expected and to be parsed next.
1114 */
Ronald Cron828aff62022-05-31 12:04:31 +02001115#define SSL_SERVER_HELLO 0
1116#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001117MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001118static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1119 const unsigned char *buf,
1120 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001121{
Jerry Yue1b9c292021-09-10 10:08:31 +08001122
1123 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1124 *
Jerry Yu4a173382021-10-11 21:45:31 +08001125 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001126 * special value of the SHA-256 of "HelloRetryRequest".
1127 *
1128 * struct {
1129 * ProtocolVersion legacy_version = 0x0303;
1130 * Random random;
1131 * opaque legacy_session_id_echo<0..32>;
1132 * CipherSuite cipher_suite;
1133 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001134 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001135 * } ServerHello;
1136 *
1137 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001138 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1139 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001140
Jerry Yu93a13f22022-04-11 23:00:01 +08001141 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1142 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001143 {
Ronald Cron828aff62022-05-31 12:04:31 +02001144 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001145 }
1146
Ronald Cron828aff62022-05-31 12:04:31 +02001147 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001148}
1149
Ronald Cron828aff62022-05-31 12:04:31 +02001150/*
Jerry Yu745bb612021-10-13 22:01:04 +08001151 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001152 * - SSL_SERVER_HELLO or
1153 * - SSL_SERVER_HELLO_HRR or
1154 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001155 */
Ronald Cron828aff62022-05-31 12:04:31 +02001156#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001157MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001158static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001159 const unsigned char *buf,
1160 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001161{
Jerry Yu4a173382021-10-11 21:45:31 +08001162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001163 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001164
Ronald Cron9f0fba32022-02-10 16:45:15 +01001165 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001166 ssl, buf, end ) );
Jerry Yua66fece2022-07-13 14:30:29 +08001167
Ronald Cron9f0fba32022-02-10 16:45:15 +01001168 if( ret == 0 )
1169 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001170 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001171 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001172
1173 /* If the server is negotiating TLS 1.2 or below and:
1174 * . we did not propose TLS 1.2 or
1175 * . the server responded it is TLS 1.3 capable but negotiating a lower
1176 * version of the protocol and thus we are under downgrade attack
1177 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001178 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001179 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001180 {
1181 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1182 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1183 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1184 }
1185
1186 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001187 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001188 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001189 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001190
1191 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1192 {
1193 ret = ssl_tls13_reset_key_share( ssl );
1194 if( ret != 0 )
1195 return( ret );
1196 }
1197
Ronald Cron828aff62022-05-31 12:04:31 +02001198 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001199 }
1200
Jerry Yua66fece2022-07-13 14:30:29 +08001201#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1202 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1203 ssl->session_negotiate->tls_version = ssl->tls_version;
1204#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1205
Ronald Cron5afb9042022-05-31 12:11:39 +02001206 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1207
Ronald Crondb5dfa12022-05-31 11:44:38 +02001208 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001209 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001210 {
Ronald Cron828aff62022-05-31 12:04:31 +02001211 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1213 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001214 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001216 /* If a client receives a second
1217 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1218 * was itself in response to a HelloRetryRequest), it MUST abort the
1219 * handshake with an "unexpected_message" alert.
1220 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001221 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001222 {
1223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1224 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1225 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1226 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1227 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001228 /*
1229 * Clients must abort the handshake with an "illegal_parameter"
1230 * alert if the HelloRetryRequest would not result in any change
1231 * in the ClientHello.
1232 * In a PSK only key exchange that what we expect.
1233 */
1234 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1235 {
1236 MBEDTLS_SSL_DEBUG_MSG( 1,
1237 ( "Unexpected HRR in pure PSK key exchange." ) );
1238 MBEDTLS_SSL_PEND_FATAL_ALERT(
1239 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1240 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1241 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1242 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001243
Ronald Cron5afb9042022-05-31 12:11:39 +02001244 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001245
Jerry Yu745bb612021-10-13 22:01:04 +08001246 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 }
1248
1249cleanup:
1250
1251 return( ret );
1252}
1253
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001254MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001255static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1256 const unsigned char **buf,
1257 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001258{
1259 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001260 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001261
Jerry Yude4fb2c2021-09-19 18:05:08 +08001262 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001263 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001264
Jerry Yu4a173382021-10-11 21:45:31 +08001265 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001266
1267 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001268 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1269 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001270 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001271 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1272 ssl->session_negotiate->id,
1273 ssl->session_negotiate->id_len );
1274 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001275 legacy_session_id_echo_len );
1276
1277 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1278 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1279
Jerry Yue1b9c292021-09-10 10:08:31 +08001280 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1281 }
1282
Jerry Yu4a173382021-10-11 21:45:31 +08001283 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001284 *buf = p;
1285
1286 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001287 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001288 return( 0 );
1289}
1290
XiaokangQianeb69aee2022-07-05 08:21:43 +00001291#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1292/*
1293 * struct {
1294 * opaque identity<1..2^16-1>;
1295 * uint32 obfuscated_ticket_age;
1296 * } PskIdentity;
1297 *
1298 * opaque PskBinderEntry<32..255>;
1299 *
1300 * struct {
XiaokangQian008d2bf2022-07-14 07:54:01 +00001301 *
XiaokangQian86981952022-07-19 09:51:50 +00001302 * select (Handshake.msg_type) {
1303 * ...
1304 * case server_hello: uint16 selected_identity;
1305 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +00001306 *
1307 * } PreSharedKeyExtension;
1308 *
1309 */
1310
XiaokangQian86981952022-07-19 09:51:50 +00001311MBEDTLS_CHECK_RETURN_CRITICAL
1312static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1313 const unsigned char *buf,
1314 const unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001315{
1316 int ret = 0;
1317 size_t selected_identity;
1318
1319 const unsigned char *psk;
1320 size_t psk_len;
1321 const unsigned char *psk_identity;
1322 size_t psk_identity_len;
Jerry Yudb8c5fa2022-08-03 12:10:13 +08001323 int psk_type;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001324
1325 /* Check which PSK we've offered.
1326 *
1327 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1328 * case, we need to iterate over them here.
1329 */
Jerry Yudb8c5fa2022-08-03 12:10:13 +08001330 if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
1331 &psk_identity, &psk_identity_len ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001332 {
1333 /* If we haven't offered a PSK, the server must not send
1334 * a PSK identity extension. */
1335 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1336 }
1337
XiaokangQian008d2bf2022-07-14 07:54:01 +00001338 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001339 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1340
1341 /* We have offered only one PSK, so the only valid choice
1342 * for the server is PSK index 0.
1343 *
1344 * This will change once we support multiple PSKs. */
1345 if( selected_identity > 0 )
1346 {
1347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1348
1349 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1351 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1352 {
1353 return( ret );
1354 }
1355
1356 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1357 }
1358
1359 /* Set the chosen PSK
1360 *
1361 * TODO: We don't have to do this in case we offered 0-RTT and the
1362 * server accepted it, because in this case we've already
1363 * set the handshake PSK. */
1364 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1365 if( ret != 0 )
1366 {
1367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1368 return( ret );
1369 }
1370
1371 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1372 return( 0 );
1373}
1374
1375#endif
1376
Jerry Yue1b9c292021-09-10 10:08:31 +08001377/* Parse ServerHello message and configure context
1378 *
1379 * struct {
1380 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1381 * Random random;
1382 * opaque legacy_session_id_echo<0..32>;
1383 * CipherSuite cipher_suite;
1384 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001385 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001386 * } ServerHello;
1387 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001388MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001389static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1390 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001391 const unsigned char *end,
1392 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001393{
Jerry Yub85277e2021-10-13 13:36:05 +08001394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001395 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001396 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001397 size_t extensions_len;
1398 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001399 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001400 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001401 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001402
1403 /*
1404 * Check there is space for minimal fields
1405 *
1406 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001407 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001408 * - legacy_session_id_echo ( 1 byte ), minimum size
1409 * - cipher_suite ( 2 bytes)
1410 * - legacy_compression_method ( 1 byte )
1411 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001412 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001413
1414 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001415 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1416
Jerry Yu4a173382021-10-11 21:45:31 +08001417 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001418 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001419 * ...
1420 * with ProtocolVersion defined as:
1421 * uint16 ProtocolVersion;
1422 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001423 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1424 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001425 {
1426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1427 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1428 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001429 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1430 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001431 }
1432 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001433
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001434 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001435 * Random random;
1436 * ...
1437 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001438 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001439 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001440 if( !is_hrr )
1441 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001442 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001443 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1444 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1445 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1446 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001447 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001448
Jerry Yu4a173382021-10-11 21:45:31 +08001449 /* ...
1450 * opaque legacy_session_id_echo<0..32>;
1451 * ...
1452 */
1453 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001454 {
XiaokangQian52da5582022-01-26 09:49:29 +00001455 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001456 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001457 }
1458
Jerry Yu4a173382021-10-11 21:45:31 +08001459 /* ...
1460 * CipherSuite cipher_suite;
1461 * ...
1462 * with CipherSuite defined as:
1463 * uint8 CipherSuite[2];
1464 */
1465 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001466 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1467 p += 2;
1468
Jerry Yu4a173382021-10-11 21:45:31 +08001469
XiaokangQian355e09a2022-01-20 11:14:50 +00001470 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001471 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001472 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001473 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001474 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1475 ssl->tls_version,
1476 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001477 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001478 {
XiaokangQian52da5582022-01-26 09:49:29 +00001479 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001480 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001481 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001482 * If we received an HRR before and that the proposed selected
1483 * ciphersuite in this server hello is not the same as the one
1484 * proposed in the HRR, we abort the handshake and send an
1485 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001486 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001487 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1488 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001489 {
XiaokangQian52da5582022-01-26 09:49:29 +00001490 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001491 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001492
XiaokangQian52da5582022-01-26 09:49:29 +00001493 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001494 {
1495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1496 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001497 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001498 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001499
Jerry Yu4a173382021-10-11 21:45:31 +08001500 /* Configure ciphersuites */
1501 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1502
XiaokangQian355e09a2022-01-20 11:14:50 +00001503 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001504 ssl->session_negotiate->ciphersuite = cipher_suite;
1505
Jerry Yue1b9c292021-09-10 10:08:31 +08001506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1507 cipher_suite, ciphersuite_info->name ) );
1508
1509#if defined(MBEDTLS_HAVE_TIME)
1510 ssl->session_negotiate->start = time( NULL );
1511#endif /* MBEDTLS_HAVE_TIME */
1512
Jerry Yu4a173382021-10-11 21:45:31 +08001513 /* ...
1514 * uint8 legacy_compression_method = 0;
1515 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001516 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001517 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Thomas Daubney31e03a82022-07-25 15:59:25 +01001518 if( p[0] != MBEDTLS_SSL_COMPRESS_NULL )
Jerry Yue1b9c292021-09-10 10:08:31 +08001519 {
Jerry Yub85277e2021-10-13 13:36:05 +08001520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001521 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001522 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001523 }
1524 p++;
1525
Jerry Yub85277e2021-10-13 13:36:05 +08001526 /* ...
1527 * Extension extensions<6..2^16-1>;
1528 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001529 * struct {
1530 * ExtensionType extension_type; (2 bytes)
1531 * opaque extension_data<0..2^16-1>;
1532 * } Extension;
1533 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001534 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001535 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001536 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001537
Jerry Yu4a173382021-10-11 21:45:31 +08001538 /* Check extensions do not go beyond the buffer of data. */
1539 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1540 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001541
Jerry Yu4a173382021-10-11 21:45:31 +08001542 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001543
Jerry Yu4a173382021-10-11 21:45:31 +08001544 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001545 {
1546 unsigned int extension_type;
1547 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001548 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001549
Jerry Yu4a173382021-10-11 21:45:31 +08001550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001551 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1552 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1553 p += 4;
1554
Jerry Yu4a173382021-10-11 21:45:31 +08001555 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001556 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001557
1558 switch( extension_type )
1559 {
XiaokangQianb851da82022-01-14 04:03:11 +00001560 case MBEDTLS_TLS_EXT_COOKIE:
1561
XiaokangQiand9e068e2022-01-18 06:23:32 +00001562 if( !is_hrr )
1563 {
XiaokangQian52da5582022-01-26 09:49:29 +00001564 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001565 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001566 }
1567
XiaokangQian43550bd2022-01-21 04:32:58 +00001568 ret = ssl_tls13_parse_cookie_ext( ssl,
1569 p, extension_data_end );
1570 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001571 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001572 MBEDTLS_SSL_DEBUG_RET( 1,
1573 "ssl_tls13_parse_cookie_ext",
1574 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001575 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001576 }
XiaokangQianb851da82022-01-14 04:03:11 +00001577 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001578
Jerry Yue1b9c292021-09-10 10:08:31 +08001579 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001580 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001581 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001582 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001583 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001584 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001585 break;
1586
XiaokangQianeb69aee2022-07-05 08:21:43 +00001587#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001588 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1590 if( is_hrr )
1591 {
1592 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1593 goto cleanup;
1594 }
Jerry Yu4a173382021-10-11 21:45:31 +08001595
XiaokangQian86981952022-07-19 09:51:50 +00001596 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001597 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001598 {
1599 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001600 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001601 return( ret );
1602 }
1603 break;
1604#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001605
Jerry Yue1b9c292021-09-10 10:08:31 +08001606 case MBEDTLS_TLS_EXT_KEY_SHARE:
1607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001608 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1609 {
XiaokangQian52da5582022-01-26 09:49:29 +00001610 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001611 goto cleanup;
1612 }
1613
XiaokangQian53f20b72022-01-18 10:47:33 +00001614 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001615 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001616 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001617 else
XiaokangQianb851da82022-01-14 04:03:11 +00001618 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001619 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001620 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001621 {
1622 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001623 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001624 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001625 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001626 }
1627 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001628
1629 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001630 MBEDTLS_SSL_DEBUG_MSG(
1631 3,
1632 ( "unknown extension found: %u ( ignoring )",
1633 extension_type ) );
1634
XiaokangQian52da5582022-01-26 09:49:29 +00001635 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001636 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001637 }
1638
1639 p += extension_data_len;
1640 }
1641
XiaokangQiand59be772022-01-24 10:12:51 +00001642cleanup:
1643
XiaokangQian52da5582022-01-26 09:49:29 +00001644 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001645 {
1646 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1647 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1648 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1649 }
XiaokangQian52da5582022-01-26 09:49:29 +00001650 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001651 {
1652 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1653 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1654 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1655 }
1656 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001657}
1658
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001659MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001660static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001661{
Jerry Yub85277e2021-10-13 13:36:05 +08001662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001663 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001664
Jerry Yub85277e2021-10-13 13:36:05 +08001665 /* Determine the key exchange mode:
1666 * 1) If both the pre_shared_key and key_share extensions were received
1667 * then the key exchange mode is PSK with EPHEMERAL.
1668 * 2) If only the pre_shared_key extension was received then the key
1669 * exchange mode is PSK-only.
1670 * 3) If only the key_share extension was received then the key
1671 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001672 */
Jerry Yub85277e2021-10-13 13:36:05 +08001673 switch( handshake->extensions_present &
1674 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001675 {
Jerry Yu745bb612021-10-13 22:01:04 +08001676 /* Only the pre_shared_key extension was received */
1677 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001678 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001679 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001680
Jerry Yu745bb612021-10-13 22:01:04 +08001681 /* Only the key_share extension was received */
1682 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001683 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001684 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001685
Jerry Yu745bb612021-10-13 22:01:04 +08001686 /* Both the pre_shared_key and key_share extensions were received */
1687 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001688 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001689 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001690
Jerry Yu745bb612021-10-13 22:01:04 +08001691 /* Neither pre_shared_key nor key_share extension was received */
1692 default:
1693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1694 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1695 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001696 }
1697
1698 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1699 *
1700 * TODO: We don't have to do this in case we offered 0-RTT and the
1701 * server accepted it. In this case, we could skip generating
1702 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001703 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001704 if( ret != 0 )
1705 {
Jerry Yue110d252022-05-05 10:19:22 +08001706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001707 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001708 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001709 }
1710
Jerry Yuf86eb752022-05-06 11:16:55 +08001711 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001712 if( ret != 0 )
1713 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001714 MBEDTLS_SSL_DEBUG_RET( 1,
1715 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001716 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001717 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001718 }
1719
Jerry Yue110d252022-05-05 10:19:22 +08001720 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1722 ssl->session_in = ssl->session_negotiate;
1723
Jerry Yu4a173382021-10-11 21:45:31 +08001724cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001725 if( ret != 0 )
1726 {
Jerry Yu4a173382021-10-11 21:45:31 +08001727 MBEDTLS_SSL_PEND_FATAL_ALERT(
1728 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1729 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1730 }
Jerry Yue110d252022-05-05 10:19:22 +08001731
Jerry Yu4a173382021-10-11 21:45:31 +08001732 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001733}
1734
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001735MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001736static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001737{
1738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1739
XiaokangQian78b1fa72022-01-19 06:56:30 +00001740 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001741
XiaokangQian78b1fa72022-01-19 06:56:30 +00001742 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001743 * We are going to re-generate a shared secret corresponding to the group
1744 * selected by the server, which is different from the group for which we
1745 * generated a shared secret in the first client hello.
1746 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001747 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001748 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001749 if( ret != 0 )
1750 return( ret );
1751
1752 return( 0 );
1753}
1754
Jerry Yue1b9c292021-09-10 10:08:31 +08001755/*
Jerry Yu4a173382021-10-11 21:45:31 +08001756 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001757 * Handler for MBEDTLS_SSL_SERVER_HELLO
1758 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001759MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001760static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001761{
Jerry Yu4a173382021-10-11 21:45:31 +08001762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001763 unsigned char *buf = NULL;
1764 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001765 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001766
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1768
Ronald Crondb5dfa12022-05-31 11:44:38 +02001769 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1770 MBEDTLS_SSL_HS_SERVER_HELLO,
1771 &buf, &buf_len ) );
1772
Ronald Cron828aff62022-05-31 12:04:31 +02001773 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001774 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001775 goto cleanup;
1776 else
Ronald Cron828aff62022-05-31 12:04:31 +02001777 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001778
Ronald Cron828aff62022-05-31 12:04:31 +02001779 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001780 {
1781 ret = 0;
1782 goto cleanup;
1783 }
1784
XiaokangQianb851da82022-01-14 04:03:11 +00001785 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001786 buf + buf_len,
1787 is_hrr ) );
1788 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001789 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1790
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001791 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1792 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001793
XiaokangQiand9e068e2022-01-18 06:23:32 +00001794 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001795 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001796 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001797#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1798 /* If not offering early data, the client sends a dummy CCS record
1799 * immediately before its second flight. This may either be before
1800 * its second ClientHello or before its encrypted handshake flight.
1801 */
1802 mbedtls_ssl_handshake_set_state( ssl,
1803 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1804#else
1805 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1806#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1807 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001808 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001809 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001810 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001811 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1812 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001813
1814cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1816 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001817 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001818}
1819
1820/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001821 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001822 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001823 *
1824 * The EncryptedExtensions message contains any extensions which
1825 * should be protected, i.e., any which are not needed to establish
1826 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001827 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001828
XiaokangQian08da26c2021-10-09 10:12:11 +00001829/* Parse EncryptedExtensions message
1830 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001831 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001832 * } EncryptedExtensions;
1833 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001834MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001835static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1836 const unsigned char *buf,
1837 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001838{
1839 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001840 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001841 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001842 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001843
XiaokangQian08da26c2021-10-09 10:12:11 +00001844 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001845 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001846 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001847
XiaokangQian97799ac2021-10-11 10:05:54 +00001848 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001849 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001850 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001851
XiaokangQian8db25ff2021-10-13 05:56:18 +00001852 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001853 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001854 unsigned int extension_type;
1855 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001856
XiaokangQian08da26c2021-10-09 10:12:11 +00001857 /*
1858 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001859 * ExtensionType extension_type; (2 bytes)
1860 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001861 * } Extension;
1862 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001863 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001864 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1865 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1866 p += 4;
1867
XiaokangQian8db25ff2021-10-13 05:56:18 +00001868 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001869
XiaokangQian97799ac2021-10-11 10:05:54 +00001870 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001871 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001872 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001873 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001874 switch( extension_type )
1875 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001876
XiaokangQian08da26c2021-10-09 10:12:11 +00001877 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1879 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001880
lhuang0486cacac2022-01-21 07:34:27 -08001881#if defined(MBEDTLS_SSL_ALPN)
1882 case MBEDTLS_TLS_EXT_ALPN:
1883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1884
1885 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1886 {
1887 return( ret );
1888 }
1889
1890 break;
1891#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001892 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001893 MBEDTLS_SSL_DEBUG_MSG(
1894 3, ( "unsupported extension found: %u ", extension_type) );
1895 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001896 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001897 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1898 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001899 }
1900
XiaokangQian08da26c2021-10-09 10:12:11 +00001901 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001902 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001903
XiaokangQian97799ac2021-10-11 10:05:54 +00001904 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001905 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001906 {
1907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001908 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001909 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001910 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001911 }
1912
1913 return( ret );
1914}
1915
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001916MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001917static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1918{
1919 int ret;
1920 unsigned char *buf;
1921 size_t buf_len;
1922
1923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1924
1925 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1926 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1927 &buf, &buf_len ) );
1928
1929 /* Process the message contents */
1930 MBEDTLS_SSL_PROC_CHK(
1931 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1932
1933 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1934 buf, buf_len );
1935
Ronald Cronfb508b82022-05-31 14:49:55 +02001936#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001937 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001938 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1939 else
1940 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1941#else
1942 ((void) ssl);
1943 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1944#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001945
1946cleanup:
1947
1948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1949 return( ret );
1950
1951}
1952
Jerry Yua93ac112021-10-27 16:31:48 +08001953#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001954/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955 * STATE HANDLING: CertificateRequest
1956 *
Jerry Yud2674312021-10-29 10:08:19 +08001957 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001958#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1959#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001960/* Coordination:
1961 * Deals with the ambiguity of not knowing if a CertificateRequest
1962 * will be sent. Returns a negative code on failure, or
1963 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1964 * - SSL_CERTIFICATE_REQUEST_SKIP
1965 * indicating if a Certificate Request is expected or not.
1966 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001967MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001968static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001969{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001971
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001972 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001973 {
1974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1975 return( ret );
1976 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001977 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001978
1979 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1980 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1981 {
Ronald Cron19385882022-06-15 16:26:13 +02001982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001983 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001984 }
1985
Ronald Cron19385882022-06-15 16:26:13 +02001986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1987
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001988 return( SSL_CERTIFICATE_REQUEST_SKIP );
1989}
1990
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001991/*
1992 * ssl_tls13_parse_certificate_request()
1993 * Parse certificate request
1994 * struct {
1995 * opaque certificate_request_context<0..2^8-1>;
1996 * Extension extensions<2..2^16-1>;
1997 * } CertificateRequest;
1998 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001999MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002000static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
2001 const unsigned char *buf,
2002 const unsigned char *end )
2003{
2004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2005 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002006 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002007 size_t extensions_len = 0;
2008 const unsigned char *extensions_end;
2009 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002010
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002011 /* ...
2012 * opaque certificate_request_context<0..2^8-1>
2013 * ...
2014 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002015 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
2016 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002017 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002018
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002019 if( certificate_request_context_len > 0 )
2020 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002021 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002022 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
2023 p, certificate_request_context_len );
2024
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002025 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002026 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002027 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002028 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002029 {
2030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2031 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2032 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002033 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002034 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002035 p += certificate_request_context_len;
2036 }
2037
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002038 /* ...
2039 * Extension extensions<2..2^16-1>;
2040 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002041 */
2042 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2043 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2044 p += 2;
2045
2046 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2047 extensions_end = p + extensions_len;
2048
2049 while( p < extensions_end )
2050 {
2051 unsigned int extension_type;
2052 size_t extension_data_len;
2053
2054 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2055 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2056 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2057 p += 4;
2058
2059 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2060
2061 switch( extension_type )
2062 {
2063 case MBEDTLS_TLS_EXT_SIG_ALG:
2064 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002065 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002066 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002067 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002068 if( ret != 0 )
2069 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002070 if( ! sig_alg_ext_found )
2071 sig_alg_ext_found = 1;
2072 else
2073 {
2074 MBEDTLS_SSL_DEBUG_MSG( 3,
2075 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002076 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002077 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002078 break;
2079
2080 default:
2081 MBEDTLS_SSL_DEBUG_MSG(
2082 3,
2083 ( "unknown extension found: %u ( ignoring )",
2084 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002085 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002086 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002087 p += extension_data_len;
2088 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002089 /* Check that we consumed all the message. */
2090 if( p != end )
2091 {
2092 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002093 ( "CertificateRequest misaligned" ) );
2094 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002095 }
2096 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002097 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002098 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002099 MBEDTLS_SSL_DEBUG_MSG( 3,
2100 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002101 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002102 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002103
Jerry Yu7840f812022-01-29 10:26:51 +08002104 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002105 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002106
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002107decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2109 MBEDTLS_ERR_SSL_DECODE_ERROR );
2110 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002111}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002112
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002113/*
2114 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2115 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002116MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002117static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002118{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002120
2121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2122
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002123 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2124
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002125 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2126 {
2127 unsigned char *buf;
2128 size_t buf_len;
2129
2130 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2131 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2132 &buf, &buf_len ) );
2133
2134 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2135 buf, buf + buf_len ) );
2136
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002137 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2138 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002139 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002140 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002141 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002142 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002143 }
2144 else
2145 {
2146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002147 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2148 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002149 }
2150
Jerry Yud2674312021-10-29 10:08:19 +08002151 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2152
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002153cleanup:
2154
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2156 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002157}
2158
2159/*
Jerry Yu687101b2021-09-14 16:03:56 +08002160 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2161 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002162MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002163static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002164{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002165 int ret;
2166
2167 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002168 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002169 return( ret );
2170
Jerry Yu687101b2021-09-14 16:03:56 +08002171 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2172 return( 0 );
2173}
2174
2175/*
2176 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2177 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002178MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002179static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002180{
Jerry Yu30b071c2021-09-12 20:16:03 +08002181 int ret;
2182
2183 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2184 if( ret != 0 )
2185 return( ret );
2186
Jerry Yu687101b2021-09-14 16:03:56 +08002187 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2188 return( 0 );
2189}
Jerry Yua93ac112021-10-27 16:31:48 +08002190#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002191
Jerry Yu687101b2021-09-14 16:03:56 +08002192/*
2193 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2194 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002195MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002196static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002197{
XiaokangQianac0385c2021-11-03 06:40:11 +00002198 int ret;
2199
XiaokangQianc5c39d52021-11-09 11:55:10 +00002200 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002201 if( ret != 0 )
2202 return( ret );
2203
Jerry Yue3d67cb2022-05-19 15:33:10 +08002204 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2205 if( ret != 0 )
2206 {
2207 MBEDTLS_SSL_PEND_FATAL_ALERT(
2208 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2209 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2210 return( ret );
2211 }
2212
Ronald Cron49ad6192021-11-24 16:25:31 +01002213#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2214 mbedtls_ssl_handshake_set_state(
2215 ssl,
2216 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2217#else
Jerry Yuca133a32022-02-15 14:22:05 +08002218 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002219#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002220
XiaokangQianac0385c2021-11-03 06:40:11 +00002221 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002222}
2223
2224/*
Jerry Yu566c7812022-01-26 15:41:22 +08002225 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2226 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002227MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002228static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2229{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002230 int non_empty_certificate_msg = 0;
2231
Jerry Yu5cc35062022-01-28 16:16:08 +08002232 MBEDTLS_SSL_DEBUG_MSG( 1,
2233 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002234 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002235
Ronald Cron9df7c802022-03-08 18:38:54 +01002236#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002237 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002238 {
2239 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2240 if( ret != 0 )
2241 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002242
Ronald Cron7a94aca2022-03-09 07:44:27 +01002243 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2244 non_empty_certificate_msg = 1;
2245 }
2246 else
2247 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002249 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002250#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002251
Ronald Cron7a94aca2022-03-09 07:44:27 +01002252 if( non_empty_certificate_msg )
2253 {
2254 mbedtls_ssl_handshake_set_state( ssl,
2255 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2256 }
2257 else
Ronald Cron19385882022-06-15 16:26:13 +02002258 {
2259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002260 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002261 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002262
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002263 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002264}
2265
Ronald Cron9df7c802022-03-08 18:38:54 +01002266#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002267/*
2268 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2269 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002270MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002271static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2272{
Ronald Crona8b38872022-03-09 07:59:25 +01002273 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2274
2275 if( ret == 0 )
2276 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2277
2278 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002279}
Jerry Yu90f152d2022-01-29 22:12:42 +08002280#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002281
2282/*
Jerry Yu687101b2021-09-14 16:03:56 +08002283 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2284 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002285MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002286static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002287{
XiaokangQian0fa66432021-11-15 03:33:57 +00002288 int ret;
2289
2290 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2291 if( ret != 0 )
2292 return( ret );
2293
Jerry Yue3d67cb2022-05-19 15:33:10 +08002294 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2295 if( ret != 0 )
2296 {
2297 MBEDTLS_SSL_DEBUG_RET( 1,
2298 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2299 return ( ret );
2300 }
2301
XiaokangQian0fa66432021-11-15 03:33:57 +00002302 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2303 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002304}
2305
2306/*
2307 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2308 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002309MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002310static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002311{
Jerry Yu378254d2021-10-30 21:44:47 +08002312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002313 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002314 return( 0 );
2315}
2316
2317/*
2318 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2319 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002320MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002321static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002322{
Jerry Yu378254d2021-10-30 21:44:47 +08002323
2324 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2325
2326 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2327 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002328}
2329
Jerry Yuf8a49942022-07-07 11:32:32 +00002330#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2331
Jerry Yua0446a02022-07-13 11:22:55 +08002332MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002333static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2334 const unsigned char *buf,
2335 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002336{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002337 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002338
2339 ((void) ssl);
2340
2341 while( p < end )
2342 {
2343 unsigned int extension_type;
2344 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002345
Jerry Yuf8a49942022-07-07 11:32:32 +00002346 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2347 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2348 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2349 p += 4;
2350
2351 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002352
2353 switch( extension_type )
2354 {
2355 case MBEDTLS_TLS_EXT_EARLY_DATA:
2356 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2357 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002358
Jerry Yuf8a49942022-07-07 11:32:32 +00002359 default:
2360 break;
2361 }
2362 p += extension_data_len;
2363 }
2364
2365 return( 0 );
2366}
2367
2368/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002369 * From RFC8446, page 74
2370 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002371 * struct {
2372 * uint32 ticket_lifetime;
2373 * uint32 ticket_age_add;
2374 * opaque ticket_nonce<0..255>;
2375 * opaque ticket<1..2^16-1>;
2376 * Extension extensions<0..2^16-2>;
2377 * } NewSessionTicket;
2378 *
2379 */
Jerry Yua0446a02022-07-13 11:22:55 +08002380MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002381static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2382 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002383 unsigned char *end,
2384 unsigned char **ticket_nonce,
2385 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002386{
2387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2388 unsigned char *p = buf;
2389 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002390 size_t ticket_len;
2391 unsigned char *ticket;
2392 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002393
Jerry Yucb3b1392022-07-12 06:09:38 +00002394 *ticket_nonce = NULL;
2395 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002396 /*
2397 * ticket_lifetime 4 bytes
2398 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002399 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002400 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002401 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002402
2403 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002404 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002405 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002406 ( unsigned int )session->ticket_lifetime ) );
2407
Jerry Yucb3b1392022-07-12 06:09:38 +00002408 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002409 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002410 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002411 ( unsigned int )session->ticket_age_add ) );
2412
Jerry Yucb3b1392022-07-12 06:09:38 +00002413 *ticket_nonce_len = p[8];
2414 p += 9;
2415
2416 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2417 *ticket_nonce = p;
2418 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2419 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002420
2421 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002422 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002423 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2424 p += 2;
2425 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002426 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2427
2428 /* Check if we previously received a ticket already. */
2429 if( session->ticket != NULL || session->ticket_len > 0 )
2430 {
2431 mbedtls_free( session->ticket );
2432 session->ticket = NULL;
2433 session->ticket_len = 0;
2434 }
2435
2436 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2437 {
2438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2439 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2440 }
2441 memcpy( ticket, p, ticket_len );
2442 p += ticket_len;
2443 session->ticket = ticket;
2444 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002445
Jerry Yucb3b1392022-07-12 06:09:38 +00002446 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002447 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2448 p += 2;
2449 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2450
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002451 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002452
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002453 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002454 if( ret != 0 )
2455 {
2456 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002457 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002458 ret );
2459 return( ret );
2460 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002461
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002462 /* session has been updated, allow export */
2463 session->exported = 0;
2464
Jerry Yucb3b1392022-07-12 06:09:38 +00002465 return( 0 );
2466}
2467
Jerry Yua0446a02022-07-13 11:22:55 +08002468MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002469static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2470 unsigned char *ticket_nonce,
2471 size_t ticket_nonce_len )
2472{
2473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2474 mbedtls_ssl_session *session = ssl->session;
2475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2476 psa_algorithm_t psa_hash_alg;
2477 int hash_length;
2478
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002479#if defined(MBEDTLS_HAVE_TIME)
2480 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002481 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002482#endif
2483
Jerry Yuf8a49942022-07-07 11:32:32 +00002484 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2485 if( ciphersuite_info == NULL )
2486 {
2487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2489 }
2490
2491 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2492 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002493 if( hash_length == -1 ||
2494 ( size_t )hash_length > sizeof( session->resumption_key ) )
2495 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002497 }
2498
Jerry Yuf8a49942022-07-07 11:32:32 +00002499
2500 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2501 session->app_secrets.resumption_master_secret,
2502 hash_length );
2503
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002504 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002505 *
2506 * HKDF-Expand-Label( resumption_master_secret,
2507 * "resumption", ticket_nonce, Hash.length )
2508 */
2509 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2510 psa_hash_alg,
2511 session->app_secrets.resumption_master_secret,
2512 hash_length,
2513 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2514 ticket_nonce,
2515 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002516 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002517 hash_length );
2518
2519 if( ret != 0 )
2520 {
2521 MBEDTLS_SSL_DEBUG_RET( 2,
2522 "Creating the ticket-resumed PSK failed",
2523 ret );
2524 return( ret );
2525 }
2526
Jerry Yu0a430c82022-07-20 11:02:48 +08002527 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002528
2529 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002530 session->resumption_key,
2531 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002532
Jerry Yuf8a49942022-07-07 11:32:32 +00002533 return( 0 );
2534}
2535
2536/*
Jerry Yua357cf42022-07-12 05:36:45 +00002537 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002538 */
Jerry Yua0446a02022-07-13 11:22:55 +08002539MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002540static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2541{
2542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2543 unsigned char *buf;
2544 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002545 unsigned char *ticket_nonce;
2546 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002547
2548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2549
2550 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2551 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2552 &buf, &buf_len ) );
2553
Jerry Yucb3b1392022-07-12 06:09:38 +00002554 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2555 ssl, buf, buf + buf_len,
2556 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002557
Jerry Yucb3b1392022-07-12 06:09:38 +00002558 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2559 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002560
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002561 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2562
Jerry Yuf8a49942022-07-07 11:32:32 +00002563cleanup:
2564
2565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2566 return( ret );
2567}
2568#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2569
Jerry Yu92c6b402021-08-27 16:59:09 +08002570int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002571{
Jerry Yu92c6b402021-08-27 16:59:09 +08002572 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002573
Jerry Yu92c6b402021-08-27 16:59:09 +08002574 switch( ssl->state )
2575 {
2576 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002577 * ssl->state is initialized as HELLO_REQUEST. It is the same
2578 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002579 */
2580 case MBEDTLS_SSL_HELLO_REQUEST:
2581 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002582 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002583 break;
2584
2585 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002586 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002587 break;
2588
2589 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002590 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002591 break;
2592
Jerry Yua93ac112021-10-27 16:31:48 +08002593#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002594 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2595 ret = ssl_tls13_process_certificate_request( ssl );
2596 break;
2597
Jerry Yu687101b2021-09-14 16:03:56 +08002598 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002599 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002600 break;
2601
2602 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002603 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002604 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002605#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002606
2607 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002608 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002609 break;
2610
Jerry Yu566c7812022-01-26 15:41:22 +08002611 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2612 ret = ssl_tls13_write_client_certificate( ssl );
2613 break;
2614
Ronald Cron9df7c802022-03-08 18:38:54 +01002615#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002616 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2617 ret = ssl_tls13_write_client_certificate_verify( ssl );
2618 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002619#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002620
Jerry Yu687101b2021-09-14 16:03:56 +08002621 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002622 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002623 break;
2624
2625 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002626 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002627 break;
2628
2629 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002630 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002631 break;
2632
Ronald Cron49ad6192021-11-24 16:25:31 +01002633 /*
2634 * Injection of dummy-CCS's for middlebox compatibility
2635 */
2636#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002637 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002638 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2639 if( ret == 0 )
2640 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2641 break;
2642
2643 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2644 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2645 if( ret == 0 )
2646 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002647 break;
2648#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2649
Jerry Yuf8a49942022-07-07 11:32:32 +00002650#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002651 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002652 ret = ssl_tls13_process_new_session_ticket( ssl );
2653 if( ret != 0 )
2654 break;
2655 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2656 break;
2657#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2658
Jerry Yu92c6b402021-08-27 16:59:09 +08002659 default:
2660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2662 }
2663
2664 return( ret );
2665}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002666
Jerry Yufb4b6472022-01-27 15:03:26 +08002667#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002668
Jerry Yufb4b6472022-01-27 15:03:26 +08002669