blob: cd3be46c9ab363e134dffd8ae083b8f4b1943eab [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}
667#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
668
669/*
670 * mbedtls_ssl_tls13_write_pre_shared_key_ext() structure:
671 *
672 * struct {
673 * opaque identity<1..2^16-1>;
674 * uint32 obfuscated_ticket_age;
675 * } PskIdentity;
676 *
677 * opaque PskBinderEntry<32..255>;
678 *
679 * struct {
XiaokangQian86981952022-07-19 09:51:50 +0000680 * PskIdentity identities<7..2^16-1>;
681 * PskBinderEntry binders<33..2^16-1>;
682 * } OfferedPsks;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000683 *
XiaokangQian86981952022-07-19 09:51:50 +0000684 * struct {
685 * select (Handshake.msg_type) {
686 * case client_hello: OfferedPsks;
687 * ...
688 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +0000689 * } PreSharedKeyExtension;
690 *
XiaokangQianeb69aee2022-07-05 08:21:43 +0000691 */
692
693#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
694
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000695int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000696 mbedtls_ssl_context *ssl,
697 unsigned char *buf, unsigned char *end,
698 size_t *out_len, size_t *binders_len )
699{
700 unsigned char *p = buf;
701 const unsigned char *psk;
702 size_t psk_len;
703 const unsigned char *psk_identity;
704 size_t psk_identity_len;
XiaokangQian86981952022-07-19 09:51:50 +0000705 int psk_type;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000706 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000707 const int *ciphersuites;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000708 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000709 int hash_len = 0;
710 size_t identities_len, l_binders_len;
711 uint32_t obfuscated_ticket_age = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000712
713 *out_len = 0;
714 *binders_len = 0;
715
716 /* Check if we have any PSKs to offer. If so, return the first.
717 *
718 * NOTE: Ultimately, we want to be able to offer multiple PSKs,
719 * in which case we want to iterate over them here.
720 *
721 * As it stands, however, we only ever offer one, chosen
722 * by the following heuristic:
723 * - If a ticket has been configured, offer the corresponding PSK.
724 * - If no ticket has been configured by an external PSK has been
725 * configured, offer that.
726 * - Otherwise, skip the PSK extension.
727 */
728
XiaokangQian86981952022-07-19 09:51:50 +0000729 if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000730 &psk_identity, &psk_identity_len ) != 0 )
731 {
732 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
733 return( 0 );
734 }
735
XiaokangQian86981952022-07-19 09:51:50 +0000736 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000737 {
XiaokangQian7c12d312022-07-20 07:25:43 +0000738 /*
739 * Ciphersuite list
740 */
741 ciphersuites = ssl->conf->ciphersuite_list;
XiaokangQian86981952022-07-19 09:51:50 +0000742 for( int i = 0; ciphersuites[i] != 0; i++ )
743 {
744 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
745 ciphersuites[i] );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000746
XiaokangQian86981952022-07-19 09:51:50 +0000747 if( mbedtls_ssl_validate_ciphersuite(
XiaokangQian008d2bf2022-07-14 07:54:01 +0000748 ssl, ciphersuite_info,
749 MBEDTLS_SSL_VERSION_TLS1_3,
750 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
XiaokangQian86981952022-07-19 09:51:50 +0000751 continue;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000752
XiaokangQian86981952022-07-19 09:51:50 +0000753 /* In this implementation we only add one pre-shared-key
754 * extension.
755 */
756 ssl->session_negotiate->ciphersuite = ciphersuites[i];
757 break;
758 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000759 }
760
XiaokangQian008d2bf2022-07-14 07:54:01 +0000761 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
762 ssl->session_negotiate->ciphersuite );
763 /* No suitable ciphersuite for the PSK */
764 if( ciphersuite_info == NULL )
765 return( 0 );
766
767 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
768 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000769 if( hash_len == -1 )
770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
771
772 /* Check if we have space to write the extension, binder included.
773 * - extension_type (2 bytes)
774 * - extension_data_len (2 bytes)
775 * - identities_len (2 bytes)
776 * - identity_len (2 bytes)
777 * - identity (psk_identity_len bytes)
778 * - obfuscated_ticket_age (4 bytes)
779 * - binders_len (2 bytes)
780 * - binder_len (1 byte)
781 * - binder (hash_len bytes)
782 */
783
784 identities_len = 6 + psk_identity_len;
785 l_binders_len = 1 + hash_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000786
787 MBEDTLS_SSL_DEBUG_MSG( 3,
788 ( "client hello, adding pre_shared_key extension, "
789 "omitting PSK binder list" ) );
790
791 /* Extension header */
XiaokangQianbee71452022-07-21 08:19:06 +0000792 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000793 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
794 MBEDTLS_PUT_UINT16_BE( 2 + identities_len + 2 + l_binders_len , p, 2 );
795
796 MBEDTLS_PUT_UINT16_BE( identities_len, p, 4 );
797 MBEDTLS_PUT_UINT16_BE( psk_identity_len, p, 6 );
798 p += 8;
XiaokangQianbee71452022-07-21 08:19:06 +0000799 MBEDTLS_SSL_CHK_BUF_PTR( p, end, psk_identity_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000800 memcpy( p, psk_identity, psk_identity_len );
801 p += psk_identity_len;
802
803 /* add obfuscated ticket age */
XiaokangQianbee71452022-07-21 08:19:06 +0000804 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000805 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 0 );
806 p += 4;
807
XiaokangQianbee71452022-07-21 08:19:06 +0000808 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 + l_binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000809 *out_len = ( p - buf ) + l_binders_len + 2;
810 *binders_len = l_binders_len + 2;
811
812 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
813
814 return( 0 );
815}
816
XiaokangQian86981952022-07-19 09:51:50 +0000817int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000818 mbedtls_ssl_context *ssl,
819 unsigned char *buf, unsigned char *end )
820{
821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
822 unsigned char *p = buf;
XiaokangQianadab9a62022-07-18 07:41:26 +0000823 const unsigned char *psk_identity;
824 size_t psk_identity_len;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000825 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
826 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000827 int hash_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000828 const unsigned char *psk = NULL;
829 size_t psk_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000830 int psk_type;
831 unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
832 size_t transcript_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000833
XiaokangQianadab9a62022-07-18 07:41:26 +0000834 if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
835 &psk_identity, &psk_identity_len ) != 0 )
836 {
837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
838 }
839
XiaokangQian008d2bf2022-07-14 07:54:01 +0000840 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
841 ssl->session_negotiate->ciphersuite );
842 if( ciphersuite_info == NULL )
843 return( 0 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000844
XiaokangQian008d2bf2022-07-14 07:54:01 +0000845 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
846 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000847 if( ( hash_len == -1 ) || ( ( end - buf ) != 3 + hash_len ) )
848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
849
850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list" ) );
851
XiaokangQian008d2bf2022-07-14 07:54:01 +0000852 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 + hash_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000853 /* 2 bytes length field for array of psk binders */
854 MBEDTLS_PUT_UINT16_BE( hash_len + 1, p, 0 );
855 p += 2;
856
857 /* 1 bytes length field for next psk binder */
858 *p++ = MBEDTLS_BYTE_0( hash_len );
859
XiaokangQianeb69aee2022-07-05 08:21:43 +0000860 /* Get current state of handshake transcript. */
XiaokangQian008d2bf2022-07-14 07:54:01 +0000861 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000862 transcript, sizeof( transcript ),
863 &transcript_len );
864 if( ret != 0 )
865 return( ret );
866
867 ret = mbedtls_ssl_tls13_create_psk_binder( ssl,
XiaokangQian008d2bf2022-07-14 07:54:01 +0000868 mbedtls_psa_translate_md( ciphersuite_info->mac ),
XiaokangQianeb69aee2022-07-05 08:21:43 +0000869 psk, psk_len, psk_type,
870 transcript, p );
871 if( ret != 0 )
872 {
873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
874 return( ret );
875 }
876
877 return( 0 );
878}
879#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
880
Ronald Cron3d580bf2022-02-18 17:24:56 +0100881int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
882 unsigned char *buf,
883 unsigned char *end,
884 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100885{
886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
887 unsigned char *p = buf;
888 size_t ext_len;
889
890 *out_len = 0;
891
892 /* Write supported_versions extension
893 *
894 * Supported Versions Extension is mandatory with TLS 1.3.
895 */
896 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
897 if( ret != 0 )
898 return( ret );
899 p += ext_len;
900
901 /* Echo the cookie if the server provided one in its preceding
902 * HelloRetryRequest message.
903 */
904 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
905 if( ret != 0 )
906 return( ret );
907 p += ext_len;
908
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100909 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
910 {
911 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
912 if( ret != 0 )
913 return( ret );
914 p += ext_len;
915 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100916
XiaokangQianeb69aee2022-07-05 08:21:43 +0000917#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
918 /* For PSK-based key exchange we need the pre_shared_key extension
919 * and the psk_key_exchange_modes extension.
920 *
921 * The pre_shared_key extension MUST be the last extension in the
922 * ClientHello. Servers MUST check that it is the last extension and
923 * otherwise fail the handshake with an "illegal_parameter" alert.
924 *
925 * Add the psk_key_exchange_modes extension.
926 */
927 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
928 if( ret != 0 )
929 return( ret );
930 p += ext_len;
931#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
932
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100933 *out_len = p - buf;
934
935 return( 0 );
936}
937
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800938/*
Jerry Yu4a173382021-10-11 21:45:31 +0800939 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800940 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200941
Ronald Cronda41b382022-03-30 09:57:11 +0200942/**
943 * \brief Detect if the ServerHello contains a supported_versions extension
944 * or not.
945 *
946 * \param[in] ssl SSL context
947 * \param[in] buf Buffer containing the ServerHello message
948 * \param[in] end End of the buffer containing the ServerHello message
949 *
950 * \return 0 if the ServerHello does not contain a supported_versions extension
951 * \return 1 if the ServerHello contains a supported_versions extension
952 * \return A negative value if an error occurred while parsing the ServerHello.
953 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200954MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +0100955static int ssl_tls13_is_supported_versions_ext_present(
956 mbedtls_ssl_context *ssl,
957 const unsigned char *buf,
958 const unsigned char *end )
959{
960 const unsigned char *p = buf;
961 size_t legacy_session_id_echo_len;
962 size_t extensions_len;
963 const unsigned char *extensions_end;
964
965 /*
966 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200967 * length:
968 * - legacy_version 2 bytes
969 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
970 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100971 */
972 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
973 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
974 legacy_session_id_echo_len = *p;
975
976 /*
977 * Jump to the extensions, jumping over:
978 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
979 * - cipher_suite 2 bytes
980 * - legacy_compression_method 1 byte
981 */
Ronald Cron28271062022-06-10 14:43:55 +0200982 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100983 p += legacy_session_id_echo_len + 4;
984
985 /* Case of no extension */
986 if( p == end )
987 return( 0 );
988
989 /* ...
990 * Extension extensions<6..2^16-1>;
991 * ...
992 * struct {
993 * ExtensionType extension_type; (2 bytes)
994 * opaque extension_data<0..2^16-1>;
995 * } Extension;
996 */
997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
998 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
999 p += 2;
1000
1001 /* Check extensions do not go beyond the buffer of data. */
1002 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1003 extensions_end = p + extensions_len;
1004
1005 while( p < extensions_end )
1006 {
1007 unsigned int extension_type;
1008 size_t extension_data_len;
1009
1010 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1011 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1012 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1013 p += 4;
1014
1015 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1016 return( 1 );
1017
1018 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1019 p += extension_data_len;
1020 }
1021
1022 return( 0 );
1023}
1024
Jerry Yu7a186a02021-10-15 18:46:14 +08001025/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001026 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1027 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1028 * - 0 otherwise
1029 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001030MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001031static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1032 const unsigned char *buf,
1033 const unsigned char *end )
1034{
1035 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1036 static const unsigned char magic_downgrade_string[] =
1037 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1038 const unsigned char *last_eight_bytes_of_random;
1039 unsigned char last_byte_of_random;
1040
1041 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1042 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1043
1044 if( memcmp( last_eight_bytes_of_random,
1045 magic_downgrade_string,
1046 sizeof( magic_downgrade_string ) ) == 0 )
1047 {
1048 last_byte_of_random = last_eight_bytes_of_random[7];
1049 return( last_byte_of_random == 0 ||
1050 last_byte_of_random == 1 );
1051 }
1052
1053 return( 0 );
1054}
1055
1056/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001057 * - SSL_SERVER_HELLO or
1058 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001059 * to indicate which message is expected and to be parsed next.
1060 */
Ronald Cron828aff62022-05-31 12:04:31 +02001061#define SSL_SERVER_HELLO 0
1062#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001063MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001064static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1065 const unsigned char *buf,
1066 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067{
Jerry Yue1b9c292021-09-10 10:08:31 +08001068
1069 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1070 *
Jerry Yu4a173382021-10-11 21:45:31 +08001071 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 * special value of the SHA-256 of "HelloRetryRequest".
1073 *
1074 * struct {
1075 * ProtocolVersion legacy_version = 0x0303;
1076 * Random random;
1077 * opaque legacy_session_id_echo<0..32>;
1078 * CipherSuite cipher_suite;
1079 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001080 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 * } ServerHello;
1082 *
1083 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1085 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001086
Jerry Yu93a13f22022-04-11 23:00:01 +08001087 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1088 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 {
Ronald Cron828aff62022-05-31 12:04:31 +02001090 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 }
1092
Ronald Cron828aff62022-05-31 12:04:31 +02001093 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001094}
1095
Ronald Cron828aff62022-05-31 12:04:31 +02001096/*
Jerry Yu745bb612021-10-13 22:01:04 +08001097 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001098 * - SSL_SERVER_HELLO or
1099 * - SSL_SERVER_HELLO_HRR or
1100 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001101 */
Ronald Cron828aff62022-05-31 12:04:31 +02001102#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001103MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001104static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001105 const unsigned char *buf,
1106 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001107{
Jerry Yu4a173382021-10-11 21:45:31 +08001108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001109 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001110
Ronald Cron9f0fba32022-02-10 16:45:15 +01001111 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001112 ssl, buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001113 if( ret == 0 )
1114 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001115 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001116 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001117
1118 /* If the server is negotiating TLS 1.2 or below and:
1119 * . we did not propose TLS 1.2 or
1120 * . the server responded it is TLS 1.3 capable but negotiating a lower
1121 * version of the protocol and thus we are under downgrade attack
1122 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001123 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001124 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001125 {
1126 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1127 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1128 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1129 }
1130
1131 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001132 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001133 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001134 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001135
1136 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1137 {
1138 ret = ssl_tls13_reset_key_share( ssl );
1139 if( ret != 0 )
1140 return( ret );
1141 }
1142
Ronald Cron828aff62022-05-31 12:04:31 +02001143 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001144 }
1145
Ronald Cron5afb9042022-05-31 12:11:39 +02001146 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1147
Ronald Crondb5dfa12022-05-31 11:44:38 +02001148 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001149 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001150 {
Ronald Cron828aff62022-05-31 12:04:31 +02001151 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1153 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001154 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001156 /* If a client receives a second
1157 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1158 * was itself in response to a HelloRetryRequest), it MUST abort the
1159 * handshake with an "unexpected_message" alert.
1160 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001161 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001162 {
1163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1164 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1165 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1166 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1167 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001168 /*
1169 * Clients must abort the handshake with an "illegal_parameter"
1170 * alert if the HelloRetryRequest would not result in any change
1171 * in the ClientHello.
1172 * In a PSK only key exchange that what we expect.
1173 */
1174 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1175 {
1176 MBEDTLS_SSL_DEBUG_MSG( 1,
1177 ( "Unexpected HRR in pure PSK key exchange." ) );
1178 MBEDTLS_SSL_PEND_FATAL_ALERT(
1179 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1180 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1181 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1182 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001183
Ronald Cron5afb9042022-05-31 12:11:39 +02001184 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001185
Jerry Yu745bb612021-10-13 22:01:04 +08001186 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001187 }
1188
1189cleanup:
1190
1191 return( ret );
1192}
1193
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001194MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001195static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1196 const unsigned char **buf,
1197 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001198{
1199 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001200 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
Jerry Yude4fb2c2021-09-19 18:05:08 +08001202 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001203 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001204
Jerry Yu4a173382021-10-11 21:45:31 +08001205 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001206
1207 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001208 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1209 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001210 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001211 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1212 ssl->session_negotiate->id,
1213 ssl->session_negotiate->id_len );
1214 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001215 legacy_session_id_echo_len );
1216
1217 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1218 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1219
Jerry Yue1b9c292021-09-10 10:08:31 +08001220 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1221 }
1222
Jerry Yu4a173382021-10-11 21:45:31 +08001223 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 *buf = p;
1225
1226 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001227 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001228 return( 0 );
1229}
1230
XiaokangQianeb69aee2022-07-05 08:21:43 +00001231#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1232/*
1233 * struct {
1234 * opaque identity<1..2^16-1>;
1235 * uint32 obfuscated_ticket_age;
1236 * } PskIdentity;
1237 *
1238 * opaque PskBinderEntry<32..255>;
1239 *
1240 * struct {
XiaokangQian008d2bf2022-07-14 07:54:01 +00001241 *
XiaokangQian86981952022-07-19 09:51:50 +00001242 * select (Handshake.msg_type) {
1243 * ...
1244 * case server_hello: uint16 selected_identity;
1245 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +00001246 *
1247 * } PreSharedKeyExtension;
1248 *
1249 */
1250
XiaokangQian86981952022-07-19 09:51:50 +00001251MBEDTLS_CHECK_RETURN_CRITICAL
1252static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1253 const unsigned char *buf,
1254 const unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001255{
1256 int ret = 0;
1257 size_t selected_identity;
1258
1259 const unsigned char *psk;
1260 size_t psk_len;
1261 const unsigned char *psk_identity;
1262 size_t psk_identity_len;
1263
1264
1265 /* Check which PSK we've offered.
1266 *
1267 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1268 * case, we need to iterate over them here.
1269 */
XiaokangQian008d2bf2022-07-14 07:54:01 +00001270 if( mbedtls_ssl_get_psk_to_offer( ssl, NULL, &psk, &psk_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +00001271 &psk_identity, &psk_identity_len ) != 0 )
1272 {
1273 /* If we haven't offered a PSK, the server must not send
1274 * a PSK identity extension. */
1275 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1276 }
1277
XiaokangQian008d2bf2022-07-14 07:54:01 +00001278 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001279 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1280
1281 /* We have offered only one PSK, so the only valid choice
1282 * for the server is PSK index 0.
1283 *
1284 * This will change once we support multiple PSKs. */
1285 if( selected_identity > 0 )
1286 {
1287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1288
1289 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1290 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1291 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1292 {
1293 return( ret );
1294 }
1295
1296 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1297 }
1298
1299 /* Set the chosen PSK
1300 *
1301 * TODO: We don't have to do this in case we offered 0-RTT and the
1302 * server accepted it, because in this case we've already
1303 * set the handshake PSK. */
1304 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1305 if( ret != 0 )
1306 {
1307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1308 return( ret );
1309 }
1310
1311 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1312 return( 0 );
1313}
1314
1315#endif
1316
Jerry Yue1b9c292021-09-10 10:08:31 +08001317/* Parse ServerHello message and configure context
1318 *
1319 * struct {
1320 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1321 * Random random;
1322 * opaque legacy_session_id_echo<0..32>;
1323 * CipherSuite cipher_suite;
1324 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001325 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001326 * } ServerHello;
1327 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001329static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1330 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001331 const unsigned char *end,
1332 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001333{
Jerry Yub85277e2021-10-13 13:36:05 +08001334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001335 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001336 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001337 size_t extensions_len;
1338 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001339 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001340 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001341 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001342
1343 /*
1344 * Check there is space for minimal fields
1345 *
1346 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001347 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001348 * - legacy_session_id_echo ( 1 byte ), minimum size
1349 * - cipher_suite ( 2 bytes)
1350 * - legacy_compression_method ( 1 byte )
1351 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001352 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001353
1354 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001355 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1356
Jerry Yu4a173382021-10-11 21:45:31 +08001357 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001358 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001359 * ...
1360 * with ProtocolVersion defined as:
1361 * uint16 ProtocolVersion;
1362 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001363 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1364 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001365 {
1366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1367 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1368 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001369 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1370 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001371 }
1372 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001373
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001374 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001375 * Random random;
1376 * ...
1377 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001378 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001379 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001380 if( !is_hrr )
1381 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001382 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001383 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1384 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1385 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1386 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001387 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001388
Jerry Yu4a173382021-10-11 21:45:31 +08001389 /* ...
1390 * opaque legacy_session_id_echo<0..32>;
1391 * ...
1392 */
1393 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001394 {
XiaokangQian52da5582022-01-26 09:49:29 +00001395 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001396 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001397 }
1398
Jerry Yu4a173382021-10-11 21:45:31 +08001399 /* ...
1400 * CipherSuite cipher_suite;
1401 * ...
1402 * with CipherSuite defined as:
1403 * uint8 CipherSuite[2];
1404 */
1405 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001406 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1407 p += 2;
1408
Jerry Yu4a173382021-10-11 21:45:31 +08001409
XiaokangQian355e09a2022-01-20 11:14:50 +00001410 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001411 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001412 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001413 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001414 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1415 ssl->tls_version,
1416 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001417 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001418 {
XiaokangQian52da5582022-01-26 09:49:29 +00001419 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001420 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001421 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001422 * If we received an HRR before and that the proposed selected
1423 * ciphersuite in this server hello is not the same as the one
1424 * proposed in the HRR, we abort the handshake and send an
1425 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001426 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001427 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1428 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001429 {
XiaokangQian52da5582022-01-26 09:49:29 +00001430 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001431 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001432
XiaokangQian52da5582022-01-26 09:49:29 +00001433 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001434 {
1435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1436 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001437 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001438 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001439
Jerry Yu4a173382021-10-11 21:45:31 +08001440 /* Configure ciphersuites */
1441 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1442
XiaokangQian355e09a2022-01-20 11:14:50 +00001443 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001444 ssl->session_negotiate->ciphersuite = cipher_suite;
1445
Jerry Yue1b9c292021-09-10 10:08:31 +08001446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1447 cipher_suite, ciphersuite_info->name ) );
1448
1449#if defined(MBEDTLS_HAVE_TIME)
1450 ssl->session_negotiate->start = time( NULL );
1451#endif /* MBEDTLS_HAVE_TIME */
1452
Jerry Yu4a173382021-10-11 21:45:31 +08001453 /* ...
1454 * uint8 legacy_compression_method = 0;
1455 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001457 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001458 if( p[0] != 0 )
1459 {
Jerry Yub85277e2021-10-13 13:36:05 +08001460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001461 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001462 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001463 }
1464 p++;
1465
Jerry Yub85277e2021-10-13 13:36:05 +08001466 /* ...
1467 * Extension extensions<6..2^16-1>;
1468 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001469 * struct {
1470 * ExtensionType extension_type; (2 bytes)
1471 * opaque extension_data<0..2^16-1>;
1472 * } Extension;
1473 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001474 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001475 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001476 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001477
Jerry Yu4a173382021-10-11 21:45:31 +08001478 /* Check extensions do not go beyond the buffer of data. */
1479 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1480 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001481
Jerry Yu4a173382021-10-11 21:45:31 +08001482 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001483
Jerry Yu4a173382021-10-11 21:45:31 +08001484 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001485 {
1486 unsigned int extension_type;
1487 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001488 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001489
Jerry Yu4a173382021-10-11 21:45:31 +08001490 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001491 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1492 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1493 p += 4;
1494
Jerry Yu4a173382021-10-11 21:45:31 +08001495 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001496 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001497
1498 switch( extension_type )
1499 {
XiaokangQianb851da82022-01-14 04:03:11 +00001500 case MBEDTLS_TLS_EXT_COOKIE:
1501
XiaokangQiand9e068e2022-01-18 06:23:32 +00001502 if( !is_hrr )
1503 {
XiaokangQian52da5582022-01-26 09:49:29 +00001504 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001505 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001506 }
1507
XiaokangQian43550bd2022-01-21 04:32:58 +00001508 ret = ssl_tls13_parse_cookie_ext( ssl,
1509 p, extension_data_end );
1510 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001511 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001512 MBEDTLS_SSL_DEBUG_RET( 1,
1513 "ssl_tls13_parse_cookie_ext",
1514 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001515 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001516 }
XiaokangQianb851da82022-01-14 04:03:11 +00001517 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001518
Jerry Yue1b9c292021-09-10 10:08:31 +08001519 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001520 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001521 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001522 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001523 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001524 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001525 break;
1526
XiaokangQianeb69aee2022-07-05 08:21:43 +00001527#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001528 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1530 if( is_hrr )
1531 {
1532 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1533 goto cleanup;
1534 }
Jerry Yu4a173382021-10-11 21:45:31 +08001535
XiaokangQian86981952022-07-19 09:51:50 +00001536 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001537 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001538 {
1539 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001540 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001541 return( ret );
1542 }
1543 break;
1544#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001545
Jerry Yue1b9c292021-09-10 10:08:31 +08001546 case MBEDTLS_TLS_EXT_KEY_SHARE:
1547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001548 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1549 {
XiaokangQian52da5582022-01-26 09:49:29 +00001550 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001551 goto cleanup;
1552 }
1553
XiaokangQian53f20b72022-01-18 10:47:33 +00001554 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001555 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001556 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001557 else
XiaokangQianb851da82022-01-14 04:03:11 +00001558 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001559 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001560 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001561 {
1562 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001563 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001564 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001565 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001566 }
1567 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001568
1569 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001570 MBEDTLS_SSL_DEBUG_MSG(
1571 3,
1572 ( "unknown extension found: %u ( ignoring )",
1573 extension_type ) );
1574
XiaokangQian52da5582022-01-26 09:49:29 +00001575 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001576 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001577 }
1578
1579 p += extension_data_len;
1580 }
1581
XiaokangQiand59be772022-01-24 10:12:51 +00001582cleanup:
1583
XiaokangQian52da5582022-01-26 09:49:29 +00001584 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001585 {
1586 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1587 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1588 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1589 }
XiaokangQian52da5582022-01-26 09:49:29 +00001590 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001591 {
1592 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1593 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1594 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1595 }
1596 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001597}
1598
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001599MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001600static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001601{
Jerry Yub85277e2021-10-13 13:36:05 +08001602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001604
Jerry Yub85277e2021-10-13 13:36:05 +08001605 /* Determine the key exchange mode:
1606 * 1) If both the pre_shared_key and key_share extensions were received
1607 * then the key exchange mode is PSK with EPHEMERAL.
1608 * 2) If only the pre_shared_key extension was received then the key
1609 * exchange mode is PSK-only.
1610 * 3) If only the key_share extension was received then the key
1611 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001612 */
Jerry Yub85277e2021-10-13 13:36:05 +08001613 switch( handshake->extensions_present &
1614 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001615 {
Jerry Yu745bb612021-10-13 22:01:04 +08001616 /* Only the pre_shared_key extension was received */
1617 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001618 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001619 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001620
Jerry Yu745bb612021-10-13 22:01:04 +08001621 /* Only the key_share extension was received */
1622 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001623 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001624 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001625
Jerry Yu745bb612021-10-13 22:01:04 +08001626 /* Both the pre_shared_key and key_share extensions were received */
1627 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001628 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001629 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001630
Jerry Yu745bb612021-10-13 22:01:04 +08001631 /* Neither pre_shared_key nor key_share extension was received */
1632 default:
1633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1634 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1635 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001636 }
1637
1638 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1639 *
1640 * TODO: We don't have to do this in case we offered 0-RTT and the
1641 * server accepted it. In this case, we could skip generating
1642 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001643 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001644 if( ret != 0 )
1645 {
Jerry Yue110d252022-05-05 10:19:22 +08001646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001647 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001648 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001649 }
1650
Jerry Yuf86eb752022-05-06 11:16:55 +08001651 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001652 if( ret != 0 )
1653 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001654 MBEDTLS_SSL_DEBUG_RET( 1,
1655 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001656 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001657 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001658 }
1659
Jerry Yue110d252022-05-05 10:19:22 +08001660 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1662 ssl->session_in = ssl->session_negotiate;
1663
Jerry Yu4a173382021-10-11 21:45:31 +08001664cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001665 if( ret != 0 )
1666 {
Jerry Yu4a173382021-10-11 21:45:31 +08001667 MBEDTLS_SSL_PEND_FATAL_ALERT(
1668 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1669 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1670 }
Jerry Yue110d252022-05-05 10:19:22 +08001671
Jerry Yu4a173382021-10-11 21:45:31 +08001672 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001673}
1674
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001675MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001676static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001677{
1678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1679
XiaokangQian78b1fa72022-01-19 06:56:30 +00001680 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001681
XiaokangQian78b1fa72022-01-19 06:56:30 +00001682 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001683 * We are going to re-generate a shared secret corresponding to the group
1684 * selected by the server, which is different from the group for which we
1685 * generated a shared secret in the first client hello.
1686 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001687 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001688 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001689 if( ret != 0 )
1690 return( ret );
1691
1692 return( 0 );
1693}
1694
Jerry Yue1b9c292021-09-10 10:08:31 +08001695/*
Jerry Yu4a173382021-10-11 21:45:31 +08001696 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001697 * Handler for MBEDTLS_SSL_SERVER_HELLO
1698 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001699MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001700static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001701{
Jerry Yu4a173382021-10-11 21:45:31 +08001702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001703 unsigned char *buf = NULL;
1704 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001705 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001706
1707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1708
Ronald Crondb5dfa12022-05-31 11:44:38 +02001709 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1710 MBEDTLS_SSL_HS_SERVER_HELLO,
1711 &buf, &buf_len ) );
1712
Ronald Cron828aff62022-05-31 12:04:31 +02001713 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001714 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001715 goto cleanup;
1716 else
Ronald Cron828aff62022-05-31 12:04:31 +02001717 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001718
Ronald Cron828aff62022-05-31 12:04:31 +02001719 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001720 {
1721 ret = 0;
1722 goto cleanup;
1723 }
1724
XiaokangQianb851da82022-01-14 04:03:11 +00001725 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001726 buf + buf_len,
1727 is_hrr ) );
1728 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001729 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1730
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001731 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1732 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001733
XiaokangQiand9e068e2022-01-18 06:23:32 +00001734 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001735 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001736 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001737#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1738 /* If not offering early data, the client sends a dummy CCS record
1739 * immediately before its second flight. This may either be before
1740 * its second ClientHello or before its encrypted handshake flight.
1741 */
1742 mbedtls_ssl_handshake_set_state( ssl,
1743 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1744#else
1745 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1746#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1747 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001748 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001749 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001750 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1752 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001753
1754cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1756 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001757 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001758}
1759
1760/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001761 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001762 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001763 *
1764 * The EncryptedExtensions message contains any extensions which
1765 * should be protected, i.e., any which are not needed to establish
1766 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001767 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001768
XiaokangQian08da26c2021-10-09 10:12:11 +00001769/* Parse EncryptedExtensions message
1770 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001771 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001772 * } EncryptedExtensions;
1773 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001774MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001775static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1776 const unsigned char *buf,
1777 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001778{
1779 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001780 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001781 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001782 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001783
XiaokangQian08da26c2021-10-09 10:12:11 +00001784 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001785 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001786 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001787
XiaokangQian97799ac2021-10-11 10:05:54 +00001788 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001789 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001790 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001791
XiaokangQian8db25ff2021-10-13 05:56:18 +00001792 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001793 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001794 unsigned int extension_type;
1795 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001796
XiaokangQian08da26c2021-10-09 10:12:11 +00001797 /*
1798 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001799 * ExtensionType extension_type; (2 bytes)
1800 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001801 * } Extension;
1802 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001803 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001804 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1805 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1806 p += 4;
1807
XiaokangQian8db25ff2021-10-13 05:56:18 +00001808 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001809
XiaokangQian97799ac2021-10-11 10:05:54 +00001810 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001811 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001812 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001813 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001814 switch( extension_type )
1815 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001816
XiaokangQian08da26c2021-10-09 10:12:11 +00001817 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1819 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001820
lhuang0486cacac2022-01-21 07:34:27 -08001821#if defined(MBEDTLS_SSL_ALPN)
1822 case MBEDTLS_TLS_EXT_ALPN:
1823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1824
1825 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1826 {
1827 return( ret );
1828 }
1829
1830 break;
1831#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001832 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001833 MBEDTLS_SSL_DEBUG_MSG(
1834 3, ( "unsupported extension found: %u ", extension_type) );
1835 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001836 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001837 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1838 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001839 }
1840
XiaokangQian08da26c2021-10-09 10:12:11 +00001841 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001842 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001843
XiaokangQian97799ac2021-10-11 10:05:54 +00001844 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001845 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001846 {
1847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001848 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001849 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001850 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001851 }
1852
1853 return( ret );
1854}
1855
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001856MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001857static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1858{
1859 int ret;
1860 unsigned char *buf;
1861 size_t buf_len;
1862
1863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1864
1865 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1866 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1867 &buf, &buf_len ) );
1868
1869 /* Process the message contents */
1870 MBEDTLS_SSL_PROC_CHK(
1871 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1872
1873 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1874 buf, buf_len );
1875
Ronald Cronfb508b82022-05-31 14:49:55 +02001876#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001877 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001878 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1879 else
1880 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1881#else
1882 ((void) ssl);
1883 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1884#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001885
1886cleanup:
1887
1888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1889 return( ret );
1890
1891}
1892
Jerry Yua93ac112021-10-27 16:31:48 +08001893#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001894/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001895 * STATE HANDLING: CertificateRequest
1896 *
Jerry Yud2674312021-10-29 10:08:19 +08001897 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001898#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1899#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001900/* Coordination:
1901 * Deals with the ambiguity of not knowing if a CertificateRequest
1902 * will be sent. Returns a negative code on failure, or
1903 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1904 * - SSL_CERTIFICATE_REQUEST_SKIP
1905 * indicating if a Certificate Request is expected or not.
1906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001908static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001909{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001911
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001912 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001913 {
1914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1915 return( ret );
1916 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001917 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001918
1919 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1920 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1921 {
Ronald Cron19385882022-06-15 16:26:13 +02001922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001923 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001924 }
1925
Ronald Cron19385882022-06-15 16:26:13 +02001926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1927
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001928 return( SSL_CERTIFICATE_REQUEST_SKIP );
1929}
1930
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001931/*
1932 * ssl_tls13_parse_certificate_request()
1933 * Parse certificate request
1934 * struct {
1935 * opaque certificate_request_context<0..2^8-1>;
1936 * Extension extensions<2..2^16-1>;
1937 * } CertificateRequest;
1938 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001939MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001940static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1941 const unsigned char *buf,
1942 const unsigned char *end )
1943{
1944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1945 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001946 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001947 size_t extensions_len = 0;
1948 const unsigned char *extensions_end;
1949 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001950
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001951 /* ...
1952 * opaque certificate_request_context<0..2^8-1>
1953 * ...
1954 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1956 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001957 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001958
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001959 if( certificate_request_context_len > 0 )
1960 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001961 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001962 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1963 p, certificate_request_context_len );
1964
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001965 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001966 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001967 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001968 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001969 {
1970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1971 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1972 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001973 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001974 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001975 p += certificate_request_context_len;
1976 }
1977
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001978 /* ...
1979 * Extension extensions<2..2^16-1>;
1980 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001981 */
1982 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1983 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1984 p += 2;
1985
1986 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1987 extensions_end = p + extensions_len;
1988
1989 while( p < extensions_end )
1990 {
1991 unsigned int extension_type;
1992 size_t extension_data_len;
1993
1994 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1995 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1996 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1997 p += 4;
1998
1999 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2000
2001 switch( extension_type )
2002 {
2003 case MBEDTLS_TLS_EXT_SIG_ALG:
2004 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002005 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002006 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002007 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002008 if( ret != 0 )
2009 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002010 if( ! sig_alg_ext_found )
2011 sig_alg_ext_found = 1;
2012 else
2013 {
2014 MBEDTLS_SSL_DEBUG_MSG( 3,
2015 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002016 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002017 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002018 break;
2019
2020 default:
2021 MBEDTLS_SSL_DEBUG_MSG(
2022 3,
2023 ( "unknown extension found: %u ( ignoring )",
2024 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002025 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002026 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002027 p += extension_data_len;
2028 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002029 /* Check that we consumed all the message. */
2030 if( p != end )
2031 {
2032 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002033 ( "CertificateRequest misaligned" ) );
2034 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002035 }
2036 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002037 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002038 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002039 MBEDTLS_SSL_DEBUG_MSG( 3,
2040 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002041 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002042 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002043
Jerry Yu7840f812022-01-29 10:26:51 +08002044 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002045 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002046
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002047decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002048 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2049 MBEDTLS_ERR_SSL_DECODE_ERROR );
2050 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002051}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002052
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002053/*
2054 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2055 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002056MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002057static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002058{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002060
2061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2062
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002063 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2064
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002065 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2066 {
2067 unsigned char *buf;
2068 size_t buf_len;
2069
2070 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2071 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2072 &buf, &buf_len ) );
2073
2074 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2075 buf, buf + buf_len ) );
2076
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002077 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2078 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002079 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002080 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002081 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002082 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002083 }
2084 else
2085 {
2086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002087 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2088 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002089 }
2090
Jerry Yud2674312021-10-29 10:08:19 +08002091 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2092
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002093cleanup:
2094
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2096 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002097}
2098
2099/*
Jerry Yu687101b2021-09-14 16:03:56 +08002100 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2101 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002102MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002103static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002104{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002105 int ret;
2106
2107 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002108 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002109 return( ret );
2110
Jerry Yu687101b2021-09-14 16:03:56 +08002111 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2112 return( 0 );
2113}
2114
2115/*
2116 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2117 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002118MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002119static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002120{
Jerry Yu30b071c2021-09-12 20:16:03 +08002121 int ret;
2122
2123 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2124 if( ret != 0 )
2125 return( ret );
2126
Jerry Yu687101b2021-09-14 16:03:56 +08002127 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2128 return( 0 );
2129}
Jerry Yua93ac112021-10-27 16:31:48 +08002130#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002131
Jerry Yu687101b2021-09-14 16:03:56 +08002132/*
2133 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002135MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002136static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002137{
XiaokangQianac0385c2021-11-03 06:40:11 +00002138 int ret;
2139
XiaokangQianc5c39d52021-11-09 11:55:10 +00002140 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002141 if( ret != 0 )
2142 return( ret );
2143
Jerry Yue3d67cb2022-05-19 15:33:10 +08002144 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2145 if( ret != 0 )
2146 {
2147 MBEDTLS_SSL_PEND_FATAL_ALERT(
2148 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2149 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2150 return( ret );
2151 }
2152
Ronald Cron49ad6192021-11-24 16:25:31 +01002153#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2154 mbedtls_ssl_handshake_set_state(
2155 ssl,
2156 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2157#else
Jerry Yuca133a32022-02-15 14:22:05 +08002158 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002159#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002160
XiaokangQianac0385c2021-11-03 06:40:11 +00002161 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002162}
2163
2164/*
Jerry Yu566c7812022-01-26 15:41:22 +08002165 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2166 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002167MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002168static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2169{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002170 int non_empty_certificate_msg = 0;
2171
Jerry Yu5cc35062022-01-28 16:16:08 +08002172 MBEDTLS_SSL_DEBUG_MSG( 1,
2173 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002174 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002175
Ronald Cron9df7c802022-03-08 18:38:54 +01002176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002177 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002178 {
2179 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2180 if( ret != 0 )
2181 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002182
Ronald Cron7a94aca2022-03-09 07:44:27 +01002183 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2184 non_empty_certificate_msg = 1;
2185 }
2186 else
2187 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002189 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002190#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002191
Ronald Cron7a94aca2022-03-09 07:44:27 +01002192 if( non_empty_certificate_msg )
2193 {
2194 mbedtls_ssl_handshake_set_state( ssl,
2195 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2196 }
2197 else
Ronald Cron19385882022-06-15 16:26:13 +02002198 {
2199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002200 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002201 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002202
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002203 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002204}
2205
Ronald Cron9df7c802022-03-08 18:38:54 +01002206#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002207/*
2208 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2209 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002210MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002211static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2212{
Ronald Crona8b38872022-03-09 07:59:25 +01002213 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2214
2215 if( ret == 0 )
2216 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2217
2218 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002219}
Jerry Yu90f152d2022-01-29 22:12:42 +08002220#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002221
2222/*
Jerry Yu687101b2021-09-14 16:03:56 +08002223 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2224 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002225MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002226static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002227{
XiaokangQian0fa66432021-11-15 03:33:57 +00002228 int ret;
2229
2230 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2231 if( ret != 0 )
2232 return( ret );
2233
Jerry Yue3d67cb2022-05-19 15:33:10 +08002234 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2235 if( ret != 0 )
2236 {
2237 MBEDTLS_SSL_DEBUG_RET( 1,
2238 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2239 return ( ret );
2240 }
2241
XiaokangQian0fa66432021-11-15 03:33:57 +00002242 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2243 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002244}
2245
2246/*
2247 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002249MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002250static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002251{
Jerry Yu378254d2021-10-30 21:44:47 +08002252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002253 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002254 return( 0 );
2255}
2256
2257/*
2258 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2259 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002260MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002261static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002262{
Jerry Yu378254d2021-10-30 21:44:47 +08002263
2264 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2265
2266 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2267 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002268}
2269
Jerry Yuf8a49942022-07-07 11:32:32 +00002270#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2271
Jerry Yua0446a02022-07-13 11:22:55 +08002272MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002273static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2274 const unsigned char *buf,
2275 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002276{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002277 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002278
2279 ((void) ssl);
2280
2281 while( p < end )
2282 {
2283 unsigned int extension_type;
2284 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002285
Jerry Yuf8a49942022-07-07 11:32:32 +00002286 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2287 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2288 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2289 p += 4;
2290
2291 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002292
2293 switch( extension_type )
2294 {
2295 case MBEDTLS_TLS_EXT_EARLY_DATA:
2296 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2297 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002298
Jerry Yuf8a49942022-07-07 11:32:32 +00002299 default:
2300 break;
2301 }
2302 p += extension_data_len;
2303 }
2304
2305 return( 0 );
2306}
2307
2308/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002309 * From RFC8446, page 74
2310 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002311 * struct {
2312 * uint32 ticket_lifetime;
2313 * uint32 ticket_age_add;
2314 * opaque ticket_nonce<0..255>;
2315 * opaque ticket<1..2^16-1>;
2316 * Extension extensions<0..2^16-2>;
2317 * } NewSessionTicket;
2318 *
2319 */
Jerry Yua0446a02022-07-13 11:22:55 +08002320MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002321static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2322 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002323 unsigned char *end,
2324 unsigned char **ticket_nonce,
2325 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002326{
2327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2328 unsigned char *p = buf;
2329 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002330 size_t ticket_len;
2331 unsigned char *ticket;
2332 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002333
Jerry Yucb3b1392022-07-12 06:09:38 +00002334 *ticket_nonce = NULL;
2335 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002336 /*
2337 * ticket_lifetime 4 bytes
2338 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002339 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002340 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002341 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002342
2343 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002344 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002345 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002346 ( unsigned int )session->ticket_lifetime ) );
2347
Jerry Yucb3b1392022-07-12 06:09:38 +00002348 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002349 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002350 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002351 ( unsigned int )session->ticket_age_add ) );
2352
Jerry Yucb3b1392022-07-12 06:09:38 +00002353 *ticket_nonce_len = p[8];
2354 p += 9;
2355
2356 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2357 *ticket_nonce = p;
2358 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2359 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002360
2361 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002362 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002363 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2364 p += 2;
2365 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002366 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2367
2368 /* Check if we previously received a ticket already. */
2369 if( session->ticket != NULL || session->ticket_len > 0 )
2370 {
2371 mbedtls_free( session->ticket );
2372 session->ticket = NULL;
2373 session->ticket_len = 0;
2374 }
2375
2376 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2377 {
2378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2380 }
2381 memcpy( ticket, p, ticket_len );
2382 p += ticket_len;
2383 session->ticket = ticket;
2384 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002385
Jerry Yucb3b1392022-07-12 06:09:38 +00002386 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002387 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2388 p += 2;
2389 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2390
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002391 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002392
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002393 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002394 if( ret != 0 )
2395 {
2396 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002397 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002398 ret );
2399 return( ret );
2400 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002401
2402 return( 0 );
2403}
2404
Jerry Yua0446a02022-07-13 11:22:55 +08002405MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002406static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2407 unsigned char *ticket_nonce,
2408 size_t ticket_nonce_len )
2409{
2410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2411 mbedtls_ssl_session *session = ssl->session;
2412 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2413 psa_algorithm_t psa_hash_alg;
2414 int hash_length;
2415
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002416#if defined(MBEDTLS_HAVE_TIME)
2417 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002418 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002419#endif
2420
Jerry Yuf8a49942022-07-07 11:32:32 +00002421 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2422 if( ciphersuite_info == NULL )
2423 {
2424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2426 }
2427
2428 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2429 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002430 if( hash_length == -1 ||
2431 ( size_t )hash_length > sizeof( session->resumption_key ) )
2432 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002434 }
2435
Jerry Yuf8a49942022-07-07 11:32:32 +00002436
2437 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2438 session->app_secrets.resumption_master_secret,
2439 hash_length );
2440
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002441 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002442 *
2443 * HKDF-Expand-Label( resumption_master_secret,
2444 * "resumption", ticket_nonce, Hash.length )
2445 */
2446 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2447 psa_hash_alg,
2448 session->app_secrets.resumption_master_secret,
2449 hash_length,
2450 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2451 ticket_nonce,
2452 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002453 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002454 hash_length );
2455
2456 if( ret != 0 )
2457 {
2458 MBEDTLS_SSL_DEBUG_RET( 2,
2459 "Creating the ticket-resumed PSK failed",
2460 ret );
2461 return( ret );
2462 }
2463
Jerry Yu0a430c82022-07-20 11:02:48 +08002464 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002465
2466 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002467 session->resumption_key,
2468 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002469
Jerry Yuf8a49942022-07-07 11:32:32 +00002470 return( 0 );
2471}
2472
2473/*
Jerry Yua357cf42022-07-12 05:36:45 +00002474 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002475 */
Jerry Yua0446a02022-07-13 11:22:55 +08002476MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002477static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2478{
2479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2480 unsigned char *buf;
2481 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002482 unsigned char *ticket_nonce;
2483 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002484
2485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2486
2487 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2488 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2489 &buf, &buf_len ) );
2490
Jerry Yucb3b1392022-07-12 06:09:38 +00002491 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2492 ssl, buf, buf + buf_len,
2493 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002494
Jerry Yucb3b1392022-07-12 06:09:38 +00002495 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2496 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002497
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002498 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2499
Jerry Yuf8a49942022-07-07 11:32:32 +00002500cleanup:
2501
2502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2503 return( ret );
2504}
2505#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2506
Jerry Yu92c6b402021-08-27 16:59:09 +08002507int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002508{
Jerry Yu92c6b402021-08-27 16:59:09 +08002509 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002510
Jerry Yu92c6b402021-08-27 16:59:09 +08002511 switch( ssl->state )
2512 {
2513 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002514 * ssl->state is initialized as HELLO_REQUEST. It is the same
2515 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002516 */
2517 case MBEDTLS_SSL_HELLO_REQUEST:
2518 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002519 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002520 break;
2521
2522 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002523 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002524 break;
2525
2526 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002527 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002528 break;
2529
Jerry Yua93ac112021-10-27 16:31:48 +08002530#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002531 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2532 ret = ssl_tls13_process_certificate_request( ssl );
2533 break;
2534
Jerry Yu687101b2021-09-14 16:03:56 +08002535 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002536 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002537 break;
2538
2539 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002540 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002541 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002542#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002543
2544 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002545 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002546 break;
2547
Jerry Yu566c7812022-01-26 15:41:22 +08002548 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2549 ret = ssl_tls13_write_client_certificate( ssl );
2550 break;
2551
Ronald Cron9df7c802022-03-08 18:38:54 +01002552#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002553 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2554 ret = ssl_tls13_write_client_certificate_verify( ssl );
2555 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002556#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002557
Jerry Yu687101b2021-09-14 16:03:56 +08002558 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002559 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002560 break;
2561
2562 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002563 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002564 break;
2565
2566 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002567 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002568 break;
2569
Ronald Cron49ad6192021-11-24 16:25:31 +01002570 /*
2571 * Injection of dummy-CCS's for middlebox compatibility
2572 */
2573#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002574 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002575 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2576 if( ret == 0 )
2577 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2578 break;
2579
2580 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2581 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2582 if( ret == 0 )
2583 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002584 break;
2585#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2586
Jerry Yuf8a49942022-07-07 11:32:32 +00002587#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002588 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002589 ret = ssl_tls13_process_new_session_ticket( ssl );
2590 if( ret != 0 )
2591 break;
2592 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2593 break;
2594#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2595
Jerry Yu92c6b402021-08-27 16:59:09 +08002596 default:
2597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2599 }
2600
2601 return( ret );
2602}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002603
Jerry Yufb4b6472022-01-27 15:03:26 +08002604#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002605
Jerry Yufb4b6472022-01-27 15:03:26 +08002606