blob: 5c693bdb0e0ba632f79275c8af5450799463bc41 [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
695int mbedtls_ssl_tls13_write_pre_shared_key_ext_without_binders(
696 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;
786 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + 2 + identities_len + 2 + l_binders_len );
787
788 MBEDTLS_SSL_DEBUG_MSG( 3,
789 ( "client hello, adding pre_shared_key extension, "
790 "omitting PSK binder list" ) );
791
792 /* Extension header */
793 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;
799 memcpy( p, psk_identity, psk_identity_len );
800 p += psk_identity_len;
801
802 /* add obfuscated ticket age */
803 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 0 );
804 p += 4;
805
806 *out_len = ( p - buf ) + l_binders_len + 2;
807 *binders_len = l_binders_len + 2;
808
809 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
810
811 return( 0 );
812}
813
XiaokangQian86981952022-07-19 09:51:50 +0000814int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000815 mbedtls_ssl_context *ssl,
816 unsigned char *buf, unsigned char *end )
817{
818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
819 unsigned char *p = buf;
XiaokangQianadab9a62022-07-18 07:41:26 +0000820 const unsigned char *psk_identity;
821 size_t psk_identity_len;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000822 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
823 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000824 int hash_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000825 const unsigned char *psk = NULL;
826 size_t psk_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000827 int psk_type;
828 unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
829 size_t transcript_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000830
XiaokangQianadab9a62022-07-18 07:41:26 +0000831 if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
832 &psk_identity, &psk_identity_len ) != 0 )
833 {
834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
835 }
836
XiaokangQian008d2bf2022-07-14 07:54:01 +0000837 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
838 ssl->session_negotiate->ciphersuite );
839 if( ciphersuite_info == NULL )
840 return( 0 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000841
XiaokangQian008d2bf2022-07-14 07:54:01 +0000842 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
843 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000844 if( ( hash_len == -1 ) || ( ( end - buf ) != 3 + hash_len ) )
845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
846
847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list" ) );
848
XiaokangQian008d2bf2022-07-14 07:54:01 +0000849 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 + hash_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000850 /* 2 bytes length field for array of psk binders */
851 MBEDTLS_PUT_UINT16_BE( hash_len + 1, p, 0 );
852 p += 2;
853
854 /* 1 bytes length field for next psk binder */
855 *p++ = MBEDTLS_BYTE_0( hash_len );
856
XiaokangQianeb69aee2022-07-05 08:21:43 +0000857 /* Get current state of handshake transcript. */
XiaokangQian008d2bf2022-07-14 07:54:01 +0000858 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000859 transcript, sizeof( transcript ),
860 &transcript_len );
861 if( ret != 0 )
862 return( ret );
863
864 ret = mbedtls_ssl_tls13_create_psk_binder( ssl,
XiaokangQian008d2bf2022-07-14 07:54:01 +0000865 mbedtls_psa_translate_md( ciphersuite_info->mac ),
XiaokangQianeb69aee2022-07-05 08:21:43 +0000866 psk, psk_len, psk_type,
867 transcript, p );
868 if( ret != 0 )
869 {
870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
871 return( ret );
872 }
873
874 return( 0 );
875}
876#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
877
Ronald Cron3d580bf2022-02-18 17:24:56 +0100878int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
879 unsigned char *buf,
880 unsigned char *end,
881 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100882{
883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
884 unsigned char *p = buf;
885 size_t ext_len;
886
887 *out_len = 0;
888
889 /* Write supported_versions extension
890 *
891 * Supported Versions Extension is mandatory with TLS 1.3.
892 */
893 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
894 if( ret != 0 )
895 return( ret );
896 p += ext_len;
897
898 /* Echo the cookie if the server provided one in its preceding
899 * HelloRetryRequest message.
900 */
901 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
902 if( ret != 0 )
903 return( ret );
904 p += ext_len;
905
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100906 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
907 {
908 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
909 if( ret != 0 )
910 return( ret );
911 p += ext_len;
912 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100913
XiaokangQianeb69aee2022-07-05 08:21:43 +0000914#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
915 /* For PSK-based key exchange we need the pre_shared_key extension
916 * and the psk_key_exchange_modes extension.
917 *
918 * The pre_shared_key extension MUST be the last extension in the
919 * ClientHello. Servers MUST check that it is the last extension and
920 * otherwise fail the handshake with an "illegal_parameter" alert.
921 *
922 * Add the psk_key_exchange_modes extension.
923 */
924 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
925 if( ret != 0 )
926 return( ret );
927 p += ext_len;
928#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
929
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100930 *out_len = p - buf;
931
932 return( 0 );
933}
934
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800935/*
Jerry Yu4a173382021-10-11 21:45:31 +0800936 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800937 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200938
Ronald Cronda41b382022-03-30 09:57:11 +0200939/**
940 * \brief Detect if the ServerHello contains a supported_versions extension
941 * or not.
942 *
943 * \param[in] ssl SSL context
944 * \param[in] buf Buffer containing the ServerHello message
945 * \param[in] end End of the buffer containing the ServerHello message
946 *
947 * \return 0 if the ServerHello does not contain a supported_versions extension
948 * \return 1 if the ServerHello contains a supported_versions extension
949 * \return A negative value if an error occurred while parsing the ServerHello.
950 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200951MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +0100952static int ssl_tls13_is_supported_versions_ext_present(
953 mbedtls_ssl_context *ssl,
954 const unsigned char *buf,
955 const unsigned char *end )
956{
957 const unsigned char *p = buf;
958 size_t legacy_session_id_echo_len;
959 size_t extensions_len;
960 const unsigned char *extensions_end;
961
962 /*
963 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200964 * length:
965 * - legacy_version 2 bytes
966 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
967 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100968 */
969 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
970 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
971 legacy_session_id_echo_len = *p;
972
973 /*
974 * Jump to the extensions, jumping over:
975 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
976 * - cipher_suite 2 bytes
977 * - legacy_compression_method 1 byte
978 */
Ronald Cron28271062022-06-10 14:43:55 +0200979 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100980 p += legacy_session_id_echo_len + 4;
981
982 /* Case of no extension */
983 if( p == end )
984 return( 0 );
985
986 /* ...
987 * Extension extensions<6..2^16-1>;
988 * ...
989 * struct {
990 * ExtensionType extension_type; (2 bytes)
991 * opaque extension_data<0..2^16-1>;
992 * } Extension;
993 */
994 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
995 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
996 p += 2;
997
998 /* Check extensions do not go beyond the buffer of data. */
999 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1000 extensions_end = p + extensions_len;
1001
1002 while( p < extensions_end )
1003 {
1004 unsigned int extension_type;
1005 size_t extension_data_len;
1006
1007 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1008 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1009 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1010 p += 4;
1011
1012 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1013 return( 1 );
1014
1015 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1016 p += extension_data_len;
1017 }
1018
1019 return( 0 );
1020}
1021
Jerry Yu7a186a02021-10-15 18:46:14 +08001022/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001023 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1024 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1025 * - 0 otherwise
1026 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001027MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001028static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1029 const unsigned char *buf,
1030 const unsigned char *end )
1031{
1032 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1033 static const unsigned char magic_downgrade_string[] =
1034 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1035 const unsigned char *last_eight_bytes_of_random;
1036 unsigned char last_byte_of_random;
1037
1038 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1039 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1040
1041 if( memcmp( last_eight_bytes_of_random,
1042 magic_downgrade_string,
1043 sizeof( magic_downgrade_string ) ) == 0 )
1044 {
1045 last_byte_of_random = last_eight_bytes_of_random[7];
1046 return( last_byte_of_random == 0 ||
1047 last_byte_of_random == 1 );
1048 }
1049
1050 return( 0 );
1051}
1052
1053/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001054 * - SSL_SERVER_HELLO or
1055 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001056 * to indicate which message is expected and to be parsed next.
1057 */
Ronald Cron828aff62022-05-31 12:04:31 +02001058#define SSL_SERVER_HELLO 0
1059#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001060MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001061static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1062 const unsigned char *buf,
1063 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001064{
Jerry Yue1b9c292021-09-10 10:08:31 +08001065
1066 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1067 *
Jerry Yu4a173382021-10-11 21:45:31 +08001068 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001069 * special value of the SHA-256 of "HelloRetryRequest".
1070 *
1071 * struct {
1072 * ProtocolVersion legacy_version = 0x0303;
1073 * Random random;
1074 * opaque legacy_session_id_echo<0..32>;
1075 * CipherSuite cipher_suite;
1076 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001077 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001078 * } ServerHello;
1079 *
1080 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001081 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1082 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001083
Jerry Yu93a13f22022-04-11 23:00:01 +08001084 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1085 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001086 {
Ronald Cron828aff62022-05-31 12:04:31 +02001087 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001088 }
1089
Ronald Cron828aff62022-05-31 12:04:31 +02001090 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091}
1092
Ronald Cron828aff62022-05-31 12:04:31 +02001093/*
Jerry Yu745bb612021-10-13 22:01:04 +08001094 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001095 * - SSL_SERVER_HELLO or
1096 * - SSL_SERVER_HELLO_HRR or
1097 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001098 */
Ronald Cron828aff62022-05-31 12:04:31 +02001099#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001100MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001101static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001102 const unsigned char *buf,
1103 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001104{
Jerry Yu4a173382021-10-11 21:45:31 +08001105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001106 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001107
Ronald Cron9f0fba32022-02-10 16:45:15 +01001108 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001109 ssl, buf, end ) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001110 if( ret == 0 )
1111 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001112 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001113 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001114
1115 /* If the server is negotiating TLS 1.2 or below and:
1116 * . we did not propose TLS 1.2 or
1117 * . the server responded it is TLS 1.3 capable but negotiating a lower
1118 * version of the protocol and thus we are under downgrade attack
1119 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001120 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001121 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001122 {
1123 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1124 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1125 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1126 }
1127
1128 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001129 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001130 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001131 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001132
1133 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1134 {
1135 ret = ssl_tls13_reset_key_share( ssl );
1136 if( ret != 0 )
1137 return( ret );
1138 }
1139
Ronald Cron828aff62022-05-31 12:04:31 +02001140 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001141 }
1142
Ronald Cron5afb9042022-05-31 12:11:39 +02001143 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1144
Ronald Crondb5dfa12022-05-31 11:44:38 +02001145 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001146 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001147 {
Ronald Cron828aff62022-05-31 12:04:31 +02001148 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1150 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001151 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001153 /* If a client receives a second
1154 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1155 * was itself in response to a HelloRetryRequest), it MUST abort the
1156 * handshake with an "unexpected_message" alert.
1157 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001158 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001159 {
1160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1161 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1162 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1163 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1164 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001165 /*
1166 * Clients must abort the handshake with an "illegal_parameter"
1167 * alert if the HelloRetryRequest would not result in any change
1168 * in the ClientHello.
1169 * In a PSK only key exchange that what we expect.
1170 */
1171 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1172 {
1173 MBEDTLS_SSL_DEBUG_MSG( 1,
1174 ( "Unexpected HRR in pure PSK key exchange." ) );
1175 MBEDTLS_SSL_PEND_FATAL_ALERT(
1176 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1177 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1178 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1179 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001180
Ronald Cron5afb9042022-05-31 12:11:39 +02001181 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001182
Jerry Yu745bb612021-10-13 22:01:04 +08001183 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001184 }
1185
1186cleanup:
1187
1188 return( ret );
1189}
1190
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001191MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001192static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1193 const unsigned char **buf,
1194 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001195{
1196 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001197 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001198
Jerry Yude4fb2c2021-09-19 18:05:08 +08001199 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001200 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001201
Jerry Yu4a173382021-10-11 21:45:31 +08001202 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001203
1204 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001205 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1206 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001207 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001208 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1209 ssl->session_negotiate->id,
1210 ssl->session_negotiate->id_len );
1211 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001212 legacy_session_id_echo_len );
1213
1214 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1215 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1216
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1218 }
1219
Jerry Yu4a173382021-10-11 21:45:31 +08001220 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001221 *buf = p;
1222
1223 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001224 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001225 return( 0 );
1226}
1227
XiaokangQianeb69aee2022-07-05 08:21:43 +00001228#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1229/*
1230 * struct {
1231 * opaque identity<1..2^16-1>;
1232 * uint32 obfuscated_ticket_age;
1233 * } PskIdentity;
1234 *
1235 * opaque PskBinderEntry<32..255>;
1236 *
1237 * struct {
XiaokangQian008d2bf2022-07-14 07:54:01 +00001238 *
XiaokangQian86981952022-07-19 09:51:50 +00001239 * select (Handshake.msg_type) {
1240 * ...
1241 * case server_hello: uint16 selected_identity;
1242 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +00001243 *
1244 * } PreSharedKeyExtension;
1245 *
1246 */
1247
XiaokangQian86981952022-07-19 09:51:50 +00001248MBEDTLS_CHECK_RETURN_CRITICAL
1249static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1250 const unsigned char *buf,
1251 const unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001252{
1253 int ret = 0;
1254 size_t selected_identity;
1255
1256 const unsigned char *psk;
1257 size_t psk_len;
1258 const unsigned char *psk_identity;
1259 size_t psk_identity_len;
1260
1261
1262 /* Check which PSK we've offered.
1263 *
1264 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1265 * case, we need to iterate over them here.
1266 */
XiaokangQian008d2bf2022-07-14 07:54:01 +00001267 if( mbedtls_ssl_get_psk_to_offer( ssl, NULL, &psk, &psk_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +00001268 &psk_identity, &psk_identity_len ) != 0 )
1269 {
1270 /* If we haven't offered a PSK, the server must not send
1271 * a PSK identity extension. */
1272 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1273 }
1274
XiaokangQian008d2bf2022-07-14 07:54:01 +00001275 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001276 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1277
1278 /* We have offered only one PSK, so the only valid choice
1279 * for the server is PSK index 0.
1280 *
1281 * This will change once we support multiple PSKs. */
1282 if( selected_identity > 0 )
1283 {
1284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1285
1286 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1287 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1288 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1289 {
1290 return( ret );
1291 }
1292
1293 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1294 }
1295
1296 /* Set the chosen PSK
1297 *
1298 * TODO: We don't have to do this in case we offered 0-RTT and the
1299 * server accepted it, because in this case we've already
1300 * set the handshake PSK. */
1301 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1302 if( ret != 0 )
1303 {
1304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1305 return( ret );
1306 }
1307
1308 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1309 return( 0 );
1310}
1311
1312#endif
1313
Jerry Yue1b9c292021-09-10 10:08:31 +08001314/* Parse ServerHello message and configure context
1315 *
1316 * struct {
1317 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1318 * Random random;
1319 * opaque legacy_session_id_echo<0..32>;
1320 * CipherSuite cipher_suite;
1321 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001322 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001323 * } ServerHello;
1324 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001325MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001326static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1327 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001328 const unsigned char *end,
1329 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001330{
Jerry Yub85277e2021-10-13 13:36:05 +08001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001333 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001334 size_t extensions_len;
1335 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001336 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001337 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001338 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001339
1340 /*
1341 * Check there is space for minimal fields
1342 *
1343 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001344 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 * - legacy_session_id_echo ( 1 byte ), minimum size
1346 * - cipher_suite ( 2 bytes)
1347 * - legacy_compression_method ( 1 byte )
1348 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001349 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001350
1351 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001352 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1353
Jerry Yu4a173382021-10-11 21:45:31 +08001354 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001355 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001356 * ...
1357 * with ProtocolVersion defined as:
1358 * uint16 ProtocolVersion;
1359 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001360 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1361 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001362 {
1363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1364 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1365 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001366 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1367 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001368 }
1369 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001370
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001371 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001372 * Random random;
1373 * ...
1374 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001375 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001376 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001377 if( !is_hrr )
1378 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001379 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001380 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1381 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1382 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1383 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001384 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001385
Jerry Yu4a173382021-10-11 21:45:31 +08001386 /* ...
1387 * opaque legacy_session_id_echo<0..32>;
1388 * ...
1389 */
1390 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001391 {
XiaokangQian52da5582022-01-26 09:49:29 +00001392 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001393 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001394 }
1395
Jerry Yu4a173382021-10-11 21:45:31 +08001396 /* ...
1397 * CipherSuite cipher_suite;
1398 * ...
1399 * with CipherSuite defined as:
1400 * uint8 CipherSuite[2];
1401 */
1402 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001403 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1404 p += 2;
1405
Jerry Yu4a173382021-10-11 21:45:31 +08001406
XiaokangQian355e09a2022-01-20 11:14:50 +00001407 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001408 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001409 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001410 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001411 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1412 ssl->tls_version,
1413 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001414 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001415 {
XiaokangQian52da5582022-01-26 09:49:29 +00001416 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001417 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001418 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001419 * If we received an HRR before and that the proposed selected
1420 * ciphersuite in this server hello is not the same as the one
1421 * proposed in the HRR, we abort the handshake and send an
1422 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001423 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001424 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1425 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001426 {
XiaokangQian52da5582022-01-26 09:49:29 +00001427 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001428 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001429
XiaokangQian52da5582022-01-26 09:49:29 +00001430 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001431 {
1432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1433 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001434 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001435 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001436
Jerry Yu4a173382021-10-11 21:45:31 +08001437 /* Configure ciphersuites */
1438 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1439
XiaokangQian355e09a2022-01-20 11:14:50 +00001440 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001441 ssl->session_negotiate->ciphersuite = cipher_suite;
1442
Jerry Yue1b9c292021-09-10 10:08:31 +08001443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1444 cipher_suite, ciphersuite_info->name ) );
1445
1446#if defined(MBEDTLS_HAVE_TIME)
1447 ssl->session_negotiate->start = time( NULL );
1448#endif /* MBEDTLS_HAVE_TIME */
1449
Jerry Yu4a173382021-10-11 21:45:31 +08001450 /* ...
1451 * uint8 legacy_compression_method = 0;
1452 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001453 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001454 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001455 if( p[0] != 0 )
1456 {
Jerry Yub85277e2021-10-13 13:36:05 +08001457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001458 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001459 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001460 }
1461 p++;
1462
Jerry Yub85277e2021-10-13 13:36:05 +08001463 /* ...
1464 * Extension extensions<6..2^16-1>;
1465 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001466 * struct {
1467 * ExtensionType extension_type; (2 bytes)
1468 * opaque extension_data<0..2^16-1>;
1469 * } Extension;
1470 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001472 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001473 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001474
Jerry Yu4a173382021-10-11 21:45:31 +08001475 /* Check extensions do not go beyond the buffer of data. */
1476 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1477 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001478
Jerry Yu4a173382021-10-11 21:45:31 +08001479 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001480
Jerry Yu4a173382021-10-11 21:45:31 +08001481 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001482 {
1483 unsigned int extension_type;
1484 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001485 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001486
Jerry Yu4a173382021-10-11 21:45:31 +08001487 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001488 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1489 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1490 p += 4;
1491
Jerry Yu4a173382021-10-11 21:45:31 +08001492 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001493 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001494
1495 switch( extension_type )
1496 {
XiaokangQianb851da82022-01-14 04:03:11 +00001497 case MBEDTLS_TLS_EXT_COOKIE:
1498
XiaokangQiand9e068e2022-01-18 06:23:32 +00001499 if( !is_hrr )
1500 {
XiaokangQian52da5582022-01-26 09:49:29 +00001501 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001502 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001503 }
1504
XiaokangQian43550bd2022-01-21 04:32:58 +00001505 ret = ssl_tls13_parse_cookie_ext( ssl,
1506 p, extension_data_end );
1507 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001508 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001509 MBEDTLS_SSL_DEBUG_RET( 1,
1510 "ssl_tls13_parse_cookie_ext",
1511 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001512 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001513 }
XiaokangQianb851da82022-01-14 04:03:11 +00001514 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001515
Jerry Yue1b9c292021-09-10 10:08:31 +08001516 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001517 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001518 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001519 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001520 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001521 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001522 break;
1523
XiaokangQianeb69aee2022-07-05 08:21:43 +00001524#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001525 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1527 if( is_hrr )
1528 {
1529 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1530 goto cleanup;
1531 }
Jerry Yu4a173382021-10-11 21:45:31 +08001532
XiaokangQian86981952022-07-19 09:51:50 +00001533 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001534 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001535 {
1536 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001537 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001538 return( ret );
1539 }
1540 break;
1541#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001542
Jerry Yue1b9c292021-09-10 10:08:31 +08001543 case MBEDTLS_TLS_EXT_KEY_SHARE:
1544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001545 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1546 {
XiaokangQian52da5582022-01-26 09:49:29 +00001547 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001548 goto cleanup;
1549 }
1550
XiaokangQian53f20b72022-01-18 10:47:33 +00001551 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001552 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001553 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001554 else
XiaokangQianb851da82022-01-14 04:03:11 +00001555 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001556 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001557 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001558 {
1559 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001560 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001561 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001562 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001563 }
1564 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001565
1566 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001567 MBEDTLS_SSL_DEBUG_MSG(
1568 3,
1569 ( "unknown extension found: %u ( ignoring )",
1570 extension_type ) );
1571
XiaokangQian52da5582022-01-26 09:49:29 +00001572 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001573 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001574 }
1575
1576 p += extension_data_len;
1577 }
1578
XiaokangQiand59be772022-01-24 10:12:51 +00001579cleanup:
1580
XiaokangQian52da5582022-01-26 09:49:29 +00001581 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001582 {
1583 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1584 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1585 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1586 }
XiaokangQian52da5582022-01-26 09:49:29 +00001587 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001588 {
1589 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1590 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1591 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1592 }
1593 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001594}
1595
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001596MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001597static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001598{
Jerry Yub85277e2021-10-13 13:36:05 +08001599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001600 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001601
Jerry Yub85277e2021-10-13 13:36:05 +08001602 /* Determine the key exchange mode:
1603 * 1) If both the pre_shared_key and key_share extensions were received
1604 * then the key exchange mode is PSK with EPHEMERAL.
1605 * 2) If only the pre_shared_key extension was received then the key
1606 * exchange mode is PSK-only.
1607 * 3) If only the key_share extension was received then the key
1608 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001609 */
Jerry Yub85277e2021-10-13 13:36:05 +08001610 switch( handshake->extensions_present &
1611 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001612 {
Jerry Yu745bb612021-10-13 22:01:04 +08001613 /* Only the pre_shared_key extension was received */
1614 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001615 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001616 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001617
Jerry Yu745bb612021-10-13 22:01:04 +08001618 /* Only the key_share extension was received */
1619 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001620 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001621 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001622
Jerry Yu745bb612021-10-13 22:01:04 +08001623 /* Both the pre_shared_key and key_share extensions were received */
1624 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001625 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001626 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001627
Jerry Yu745bb612021-10-13 22:01:04 +08001628 /* Neither pre_shared_key nor key_share extension was received */
1629 default:
1630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1631 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1632 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001633 }
1634
1635 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1636 *
1637 * TODO: We don't have to do this in case we offered 0-RTT and the
1638 * server accepted it. In this case, we could skip generating
1639 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001640 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001641 if( ret != 0 )
1642 {
Jerry Yue110d252022-05-05 10:19:22 +08001643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001644 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001645 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001646 }
1647
Jerry Yuf86eb752022-05-06 11:16:55 +08001648 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001649 if( ret != 0 )
1650 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001651 MBEDTLS_SSL_DEBUG_RET( 1,
1652 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001653 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001654 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001655 }
1656
Jerry Yue110d252022-05-05 10:19:22 +08001657 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1659 ssl->session_in = ssl->session_negotiate;
1660
Jerry Yu4a173382021-10-11 21:45:31 +08001661cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001662 if( ret != 0 )
1663 {
Jerry Yu4a173382021-10-11 21:45:31 +08001664 MBEDTLS_SSL_PEND_FATAL_ALERT(
1665 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1666 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1667 }
Jerry Yue110d252022-05-05 10:19:22 +08001668
Jerry Yu4a173382021-10-11 21:45:31 +08001669 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001670}
1671
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001672MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001673static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001674{
1675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1676
XiaokangQian78b1fa72022-01-19 06:56:30 +00001677 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001678
XiaokangQian78b1fa72022-01-19 06:56:30 +00001679 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001680 * We are going to re-generate a shared secret corresponding to the group
1681 * selected by the server, which is different from the group for which we
1682 * generated a shared secret in the first client hello.
1683 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001684 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001685 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001686 if( ret != 0 )
1687 return( ret );
1688
1689 return( 0 );
1690}
1691
Jerry Yue1b9c292021-09-10 10:08:31 +08001692/*
Jerry Yu4a173382021-10-11 21:45:31 +08001693 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001694 * Handler for MBEDTLS_SSL_SERVER_HELLO
1695 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001696MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001697static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001698{
Jerry Yu4a173382021-10-11 21:45:31 +08001699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001700 unsigned char *buf = NULL;
1701 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001702 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1705
Ronald Crondb5dfa12022-05-31 11:44:38 +02001706 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1707 MBEDTLS_SSL_HS_SERVER_HELLO,
1708 &buf, &buf_len ) );
1709
Ronald Cron828aff62022-05-31 12:04:31 +02001710 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001711 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001712 goto cleanup;
1713 else
Ronald Cron828aff62022-05-31 12:04:31 +02001714 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001715
Ronald Cron828aff62022-05-31 12:04:31 +02001716 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001717 {
1718 ret = 0;
1719 goto cleanup;
1720 }
1721
XiaokangQianb851da82022-01-14 04:03:11 +00001722 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001723 buf + buf_len,
1724 is_hrr ) );
1725 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001726 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1727
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001728 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1729 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001730
XiaokangQiand9e068e2022-01-18 06:23:32 +00001731 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001732 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001733 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001734#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1735 /* If not offering early data, the client sends a dummy CCS record
1736 * immediately before its second flight. This may either be before
1737 * its second ClientHello or before its encrypted handshake flight.
1738 */
1739 mbedtls_ssl_handshake_set_state( ssl,
1740 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1741#else
1742 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1743#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1744 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001745 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001746 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001747 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001748 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1749 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001750
1751cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1753 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001754 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001755}
1756
1757/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001758 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001759 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001760 *
1761 * The EncryptedExtensions message contains any extensions which
1762 * should be protected, i.e., any which are not needed to establish
1763 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001764 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001765
XiaokangQian08da26c2021-10-09 10:12:11 +00001766/* Parse EncryptedExtensions message
1767 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001768 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001769 * } EncryptedExtensions;
1770 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001771MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001772static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1773 const unsigned char *buf,
1774 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001775{
1776 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001777 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001778 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001779 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001780
XiaokangQian08da26c2021-10-09 10:12:11 +00001781 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001782 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001783 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784
XiaokangQian97799ac2021-10-11 10:05:54 +00001785 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001786 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001787 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001788
XiaokangQian8db25ff2021-10-13 05:56:18 +00001789 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001790 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001791 unsigned int extension_type;
1792 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001793
XiaokangQian08da26c2021-10-09 10:12:11 +00001794 /*
1795 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001796 * ExtensionType extension_type; (2 bytes)
1797 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001798 * } Extension;
1799 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001800 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001801 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1802 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1803 p += 4;
1804
XiaokangQian8db25ff2021-10-13 05:56:18 +00001805 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001806
XiaokangQian97799ac2021-10-11 10:05:54 +00001807 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001808 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001809 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001810 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001811 switch( extension_type )
1812 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001813
XiaokangQian08da26c2021-10-09 10:12:11 +00001814 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1816 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001817
lhuang0486cacac2022-01-21 07:34:27 -08001818#if defined(MBEDTLS_SSL_ALPN)
1819 case MBEDTLS_TLS_EXT_ALPN:
1820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1821
1822 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1823 {
1824 return( ret );
1825 }
1826
1827 break;
1828#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001829 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001830 MBEDTLS_SSL_DEBUG_MSG(
1831 3, ( "unsupported extension found: %u ", extension_type) );
1832 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001833 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001834 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1835 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001836 }
1837
XiaokangQian08da26c2021-10-09 10:12:11 +00001838 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001839 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001840
XiaokangQian97799ac2021-10-11 10:05:54 +00001841 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001842 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001843 {
1844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001845 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001846 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001847 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001848 }
1849
1850 return( ret );
1851}
1852
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001853MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001854static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1855{
1856 int ret;
1857 unsigned char *buf;
1858 size_t buf_len;
1859
1860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1861
1862 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1863 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1864 &buf, &buf_len ) );
1865
1866 /* Process the message contents */
1867 MBEDTLS_SSL_PROC_CHK(
1868 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1869
1870 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1871 buf, buf_len );
1872
Ronald Cronfb508b82022-05-31 14:49:55 +02001873#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1874 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1875 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1876 else
1877 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1878#else
1879 ((void) ssl);
1880 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1881#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001882
1883cleanup:
1884
1885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1886 return( ret );
1887
1888}
1889
Jerry Yua93ac112021-10-27 16:31:48 +08001890#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001891/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001892 * STATE HANDLING: CertificateRequest
1893 *
Jerry Yud2674312021-10-29 10:08:19 +08001894 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001895#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1896#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001897/* Coordination:
1898 * Deals with the ambiguity of not knowing if a CertificateRequest
1899 * will be sent. Returns a negative code on failure, or
1900 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1901 * - SSL_CERTIFICATE_REQUEST_SKIP
1902 * indicating if a Certificate Request is expected or not.
1903 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001904MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001905static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001906{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001908
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001909 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001910 {
1911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1912 return( SSL_CERTIFICATE_REQUEST_SKIP );
1913 }
1914
1915 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1918 return( ret );
1919 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001920 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001921
1922 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1923 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1924 {
Ronald Cron19385882022-06-15 16:26:13 +02001925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001926 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001927 }
1928
Ronald Cron19385882022-06-15 16:26:13 +02001929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1930
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001931 return( SSL_CERTIFICATE_REQUEST_SKIP );
1932}
1933
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001934/*
1935 * ssl_tls13_parse_certificate_request()
1936 * Parse certificate request
1937 * struct {
1938 * opaque certificate_request_context<0..2^8-1>;
1939 * Extension extensions<2..2^16-1>;
1940 * } CertificateRequest;
1941 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001942MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001943static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1944 const unsigned char *buf,
1945 const unsigned char *end )
1946{
1947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1948 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001949 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001950 size_t extensions_len = 0;
1951 const unsigned char *extensions_end;
1952 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001953
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001954 /* ...
1955 * opaque certificate_request_context<0..2^8-1>
1956 * ...
1957 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001958 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1959 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001960 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001962 if( certificate_request_context_len > 0 )
1963 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001964 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001965 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1966 p, certificate_request_context_len );
1967
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001968 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001969 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001970 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001971 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001972 {
1973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1974 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1975 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001976 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001977 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001978 p += certificate_request_context_len;
1979 }
1980
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001981 /* ...
1982 * Extension extensions<2..2^16-1>;
1983 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001984 */
1985 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1986 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1987 p += 2;
1988
1989 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1990 extensions_end = p + extensions_len;
1991
1992 while( p < extensions_end )
1993 {
1994 unsigned int extension_type;
1995 size_t extension_data_len;
1996
1997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1998 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1999 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2000 p += 4;
2001
2002 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2003
2004 switch( extension_type )
2005 {
2006 case MBEDTLS_TLS_EXT_SIG_ALG:
2007 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002008 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002009 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002010 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002011 if( ret != 0 )
2012 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002013 if( ! sig_alg_ext_found )
2014 sig_alg_ext_found = 1;
2015 else
2016 {
2017 MBEDTLS_SSL_DEBUG_MSG( 3,
2018 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002019 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002020 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002021 break;
2022
2023 default:
2024 MBEDTLS_SSL_DEBUG_MSG(
2025 3,
2026 ( "unknown extension found: %u ( ignoring )",
2027 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002028 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002029 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002030 p += extension_data_len;
2031 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002032 /* Check that we consumed all the message. */
2033 if( p != end )
2034 {
2035 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002036 ( "CertificateRequest misaligned" ) );
2037 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002038 }
2039 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002040 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002041 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002042 MBEDTLS_SSL_DEBUG_MSG( 3,
2043 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002044 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002045 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002046
Jerry Yu7840f812022-01-29 10:26:51 +08002047 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002048 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002049
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002050decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002051 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2052 MBEDTLS_ERR_SSL_DECODE_ERROR );
2053 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002054}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002055
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002056/*
2057 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2058 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002059MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002060static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002061{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002063
2064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2065
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002066 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2067
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002068 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2069 {
2070 unsigned char *buf;
2071 size_t buf_len;
2072
2073 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2074 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2075 &buf, &buf_len ) );
2076
2077 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2078 buf, buf + buf_len ) );
2079
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002080 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2081 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002082 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002083 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002084 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002085 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002086 }
2087 else
2088 {
2089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002090 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2091 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002092 }
2093
Jerry Yud2674312021-10-29 10:08:19 +08002094 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2095
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002096cleanup:
2097
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2099 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002100}
2101
2102/*
Jerry Yu687101b2021-09-14 16:03:56 +08002103 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2104 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002105MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002106static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002107{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002108 int ret;
2109
2110 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002111 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002112 return( ret );
2113
Jerry Yu687101b2021-09-14 16:03:56 +08002114 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2115 return( 0 );
2116}
2117
2118/*
2119 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2120 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002121MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002122static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002123{
Jerry Yu30b071c2021-09-12 20:16:03 +08002124 int ret;
2125
2126 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2127 if( ret != 0 )
2128 return( ret );
2129
Jerry Yu687101b2021-09-14 16:03:56 +08002130 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2131 return( 0 );
2132}
Jerry Yua93ac112021-10-27 16:31:48 +08002133#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002134
Jerry Yu687101b2021-09-14 16:03:56 +08002135/*
2136 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2137 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002138MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002139static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002140{
XiaokangQianac0385c2021-11-03 06:40:11 +00002141 int ret;
2142
XiaokangQianc5c39d52021-11-09 11:55:10 +00002143 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002144 if( ret != 0 )
2145 return( ret );
2146
Jerry Yue3d67cb2022-05-19 15:33:10 +08002147 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2148 if( ret != 0 )
2149 {
2150 MBEDTLS_SSL_PEND_FATAL_ALERT(
2151 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2152 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2153 return( ret );
2154 }
2155
Ronald Cron49ad6192021-11-24 16:25:31 +01002156#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2157 mbedtls_ssl_handshake_set_state(
2158 ssl,
2159 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2160#else
Jerry Yuca133a32022-02-15 14:22:05 +08002161 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002162#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002163
XiaokangQianac0385c2021-11-03 06:40:11 +00002164 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002165}
2166
2167/*
Jerry Yu566c7812022-01-26 15:41:22 +08002168 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2169 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002170MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002171static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2172{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002173 int non_empty_certificate_msg = 0;
2174
Jerry Yu5cc35062022-01-28 16:16:08 +08002175 MBEDTLS_SSL_DEBUG_MSG( 1,
2176 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002177 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002178
Ronald Cron9df7c802022-03-08 18:38:54 +01002179#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002180 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002181 {
2182 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2183 if( ret != 0 )
2184 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002185
Ronald Cron7a94aca2022-03-09 07:44:27 +01002186 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2187 non_empty_certificate_msg = 1;
2188 }
2189 else
2190 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002192 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002193#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002194
Ronald Cron7a94aca2022-03-09 07:44:27 +01002195 if( non_empty_certificate_msg )
2196 {
2197 mbedtls_ssl_handshake_set_state( ssl,
2198 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2199 }
2200 else
Ronald Cron19385882022-06-15 16:26:13 +02002201 {
2202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002203 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002204 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002205
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002206 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002207}
2208
Ronald Cron9df7c802022-03-08 18:38:54 +01002209#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002210/*
2211 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2212 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002213MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002214static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2215{
Ronald Crona8b38872022-03-09 07:59:25 +01002216 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2217
2218 if( ret == 0 )
2219 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2220
2221 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002222}
Jerry Yu90f152d2022-01-29 22:12:42 +08002223#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002224
2225/*
Jerry Yu687101b2021-09-14 16:03:56 +08002226 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2227 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002228MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002229static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002230{
XiaokangQian0fa66432021-11-15 03:33:57 +00002231 int ret;
2232
2233 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2234 if( ret != 0 )
2235 return( ret );
2236
Jerry Yue3d67cb2022-05-19 15:33:10 +08002237 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2238 if( ret != 0 )
2239 {
2240 MBEDTLS_SSL_DEBUG_RET( 1,
2241 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2242 return ( ret );
2243 }
2244
XiaokangQian0fa66432021-11-15 03:33:57 +00002245 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2246 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002247}
2248
2249/*
2250 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2251 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002252MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002253static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002254{
Jerry Yu378254d2021-10-30 21:44:47 +08002255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002256 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002257 return( 0 );
2258}
2259
2260/*
2261 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2262 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002263MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002264static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002265{
Jerry Yu378254d2021-10-30 21:44:47 +08002266
2267 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2268
2269 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2270 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002271}
2272
Jerry Yuf8a49942022-07-07 11:32:32 +00002273#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2274
Jerry Yua0446a02022-07-13 11:22:55 +08002275MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002276static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2277 const unsigned char *buf,
2278 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002279{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002280 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002281
2282 ((void) ssl);
2283
2284 while( p < end )
2285 {
2286 unsigned int extension_type;
2287 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002288
Jerry Yuf8a49942022-07-07 11:32:32 +00002289 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2290 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2291 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2292 p += 4;
2293
2294 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002295
2296 switch( extension_type )
2297 {
2298 case MBEDTLS_TLS_EXT_EARLY_DATA:
2299 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2300 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002301
Jerry Yuf8a49942022-07-07 11:32:32 +00002302 default:
2303 break;
2304 }
2305 p += extension_data_len;
2306 }
2307
2308 return( 0 );
2309}
2310
2311/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002312 * From RFC8446, page 74
2313 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002314 * struct {
2315 * uint32 ticket_lifetime;
2316 * uint32 ticket_age_add;
2317 * opaque ticket_nonce<0..255>;
2318 * opaque ticket<1..2^16-1>;
2319 * Extension extensions<0..2^16-2>;
2320 * } NewSessionTicket;
2321 *
2322 */
Jerry Yua0446a02022-07-13 11:22:55 +08002323MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002324static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2325 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002326 unsigned char *end,
2327 unsigned char **ticket_nonce,
2328 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002329{
2330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2331 unsigned char *p = buf;
2332 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002333 size_t ticket_len;
2334 unsigned char *ticket;
2335 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002336
Jerry Yucb3b1392022-07-12 06:09:38 +00002337 *ticket_nonce = NULL;
2338 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002339 /*
2340 * ticket_lifetime 4 bytes
2341 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002342 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002343 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002344 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002345
2346 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002347 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002348 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002349 ( unsigned int )session->ticket_lifetime ) );
2350
Jerry Yucb3b1392022-07-12 06:09:38 +00002351 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002352 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002353 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002354 ( unsigned int )session->ticket_age_add ) );
2355
Jerry Yucb3b1392022-07-12 06:09:38 +00002356 *ticket_nonce_len = p[8];
2357 p += 9;
2358
2359 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2360 *ticket_nonce = p;
2361 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2362 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002363
2364 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002365 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002366 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2367 p += 2;
2368 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002369 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2370
2371 /* Check if we previously received a ticket already. */
2372 if( session->ticket != NULL || session->ticket_len > 0 )
2373 {
2374 mbedtls_free( session->ticket );
2375 session->ticket = NULL;
2376 session->ticket_len = 0;
2377 }
2378
2379 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2380 {
2381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2382 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2383 }
2384 memcpy( ticket, p, ticket_len );
2385 p += ticket_len;
2386 session->ticket = ticket;
2387 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002388
Jerry Yucb3b1392022-07-12 06:09:38 +00002389 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002390 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2391 p += 2;
2392 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2393
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002394 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002395
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002396 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002397 if( ret != 0 )
2398 {
2399 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002400 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002401 ret );
2402 return( ret );
2403 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002404
2405 return( 0 );
2406}
2407
Jerry Yua0446a02022-07-13 11:22:55 +08002408MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002409static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2410 unsigned char *ticket_nonce,
2411 size_t ticket_nonce_len )
2412{
2413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2414 mbedtls_ssl_session *session = ssl->session;
2415 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2416 psa_algorithm_t psa_hash_alg;
2417 int hash_length;
2418
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002419#if defined(MBEDTLS_HAVE_TIME)
2420 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002421 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002422#endif
2423
Jerry Yuf8a49942022-07-07 11:32:32 +00002424 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2425 if( ciphersuite_info == NULL )
2426 {
2427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2429 }
2430
2431 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2432 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002433 if( hash_length == -1 ||
2434 ( size_t )hash_length > sizeof( session->resumption_key ) )
2435 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002437 }
2438
Jerry Yuf8a49942022-07-07 11:32:32 +00002439
2440 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2441 session->app_secrets.resumption_master_secret,
2442 hash_length );
2443
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002444 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002445 *
2446 * HKDF-Expand-Label( resumption_master_secret,
2447 * "resumption", ticket_nonce, Hash.length )
2448 */
2449 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2450 psa_hash_alg,
2451 session->app_secrets.resumption_master_secret,
2452 hash_length,
2453 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2454 ticket_nonce,
2455 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002456 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002457 hash_length );
2458
2459 if( ret != 0 )
2460 {
2461 MBEDTLS_SSL_DEBUG_RET( 2,
2462 "Creating the ticket-resumed PSK failed",
2463 ret );
2464 return( ret );
2465 }
2466
Jerry Yu0a430c82022-07-20 11:02:48 +08002467 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002468
2469 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002470 session->resumption_key,
2471 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002472
Jerry Yuf8a49942022-07-07 11:32:32 +00002473 return( 0 );
2474}
2475
2476/*
Jerry Yua357cf42022-07-12 05:36:45 +00002477 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002478 */
Jerry Yua0446a02022-07-13 11:22:55 +08002479MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002480static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2481{
2482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2483 unsigned char *buf;
2484 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002485 unsigned char *ticket_nonce;
2486 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002487
2488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2489
2490 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2491 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2492 &buf, &buf_len ) );
2493
Jerry Yucb3b1392022-07-12 06:09:38 +00002494 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2495 ssl, buf, buf + buf_len,
2496 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002497
Jerry Yucb3b1392022-07-12 06:09:38 +00002498 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2499 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002500
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002501 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2502
Jerry Yuf8a49942022-07-07 11:32:32 +00002503cleanup:
2504
2505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2506 return( ret );
2507}
2508#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2509
Jerry Yu92c6b402021-08-27 16:59:09 +08002510int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002511{
Jerry Yu92c6b402021-08-27 16:59:09 +08002512 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002513
Jerry Yu92c6b402021-08-27 16:59:09 +08002514 switch( ssl->state )
2515 {
2516 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002517 * ssl->state is initialized as HELLO_REQUEST. It is the same
2518 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002519 */
2520 case MBEDTLS_SSL_HELLO_REQUEST:
2521 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002522 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002523 break;
2524
2525 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002526 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002527 break;
2528
2529 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002530 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002531 break;
2532
Jerry Yua93ac112021-10-27 16:31:48 +08002533#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002534 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2535 ret = ssl_tls13_process_certificate_request( ssl );
2536 break;
2537
Jerry Yu687101b2021-09-14 16:03:56 +08002538 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002539 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002540 break;
2541
2542 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002543 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002544 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002545#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002546
2547 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002548 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002549 break;
2550
Jerry Yu566c7812022-01-26 15:41:22 +08002551 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2552 ret = ssl_tls13_write_client_certificate( ssl );
2553 break;
2554
Ronald Cron9df7c802022-03-08 18:38:54 +01002555#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002556 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2557 ret = ssl_tls13_write_client_certificate_verify( ssl );
2558 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002559#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002560
Jerry Yu687101b2021-09-14 16:03:56 +08002561 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002562 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002563 break;
2564
2565 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002566 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002567 break;
2568
2569 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002570 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002571 break;
2572
Ronald Cron49ad6192021-11-24 16:25:31 +01002573 /*
2574 * Injection of dummy-CCS's for middlebox compatibility
2575 */
2576#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002577 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002578 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2579 if( ret == 0 )
2580 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2581 break;
2582
2583 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2584 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2585 if( ret == 0 )
2586 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002587 break;
2588#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2589
Jerry Yuf8a49942022-07-07 11:32:32 +00002590#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002591 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002592 ret = ssl_tls13_process_new_session_ticket( ssl );
2593 if( ret != 0 )
2594 break;
2595 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2596 break;
2597#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2598
Jerry Yu92c6b402021-08-27 16:59:09 +08002599 default:
2600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2602 }
2603
2604 return( ret );
2605}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002606
Jerry Yufb4b6472022-01-27 15:03:26 +08002607#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002608
Jerry Yufb4b6472022-01-27 15:03:26 +08002609