blob: 91c205801ff8d1445eb6d0adc78cd4f069807dec [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
Jerry Yucc43c6b2022-01-28 10:24:45 +080024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080025
Jerry Yubc20bdd2021-08-24 15:59:48 +080026#include <string.h>
27
Jerry Yu56fc07f2021-09-01 17:48:49 +080028#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080030#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031
Jerry Yubdc71882021-09-14 19:30:36 +080032#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010033#include "ssl_client.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080034#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020045MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf4436812021-08-26 22:59:56 +080046static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080047 unsigned char *buf,
48 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +000049 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +080050{
51 unsigned char *p = buf;
Glenn Strausscd78df62022-04-07 19:07:11 -040052 unsigned char versions_len = ( ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2 ) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080054
Xiaofei Baid25fab62021-12-02 06:36:27 +000055 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080056
Jerry Yu159c5a02021-08-31 12:51:25 +080057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080058
Jerry Yu388bd0d2021-09-15 18:41:02 +080059 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080060 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020063 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080064 */
Ronald Crona77fc272022-03-30 17:20:47 +020065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + versions_len );
Ronald Crondbe87f02022-02-10 14:35:27 +010066
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Ronald Crondbe87f02022-02-10 14:35:27 +010068 MBEDTLS_PUT_UINT16_BE( versions_len + 1, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080071 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010072 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080073
Jerry Yu0c63af62021-09-02 12:59:12 +080074 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080075 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010076 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080077 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -040078 mbedtls_ssl_write_version( p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3 );
Ronald Crondbe87f02022-02-10 14:35:27 +010080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:4]" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080081
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Glenn Strausscd78df62022-04-07 19:07:11 -040083 if( ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Crondbe87f02022-02-10 14:35:27 +010084 {
Glenn Strausse3af4cb2022-03-15 03:23:42 -040085 mbedtls_ssl_write_version( p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Crondbe87f02022-02-10 14:35:27 +010087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [3:3]" ) );
88 }
89
90 *out_len = 5 + versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080091
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020095MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +080096static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
Jerry Yub85277e2021-10-13 13:36:05 +080098 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +080099{
Jerry Yue1b9c292021-09-10 10:08:31 +0800100 ((void) ssl);
101
Ronald Cron98473382022-03-30 20:04:10 +0200102 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400103 if( mbedtls_ssl_read_version( buf, ssl->conf->transport ) !=
104 MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800105 {
106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800107
108 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
109 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800111 }
112
Ronald Cron98473382022-03-30 20:04:10 +0200113 if( &buf[2] != end )
114 {
115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions ext data length incorrect" ) );
116 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
117 MBEDTLS_ERR_SSL_DECODE_ERROR );
118 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
119 }
120
Jerry Yue1b9c292021-09-10 10:08:31 +0800121 return( 0 );
122}
123
lhuang0486cacac2022-01-21 07:34:27 -0800124#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
lhuang0486cacac2022-01-21 07:34:27 -0800126static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
Ronald Cron81a334f2022-05-31 16:04:11 +0200127 const unsigned char *buf, size_t len )
lhuang0486cacac2022-01-21 07:34:27 -0800128{
lhuang0486cacac2022-01-21 07:34:27 -0800129 const unsigned char *p = buf;
130 const unsigned char *end = buf + len;
Ronald Cron81a334f2022-05-31 16:04:11 +0200131 size_t protocol_name_list_len, protocol_name_len;
132 const unsigned char *protocol_name_list_end;
lhuang0486cacac2022-01-21 07:34:27 -0800133
134 /* If we didn't send it, the server shouldn't send it */
135 if( ssl->conf->alpn_list == NULL )
136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
137
138 /*
139 * opaque ProtocolName<1..2^8-1>;
140 *
141 * struct {
142 * ProtocolName protocol_name_list<2..2^16-1>
143 * } ProtocolNameList;
144 *
145 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
146 */
147
Ronald Cron81a334f2022-05-31 16:04:11 +0200148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
149 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
lhuang0486cacac2022-01-21 07:34:27 -0800150 p += 2;
lhuang0486cacac2022-01-21 07:34:27 -0800151
Ronald Cron81a334f2022-05-31 16:04:11 +0200152 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
153 protocol_name_list_end = p + protocol_name_list_len;
154
155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, 1 );
156 protocol_name_len = *p++;
lhuang0486cacac2022-01-21 07:34:27 -0800157
158 /* Check that the server chosen protocol was in our list and save it */
Ronald Cron81a334f2022-05-31 16:04:11 +0200159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, protocol_name_len );
160 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
lhuang0486cacac2022-01-21 07:34:27 -0800161 {
Ronald Cron81a334f2022-05-31 16:04:11 +0200162 if( protocol_name_len == strlen( *alpn ) &&
163 memcmp( p, *alpn, protocol_name_len ) == 0 )
lhuang0486cacac2022-01-21 07:34:27 -0800164 {
165 ssl->alpn_chosen = *alpn;
166 return( 0 );
167 }
168 }
169
170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
171}
172#endif /* MBEDTLS_SSL_ALPN */
173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200174MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian16acd4b2022-01-14 07:35:47 +0000175static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000176{
177 uint16_t group_id = ssl->handshake->offered_group_id;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100178
XiaokangQian647719a2021-12-07 09:16:29 +0000179 if( group_id == 0 )
180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
181
XiaokangQian355e09a2022-01-20 11:14:50 +0000182#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000183 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000184 {
Ronald Cron5b98ac92022-03-15 10:19:18 +0100185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
187
188 /* Destroy generated private key. */
189 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
190 if( status != PSA_SUCCESS )
191 {
192 ret = psa_ssl_status_to_mbedtls( status );
193 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
194 return( ret );
195 }
196
197 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
XiaokangQian78b1fa72022-01-19 06:56:30 +0000198 return( 0 );
199 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000200 else
201#endif /* MBEDTLS_ECDH_C */
202 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000203 {
204 /* Do something */
205 }
206
207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
208}
209
210/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800211 * Functions for writing key_share extension.
212 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200213MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub60e3cf2021-09-08 16:41:02 +0800214static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
215 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800216{
217 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
218
Jerry Yu56fc07f2021-09-01 17:48:49 +0800219
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100221 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800222 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100223 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800224 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
225
Brett Warren14efd332021-10-06 09:32:11 +0100226 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800227 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000228 const mbedtls_ecp_curve_info *curve_info;
229 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
230 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100231 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800232 {
Brett Warren14efd332021-10-06 09:32:11 +0100233 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800234 return( 0 );
235 }
236 }
237#else
238 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800239 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800240#endif /* MBEDTLS_ECDH_C */
241
242 /*
243 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800244 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800245 */
246
247 return( ret );
248}
249
250/*
251 * ssl_tls13_write_key_share_ext
252 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800253 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800254 *
255 * struct {
256 * NamedGroup group;
257 * opaque key_exchange<1..2^16-1>;
258 * } KeyShareEntry;
259 * struct {
260 * KeyShareEntry client_shares<0..2^16-1>;
261 * } KeyShareClientHello;
262 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
265 unsigned char *buf,
266 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268{
269 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000270 unsigned char *client_shares; /* Start of client_shares */
271 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
Xiaofei Baid25fab62021-12-02 06:36:27 +0000275 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276
Jerry Yub60e3cf2021-09-08 16:41:02 +0800277 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 * - extension_type (2 bytes)
279 * - extension_data_length (2 bytes)
280 * - client_shares_length (2 bytes)
281 */
282 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
283 p += 6;
284
285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
286
287 /* HRR could already have requested something else. */
288 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800289 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
290 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800291 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800292 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800293 &group_id ) );
294 }
295
296 /*
297 * Dispatch to type-specific key generation function.
298 *
299 * So far, we're only supporting ECDHE. With the introduction
300 * of PQC KEMs, we'll want to have multiple branches, one per
301 * type of KEM, and dispatch to the corresponding crypto. And
302 * only one key share entry is allowed.
303 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000304 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800305#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800306 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800308 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000309 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100311 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312
313 /* Check there is space for header of KeyShareEntry
314 * - group (2 bytes)
315 * - key_exchange_length (2 bytes)
316 */
317 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
318 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800319 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
320 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800321 p += key_exchange_len;
322 if( ret != 0 )
323 return( ret );
324
325 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000326 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000328 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800329 }
330 else
331#endif /* MBEDTLS_ECDH_C */
332 if( 0 /* other KEMs? */ )
333 {
334 /* Do something */
335 }
336 else
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
338
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000340 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800341 if( client_shares_len == 0)
342 {
343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800345 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346 /* Write extension_type */
347 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
348 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800349 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800351 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
353 /* Update offered_group_id field */
354 ssl->handshake->offered_group_id = group_id;
355
356 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000357 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358
Xiaofei Baid25fab62021-12-02 06:36:27 +0000359 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800360
361 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
362
363cleanup:
364
365 return( ret );
366}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800367
Jerry Yue1b9c292021-09-10 10:08:31 +0800368
XiaokangQiand59be772022-01-24 10:12:51 +0000369/*
370 * ssl_tls13_parse_hrr_key_share_ext()
371 * Parse key_share extension in Hello Retry Request
372 *
373 * struct {
374 * NamedGroup selected_group;
375 * } KeyShareHelloRetryRequest;
376 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand9e068e2022-01-18 06:23:32 +0000378static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000379 const unsigned char *buf,
380 const unsigned char *end )
381{
XiaokangQianb851da82022-01-14 04:03:11 +0000382 const mbedtls_ecp_curve_info *curve_info = NULL;
383 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000384 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000385 int found = 0;
386
387 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
388 if( group_list == NULL )
389 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
390
391 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
392
393 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000394 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000395 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000397
398 /* Upon receipt of this extension in a HelloRetryRequest, the client
399 * MUST first verify that the selected_group field corresponds to a
400 * group which was provided in the "supported_groups" extension in the
401 * original ClientHello.
402 * The supported_group was based on the info in ssl->conf->group_list.
403 *
404 * If the server provided a key share that was not sent in the ClientHello
405 * then the client MUST abort the handshake with an "illegal_parameter" alert.
406 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000407 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000408 {
409 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000410 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000411 continue;
412
413 /* We found a match */
414 found = 1;
415 break;
416 }
417
418 /* Client MUST verify that the selected_group field does not
419 * correspond to a group which was provided in the "key_share"
420 * extension in the original ClientHello. If the server sent an
421 * HRR message with a key share already provided in the
422 * ClientHello then the client MUST abort the handshake with
423 * an "illegal_parameter" alert.
424 */
XiaokangQiand59be772022-01-24 10:12:51 +0000425 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000426 {
427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
428 MBEDTLS_SSL_PEND_FATAL_ALERT(
429 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
430 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
431 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
432 }
433
434 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000435 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000436
437 return( 0 );
438}
439
Jerry Yue1b9c292021-09-10 10:08:31 +0800440/*
Jerry Yub85277e2021-10-13 13:36:05 +0800441 * ssl_tls13_parse_key_share_ext()
442 * Parse key_share extension in Server Hello
443 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800444 * struct {
445 * KeyShareEntry server_share;
446 * } KeyShareServerHello;
447 * struct {
448 * NamedGroup group;
449 * opaque key_exchange<1..2^16-1>;
450 * } KeyShareEntry;
451 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800453static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800454 const unsigned char *buf,
455 const unsigned char *end )
456{
Jerry Yub85277e2021-10-13 13:36:05 +0800457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800458 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800459 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800460
Jerry Yu4a173382021-10-11 21:45:31 +0800461 /* ...
462 * NamedGroup group; (2 bytes)
463 * ...
464 */
465 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
466 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800467 p += 2;
468
Jerry Yu4a173382021-10-11 21:45:31 +0800469 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800470 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800471 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800472 {
473 MBEDTLS_SSL_DEBUG_MSG( 1,
474 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800475 (unsigned) offered_group, (unsigned) group ) );
476 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
477 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
478 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800479 }
480
481#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800482 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800483 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100484 const mbedtls_ecp_curve_info *curve_info =
485 mbedtls_ecp_curve_info_from_tls_id( group );
486 if( curve_info == NULL )
487 {
488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
490 }
491
492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
493
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000494 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 if( ret != 0 )
496 return( ret );
497 }
Jerry Yub85277e2021-10-13 13:36:05 +0800498 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800499#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800500 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800501 {
502 /* Do something */
503 }
504 else
505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
506
507 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
508 return( ret );
509}
510
XiaokangQiand59be772022-01-24 10:12:51 +0000511/*
512 * ssl_tls13_parse_cookie_ext()
513 * Parse cookie extension in Hello Retry Request
514 *
515 * struct {
516 * opaque cookie<1..2^16-1>;
517 * } Cookie;
518 *
519 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
520 * extension to the client (this is an exception to the usual rule that
521 * the only extensions that may be sent are those that appear in the
522 * ClientHello). When sending the new ClientHello, the client MUST copy
523 * the contents of the extension received in the HelloRetryRequest into
524 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
525 * cookies in their initial ClientHello in subsequent connections.
526 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200527MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian43550bd2022-01-21 04:32:58 +0000528static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
529 const unsigned char *buf,
530 const unsigned char *end )
531{
XiaokangQian25c9c902022-02-08 10:49:53 +0000532 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000533 const unsigned char *p = buf;
534 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
535
536 /* Retrieve length field of cookie */
537 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
538 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
539 p += 2;
540
541 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
542 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
543
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000544 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000545 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000546 handshake->cookie = mbedtls_calloc( 1, cookie_len );
547 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000548 {
549 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000550 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000551 cookie_len ) );
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553 }
554
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000555 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000556 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000557
558 return( 0 );
559}
XiaokangQian43550bd2022-01-21 04:32:58 +0000560
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200561MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian0b64eed2022-01-27 10:36:51 +0000562static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000563 unsigned char *buf,
564 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000565 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000566{
567 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000568 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000569 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570
XiaokangQianc02768a2022-02-10 07:31:25 +0000571 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000572 {
573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
574 return( 0 );
575 }
576
577 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000578 handshake->cookie,
579 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580
XiaokangQianc02768a2022-02-10 07:31:25 +0000581 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000582
583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
584
XiaokangQian0b64eed2022-01-27 10:36:51 +0000585 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000586 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
587 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000588 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000589
590 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000591 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000592
XiaokangQianc02768a2022-02-10 07:31:25 +0000593 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000594
595 return( 0 );
596}
597
XiaokangQianeb69aee2022-07-05 08:21:43 +0000598#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
599/*
600 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
601 *
602 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
603 *
604 * struct {
605 * PskKeyExchangeMode ke_modes<1..255>;
606 * } PskKeyExchangeModes;
607 */
XiaokangQian86981952022-07-19 09:51:50 +0000608MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianeb69aee2022-07-05 08:21:43 +0000609static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
610 unsigned char *buf,
611 unsigned char *end,
612 size_t *out_len )
613{
XiaokangQianeb69aee2022-07-05 08:21:43 +0000614 unsigned char *p = buf;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000615 int ke_modes_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000616
XiaokangQian008d2bf2022-07-14 07:54:01 +0000617 ((void) ke_modes_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000618 *out_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000619
XiaokangQianeb69aee2022-07-05 08:21:43 +0000620 /* Skip writing extension if no PSK key exchange mode
XiaokangQian7c12d312022-07-20 07:25:43 +0000621 * is enabled in the config.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000622 */
XiaokangQian86981952022-07-19 09:51:50 +0000623 if( !mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000624 {
625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip psk_key_exchange_modes extension" ) );
626 return( 0 );
627 }
628
629 /* Require 7 bytes of data, otherwise fail,
630 * even if extension might be shorter.
631 */
632 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
633 MBEDTLS_SSL_DEBUG_MSG(
634 3, ( "client hello, adding psk_key_exchange_modes extension" ) );
635
XiaokangQianeb69aee2022-07-05 08:21:43 +0000636 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0 );
637
XiaokangQian008d2bf2022-07-14 07:54:01 +0000638 /* Skip extension length (2 bytes) and
639 * ke_modes length (1 byte) for now.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000640 */
641 p += 5;
642
643 if( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) )
644 {
645 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000646 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000647
648 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding pure PSK key exchange mode" ) );
649 }
650
651 if( mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( ssl ) )
652 {
653 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000654 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000655
656 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding PSK-ECDHE key exchange mode" ) );
657 }
658
XiaokangQian008d2bf2022-07-14 07:54:01 +0000659 /* Now write the extension and ke_modes length */
660 MBEDTLS_PUT_UINT16_BE( ke_modes_len + 1, buf, 2 );
661 buf[4] = ke_modes_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662
663 *out_len = p - buf;
664 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
665 return ( 0 );
666}
667#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
668
669/*
670 * mbedtls_ssl_tls13_write_pre_shared_key_ext() structure:
671 *
672 * struct {
673 * opaque identity<1..2^16-1>;
674 * uint32 obfuscated_ticket_age;
675 * } PskIdentity;
676 *
677 * opaque PskBinderEntry<32..255>;
678 *
679 * struct {
XiaokangQian86981952022-07-19 09:51:50 +0000680 * PskIdentity identities<7..2^16-1>;
681 * PskBinderEntry binders<33..2^16-1>;
682 * } OfferedPsks;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000683 *
XiaokangQian86981952022-07-19 09:51:50 +0000684 * struct {
685 * select (Handshake.msg_type) {
686 * case client_hello: OfferedPsks;
687 * ...
688 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +0000689 * } PreSharedKeyExtension;
690 *
XiaokangQianeb69aee2022-07-05 08:21:43 +0000691 */
692
693#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
694
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000695int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000696 mbedtls_ssl_context *ssl,
697 unsigned char *buf, unsigned char *end,
698 size_t *out_len, size_t *binders_len )
699{
700 unsigned char *p = buf;
701 const unsigned char *psk;
702 size_t psk_len;
703 const unsigned char *psk_identity;
704 size_t psk_identity_len;
XiaokangQian86981952022-07-19 09:51:50 +0000705 int psk_type;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000706 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000707 const int *ciphersuites;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000708 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000709 int hash_len = 0;
710 size_t identities_len, l_binders_len;
711 uint32_t obfuscated_ticket_age = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000712
713 *out_len = 0;
714 *binders_len = 0;
715
716 /* Check if we have any PSKs to offer. If so, return the first.
717 *
718 * NOTE: Ultimately, we want to be able to offer multiple PSKs,
719 * in which case we want to iterate over them here.
720 *
721 * As it stands, however, we only ever offer one, chosen
722 * by the following heuristic:
723 * - If a ticket has been configured, offer the corresponding PSK.
724 * - If no ticket has been configured by an external PSK has been
725 * configured, offer that.
726 * - Otherwise, skip the PSK extension.
727 */
728
XiaokangQian86981952022-07-19 09:51:50 +0000729 if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000730 &psk_identity, &psk_identity_len ) != 0 )
731 {
732 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
733 return( 0 );
734 }
735
XiaokangQian86981952022-07-19 09:51:50 +0000736 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000737 {
XiaokangQian7c12d312022-07-20 07:25:43 +0000738 /*
739 * Ciphersuite list
740 */
741 ciphersuites = ssl->conf->ciphersuite_list;
XiaokangQian86981952022-07-19 09:51:50 +0000742 for( int i = 0; ciphersuites[i] != 0; i++ )
743 {
744 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
745 ciphersuites[i] );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000746
XiaokangQian86981952022-07-19 09:51:50 +0000747 if( mbedtls_ssl_validate_ciphersuite(
XiaokangQian008d2bf2022-07-14 07:54:01 +0000748 ssl, ciphersuite_info,
749 MBEDTLS_SSL_VERSION_TLS1_3,
750 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
XiaokangQian86981952022-07-19 09:51:50 +0000751 continue;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000752
XiaokangQian86981952022-07-19 09:51:50 +0000753 /* In this implementation we only add one pre-shared-key
754 * extension.
755 */
756 ssl->session_negotiate->ciphersuite = ciphersuites[i];
757 break;
758 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000759 }
760
XiaokangQian008d2bf2022-07-14 07:54:01 +0000761 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
762 ssl->session_negotiate->ciphersuite );
763 /* No suitable ciphersuite for the PSK */
764 if( ciphersuite_info == NULL )
765 return( 0 );
766
767 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
768 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000769 if( hash_len == -1 )
770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
771
772 /* Check if we have space to write the extension, binder included.
773 * - extension_type (2 bytes)
774 * - extension_data_len (2 bytes)
775 * - identities_len (2 bytes)
776 * - identity_len (2 bytes)
777 * - identity (psk_identity_len bytes)
778 * - obfuscated_ticket_age (4 bytes)
779 * - binders_len (2 bytes)
780 * - binder_len (1 byte)
781 * - binder (hash_len bytes)
782 */
783
784 identities_len = 6 + psk_identity_len;
785 l_binders_len = 1 + hash_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000786
787 MBEDTLS_SSL_DEBUG_MSG( 3,
788 ( "client hello, adding pre_shared_key extension, "
789 "omitting PSK binder list" ) );
790
791 /* Extension header */
XiaokangQianbee71452022-07-21 08:19:06 +0000792 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000793 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0 );
794 MBEDTLS_PUT_UINT16_BE( 2 + identities_len + 2 + l_binders_len , p, 2 );
795
796 MBEDTLS_PUT_UINT16_BE( identities_len, p, 4 );
797 MBEDTLS_PUT_UINT16_BE( psk_identity_len, p, 6 );
798 p += 8;
XiaokangQianbee71452022-07-21 08:19:06 +0000799 MBEDTLS_SSL_CHK_BUF_PTR( p, end, psk_identity_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000800 memcpy( p, psk_identity, psk_identity_len );
801 p += psk_identity_len;
802
803 /* add obfuscated ticket age */
XiaokangQianbee71452022-07-21 08:19:06 +0000804 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000805 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, p, 0 );
806 p += 4;
807
XiaokangQianbee71452022-07-21 08:19:06 +0000808 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 + l_binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000809 *out_len = ( p - buf ) + l_binders_len + 2;
810 *binders_len = l_binders_len + 2;
811
812 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
813
814 return( 0 );
815}
816
XiaokangQian86981952022-07-19 09:51:50 +0000817int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000818 mbedtls_ssl_context *ssl,
819 unsigned char *buf, unsigned char *end )
820{
821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
822 unsigned char *p = buf;
XiaokangQianadab9a62022-07-18 07:41:26 +0000823 const unsigned char *psk_identity;
824 size_t psk_identity_len;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000825 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
826 psa_algorithm_t psa_hash_alg;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000827 int hash_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000828 const unsigned char *psk = NULL;
829 size_t psk_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000830 int psk_type;
831 unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
832 size_t transcript_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000833
XiaokangQianadab9a62022-07-18 07:41:26 +0000834 if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
835 &psk_identity, &psk_identity_len ) != 0 )
836 {
837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
838 }
839
XiaokangQian008d2bf2022-07-14 07:54:01 +0000840 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
841 ssl->session_negotiate->ciphersuite );
842 if( ciphersuite_info == NULL )
843 return( 0 );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000844
XiaokangQian008d2bf2022-07-14 07:54:01 +0000845 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
846 hash_len = PSA_HASH_LENGTH( psa_hash_alg );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000847 if( ( hash_len == -1 ) || ( ( end - buf ) != 3 + hash_len ) )
848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
849
850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list" ) );
851
XiaokangQian008d2bf2022-07-14 07:54:01 +0000852 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 + hash_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000853 /* 2 bytes length field for array of psk binders */
854 MBEDTLS_PUT_UINT16_BE( hash_len + 1, p, 0 );
855 p += 2;
856
857 /* 1 bytes length field for next psk binder */
858 *p++ = MBEDTLS_BYTE_0( hash_len );
859
XiaokangQianeb69aee2022-07-05 08:21:43 +0000860 /* Get current state of handshake transcript. */
XiaokangQian008d2bf2022-07-14 07:54:01 +0000861 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000862 transcript, sizeof( transcript ),
863 &transcript_len );
864 if( ret != 0 )
865 return( ret );
866
867 ret = mbedtls_ssl_tls13_create_psk_binder( ssl,
XiaokangQian008d2bf2022-07-14 07:54:01 +0000868 mbedtls_psa_translate_md( ciphersuite_info->mac ),
XiaokangQianeb69aee2022-07-05 08:21:43 +0000869 psk, psk_len, psk_type,
870 transcript, p );
871 if( ret != 0 )
872 {
873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
874 return( ret );
875 }
876
877 return( 0 );
878}
879#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
880
Ronald Cron3d580bf2022-02-18 17:24:56 +0100881int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
882 unsigned char *buf,
883 unsigned char *end,
884 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100885{
886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
887 unsigned char *p = buf;
888 size_t ext_len;
889
890 *out_len = 0;
891
892 /* Write supported_versions extension
893 *
894 * Supported Versions Extension is mandatory with TLS 1.3.
895 */
896 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
897 if( ret != 0 )
898 return( ret );
899 p += ext_len;
900
901 /* Echo the cookie if the server provided one in its preceding
902 * HelloRetryRequest message.
903 */
904 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
905 if( ret != 0 )
906 return( ret );
907 p += ext_len;
908
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100909 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
910 {
911 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
912 if( ret != 0 )
913 return( ret );
914 p += ext_len;
915 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100916
XiaokangQianeb69aee2022-07-05 08:21:43 +0000917#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
918 /* For PSK-based key exchange we need the pre_shared_key extension
919 * and the psk_key_exchange_modes extension.
920 *
921 * The pre_shared_key extension MUST be the last extension in the
922 * ClientHello. Servers MUST check that it is the last extension and
923 * otherwise fail the handshake with an "illegal_parameter" alert.
924 *
925 * Add the psk_key_exchange_modes extension.
926 */
927 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
928 if( ret != 0 )
929 return( ret );
930 p += ext_len;
931#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
932
Ronald Cron04fbd2b2022-02-18 12:06:07 +0100933 *out_len = p - buf;
934
935 return( 0 );
936}
937
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800938/*
Jerry Yu4a173382021-10-11 21:45:31 +0800939 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800940 */
Ronald Cronfd6193c2022-04-05 11:04:20 +0200941
Ronald Cronda41b382022-03-30 09:57:11 +0200942/**
943 * \brief Detect if the ServerHello contains a supported_versions extension
944 * or not.
945 *
946 * \param[in] ssl SSL context
947 * \param[in] buf Buffer containing the ServerHello message
948 * \param[in] end End of the buffer containing the ServerHello message
949 *
950 * \return 0 if the ServerHello does not contain a supported_versions extension
951 * \return 1 if the ServerHello contains a supported_versions extension
952 * \return A negative value if an error occurred while parsing the ServerHello.
953 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200954MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +0100955static int ssl_tls13_is_supported_versions_ext_present(
956 mbedtls_ssl_context *ssl,
957 const unsigned char *buf,
958 const unsigned char *end )
959{
960 const unsigned char *p = buf;
961 size_t legacy_session_id_echo_len;
962 size_t extensions_len;
963 const unsigned char *extensions_end;
964
965 /*
966 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +0200967 * length:
968 * - legacy_version 2 bytes
969 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
970 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +0100971 */
972 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
973 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
974 legacy_session_id_echo_len = *p;
975
976 /*
977 * Jump to the extensions, jumping over:
978 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
979 * - cipher_suite 2 bytes
980 * - legacy_compression_method 1 byte
981 */
Ronald Cron28271062022-06-10 14:43:55 +0200982 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +0100983 p += legacy_session_id_echo_len + 4;
984
985 /* Case of no extension */
986 if( p == end )
987 return( 0 );
988
989 /* ...
990 * Extension extensions<6..2^16-1>;
991 * ...
992 * struct {
993 * ExtensionType extension_type; (2 bytes)
994 * opaque extension_data<0..2^16-1>;
995 * } Extension;
996 */
997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
998 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
999 p += 2;
1000
1001 /* Check extensions do not go beyond the buffer of data. */
1002 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1003 extensions_end = p + extensions_len;
1004
1005 while( p < extensions_end )
1006 {
1007 unsigned int extension_type;
1008 size_t extension_data_len;
1009
1010 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1011 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1012 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1013 p += 4;
1014
1015 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1016 return( 1 );
1017
1018 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1019 p += extension_data_len;
1020 }
1021
1022 return( 0 );
1023}
1024
Jerry Yu7a186a02021-10-15 18:46:14 +08001025/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001026 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1027 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1028 * - 0 otherwise
1029 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001030MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001031static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1032 const unsigned char *buf,
1033 const unsigned char *end )
1034{
1035 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1036 static const unsigned char magic_downgrade_string[] =
1037 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1038 const unsigned char *last_eight_bytes_of_random;
1039 unsigned char last_byte_of_random;
1040
1041 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1042 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1043
1044 if( memcmp( last_eight_bytes_of_random,
1045 magic_downgrade_string,
1046 sizeof( magic_downgrade_string ) ) == 0 )
1047 {
1048 last_byte_of_random = last_eight_bytes_of_random[7];
1049 return( last_byte_of_random == 0 ||
1050 last_byte_of_random == 1 );
1051 }
1052
1053 return( 0 );
1054}
1055
1056/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001057 * - SSL_SERVER_HELLO or
1058 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001059 * to indicate which message is expected and to be parsed next.
1060 */
Ronald Cron828aff62022-05-31 12:04:31 +02001061#define SSL_SERVER_HELLO 0
1062#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001063MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001064static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1065 const unsigned char *buf,
1066 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001067{
Jerry Yue1b9c292021-09-10 10:08:31 +08001068
1069 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1070 *
Jerry Yu4a173382021-10-11 21:45:31 +08001071 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001072 * special value of the SHA-256 of "HelloRetryRequest".
1073 *
1074 * struct {
1075 * ProtocolVersion legacy_version = 0x0303;
1076 * Random random;
1077 * opaque legacy_session_id_echo<0..32>;
1078 * CipherSuite cipher_suite;
1079 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001080 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001081 * } ServerHello;
1082 *
1083 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1085 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001086
Jerry Yu93a13f22022-04-11 23:00:01 +08001087 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1088 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 {
Ronald Cron828aff62022-05-31 12:04:31 +02001090 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001091 }
1092
Ronald Cron828aff62022-05-31 12:04:31 +02001093 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001094}
1095
Ronald Cron828aff62022-05-31 12:04:31 +02001096/*
Jerry Yu745bb612021-10-13 22:01:04 +08001097 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001098 * - SSL_SERVER_HELLO or
1099 * - SSL_SERVER_HELLO_HRR or
1100 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001101 */
Ronald Cron828aff62022-05-31 12:04:31 +02001102#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001103MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001104static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001105 const unsigned char *buf,
1106 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001107{
Jerry Yu4a173382021-10-11 21:45:31 +08001108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001109 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001110
Ronald Cron9f0fba32022-02-10 16:45:15 +01001111 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001112 ssl, buf, end ) );
Jerry Yua66fece2022-07-13 14:30:29 +08001113
Ronald Cron9f0fba32022-02-10 16:45:15 +01001114 if( ret == 0 )
1115 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001116 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001117 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001118
1119 /* If the server is negotiating TLS 1.2 or below and:
1120 * . we did not propose TLS 1.2 or
1121 * . the server responded it is TLS 1.3 capable but negotiating a lower
1122 * version of the protocol and thus we are under downgrade attack
1123 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001124 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001125 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001126 {
1127 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1128 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1129 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1130 }
1131
1132 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001133 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001134 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001135 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001136
1137 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1138 {
1139 ret = ssl_tls13_reset_key_share( ssl );
1140 if( ret != 0 )
1141 return( ret );
1142 }
1143
Ronald Cron828aff62022-05-31 12:04:31 +02001144 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001145 }
1146
Jerry Yua66fece2022-07-13 14:30:29 +08001147#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1148 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1149 ssl->session_negotiate->tls_version = ssl->tls_version;
1150#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1151
Ronald Cron5afb9042022-05-31 12:11:39 +02001152 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1153
Ronald Crondb5dfa12022-05-31 11:44:38 +02001154 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001155 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001156 {
Ronald Cron828aff62022-05-31 12:04:31 +02001157 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1159 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001160 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001162 /* If a client receives a second
1163 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1164 * was itself in response to a HelloRetryRequest), it MUST abort the
1165 * handshake with an "unexpected_message" alert.
1166 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001167 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001168 {
1169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1170 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1171 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1172 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1173 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001174 /*
1175 * Clients must abort the handshake with an "illegal_parameter"
1176 * alert if the HelloRetryRequest would not result in any change
1177 * in the ClientHello.
1178 * In a PSK only key exchange that what we expect.
1179 */
1180 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1181 {
1182 MBEDTLS_SSL_DEBUG_MSG( 1,
1183 ( "Unexpected HRR in pure PSK key exchange." ) );
1184 MBEDTLS_SSL_PEND_FATAL_ALERT(
1185 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1186 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1187 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1188 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001189
Ronald Cron5afb9042022-05-31 12:11:39 +02001190 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001191
Jerry Yu745bb612021-10-13 22:01:04 +08001192 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001193 }
1194
1195cleanup:
1196
1197 return( ret );
1198}
1199
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001200MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001201static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1202 const unsigned char **buf,
1203 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001204{
1205 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001206 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001207
Jerry Yude4fb2c2021-09-19 18:05:08 +08001208 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001209 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001210
Jerry Yu4a173382021-10-11 21:45:31 +08001211 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001212
1213 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001214 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1215 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001216 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001217 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1218 ssl->session_negotiate->id,
1219 ssl->session_negotiate->id_len );
1220 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001221 legacy_session_id_echo_len );
1222
1223 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1224 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1225
Jerry Yue1b9c292021-09-10 10:08:31 +08001226 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1227 }
1228
Jerry Yu4a173382021-10-11 21:45:31 +08001229 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001230 *buf = p;
1231
1232 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001233 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001234 return( 0 );
1235}
1236
XiaokangQianeb69aee2022-07-05 08:21:43 +00001237#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1238/*
1239 * struct {
1240 * opaque identity<1..2^16-1>;
1241 * uint32 obfuscated_ticket_age;
1242 * } PskIdentity;
1243 *
1244 * opaque PskBinderEntry<32..255>;
1245 *
1246 * struct {
XiaokangQian008d2bf2022-07-14 07:54:01 +00001247 *
XiaokangQian86981952022-07-19 09:51:50 +00001248 * select (Handshake.msg_type) {
1249 * ...
1250 * case server_hello: uint16 selected_identity;
1251 * };
XiaokangQianeb69aee2022-07-05 08:21:43 +00001252 *
1253 * } PreSharedKeyExtension;
1254 *
1255 */
1256
XiaokangQian86981952022-07-19 09:51:50 +00001257MBEDTLS_CHECK_RETURN_CRITICAL
1258static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1259 const unsigned char *buf,
1260 const unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001261{
1262 int ret = 0;
1263 size_t selected_identity;
1264
1265 const unsigned char *psk;
1266 size_t psk_len;
1267 const unsigned char *psk_identity;
1268 size_t psk_identity_len;
1269
1270
1271 /* Check which PSK we've offered.
1272 *
1273 * NOTE: Ultimately, we want to offer multiple PSKs, and in this
1274 * case, we need to iterate over them here.
1275 */
XiaokangQian008d2bf2022-07-14 07:54:01 +00001276 if( mbedtls_ssl_get_psk_to_offer( ssl, NULL, &psk, &psk_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +00001277 &psk_identity, &psk_identity_len ) != 0 )
1278 {
1279 /* If we haven't offered a PSK, the server must not send
1280 * a PSK identity extension. */
1281 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1282 }
1283
XiaokangQian008d2bf2022-07-14 07:54:01 +00001284 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001285 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
1286
1287 /* We have offered only one PSK, so the only valid choice
1288 * for the server is PSK index 0.
1289 *
1290 * This will change once we support multiple PSKs. */
1291 if( selected_identity > 0 )
1292 {
1293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server's chosen PSK identity out of range" ) );
1294
1295 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
1296 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1297 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ) ) != 0 )
1298 {
1299 return( ret );
1300 }
1301
1302 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1303 }
1304
1305 /* Set the chosen PSK
1306 *
1307 * TODO: We don't have to do this in case we offered 0-RTT and the
1308 * server accepted it, because in this case we've already
1309 * set the handshake PSK. */
1310 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1311 if( ret != 0 )
1312 {
1313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
1314 return( ret );
1315 }
1316
1317 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1318 return( 0 );
1319}
1320
1321#endif
1322
Jerry Yue1b9c292021-09-10 10:08:31 +08001323/* Parse ServerHello message and configure context
1324 *
1325 * struct {
1326 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1327 * Random random;
1328 * opaque legacy_session_id_echo<0..32>;
1329 * CipherSuite cipher_suite;
1330 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001331 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 * } ServerHello;
1333 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001334MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001335static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1336 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001337 const unsigned char *end,
1338 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001339{
Jerry Yub85277e2021-10-13 13:36:05 +08001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001341 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001342 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001343 size_t extensions_len;
1344 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001346 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001347 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001348
1349 /*
1350 * Check there is space for minimal fields
1351 *
1352 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001353 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001354 * - legacy_session_id_echo ( 1 byte ), minimum size
1355 * - cipher_suite ( 2 bytes)
1356 * - legacy_compression_method ( 1 byte )
1357 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001358 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001359
1360 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001361 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1362
Jerry Yu4a173382021-10-11 21:45:31 +08001363 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001364 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001365 * ...
1366 * with ProtocolVersion defined as:
1367 * uint16 ProtocolVersion;
1368 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001369 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1370 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001371 {
1372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1373 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1374 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001375 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1376 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001377 }
1378 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001379
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001380 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001381 * Random random;
1382 * ...
1383 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001384 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001385 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001386 if( !is_hrr )
1387 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001388 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001389 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1390 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1391 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1392 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001393 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001394
Jerry Yu4a173382021-10-11 21:45:31 +08001395 /* ...
1396 * opaque legacy_session_id_echo<0..32>;
1397 * ...
1398 */
1399 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001400 {
XiaokangQian52da5582022-01-26 09:49:29 +00001401 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001402 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001403 }
1404
Jerry Yu4a173382021-10-11 21:45:31 +08001405 /* ...
1406 * CipherSuite cipher_suite;
1407 * ...
1408 * with CipherSuite defined as:
1409 * uint8 CipherSuite[2];
1410 */
1411 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001412 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1413 p += 2;
1414
Jerry Yu4a173382021-10-11 21:45:31 +08001415
XiaokangQian355e09a2022-01-20 11:14:50 +00001416 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001417 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001418 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001419 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001420 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1421 ssl->tls_version,
1422 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001423 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001424 {
XiaokangQian52da5582022-01-26 09:49:29 +00001425 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001426 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001427 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001428 * If we received an HRR before and that the proposed selected
1429 * ciphersuite in this server hello is not the same as the one
1430 * proposed in the HRR, we abort the handshake and send an
1431 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001432 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001433 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1434 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001435 {
XiaokangQian52da5582022-01-26 09:49:29 +00001436 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001437 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001438
XiaokangQian52da5582022-01-26 09:49:29 +00001439 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001440 {
1441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1442 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001443 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001444 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001445
Jerry Yu4a173382021-10-11 21:45:31 +08001446 /* Configure ciphersuites */
1447 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1448
XiaokangQian355e09a2022-01-20 11:14:50 +00001449 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001450 ssl->session_negotiate->ciphersuite = cipher_suite;
1451
Jerry Yue1b9c292021-09-10 10:08:31 +08001452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1453 cipher_suite, ciphersuite_info->name ) );
1454
1455#if defined(MBEDTLS_HAVE_TIME)
1456 ssl->session_negotiate->start = time( NULL );
1457#endif /* MBEDTLS_HAVE_TIME */
1458
Jerry Yu4a173382021-10-11 21:45:31 +08001459 /* ...
1460 * uint8 legacy_compression_method = 0;
1461 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001462 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001463 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001464 if( p[0] != 0 )
1465 {
Jerry Yub85277e2021-10-13 13:36:05 +08001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001467 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001468 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001469 }
1470 p++;
1471
Jerry Yub85277e2021-10-13 13:36:05 +08001472 /* ...
1473 * Extension extensions<6..2^16-1>;
1474 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001475 * struct {
1476 * ExtensionType extension_type; (2 bytes)
1477 * opaque extension_data<0..2^16-1>;
1478 * } Extension;
1479 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001480 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001481 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001482 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001483
Jerry Yu4a173382021-10-11 21:45:31 +08001484 /* Check extensions do not go beyond the buffer of data. */
1485 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1486 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001487
Jerry Yu4a173382021-10-11 21:45:31 +08001488 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001489
Jerry Yu4a173382021-10-11 21:45:31 +08001490 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001491 {
1492 unsigned int extension_type;
1493 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001494 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001495
Jerry Yu4a173382021-10-11 21:45:31 +08001496 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001497 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1498 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1499 p += 4;
1500
Jerry Yu4a173382021-10-11 21:45:31 +08001501 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001502 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001503
1504 switch( extension_type )
1505 {
XiaokangQianb851da82022-01-14 04:03:11 +00001506 case MBEDTLS_TLS_EXT_COOKIE:
1507
XiaokangQiand9e068e2022-01-18 06:23:32 +00001508 if( !is_hrr )
1509 {
XiaokangQian52da5582022-01-26 09:49:29 +00001510 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001511 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001512 }
1513
XiaokangQian43550bd2022-01-21 04:32:58 +00001514 ret = ssl_tls13_parse_cookie_ext( ssl,
1515 p, extension_data_end );
1516 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001517 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001518 MBEDTLS_SSL_DEBUG_RET( 1,
1519 "ssl_tls13_parse_cookie_ext",
1520 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001521 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001522 }
XiaokangQianb851da82022-01-14 04:03:11 +00001523 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001524
Jerry Yue1b9c292021-09-10 10:08:31 +08001525 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001526 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001527 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001528 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001529 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001530 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001531 break;
1532
XiaokangQianeb69aee2022-07-05 08:21:43 +00001533#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001534 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1536 if( is_hrr )
1537 {
1538 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1539 goto cleanup;
1540 }
Jerry Yu4a173382021-10-11 21:45:31 +08001541
XiaokangQian86981952022-07-19 09:51:50 +00001542 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001543 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001544 {
1545 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001546 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001547 return( ret );
1548 }
1549 break;
1550#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +08001551
Jerry Yue1b9c292021-09-10 10:08:31 +08001552 case MBEDTLS_TLS_EXT_KEY_SHARE:
1553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001554 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1555 {
XiaokangQian52da5582022-01-26 09:49:29 +00001556 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001557 goto cleanup;
1558 }
1559
XiaokangQian53f20b72022-01-18 10:47:33 +00001560 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001561 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001562 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001563 else
XiaokangQianb851da82022-01-14 04:03:11 +00001564 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001565 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001566 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001567 {
1568 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001569 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001570 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001571 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001572 }
1573 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001574
1575 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001576 MBEDTLS_SSL_DEBUG_MSG(
1577 3,
1578 ( "unknown extension found: %u ( ignoring )",
1579 extension_type ) );
1580
XiaokangQian52da5582022-01-26 09:49:29 +00001581 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001582 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001583 }
1584
1585 p += extension_data_len;
1586 }
1587
XiaokangQiand59be772022-01-24 10:12:51 +00001588cleanup:
1589
XiaokangQian52da5582022-01-26 09:49:29 +00001590 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001591 {
1592 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1593 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1594 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1595 }
XiaokangQian52da5582022-01-26 09:49:29 +00001596 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001597 {
1598 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1599 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1600 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1601 }
1602 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001603}
1604
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001605MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001606static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001607{
Jerry Yub85277e2021-10-13 13:36:05 +08001608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001609 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001610
Jerry Yub85277e2021-10-13 13:36:05 +08001611 /* Determine the key exchange mode:
1612 * 1) If both the pre_shared_key and key_share extensions were received
1613 * then the key exchange mode is PSK with EPHEMERAL.
1614 * 2) If only the pre_shared_key extension was received then the key
1615 * exchange mode is PSK-only.
1616 * 3) If only the key_share extension was received then the key
1617 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001618 */
Jerry Yub85277e2021-10-13 13:36:05 +08001619 switch( handshake->extensions_present &
1620 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001621 {
Jerry Yu745bb612021-10-13 22:01:04 +08001622 /* Only the pre_shared_key extension was received */
1623 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001624 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001625 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001626
Jerry Yu745bb612021-10-13 22:01:04 +08001627 /* Only the key_share extension was received */
1628 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001629 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001630 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001631
Jerry Yu745bb612021-10-13 22:01:04 +08001632 /* Both the pre_shared_key and key_share extensions were received */
1633 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001634 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001635 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001636
Jerry Yu745bb612021-10-13 22:01:04 +08001637 /* Neither pre_shared_key nor key_share extension was received */
1638 default:
1639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1640 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1641 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001642 }
1643
1644 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1645 *
1646 * TODO: We don't have to do this in case we offered 0-RTT and the
1647 * server accepted it. In this case, we could skip generating
1648 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001649 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001650 if( ret != 0 )
1651 {
Jerry Yue110d252022-05-05 10:19:22 +08001652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001653 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001654 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001655 }
1656
Jerry Yuf86eb752022-05-06 11:16:55 +08001657 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001658 if( ret != 0 )
1659 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001660 MBEDTLS_SSL_DEBUG_RET( 1,
1661 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001662 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001663 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001664 }
1665
Jerry Yue110d252022-05-05 10:19:22 +08001666 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1668 ssl->session_in = ssl->session_negotiate;
1669
Jerry Yu4a173382021-10-11 21:45:31 +08001670cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001671 if( ret != 0 )
1672 {
Jerry Yu4a173382021-10-11 21:45:31 +08001673 MBEDTLS_SSL_PEND_FATAL_ALERT(
1674 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1675 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1676 }
Jerry Yue110d252022-05-05 10:19:22 +08001677
Jerry Yu4a173382021-10-11 21:45:31 +08001678 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001679}
1680
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001681MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001682static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001683{
1684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1685
XiaokangQian78b1fa72022-01-19 06:56:30 +00001686 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001687
XiaokangQian78b1fa72022-01-19 06:56:30 +00001688 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001689 * We are going to re-generate a shared secret corresponding to the group
1690 * selected by the server, which is different from the group for which we
1691 * generated a shared secret in the first client hello.
1692 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001693 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001694 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001695 if( ret != 0 )
1696 return( ret );
1697
1698 return( 0 );
1699}
1700
Jerry Yue1b9c292021-09-10 10:08:31 +08001701/*
Jerry Yu4a173382021-10-11 21:45:31 +08001702 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001703 * Handler for MBEDTLS_SSL_SERVER_HELLO
1704 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001705MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001706static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001707{
Jerry Yu4a173382021-10-11 21:45:31 +08001708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001709 unsigned char *buf = NULL;
1710 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001711 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001712
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1714
Ronald Crondb5dfa12022-05-31 11:44:38 +02001715 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1716 MBEDTLS_SSL_HS_SERVER_HELLO,
1717 &buf, &buf_len ) );
1718
Ronald Cron828aff62022-05-31 12:04:31 +02001719 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001720 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001721 goto cleanup;
1722 else
Ronald Cron828aff62022-05-31 12:04:31 +02001723 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001724
Ronald Cron828aff62022-05-31 12:04:31 +02001725 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001726 {
1727 ret = 0;
1728 goto cleanup;
1729 }
1730
XiaokangQianb851da82022-01-14 04:03:11 +00001731 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001732 buf + buf_len,
1733 is_hrr ) );
1734 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001735 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1736
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001737 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1738 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001739
XiaokangQiand9e068e2022-01-18 06:23:32 +00001740 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001741 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001742 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001743#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1744 /* If not offering early data, the client sends a dummy CCS record
1745 * immediately before its second flight. This may either be before
1746 * its second ClientHello or before its encrypted handshake flight.
1747 */
1748 mbedtls_ssl_handshake_set_state( ssl,
1749 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1750#else
1751 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1752#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1753 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001754 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001755 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001756 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001757 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1758 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001759
1760cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1762 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001763 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001764}
1765
1766/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001767 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001768 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001769 *
1770 * The EncryptedExtensions message contains any extensions which
1771 * should be protected, i.e., any which are not needed to establish
1772 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001773 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001774
XiaokangQian08da26c2021-10-09 10:12:11 +00001775/* Parse EncryptedExtensions message
1776 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001777 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001778 * } EncryptedExtensions;
1779 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001780MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001781static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1782 const unsigned char *buf,
1783 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001784{
1785 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001786 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001787 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001788 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001789
XiaokangQian08da26c2021-10-09 10:12:11 +00001790 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001791 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001792 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001793
XiaokangQian97799ac2021-10-11 10:05:54 +00001794 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001795 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001796 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001797
XiaokangQian8db25ff2021-10-13 05:56:18 +00001798 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001799 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001800 unsigned int extension_type;
1801 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001802
XiaokangQian08da26c2021-10-09 10:12:11 +00001803 /*
1804 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001805 * ExtensionType extension_type; (2 bytes)
1806 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001807 * } Extension;
1808 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001809 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001810 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1811 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1812 p += 4;
1813
XiaokangQian8db25ff2021-10-13 05:56:18 +00001814 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001815
XiaokangQian97799ac2021-10-11 10:05:54 +00001816 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001817 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001818 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001819 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001820 switch( extension_type )
1821 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001822
XiaokangQian08da26c2021-10-09 10:12:11 +00001823 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1825 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001826
lhuang0486cacac2022-01-21 07:34:27 -08001827#if defined(MBEDTLS_SSL_ALPN)
1828 case MBEDTLS_TLS_EXT_ALPN:
1829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1830
1831 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1832 {
1833 return( ret );
1834 }
1835
1836 break;
1837#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001838 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001839 MBEDTLS_SSL_DEBUG_MSG(
1840 3, ( "unsupported extension found: %u ", extension_type) );
1841 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001842 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001843 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1844 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001845 }
1846
XiaokangQian08da26c2021-10-09 10:12:11 +00001847 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001848 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001849
XiaokangQian97799ac2021-10-11 10:05:54 +00001850 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001851 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001852 {
1853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001854 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001855 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001856 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001857 }
1858
1859 return( ret );
1860}
1861
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02001863static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
1864{
1865 int ret;
1866 unsigned char *buf;
1867 size_t buf_len;
1868
1869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1870
1871 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1872 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1873 &buf, &buf_len ) );
1874
1875 /* Process the message contents */
1876 MBEDTLS_SSL_PROC_CHK(
1877 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
1878
1879 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
1880 buf, buf_len );
1881
Ronald Cronfb508b82022-05-31 14:49:55 +02001882#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02001883 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02001884 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1885 else
1886 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1887#else
1888 ((void) ssl);
1889 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1890#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02001891
1892cleanup:
1893
1894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1895 return( ret );
1896
1897}
1898
Jerry Yua93ac112021-10-27 16:31:48 +08001899#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001900/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001901 * STATE HANDLING: CertificateRequest
1902 *
Jerry Yud2674312021-10-29 10:08:19 +08001903 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001904#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1905#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001906/* Coordination:
1907 * Deals with the ambiguity of not knowing if a CertificateRequest
1908 * will be sent. Returns a negative code on failure, or
1909 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1910 * - SSL_CERTIFICATE_REQUEST_SKIP
1911 * indicating if a Certificate Request is expected or not.
1912 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001913MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001914static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001915{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001917
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001918 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001919 {
1920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1921 return( ret );
1922 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001923 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001924
1925 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1926 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1927 {
Ronald Cron19385882022-06-15 16:26:13 +02001928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001929 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001930 }
1931
Ronald Cron19385882022-06-15 16:26:13 +02001932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
1933
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001934 return( SSL_CERTIFICATE_REQUEST_SKIP );
1935}
1936
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001937/*
1938 * ssl_tls13_parse_certificate_request()
1939 * Parse certificate request
1940 * struct {
1941 * opaque certificate_request_context<0..2^8-1>;
1942 * Extension extensions<2..2^16-1>;
1943 * } CertificateRequest;
1944 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001945MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001946static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1947 const unsigned char *buf,
1948 const unsigned char *end )
1949{
1950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1951 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001952 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001953 size_t extensions_len = 0;
1954 const unsigned char *extensions_end;
1955 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001956
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001957 /* ...
1958 * opaque certificate_request_context<0..2^8-1>
1959 * ...
1960 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1962 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001963 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001964
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001965 if( certificate_request_context_len > 0 )
1966 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001967 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001968 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1969 p, certificate_request_context_len );
1970
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001971 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001972 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001973 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001974 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001975 {
1976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1977 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1978 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001979 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001980 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001981 p += certificate_request_context_len;
1982 }
1983
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001984 /* ...
1985 * Extension extensions<2..2^16-1>;
1986 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 */
1988 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1989 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1990 p += 2;
1991
1992 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1993 extensions_end = p + extensions_len;
1994
1995 while( p < extensions_end )
1996 {
1997 unsigned int extension_type;
1998 size_t extension_data_len;
1999
2000 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2001 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2002 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2003 p += 4;
2004
2005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2006
2007 switch( extension_type )
2008 {
2009 case MBEDTLS_TLS_EXT_SIG_ALG:
2010 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002011 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002012 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002013 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002014 if( ret != 0 )
2015 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002016 if( ! sig_alg_ext_found )
2017 sig_alg_ext_found = 1;
2018 else
2019 {
2020 MBEDTLS_SSL_DEBUG_MSG( 3,
2021 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002022 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002023 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002024 break;
2025
2026 default:
2027 MBEDTLS_SSL_DEBUG_MSG(
2028 3,
2029 ( "unknown extension found: %u ( ignoring )",
2030 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002031 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002032 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002033 p += extension_data_len;
2034 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002035 /* Check that we consumed all the message. */
2036 if( p != end )
2037 {
2038 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002039 ( "CertificateRequest misaligned" ) );
2040 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002041 }
2042 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002043 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002044 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002045 MBEDTLS_SSL_DEBUG_MSG( 3,
2046 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002047 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002048 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002049
Jerry Yu7840f812022-01-29 10:26:51 +08002050 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002051 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002052
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002053decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002054 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2055 MBEDTLS_ERR_SSL_DECODE_ERROR );
2056 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002057}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002058
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002059/*
2060 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2061 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002062MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002063static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002064{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002066
2067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2068
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002069 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2070
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002071 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2072 {
2073 unsigned char *buf;
2074 size_t buf_len;
2075
2076 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2077 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2078 &buf, &buf_len ) );
2079
2080 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2081 buf, buf + buf_len ) );
2082
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002083 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2084 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002085 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002086 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002087 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002088 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002089 }
2090 else
2091 {
2092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002093 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2094 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002095 }
2096
Jerry Yud2674312021-10-29 10:08:19 +08002097 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2098
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002099cleanup:
2100
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2102 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002103}
2104
2105/*
Jerry Yu687101b2021-09-14 16:03:56 +08002106 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2107 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002108MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002109static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002110{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002111 int ret;
2112
2113 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002114 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002115 return( ret );
2116
Jerry Yu687101b2021-09-14 16:03:56 +08002117 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2118 return( 0 );
2119}
2120
2121/*
2122 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2123 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002124MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002125static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002126{
Jerry Yu30b071c2021-09-12 20:16:03 +08002127 int ret;
2128
2129 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2130 if( ret != 0 )
2131 return( ret );
2132
Jerry Yu687101b2021-09-14 16:03:56 +08002133 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2134 return( 0 );
2135}
Jerry Yua93ac112021-10-27 16:31:48 +08002136#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002137
Jerry Yu687101b2021-09-14 16:03:56 +08002138/*
2139 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2140 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002141MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002142static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002143{
XiaokangQianac0385c2021-11-03 06:40:11 +00002144 int ret;
2145
XiaokangQianc5c39d52021-11-09 11:55:10 +00002146 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002147 if( ret != 0 )
2148 return( ret );
2149
Jerry Yue3d67cb2022-05-19 15:33:10 +08002150 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2151 if( ret != 0 )
2152 {
2153 MBEDTLS_SSL_PEND_FATAL_ALERT(
2154 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2155 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2156 return( ret );
2157 }
2158
Ronald Cron49ad6192021-11-24 16:25:31 +01002159#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2160 mbedtls_ssl_handshake_set_state(
2161 ssl,
2162 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2163#else
Jerry Yuca133a32022-02-15 14:22:05 +08002164 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002165#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002166
XiaokangQianac0385c2021-11-03 06:40:11 +00002167 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002168}
2169
2170/*
Jerry Yu566c7812022-01-26 15:41:22 +08002171 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2172 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002173MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002174static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2175{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002176 int non_empty_certificate_msg = 0;
2177
Jerry Yu5cc35062022-01-28 16:16:08 +08002178 MBEDTLS_SSL_DEBUG_MSG( 1,
2179 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002180 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002181
Ronald Cron9df7c802022-03-08 18:38:54 +01002182#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002183 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002184 {
2185 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2186 if( ret != 0 )
2187 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002188
Ronald Cron7a94aca2022-03-09 07:44:27 +01002189 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2190 non_empty_certificate_msg = 1;
2191 }
2192 else
2193 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002195 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002196#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002197
Ronald Cron7a94aca2022-03-09 07:44:27 +01002198 if( non_empty_certificate_msg )
2199 {
2200 mbedtls_ssl_handshake_set_state( ssl,
2201 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2202 }
2203 else
Ronald Cron19385882022-06-15 16:26:13 +02002204 {
2205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002206 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002207 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002208
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002209 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002210}
2211
Ronald Cron9df7c802022-03-08 18:38:54 +01002212#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002213/*
2214 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2215 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002216MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002217static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2218{
Ronald Crona8b38872022-03-09 07:59:25 +01002219 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2220
2221 if( ret == 0 )
2222 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2223
2224 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002225}
Jerry Yu90f152d2022-01-29 22:12:42 +08002226#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002227
2228/*
Jerry Yu687101b2021-09-14 16:03:56 +08002229 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2230 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002231MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002232static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002233{
XiaokangQian0fa66432021-11-15 03:33:57 +00002234 int ret;
2235
2236 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2237 if( ret != 0 )
2238 return( ret );
2239
Jerry Yue3d67cb2022-05-19 15:33:10 +08002240 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2241 if( ret != 0 )
2242 {
2243 MBEDTLS_SSL_DEBUG_RET( 1,
2244 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2245 return ( ret );
2246 }
2247
XiaokangQian0fa66432021-11-15 03:33:57 +00002248 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2249 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002250}
2251
2252/*
2253 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2254 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002255MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002256static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002257{
Jerry Yu378254d2021-10-30 21:44:47 +08002258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002259 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002260 return( 0 );
2261}
2262
2263/*
2264 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2265 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002266MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002267static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002268{
Jerry Yu378254d2021-10-30 21:44:47 +08002269
2270 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2271
2272 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2273 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002274}
2275
Jerry Yuf8a49942022-07-07 11:32:32 +00002276#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2277
Jerry Yua0446a02022-07-13 11:22:55 +08002278MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002279static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2280 const unsigned char *buf,
2281 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002282{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002283 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002284
2285 ((void) ssl);
2286
2287 while( p < end )
2288 {
2289 unsigned int extension_type;
2290 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002291
Jerry Yuf8a49942022-07-07 11:32:32 +00002292 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2293 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2294 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2295 p += 4;
2296
2297 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002298
2299 switch( extension_type )
2300 {
2301 case MBEDTLS_TLS_EXT_EARLY_DATA:
2302 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2303 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002304
Jerry Yuf8a49942022-07-07 11:32:32 +00002305 default:
2306 break;
2307 }
2308 p += extension_data_len;
2309 }
2310
2311 return( 0 );
2312}
2313
2314/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002315 * From RFC8446, page 74
2316 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002317 * struct {
2318 * uint32 ticket_lifetime;
2319 * uint32 ticket_age_add;
2320 * opaque ticket_nonce<0..255>;
2321 * opaque ticket<1..2^16-1>;
2322 * Extension extensions<0..2^16-2>;
2323 * } NewSessionTicket;
2324 *
2325 */
Jerry Yua0446a02022-07-13 11:22:55 +08002326MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002327static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2328 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002329 unsigned char *end,
2330 unsigned char **ticket_nonce,
2331 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002332{
2333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2334 unsigned char *p = buf;
2335 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002336 size_t ticket_len;
2337 unsigned char *ticket;
2338 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002339
Jerry Yucb3b1392022-07-12 06:09:38 +00002340 *ticket_nonce = NULL;
2341 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002342 /*
2343 * ticket_lifetime 4 bytes
2344 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002345 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002346 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002347 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002348
2349 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002350 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002351 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002352 ( unsigned int )session->ticket_lifetime ) );
2353
Jerry Yucb3b1392022-07-12 06:09:38 +00002354 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002355 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002356 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002357 ( unsigned int )session->ticket_age_add ) );
2358
Jerry Yucb3b1392022-07-12 06:09:38 +00002359 *ticket_nonce_len = p[8];
2360 p += 9;
2361
2362 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2363 *ticket_nonce = p;
2364 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2365 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002366
2367 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002368 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002369 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2370 p += 2;
2371 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002372 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2373
2374 /* Check if we previously received a ticket already. */
2375 if( session->ticket != NULL || session->ticket_len > 0 )
2376 {
2377 mbedtls_free( session->ticket );
2378 session->ticket = NULL;
2379 session->ticket_len = 0;
2380 }
2381
2382 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2383 {
2384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2386 }
2387 memcpy( ticket, p, ticket_len );
2388 p += ticket_len;
2389 session->ticket = ticket;
2390 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002391
Jerry Yucb3b1392022-07-12 06:09:38 +00002392 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002393 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2394 p += 2;
2395 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2396
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002397 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002398
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002399 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002400 if( ret != 0 )
2401 {
2402 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002403 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002404 ret );
2405 return( ret );
2406 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002407
2408 return( 0 );
2409}
2410
Jerry Yua0446a02022-07-13 11:22:55 +08002411MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002412static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2413 unsigned char *ticket_nonce,
2414 size_t ticket_nonce_len )
2415{
2416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2417 mbedtls_ssl_session *session = ssl->session;
2418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2419 psa_algorithm_t psa_hash_alg;
2420 int hash_length;
2421
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002422#if defined(MBEDTLS_HAVE_TIME)
2423 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002424 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002425#endif
2426
Jerry Yuf8a49942022-07-07 11:32:32 +00002427 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2428 if( ciphersuite_info == NULL )
2429 {
2430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2432 }
2433
2434 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2435 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002436 if( hash_length == -1 ||
2437 ( size_t )hash_length > sizeof( session->resumption_key ) )
2438 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002440 }
2441
Jerry Yuf8a49942022-07-07 11:32:32 +00002442
2443 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2444 session->app_secrets.resumption_master_secret,
2445 hash_length );
2446
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002447 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002448 *
2449 * HKDF-Expand-Label( resumption_master_secret,
2450 * "resumption", ticket_nonce, Hash.length )
2451 */
2452 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2453 psa_hash_alg,
2454 session->app_secrets.resumption_master_secret,
2455 hash_length,
2456 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2457 ticket_nonce,
2458 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002459 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002460 hash_length );
2461
2462 if( ret != 0 )
2463 {
2464 MBEDTLS_SSL_DEBUG_RET( 2,
2465 "Creating the ticket-resumed PSK failed",
2466 ret );
2467 return( ret );
2468 }
2469
Jerry Yu0a430c82022-07-20 11:02:48 +08002470 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002471
2472 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002473 session->resumption_key,
2474 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002475
Jerry Yuf8a49942022-07-07 11:32:32 +00002476 return( 0 );
2477}
2478
2479/*
Jerry Yua357cf42022-07-12 05:36:45 +00002480 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002481 */
Jerry Yua0446a02022-07-13 11:22:55 +08002482MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002483static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2484{
2485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2486 unsigned char *buf;
2487 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002488 unsigned char *ticket_nonce;
2489 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002490
2491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2492
2493 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2494 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2495 &buf, &buf_len ) );
2496
Jerry Yucb3b1392022-07-12 06:09:38 +00002497 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2498 ssl, buf, buf + buf_len,
2499 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002500
Jerry Yucb3b1392022-07-12 06:09:38 +00002501 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2502 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002503
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002504 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2505
Jerry Yuf8a49942022-07-07 11:32:32 +00002506cleanup:
2507
2508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2509 return( ret );
2510}
2511#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2512
Jerry Yu92c6b402021-08-27 16:59:09 +08002513int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002514{
Jerry Yu92c6b402021-08-27 16:59:09 +08002515 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002516
Jerry Yu92c6b402021-08-27 16:59:09 +08002517 switch( ssl->state )
2518 {
2519 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002520 * ssl->state is initialized as HELLO_REQUEST. It is the same
2521 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002522 */
2523 case MBEDTLS_SSL_HELLO_REQUEST:
2524 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002525 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002526 break;
2527
2528 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002529 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002530 break;
2531
2532 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002533 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002534 break;
2535
Jerry Yua93ac112021-10-27 16:31:48 +08002536#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002537 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2538 ret = ssl_tls13_process_certificate_request( ssl );
2539 break;
2540
Jerry Yu687101b2021-09-14 16:03:56 +08002541 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002542 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002543 break;
2544
2545 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002546 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002547 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002548#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002549
2550 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002551 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002552 break;
2553
Jerry Yu566c7812022-01-26 15:41:22 +08002554 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2555 ret = ssl_tls13_write_client_certificate( ssl );
2556 break;
2557
Ronald Cron9df7c802022-03-08 18:38:54 +01002558#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002559 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2560 ret = ssl_tls13_write_client_certificate_verify( ssl );
2561 break;
Jerry Yu90f152d2022-01-29 22:12:42 +08002562#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002563
Jerry Yu687101b2021-09-14 16:03:56 +08002564 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002565 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002566 break;
2567
2568 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002569 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002570 break;
2571
2572 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002573 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002574 break;
2575
Ronald Cron49ad6192021-11-24 16:25:31 +01002576 /*
2577 * Injection of dummy-CCS's for middlebox compatibility
2578 */
2579#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002580 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002581 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2582 if( ret == 0 )
2583 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2584 break;
2585
2586 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2587 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2588 if( ret == 0 )
2589 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002590 break;
2591#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2592
Jerry Yuf8a49942022-07-07 11:32:32 +00002593#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002594 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002595 ret = ssl_tls13_process_new_session_ticket( ssl );
2596 if( ret != 0 )
2597 break;
2598 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2599 break;
2600#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2601
Jerry Yu92c6b402021-08-27 16:59:09 +08002602 default:
2603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2605 }
2606
2607 return( ret );
2608}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002609
Jerry Yufb4b6472022-01-27 15:03:26 +08002610#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002611
Jerry Yufb4b6472022-01-27 15:03:26 +08002612