blob: ac19f63081c8eef48165676ecaccb6ded6d5a9e7 [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 */
Ronald Cron766c0cd2022-10-18 12:17:11 +0200213#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200214MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub60e3cf2021-09-08 16:41:02 +0800215static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
216 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800217{
218 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
219
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100222 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800223 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100224 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800225 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
226
Brett Warren14efd332021-10-06 09:32:11 +0100227 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000229 const mbedtls_ecp_curve_info *curve_info;
230 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
231 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100232 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233 {
Brett Warren14efd332021-10-06 09:32:11 +0100234 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800235 return( 0 );
236 }
237 }
238#else
239 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800240 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241#endif /* MBEDTLS_ECDH_C */
242
243 /*
244 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800245 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800246 */
247
248 return( ret );
249}
250
251/*
252 * ssl_tls13_write_key_share_ext
253 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800254 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 *
256 * struct {
257 * NamedGroup group;
258 * opaque key_exchange<1..2^16-1>;
259 * } KeyShareEntry;
260 * struct {
261 * KeyShareEntry client_shares<0..2^16-1>;
262 * } KeyShareClientHello;
263 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200264MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
266 unsigned char *buf,
267 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000268 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269{
270 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000271 unsigned char *client_shares; /* Start of client_shares */
272 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
275
Xiaofei Baid25fab62021-12-02 06:36:27 +0000276 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800277
Jerry Yub60e3cf2021-09-08 16:41:02 +0800278 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279 * - extension_type (2 bytes)
280 * - extension_data_length (2 bytes)
281 * - client_shares_length (2 bytes)
282 */
283 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
284 p += 6;
285
286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
287
288 /* HRR could already have requested something else. */
289 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800290 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
291 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800293 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294 &group_id ) );
295 }
296
297 /*
298 * Dispatch to type-specific key generation function.
299 *
300 * So far, we're only supporting ECDHE. With the introduction
301 * of PQC KEMs, we'll want to have multiple branches, one per
302 * type of KEM, and dispatch to the corresponding crypto. And
303 * only one key share entry is allowed.
304 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000305 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800307 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800308 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800309 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000310 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800311 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100312 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800313
314 /* Check there is space for header of KeyShareEntry
315 * - group (2 bytes)
316 * - key_exchange_length (2 bytes)
317 */
318 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
319 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800320 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
321 ssl, group_id, p, end, &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 p += key_exchange_len;
323 if( ret != 0 )
324 return( ret );
325
326 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000327 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000329 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 }
331 else
332#endif /* MBEDTLS_ECDH_C */
333 if( 0 /* other KEMs? */ )
334 {
335 /* Do something */
336 }
337 else
338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
339
Jerry Yub60e3cf2021-09-08 16:41:02 +0800340 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000341 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800342 if( client_shares_len == 0)
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800346 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800347 /* Write extension_type */
348 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
349 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800350 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800351 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800352 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353
354 /* Update offered_group_id field */
355 ssl->handshake->offered_group_id = group_id;
356
357 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000358 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359
Xiaofei Baid25fab62021-12-02 06:36:27 +0000360 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800361
362 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
363
364cleanup:
365
366 return( ret );
367}
Ronald Cron766c0cd2022-10-18 12:17:11 +0200368#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +0800369
XiaokangQiand59be772022-01-24 10:12:51 +0000370/*
371 * ssl_tls13_parse_hrr_key_share_ext()
372 * Parse key_share extension in Hello Retry Request
373 *
374 * struct {
375 * NamedGroup selected_group;
376 * } KeyShareHelloRetryRequest;
377 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200378MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand9e068e2022-01-18 06:23:32 +0000379static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000380 const unsigned char *buf,
381 const unsigned char *end )
382{
Andrzej Kurekc19fb082022-10-03 10:52:24 -0400383#if defined(MBEDTLS_ECDH_C)
XiaokangQianb851da82022-01-14 04:03:11 +0000384 const mbedtls_ecp_curve_info *curve_info = NULL;
385 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000386 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000387 int found = 0;
388
389 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
390 if( group_list == NULL )
391 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
392
393 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
394
395 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000396 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000397 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000399
400 /* Upon receipt of this extension in a HelloRetryRequest, the client
401 * MUST first verify that the selected_group field corresponds to a
402 * group which was provided in the "supported_groups" extension in the
403 * original ClientHello.
404 * The supported_group was based on the info in ssl->conf->group_list.
405 *
406 * If the server provided a key share that was not sent in the ClientHello
407 * then the client MUST abort the handshake with an "illegal_parameter" alert.
408 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000409 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000410 {
411 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000412 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000413 continue;
414
415 /* We found a match */
416 found = 1;
417 break;
418 }
419
420 /* Client MUST verify that the selected_group field does not
421 * correspond to a group which was provided in the "key_share"
422 * extension in the original ClientHello. If the server sent an
423 * HRR message with a key share already provided in the
424 * ClientHello then the client MUST abort the handshake with
425 * an "illegal_parameter" alert.
426 */
XiaokangQiand59be772022-01-24 10:12:51 +0000427 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000428 {
429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
430 MBEDTLS_SSL_PEND_FATAL_ALERT(
431 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
432 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
433 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
434 }
435
436 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000437 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000438
439 return( 0 );
Andrzej Kurekc19fb082022-10-03 10:52:24 -0400440#else
441 (void) ssl;
442 (void) buf;
443 (void) end;
444 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
445#endif
XiaokangQianb851da82022-01-14 04:03:11 +0000446}
447
Jerry Yue1b9c292021-09-10 10:08:31 +0800448/*
Jerry Yub85277e2021-10-13 13:36:05 +0800449 * ssl_tls13_parse_key_share_ext()
450 * Parse key_share extension in Server Hello
451 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800452 * struct {
453 * KeyShareEntry server_share;
454 * } KeyShareServerHello;
455 * struct {
456 * NamedGroup group;
457 * opaque key_exchange<1..2^16-1>;
458 * } KeyShareEntry;
459 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200460MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +0800461static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800462 const unsigned char *buf,
463 const unsigned char *end )
464{
Jerry Yub85277e2021-10-13 13:36:05 +0800465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800466 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800467 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800468
Jerry Yu4a173382021-10-11 21:45:31 +0800469 /* ...
470 * NamedGroup group; (2 bytes)
471 * ...
472 */
473 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
474 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800475 p += 2;
476
Jerry Yu4a173382021-10-11 21:45:31 +0800477 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800478 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800479 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800480 {
481 MBEDTLS_SSL_DEBUG_MSG( 1,
482 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800483 (unsigned) offered_group, (unsigned) group ) );
484 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
485 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
486 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800487 }
488
489#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800490 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800491 {
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100492 const mbedtls_ecp_curve_info *curve_info =
493 mbedtls_ecp_curve_info_from_tls_id( group );
494 if( curve_info == NULL )
495 {
496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) );
497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
498 }
499
500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
501
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000502 ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800503 if( ret != 0 )
504 return( ret );
505 }
Jerry Yub85277e2021-10-13 13:36:05 +0800506 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800507#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800508 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800509 {
510 /* Do something */
511 }
512 else
513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
514
515 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
516 return( ret );
517}
518
XiaokangQiand59be772022-01-24 10:12:51 +0000519/*
520 * ssl_tls13_parse_cookie_ext()
521 * Parse cookie extension in Hello Retry Request
522 *
523 * struct {
524 * opaque cookie<1..2^16-1>;
525 * } Cookie;
526 *
527 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
528 * extension to the client (this is an exception to the usual rule that
529 * the only extensions that may be sent are those that appear in the
530 * ClientHello). When sending the new ClientHello, the client MUST copy
531 * the contents of the extension received in the HelloRetryRequest into
532 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
533 * cookies in their initial ClientHello in subsequent connections.
534 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200535MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian43550bd2022-01-21 04:32:58 +0000536static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
537 const unsigned char *buf,
538 const unsigned char *end )
539{
XiaokangQian25c9c902022-02-08 10:49:53 +0000540 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000541 const unsigned char *p = buf;
542 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
543
544 /* Retrieve length field of cookie */
545 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
546 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
547 p += 2;
548
549 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
550 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
551
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000552 mbedtls_free( handshake->cookie );
XiaokangQian25c9c902022-02-08 10:49:53 +0000553 handshake->hrr_cookie_len = 0;
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000554 handshake->cookie = mbedtls_calloc( 1, cookie_len );
555 if( handshake->cookie == NULL )
XiaokangQian43550bd2022-01-21 04:32:58 +0000556 {
557 MBEDTLS_SSL_DEBUG_MSG( 1,
XiaokangQian25c9c902022-02-08 10:49:53 +0000558 ( "alloc failed ( %ud bytes )",
XiaokangQian43550bd2022-01-21 04:32:58 +0000559 cookie_len ) );
560 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
561 }
562
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000563 memcpy( handshake->cookie, p, cookie_len );
XiaokangQian25c9c902022-02-08 10:49:53 +0000564 handshake->hrr_cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000565
566 return( 0 );
567}
XiaokangQian43550bd2022-01-21 04:32:58 +0000568
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200569MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian0b64eed2022-01-27 10:36:51 +0000570static int ssl_tls13_write_cookie_ext( mbedtls_ssl_context *ssl,
XiaokangQian233397e2022-02-07 08:32:16 +0000571 unsigned char *buf,
572 unsigned char *end,
XiaokangQian9deb90f2022-02-08 10:31:07 +0000573 size_t *out_len )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000574{
575 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000576 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000577 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000578
XiaokangQianc02768a2022-02-10 07:31:25 +0000579 if( handshake->cookie == NULL )
XiaokangQian0b64eed2022-01-27 10:36:51 +0000580 {
581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no cookie to send; skip extension" ) );
582 return( 0 );
583 }
584
585 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQianc02768a2022-02-10 07:31:25 +0000586 handshake->cookie,
587 handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000588
XiaokangQianc02768a2022-02-10 07:31:25 +0000589 MBEDTLS_SSL_CHK_BUF_PTR( p, end, handshake->hrr_cookie_len + 6 );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000590
591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding cookie extension" ) );
592
XiaokangQian0b64eed2022-01-27 10:36:51 +0000593 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_COOKIE, p, 0 );
XiaokangQianc02768a2022-02-10 07:31:25 +0000594 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len + 2, p, 2 );
595 MBEDTLS_PUT_UINT16_BE( handshake->hrr_cookie_len, p, 4 );
XiaokangQian233397e2022-02-07 08:32:16 +0000596 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000597
598 /* Cookie */
XiaokangQianc02768a2022-02-10 07:31:25 +0000599 memcpy( p, handshake->cookie, handshake->hrr_cookie_len );
XiaokangQian0b64eed2022-01-27 10:36:51 +0000600
XiaokangQianc02768a2022-02-10 07:31:25 +0000601 *out_len = handshake->hrr_cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000602
603 return( 0 );
604}
605
Ronald Cron41a443a2022-10-04 16:38:25 +0200606#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000607/*
608 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
609 *
610 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
611 *
612 * struct {
613 * PskKeyExchangeMode ke_modes<1..255>;
614 * } PskKeyExchangeModes;
615 */
XiaokangQian86981952022-07-19 09:51:50 +0000616MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianeb69aee2022-07-05 08:21:43 +0000617static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
618 unsigned char *buf,
619 unsigned char *end,
620 size_t *out_len )
621{
XiaokangQianeb69aee2022-07-05 08:21:43 +0000622 unsigned char *p = buf;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000623 int ke_modes_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000624
XiaokangQian008d2bf2022-07-14 07:54:01 +0000625 ((void) ke_modes_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000626 *out_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000627
XiaokangQianeb69aee2022-07-05 08:21:43 +0000628 /* Skip writing extension if no PSK key exchange mode
XiaokangQian7c12d312022-07-20 07:25:43 +0000629 * is enabled in the config.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000630 */
XiaokangQian86981952022-07-19 09:51:50 +0000631 if( !mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000632 {
633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip psk_key_exchange_modes extension" ) );
634 return( 0 );
635 }
636
637 /* Require 7 bytes of data, otherwise fail,
638 * even if extension might be shorter.
639 */
640 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
641 MBEDTLS_SSL_DEBUG_MSG(
642 3, ( "client hello, adding psk_key_exchange_modes extension" ) );
643
XiaokangQianeb69aee2022-07-05 08:21:43 +0000644 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0 );
645
XiaokangQian008d2bf2022-07-14 07:54:01 +0000646 /* Skip extension length (2 bytes) and
647 * ke_modes length (1 byte) for now.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000648 */
649 p += 5;
650
XiaokangQianeb69aee2022-07-05 08:21:43 +0000651 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
Ronald Crona709a0f2022-09-27 16:46:11 +0200659 if( mbedtls_ssl_conf_tls13_psk_enabled( ssl ) )
660 {
661 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
662 ke_modes_len++;
663
664 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Adding pure PSK key exchange mode" ) );
665 }
666
XiaokangQian008d2bf2022-07-14 07:54:01 +0000667 /* Now write the extension and ke_modes length */
668 MBEDTLS_PUT_UINT16_BE( ke_modes_len + 1, buf, 2 );
669 buf[4] = ke_modes_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000670
671 *out_len = p - buf;
672 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
673 return ( 0 );
674}
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800675
Jerry Yua99cbfa2022-10-08 11:17:14 +0800676static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg( int ciphersuite )
Jerry Yuf7c12592022-09-28 22:09:38 +0800677{
Jerry Yu21f90952022-10-08 10:30:53 +0800678 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
679 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Jerry Yuf7c12592022-09-28 22:09:38 +0800680
Jerry Yua99cbfa2022-10-08 11:17:14 +0800681 if( ciphersuite_info != NULL )
682 return( mbedtls_psa_translate_md( ciphersuite_info->mac ) );
683
Jerry Yu21f90952022-10-08 10:30:53 +0800684 return( PSA_ALG_NONE );
Jerry Yuf7c12592022-09-28 22:09:38 +0800685}
686
Jerry Yuf75364b2022-09-30 10:30:31 +0800687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf7c12592022-09-28 22:09:38 +0800688static int ssl_tls13_has_configured_ticket( mbedtls_ssl_context *ssl )
689{
Jerry Yuf7c12592022-09-28 22:09:38 +0800690 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800691 return( ssl->handshake->resume &&
692 session != NULL && session->ticket != NULL );
Jerry Yuf7c12592022-09-28 22:09:38 +0800693}
694
Jerry Yuf7c12592022-09-28 22:09:38 +0800695MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu8b41e892022-09-30 10:00:20 +0800696static int ssl_tls13_ticket_get_identity( mbedtls_ssl_context *ssl,
Jerry Yua99cbfa2022-10-08 11:17:14 +0800697 psa_algorithm_t *hash_alg,
Jerry Yu8b41e892022-09-30 10:00:20 +0800698 const unsigned char **identity,
699 size_t *identity_len )
Jerry Yuf7c12592022-09-28 22:09:38 +0800700{
701 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu8b41e892022-09-30 10:00:20 +0800702
Jerry Yu8b41e892022-09-30 10:00:20 +0800703 if( !ssl_tls13_has_configured_ticket( ssl ) )
704 return( -1 );
705
Jerry Yua99cbfa2022-10-08 11:17:14 +0800706 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg( session->ciphersuite );
Jerry Yuf7c12592022-09-28 22:09:38 +0800707 *identity = session->ticket;
708 *identity_len = session->ticket_len;
709 return( 0 );
710}
711
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800712MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu8b41e892022-09-30 10:00:20 +0800713static int ssl_tls13_ticket_get_psk( mbedtls_ssl_context *ssl,
Jerry Yua99cbfa2022-10-08 11:17:14 +0800714 psa_algorithm_t *hash_alg,
Jerry Yu8b41e892022-09-30 10:00:20 +0800715 const unsigned char **psk,
716 size_t *psk_len )
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800717{
718
719 mbedtls_ssl_session *session = ssl->session_negotiate;
720
Jerry Yu8b41e892022-09-30 10:00:20 +0800721 if( !ssl_tls13_has_configured_ticket( ssl ) )
722 return( -1 );
723
Jerry Yua99cbfa2022-10-08 11:17:14 +0800724 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg( session->ciphersuite );
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800725 *psk = session->resumption_key;
726 *psk_len = session->resumption_key_len;
727
728 return( 0 );
729}
Jerry Yuf7c12592022-09-28 22:09:38 +0800730#endif /* MBEDTLS_SSL_SESSION_TICKETS */
731
732MBEDTLS_CHECK_RETURN_CRITICAL
733static int ssl_tls13_psk_get_identity( mbedtls_ssl_context *ssl,
Jerry Yua99cbfa2022-10-08 11:17:14 +0800734 psa_algorithm_t *hash_alg,
Jerry Yuf7c12592022-09-28 22:09:38 +0800735 const unsigned char **identity,
736 size_t *identity_len )
737{
Jerry Yu8b41e892022-09-30 10:00:20 +0800738
Ronald Cron083da8e2022-10-20 15:53:51 +0200739 if( ! mbedtls_ssl_conf_has_static_psk( ssl->conf ) )
Jerry Yu8b41e892022-09-30 10:00:20 +0800740 return( -1 );
Jerry Yuf7c12592022-09-28 22:09:38 +0800741
Jerry Yua99cbfa2022-10-08 11:17:14 +0800742 *hash_alg = PSA_ALG_SHA_256;
Jerry Yuf7c12592022-09-28 22:09:38 +0800743 *identity = ssl->conf->psk_identity;
744 *identity_len = ssl->conf->psk_identity_len;
745 return( 0 );
746}
747
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800748MBEDTLS_CHECK_RETURN_CRITICAL
749static int ssl_tls13_psk_get_psk( mbedtls_ssl_context *ssl,
Jerry Yua99cbfa2022-10-08 11:17:14 +0800750 psa_algorithm_t *hash_alg,
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800751 const unsigned char **psk,
752 size_t *psk_len )
753{
Jerry Yu8b41e892022-09-30 10:00:20 +0800754
Ronald Cron083da8e2022-10-20 15:53:51 +0200755 if( ! mbedtls_ssl_conf_has_static_psk( ssl->conf ) )
Jerry Yu8b41e892022-09-30 10:00:20 +0800756 return( -1 );
757
Jerry Yua99cbfa2022-10-08 11:17:14 +0800758 *hash_alg = PSA_ALG_SHA_256;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800759 *psk = ssl->conf->psk;
760 *psk_len = ssl->conf->psk_len;
761 return( 0 );
762}
763
Jerry Yuf75364b2022-09-30 10:30:31 +0800764static int ssl_tls13_get_configured_psk_count( mbedtls_ssl_context *ssl )
765{
766 int configured_psk_count = 0;
767#if defined(MBEDTLS_SSL_SESSION_TICKETS)
768 if( ssl_tls13_has_configured_ticket( ssl ) )
769 {
770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Ticket is configured" ) );
771 configured_psk_count++;
772 }
773#endif
Ronald Crond29e13e2022-10-19 10:33:48 +0200774 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) )
Jerry Yuf75364b2022-09-30 10:30:31 +0800775 {
776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK is configured" ) );
777 configured_psk_count++;
778 }
779 return( configured_psk_count );
780}
781
Jerry Yuf7c12592022-09-28 22:09:38 +0800782MBEDTLS_CHECK_RETURN_CRITICAL
783static int ssl_tls13_write_identity( mbedtls_ssl_context *ssl,
784 unsigned char *buf,
785 unsigned char *end,
Jerry Yuf75364b2022-09-30 10:30:31 +0800786 const unsigned char *identity,
787 size_t identity_len,
788 uint32_t obfuscated_ticket_age,
789 size_t *out_len )
Jerry Yuf7c12592022-09-28 22:09:38 +0800790{
Jerry Yuf75364b2022-09-30 10:30:31 +0800791 ((void) ssl);
Jerry Yuf7c12592022-09-28 22:09:38 +0800792 *out_len = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800793
794 /*
795 * - identity_len (2 bytes)
796 * - identity (psk_identity_len bytes)
797 * - obfuscated_ticket_age (4 bytes)
798 */
Jerry Yua99cbfa2022-10-08 11:17:14 +0800799 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 + identity_len );
Jerry Yuf7c12592022-09-28 22:09:38 +0800800
Jerry Yua99cbfa2022-10-08 11:17:14 +0800801 MBEDTLS_PUT_UINT16_BE( identity_len, buf, 0 );
802 memcpy( buf + 2, identity, identity_len );
803 MBEDTLS_PUT_UINT32_BE( obfuscated_ticket_age, buf, 2 + identity_len );
Jerry Yuf7c12592022-09-28 22:09:38 +0800804
Jerry Yua99cbfa2022-10-08 11:17:14 +0800805 MBEDTLS_SSL_DEBUG_BUF( 4, "write identity", buf, 6 + identity_len );
Jerry Yuf7c12592022-09-28 22:09:38 +0800806
807 *out_len = 6 + identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800808
809 return( 0 );
810}
811
Jerry Yu8b41e892022-09-30 10:00:20 +0800812MBEDTLS_CHECK_RETURN_CRITICAL
813static int ssl_tls13_write_binder( mbedtls_ssl_context *ssl,
814 unsigned char *buf,
815 unsigned char *end,
816 int psk_type,
Jerry Yu6183cc72022-09-30 11:08:57 +0800817 psa_algorithm_t hash_alg,
818 const unsigned char *psk,
819 size_t psk_len,
Jerry Yu8b41e892022-09-30 10:00:20 +0800820 size_t *out_len )
821{
822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8b41e892022-09-30 10:00:20 +0800823 unsigned char binder_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800824 unsigned char transcript[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
Jerry Yu8b41e892022-09-30 10:00:20 +0800825 size_t transcript_len = 0;
826
827 *out_len = 0;
828
Jerry Yu6183cc72022-09-30 11:08:57 +0800829 binder_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu8b41e892022-09-30 10:00:20 +0800830
831 /*
832 * - binder_len (1 bytes)
833 * - binder (binder_len bytes)
834 */
Jerry Yua99cbfa2022-10-08 11:17:14 +0800835 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 + binder_len );
Jerry Yu8b41e892022-09-30 10:00:20 +0800836
Jerry Yua99cbfa2022-10-08 11:17:14 +0800837 buf[0] = binder_len;
Jerry Yu8b41e892022-09-30 10:00:20 +0800838
839 /* Get current state of handshake transcript. */
840 ret = mbedtls_ssl_get_handshake_transcript(
Jerry Yu6183cc72022-09-30 11:08:57 +0800841 ssl, mbedtls_hash_info_md_from_psa( hash_alg ),
Jerry Yu6916e702022-10-10 21:33:51 +0800842 transcript, sizeof( transcript ), &transcript_len );
Jerry Yu8b41e892022-09-30 10:00:20 +0800843 if( ret != 0 )
844 return( ret );
845
Jerry Yu6183cc72022-09-30 11:08:57 +0800846 ret = mbedtls_ssl_tls13_create_psk_binder( ssl, hash_alg,
Jerry Yu8b41e892022-09-30 10:00:20 +0800847 psk, psk_len, psk_type,
Jerry Yua99cbfa2022-10-08 11:17:14 +0800848 transcript, buf + 1 );
Jerry Yu8b41e892022-09-30 10:00:20 +0800849 if( ret != 0 )
850 {
851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_create_psk_binder", ret );
852 return( ret );
853 }
Jerry Yua99cbfa2022-10-08 11:17:14 +0800854 MBEDTLS_SSL_DEBUG_BUF( 4, "write binder", buf, 1 + binder_len );
Jerry Yu8b41e892022-09-30 10:00:20 +0800855
856 *out_len = 1 + binder_len;
857
Jerry Yu6916e702022-10-10 21:33:51 +0800858 return( 0 );
Jerry Yu8b41e892022-09-30 10:00:20 +0800859}
860
861/*
862 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
863 *
864 * struct {
865 * opaque identity<1..2^16-1>;
866 * uint32 obfuscated_ticket_age;
867 * } PskIdentity;
868 *
869 * opaque PskBinderEntry<32..255>;
870 *
871 * struct {
872 * PskIdentity identities<7..2^16-1>;
873 * PskBinderEntry binders<33..2^16-1>;
874 * } OfferedPsks;
875 *
876 * struct {
877 * select (Handshake.msg_type) {
878 * case client_hello: OfferedPsks;
879 * ...
880 * };
881 * } PreSharedKeyExtension;
882 *
883 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000884int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Jerry Yuf75364b2022-09-30 10:30:31 +0800885 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
886 size_t *out_len, size_t *binders_len )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000887{
Jerry Yuf7c12592022-09-28 22:09:38 +0800888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf75364b2022-09-30 10:30:31 +0800889 int configured_psk_count = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800890 unsigned char *p = buf;
Jerry Yuf75364b2022-09-30 10:30:31 +0800891 psa_algorithm_t hash_alg;
892 const unsigned char *identity;
893 size_t identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800894 size_t l_binders_len = 0;
Jerry Yuf75364b2022-09-30 10:30:31 +0800895 size_t output_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800896
897 *out_len = 0;
898 *binders_len = 0;
899
900 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
Jerry Yuf75364b2022-09-30 10:30:31 +0800901 configured_psk_count = ssl_tls13_get_configured_psk_count( ssl );
902 if( configured_psk_count == 0 )
Jerry Yuf7c12592022-09-28 22:09:38 +0800903 {
904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
905 return( 0 );
906 }
907
908 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Pre-configured PSK number = %d",
Jerry Yuf75364b2022-09-30 10:30:31 +0800909 configured_psk_count ) );
910
Jerry Yuf7c12592022-09-28 22:09:38 +0800911 /* Check if we have space to write the extension, binders included.
912 * - extension_type (2 bytes)
913 * - extension_data_len (2 bytes)
914 * - identities_len (2 bytes)
915 */
916 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
917 p += 6;
918
Jerry Yuf75364b2022-09-30 10:30:31 +0800919#if defined(MBEDTLS_SSL_SESSION_TICKETS)
920 if( ssl_tls13_ticket_get_identity(
921 ssl, &hash_alg, &identity, &identity_len ) == 0 )
Jerry Yuf7c12592022-09-28 22:09:38 +0800922 {
Jerry Yuf75364b2022-09-30 10:30:31 +0800923#if defined(MBEDTLS_HAVE_TIME)
924 mbedtls_time_t now = mbedtls_time( NULL );
925 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu6916e702022-10-10 21:33:51 +0800926 uint32_t obfuscated_ticket_age =
927 (uint32_t)( now - session->ticket_received );
Jerry Yua99cbfa2022-10-08 11:17:14 +0800928
Jerry Yuf75364b2022-09-30 10:30:31 +0800929 obfuscated_ticket_age *= 1000;
930 obfuscated_ticket_age += session->ticket_age_add;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800931
Jerry Yuf7c12592022-09-28 22:09:38 +0800932 ret = ssl_tls13_write_identity( ssl, p, end,
Jerry Yuf75364b2022-09-30 10:30:31 +0800933 identity, identity_len,
934 obfuscated_ticket_age,
935 &output_len );
Jerry Yua99cbfa2022-10-08 11:17:14 +0800936#else
937 ret = ssl_tls13_write_identity( ssl, p, end, identity, identity_len,
938 0, &output_len );
939#endif /* MBEDTLS_HAVE_TIME */
Jerry Yuf7c12592022-09-28 22:09:38 +0800940 if( ret != 0 )
941 return( ret );
Jerry Yuf7c12592022-09-28 22:09:38 +0800942
Jerry Yuf75364b2022-09-30 10:30:31 +0800943 p += output_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800944 l_binders_len += 1 + PSA_HASH_LENGTH( hash_alg );
Jerry Yuf75364b2022-09-30 10:30:31 +0800945 }
946#endif /* MBEDTLS_SSL_SESSION_TICKETS */
947
948 if( ssl_tls13_psk_get_identity(
949 ssl, &hash_alg, &identity, &identity_len ) == 0 )
Jerry Yuf7c12592022-09-28 22:09:38 +0800950 {
Jerry Yuf75364b2022-09-30 10:30:31 +0800951
Jerry Yua99cbfa2022-10-08 11:17:14 +0800952 ret = ssl_tls13_write_identity( ssl, p, end, identity, identity_len, 0,
Jerry Yuf75364b2022-09-30 10:30:31 +0800953 &output_len );
Jerry Yuf7c12592022-09-28 22:09:38 +0800954 if( ret != 0 )
955 return( ret );
Jerry Yuf75364b2022-09-30 10:30:31 +0800956
Jerry Yuf7c12592022-09-28 22:09:38 +0800957 p += output_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800958 l_binders_len += 1 + PSA_HASH_LENGTH( hash_alg );
Jerry Yuf7c12592022-09-28 22:09:38 +0800959 }
960
961 MBEDTLS_SSL_DEBUG_MSG( 3,
962 ( "client hello, adding pre_shared_key extension, "
963 "omitting PSK binder list" ) );
Jerry Yua99cbfa2022-10-08 11:17:14 +0800964
965 /* Take into account the two bytes for the length of the binders. */
966 l_binders_len += 2;
Jerry Yu6916e702022-10-10 21:33:51 +0800967 /* Check if there is enough space for binders */
Jerry Yua99cbfa2022-10-08 11:17:14 +0800968 MBEDTLS_SSL_CHK_BUF_PTR( p, end, l_binders_len );
969
Jerry Yuf7c12592022-09-28 22:09:38 +0800970 /*
971 * - extension_type (2 bytes)
972 * - extension_data_len (2 bytes)
973 * - identities_len (2 bytes)
974 */
975 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0 );
Jerry Yua99cbfa2022-10-08 11:17:14 +0800976 MBEDTLS_PUT_UINT16_BE( p - buf - 4 + l_binders_len , buf, 2 );
Jerry Yuf7c12592022-09-28 22:09:38 +0800977 MBEDTLS_PUT_UINT16_BE( p - buf - 6 , buf, 4 );
978
Jerry Yua99cbfa2022-10-08 11:17:14 +0800979 *out_len = ( p - buf ) + l_binders_len;
980 *binders_len = l_binders_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800981
982 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key identities", buf, p - buf );
983
984 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
985
XiaokangQianeb69aee2022-07-05 08:21:43 +0000986 return( 0 );
987}
988
XiaokangQian86981952022-07-19 09:51:50 +0000989int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Jerry Yu0c6105b2022-08-12 17:26:40 +0800990 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end )
XiaokangQianeb69aee2022-07-05 08:21:43 +0000991{
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
993 unsigned char *p = buf;
Jerry Yu6183cc72022-09-30 11:08:57 +0800994 psa_algorithm_t hash_alg = PSA_ALG_NONE;
995 const unsigned char *psk;
996 size_t psk_len;
997 size_t output_len;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800998
999 /* Check if we have space to write binders_len.
1000 * - binders_len (2 bytes)
1001 */
1002 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1003 p += 2;
1004
Jerry Yu6183cc72022-09-30 11:08:57 +08001005#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1006 if( ssl_tls13_ticket_get_psk( ssl, &hash_alg, &psk, &psk_len ) == 0 )
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001007 {
Jerry Yu6183cc72022-09-30 11:08:57 +08001008
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001009 ret = ssl_tls13_write_binder( ssl, p, end,
1010 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
Jerry Yu6183cc72022-09-30 11:08:57 +08001011 hash_alg, psk, psk_len,
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001012 &output_len );
1013 if( ret != 0 )
1014 return( ret );
1015 p += output_len;
1016 }
Jerry Yu6183cc72022-09-30 11:08:57 +08001017#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001018
Jerry Yu6183cc72022-09-30 11:08:57 +08001019 if( ssl_tls13_psk_get_psk( ssl, &hash_alg, &psk, &psk_len ) == 0 )
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001020 {
Jerry Yu6183cc72022-09-30 11:08:57 +08001021
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001022 ret = ssl_tls13_write_binder( ssl, p, end,
1023 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
Jerry Yu6183cc72022-09-30 11:08:57 +08001024 hash_alg, psk, psk_len,
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001025 &output_len );
1026 if( ret != 0 )
1027 return( ret );
1028 p += output_len;
1029 }
1030
1031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding PSK binder list." ) );
1032
1033 /*
1034 * - binders_len (2 bytes)
1035 */
1036 MBEDTLS_PUT_UINT16_BE( p - buf - 2, buf, 0 );
1037
1038 MBEDTLS_SSL_DEBUG_BUF( 3, "pre_shared_key binders", buf, p - buf );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001039
1040 return( 0 );
1041}
Jerry Yu0c6105b2022-08-12 17:26:40 +08001042
1043/*
1044 * struct {
1045 * opaque identity<1..2^16-1>;
1046 * uint32 obfuscated_ticket_age;
1047 * } PskIdentity;
1048 *
1049 * opaque PskBinderEntry<32..255>;
1050 *
1051 * struct {
1052 *
1053 * select (Handshake.msg_type) {
1054 * ...
1055 * case server_hello: uint16 selected_identity;
1056 * };
1057 *
1058 * } PreSharedKeyExtension;
1059 *
1060 */
1061MBEDTLS_CHECK_RETURN_CRITICAL
1062static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
1063 const unsigned char *buf,
1064 const unsigned char *end )
1065{
Jerry Yua99cbfa2022-10-08 11:17:14 +08001066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub300e3c2022-09-28 22:12:07 +08001067 int selected_identity;
Jerry Yub300e3c2022-09-28 22:12:07 +08001068 const unsigned char *psk;
1069 size_t psk_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001070 psa_algorithm_t hash_alg;
Jerry Yub300e3c2022-09-28 22:12:07 +08001071
Jerry Yua99cbfa2022-10-08 11:17:14 +08001072 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
Jerry Yub300e3c2022-09-28 22:12:07 +08001073 selected_identity = MBEDTLS_GET_UINT16_BE( buf, 0 );
Jerry Yua99cbfa2022-10-08 11:17:14 +08001074
Jerry Yub300e3c2022-09-28 22:12:07 +08001075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_identity = %d", selected_identity ) );
Jerry Yua99cbfa2022-10-08 11:17:14 +08001076
Jerry Yu4a698342022-09-30 12:22:01 +08001077 if( selected_identity >= ssl_tls13_get_configured_psk_count( ssl ) )
Jerry Yub300e3c2022-09-28 22:12:07 +08001078 {
Jerry Yua99cbfa2022-10-08 11:17:14 +08001079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid PSK identity." ) );
Jerry Yu4a698342022-09-30 12:22:01 +08001080
1081 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1082 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1083 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yub300e3c2022-09-28 22:12:07 +08001084 }
Jerry Yua99cbfa2022-10-08 11:17:14 +08001085
Jerry Yu4a698342022-09-30 12:22:01 +08001086#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1087 if( selected_identity == 0 && ssl_tls13_has_configured_ticket( ssl ) )
Jerry Yub300e3c2022-09-28 22:12:07 +08001088 {
Jerry Yua99cbfa2022-10-08 11:17:14 +08001089 ret = ssl_tls13_ticket_get_psk( ssl, &hash_alg, &psk, &psk_len );
Jerry Yu4a698342022-09-30 12:22:01 +08001090 }
1091 else
1092#endif
Ronald Crond29e13e2022-10-19 10:33:48 +02001093 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) )
Jerry Yu4a698342022-09-30 12:22:01 +08001094 {
Jerry Yua99cbfa2022-10-08 11:17:14 +08001095 ret = ssl_tls13_psk_get_psk( ssl, &hash_alg, &psk, &psk_len );
Jerry Yub300e3c2022-09-28 22:12:07 +08001096 }
1097 else
1098 {
1099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1101 }
Jerry Yub300e3c2022-09-28 22:12:07 +08001102 if( ret != 0 )
1103 return( ret );
1104
1105 ret = mbedtls_ssl_set_hs_psk( ssl, psk, psk_len );
1106 if( ret != 0 )
1107 {
1108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_set_hs_psk", ret );
Jerry Yu6916e702022-10-10 21:33:51 +08001109 return( ret );
Jerry Yub300e3c2022-09-28 22:12:07 +08001110 }
Jerry Yub300e3c2022-09-28 22:12:07 +08001111
Jerry Yu6916e702022-10-10 21:33:51 +08001112 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PRE_SHARED_KEY;
1113
1114 return( 0 );
Jerry Yu0c6105b2022-08-12 17:26:40 +08001115}
Ronald Cron41a443a2022-10-04 16:38:25 +02001116#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001117
Ronald Cron3d580bf2022-02-18 17:24:56 +01001118int mbedtls_ssl_tls13_write_client_hello_exts( mbedtls_ssl_context *ssl,
1119 unsigned char *buf,
1120 unsigned char *end,
1121 size_t *out_len )
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001122{
1123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1124 unsigned char *p = buf;
1125 size_t ext_len;
1126
1127 *out_len = 0;
1128
1129 /* Write supported_versions extension
1130 *
1131 * Supported Versions Extension is mandatory with TLS 1.3.
1132 */
1133 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &ext_len );
1134 if( ret != 0 )
1135 return( ret );
1136 p += ext_len;
1137
1138 /* Echo the cookie if the server provided one in its preceding
1139 * HelloRetryRequest message.
1140 */
1141 ret = ssl_tls13_write_cookie_ext( ssl, p, end, &ext_len );
1142 if( ret != 0 )
1143 return( ret );
1144 p += ext_len;
1145
Ronald Cron766c0cd2022-10-18 12:17:11 +02001146#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001147 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1148 {
1149 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &ext_len );
1150 if( ret != 0 )
1151 return( ret );
1152 p += ext_len;
1153 }
Ronald Cron766c0cd2022-10-18 12:17:11 +02001154#endif
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001155
Ronald Cron41a443a2022-10-04 16:38:25 +02001156#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001157 /* For PSK-based key exchange we need the pre_shared_key extension
1158 * and the psk_key_exchange_modes extension.
1159 *
1160 * The pre_shared_key extension MUST be the last extension in the
1161 * ClientHello. Servers MUST check that it is the last extension and
1162 * otherwise fail the handshake with an "illegal_parameter" alert.
1163 *
1164 * Add the psk_key_exchange_modes extension.
1165 */
1166 ret = ssl_tls13_write_psk_key_exchange_modes_ext( ssl, p, end, &ext_len );
1167 if( ret != 0 )
1168 return( ret );
1169 p += ext_len;
Ronald Cron41a443a2022-10-04 16:38:25 +02001170#endif
XiaokangQianeb69aee2022-07-05 08:21:43 +00001171
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001172 *out_len = p - buf;
1173
1174 return( 0 );
1175}
1176
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001177/*
Jerry Yu4a173382021-10-11 21:45:31 +08001178 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001179 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001180
Ronald Cronda41b382022-03-30 09:57:11 +02001181/**
1182 * \brief Detect if the ServerHello contains a supported_versions extension
1183 * or not.
1184 *
1185 * \param[in] ssl SSL context
1186 * \param[in] buf Buffer containing the ServerHello message
1187 * \param[in] end End of the buffer containing the ServerHello message
1188 *
1189 * \return 0 if the ServerHello does not contain a supported_versions extension
1190 * \return 1 if the ServerHello contains a supported_versions extension
1191 * \return A negative value if an error occurred while parsing the ServerHello.
1192 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001193MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001194static int ssl_tls13_is_supported_versions_ext_present(
1195 mbedtls_ssl_context *ssl,
1196 const unsigned char *buf,
1197 const unsigned char *end )
1198{
1199 const unsigned char *p = buf;
1200 size_t legacy_session_id_echo_len;
1201 size_t extensions_len;
1202 const unsigned char *extensions_end;
1203
1204 /*
1205 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001206 * length:
1207 * - legacy_version 2 bytes
1208 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1209 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001210 */
1211 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3 );
1212 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1213 legacy_session_id_echo_len = *p;
1214
1215 /*
1216 * Jump to the extensions, jumping over:
1217 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1218 * - cipher_suite 2 bytes
1219 * - legacy_compression_method 1 byte
1220 */
Ronald Cron28271062022-06-10 14:43:55 +02001221 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len + 4 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001222 p += legacy_session_id_echo_len + 4;
1223
1224 /* Case of no extension */
1225 if( p == end )
1226 return( 0 );
1227
1228 /* ...
1229 * Extension extensions<6..2^16-1>;
1230 * ...
1231 * struct {
1232 * ExtensionType extension_type; (2 bytes)
1233 * opaque extension_data<0..2^16-1>;
1234 * } Extension;
1235 */
1236 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1237 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1238 p += 2;
1239
1240 /* Check extensions do not go beyond the buffer of data. */
1241 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1242 extensions_end = p + extensions_len;
1243
1244 while( p < extensions_end )
1245 {
1246 unsigned int extension_type;
1247 size_t extension_data_len;
1248
1249 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1250 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1251 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1252 p += 4;
1253
1254 if( extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS )
1255 return( 1 );
1256
1257 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1258 p += extension_data_len;
1259 }
1260
1261 return( 0 );
1262}
1263
Jerry Yu7a186a02021-10-15 18:46:14 +08001264/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001265 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1266 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1267 * - 0 otherwise
1268 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001269MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfd6193c2022-04-05 11:04:20 +02001270static int ssl_tls13_is_downgrade_negotiation( mbedtls_ssl_context *ssl,
1271 const unsigned char *buf,
1272 const unsigned char *end )
1273{
1274 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1275 static const unsigned char magic_downgrade_string[] =
1276 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1277 const unsigned char *last_eight_bytes_of_random;
1278 unsigned char last_byte_of_random;
1279
1280 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2 );
1281 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1282
1283 if( memcmp( last_eight_bytes_of_random,
1284 magic_downgrade_string,
1285 sizeof( magic_downgrade_string ) ) == 0 )
1286 {
1287 last_byte_of_random = last_eight_bytes_of_random[7];
1288 return( last_byte_of_random == 0 ||
1289 last_byte_of_random == 1 );
1290 }
1291
1292 return( 0 );
1293}
1294
1295/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001296 * - SSL_SERVER_HELLO or
1297 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001298 * to indicate which message is expected and to be parsed next.
1299 */
Ronald Cron828aff62022-05-31 12:04:31 +02001300#define SSL_SERVER_HELLO 0
1301#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001302MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yub85277e2021-10-13 13:36:05 +08001303static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1304 const unsigned char *buf,
1305 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001306{
Jerry Yue1b9c292021-09-10 10:08:31 +08001307
1308 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1309 *
Jerry Yu4a173382021-10-11 21:45:31 +08001310 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001311 * special value of the SHA-256 of "HelloRetryRequest".
1312 *
1313 * struct {
1314 * ProtocolVersion legacy_version = 0x0303;
1315 * Random random;
1316 * opaque legacy_session_id_echo<0..32>;
1317 * CipherSuite cipher_suite;
1318 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001319 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001320 * } ServerHello;
1321 *
1322 */
Jerry Yu93a13f22022-04-11 23:00:01 +08001323 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end,
1324 2 + sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001325
Jerry Yu93a13f22022-04-11 23:00:01 +08001326 if( memcmp( buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1327 sizeof( mbedtls_ssl_tls13_hello_retry_request_magic ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001328 {
Ronald Cron828aff62022-05-31 12:04:31 +02001329 return( SSL_SERVER_HELLO_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001330 }
1331
Ronald Cron828aff62022-05-31 12:04:31 +02001332 return( SSL_SERVER_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001333}
1334
Ronald Cron828aff62022-05-31 12:04:31 +02001335/*
Jerry Yu745bb612021-10-13 22:01:04 +08001336 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001337 * - SSL_SERVER_HELLO or
1338 * - SSL_SERVER_HELLO_HRR or
1339 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001340 */
Ronald Cron828aff62022-05-31 12:04:31 +02001341#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001342MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron828aff62022-05-31 12:04:31 +02001343static int ssl_tls13_preprocess_server_hello( mbedtls_ssl_context *ssl,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001344 const unsigned char *buf,
1345 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001346{
Jerry Yu4a173382021-10-11 21:45:31 +08001347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001348 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001349
Ronald Cron9f0fba32022-02-10 16:45:15 +01001350 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_is_supported_versions_ext_present(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001351 ssl, buf, end ) );
Jerry Yua66fece2022-07-13 14:30:29 +08001352
Ronald Cron9f0fba32022-02-10 16:45:15 +01001353 if( ret == 0 )
1354 {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001355 MBEDTLS_SSL_PROC_CHK_NEG(
Ronald Crondb5dfa12022-05-31 11:44:38 +02001356 ssl_tls13_is_downgrade_negotiation( ssl, buf, end ) );
Ronald Cronfd6193c2022-04-05 11:04:20 +02001357
1358 /* If the server is negotiating TLS 1.2 or below and:
1359 * . we did not propose TLS 1.2 or
1360 * . the server responded it is TLS 1.3 capable but negotiating a lower
1361 * version of the protocol and thus we are under downgrade attack
1362 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001363 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001364 if( handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001365 {
1366 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1367 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1368 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1369 }
1370
1371 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001372 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001373 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
Ronald Crondb5dfa12022-05-31 11:44:38 +02001374 buf, (size_t)(end - buf) );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001375
1376 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1377 {
1378 ret = ssl_tls13_reset_key_share( ssl );
1379 if( ret != 0 )
1380 return( ret );
1381 }
1382
Ronald Cron828aff62022-05-31 12:04:31 +02001383 return( SSL_SERVER_HELLO_TLS1_2 );
Ronald Cron9f0fba32022-02-10 16:45:15 +01001384 }
1385
Jerry Yua66fece2022-07-13 14:30:29 +08001386#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1387 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1388 ssl->session_negotiate->tls_version = ssl->tls_version;
1389#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1390
Ronald Cron5afb9042022-05-31 12:11:39 +02001391 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1392
Ronald Crondb5dfa12022-05-31 11:44:38 +02001393 ret = ssl_server_hello_is_hrr( ssl, buf, end );
Jerry Yub85277e2021-10-13 13:36:05 +08001394 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001395 {
Ronald Cron828aff62022-05-31 12:04:31 +02001396 case SSL_SERVER_HELLO:
Jerry Yu745bb612021-10-13 22:01:04 +08001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1398 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001399 case SSL_SERVER_HELLO_HRR:
Jerry Yu745bb612021-10-13 22:01:04 +08001400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001401 /* If a client receives a second
1402 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1403 * was itself in response to a HelloRetryRequest), it MUST abort the
1404 * handshake with an "unexpected_message" alert.
1405 */
Ronald Cron5afb9042022-05-31 12:11:39 +02001406 if( handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1409 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1410 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1411 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1412 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001413 /*
1414 * Clients must abort the handshake with an "illegal_parameter"
1415 * alert if the HelloRetryRequest would not result in any change
1416 * in the ClientHello.
1417 * In a PSK only key exchange that what we expect.
1418 */
1419 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1420 {
1421 MBEDTLS_SSL_DEBUG_MSG( 1,
1422 ( "Unexpected HRR in pure PSK key exchange." ) );
1423 MBEDTLS_SSL_PEND_FATAL_ALERT(
1424 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1425 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1426 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1427 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001428
Ronald Cron5afb9042022-05-31 12:11:39 +02001429 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001430
Jerry Yu745bb612021-10-13 22:01:04 +08001431 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001432 }
1433
1434cleanup:
1435
1436 return( ret );
1437}
1438
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001439MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu4a173382021-10-11 21:45:31 +08001440static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1441 const unsigned char **buf,
1442 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001443{
1444 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001445 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001446
Jerry Yude4fb2c2021-09-19 18:05:08 +08001447 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001448 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001449
Jerry Yu4a173382021-10-11 21:45:31 +08001450 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001451
1452 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001453 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1454 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001455 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001456 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1457 ssl->session_negotiate->id,
1458 ssl->session_negotiate->id_len );
1459 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001460 legacy_session_id_echo_len );
1461
1462 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1463 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1464
Jerry Yue1b9c292021-09-10 10:08:31 +08001465 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1466 }
1467
Jerry Yu4a173382021-10-11 21:45:31 +08001468 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001469 *buf = p;
1470
1471 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001472 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001473 return( 0 );
1474}
1475
Jerry Yue1b9c292021-09-10 10:08:31 +08001476/* Parse ServerHello message and configure context
1477 *
1478 * struct {
1479 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1480 * Random random;
1481 * opaque legacy_session_id_echo<0..32>;
1482 * CipherSuite cipher_suite;
1483 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001484 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001485 * } ServerHello;
1486 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001487MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +08001488static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1489 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001490 const unsigned char *end,
1491 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001492{
Jerry Yub85277e2021-10-13 13:36:05 +08001493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001494 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001495 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001496 size_t extensions_len;
1497 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001498 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001499 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001500 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001501
1502 /*
1503 * Check there is space for minimal fields
1504 *
1505 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001506 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001507 * - legacy_session_id_echo ( 1 byte ), minimum size
1508 * - cipher_suite ( 2 bytes)
1509 * - legacy_compression_method ( 1 byte )
1510 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001511 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001512
1513 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001514 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1515
Jerry Yu4a173382021-10-11 21:45:31 +08001516 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001517 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001518 * ...
1519 * with ProtocolVersion defined as:
1520 * uint16 ProtocolVersion;
1521 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001522 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
1523 MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001524 {
1525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1526 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1527 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001528 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1529 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001530 }
1531 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001532
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001533 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001534 * Random random;
1535 * ...
1536 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001537 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001538 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001539 if( !is_hrr )
1540 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001541 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001542 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1543 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1544 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1545 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001546 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001547
Jerry Yu4a173382021-10-11 21:45:31 +08001548 /* ...
1549 * opaque legacy_session_id_echo<0..32>;
1550 * ...
1551 */
1552 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001553 {
XiaokangQian52da5582022-01-26 09:49:29 +00001554 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001555 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001556 }
1557
Jerry Yu4a173382021-10-11 21:45:31 +08001558 /* ...
1559 * CipherSuite cipher_suite;
1560 * ...
1561 * with CipherSuite defined as:
1562 * uint8 CipherSuite[2];
1563 */
1564 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001565 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1566 p += 2;
1567
Jerry Yu4a173382021-10-11 21:45:31 +08001568
XiaokangQian355e09a2022-01-20 11:14:50 +00001569 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001570 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001571 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001572 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04001573 if( ( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1574 ssl->tls_version,
1575 ssl->tls_version ) != 0 ) ||
XiaokangQian08037552022-04-20 07:16:41 +00001576 !mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
Jerry Yue1b9c292021-09-10 10:08:31 +08001577 {
XiaokangQian52da5582022-01-26 09:49:29 +00001578 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001579 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001580 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001581 * If we received an HRR before and that the proposed selected
1582 * ciphersuite in this server hello is not the same as the one
1583 * proposed in the HRR, we abort the handshake and send an
1584 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001585 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001586 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1587 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001588 {
XiaokangQian52da5582022-01-26 09:49:29 +00001589 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001590 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001591
XiaokangQian52da5582022-01-26 09:49:29 +00001592 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001593 {
1594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1595 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001596 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001597 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001598
Jerry Yu4a173382021-10-11 21:45:31 +08001599 /* Configure ciphersuites */
1600 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1601
XiaokangQian355e09a2022-01-20 11:14:50 +00001602 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001603 ssl->session_negotiate->ciphersuite = cipher_suite;
1604
Jerry Yue1b9c292021-09-10 10:08:31 +08001605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1606 cipher_suite, ciphersuite_info->name ) );
1607
1608#if defined(MBEDTLS_HAVE_TIME)
1609 ssl->session_negotiate->start = time( NULL );
1610#endif /* MBEDTLS_HAVE_TIME */
1611
Jerry Yu4a173382021-10-11 21:45:31 +08001612 /* ...
1613 * uint8 legacy_compression_method = 0;
1614 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001615 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Thomas Daubney31e03a82022-07-25 15:59:25 +01001617 if( p[0] != MBEDTLS_SSL_COMPRESS_NULL )
Jerry Yue1b9c292021-09-10 10:08:31 +08001618 {
Jerry Yub85277e2021-10-13 13:36:05 +08001619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001620 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001621 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001622 }
1623 p++;
1624
Jerry Yub85277e2021-10-13 13:36:05 +08001625 /* ...
1626 * Extension extensions<6..2^16-1>;
1627 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001628 * struct {
1629 * ExtensionType extension_type; (2 bytes)
1630 * opaque extension_data<0..2^16-1>;
1631 * } Extension;
1632 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001634 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001635 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001636
Jerry Yu4a173382021-10-11 21:45:31 +08001637 /* Check extensions do not go beyond the buffer of data. */
1638 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1639 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001640
Jerry Yu4a173382021-10-11 21:45:31 +08001641 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001642
Jerry Yu4a173382021-10-11 21:45:31 +08001643 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001644 {
1645 unsigned int extension_type;
1646 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001647 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001648
Jerry Yu4a173382021-10-11 21:45:31 +08001649 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001650 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1651 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1652 p += 4;
1653
Jerry Yu4a173382021-10-11 21:45:31 +08001654 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001655 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001656
1657 switch( extension_type )
1658 {
XiaokangQianb851da82022-01-14 04:03:11 +00001659 case MBEDTLS_TLS_EXT_COOKIE:
1660
XiaokangQiand9e068e2022-01-18 06:23:32 +00001661 if( !is_hrr )
1662 {
XiaokangQian52da5582022-01-26 09:49:29 +00001663 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001664 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001665 }
1666
XiaokangQian43550bd2022-01-21 04:32:58 +00001667 ret = ssl_tls13_parse_cookie_ext( ssl,
1668 p, extension_data_end );
1669 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001670 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001671 MBEDTLS_SSL_DEBUG_RET( 1,
1672 "ssl_tls13_parse_cookie_ext",
1673 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001674 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001675 }
XiaokangQianb851da82022-01-14 04:03:11 +00001676 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001677
Jerry Yue1b9c292021-09-10 10:08:31 +08001678 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Jerry Yuc068b662021-10-11 22:30:19 +08001679 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001680 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001681 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001682 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001683 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001684 break;
1685
Ronald Cron41a443a2022-10-04 16:38:25 +02001686#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001687 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
XiaokangQianeb69aee2022-07-05 08:21:43 +00001688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension" ) );
1689 if( is_hrr )
1690 {
1691 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1692 goto cleanup;
1693 }
Jerry Yu4a173382021-10-11 21:45:31 +08001694
XiaokangQian86981952022-07-19 09:51:50 +00001695 if( ( ret = ssl_tls13_parse_server_pre_shared_key_ext(
XiaokangQian008d2bf2022-07-14 07:54:01 +00001696 ssl, p, extension_data_end ) ) != 0 )
XiaokangQianeb69aee2022-07-05 08:21:43 +00001697 {
1698 MBEDTLS_SSL_DEBUG_RET(
XiaokangQian86981952022-07-19 09:51:50 +00001699 1, ( "ssl_tls13_parse_server_pre_shared_key_ext" ), ret );
XiaokangQianeb69aee2022-07-05 08:21:43 +00001700 return( ret );
1701 }
1702 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001703#endif
Jerry Yue1b9c292021-09-10 10:08:31 +08001704
Jerry Yue1b9c292021-09-10 10:08:31 +08001705 case MBEDTLS_TLS_EXT_KEY_SHARE:
1706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001707 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1708 {
XiaokangQian52da5582022-01-26 09:49:29 +00001709 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001710 goto cleanup;
1711 }
1712
XiaokangQian53f20b72022-01-18 10:47:33 +00001713 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001714 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001715 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001716 else
XiaokangQianb851da82022-01-14 04:03:11 +00001717 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001718 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001719 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001720 {
1721 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001722 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001723 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001724 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001725 }
1726 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001727
1728 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001729 MBEDTLS_SSL_DEBUG_MSG(
1730 3,
1731 ( "unknown extension found: %u ( ignoring )",
1732 extension_type ) );
1733
XiaokangQian52da5582022-01-26 09:49:29 +00001734 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001735 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001736 }
1737
1738 p += extension_data_len;
1739 }
1740
XiaokangQiand59be772022-01-24 10:12:51 +00001741cleanup:
1742
XiaokangQian52da5582022-01-26 09:49:29 +00001743 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001744 {
1745 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1746 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1747 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1748 }
XiaokangQian52da5582022-01-26 09:49:29 +00001749 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001750 {
1751 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1752 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1753 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1754 }
1755 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001756}
1757
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001758#if defined(MBEDTLS_DEBUG_C)
1759static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001760{
1761 switch( mode )
1762 {
1763 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1764 return "psk";
1765 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1766 return "ephemeral";
1767 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1768 return "psk_ephemeral";
1769 default:
1770 return "unknown mode";
1771 }
1772}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001773#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001774
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001775MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001776static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001777{
Jerry Yub85277e2021-10-13 13:36:05 +08001778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001779 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001780
Jerry Yub85277e2021-10-13 13:36:05 +08001781 /* Determine the key exchange mode:
1782 * 1) If both the pre_shared_key and key_share extensions were received
1783 * then the key exchange mode is PSK with EPHEMERAL.
1784 * 2) If only the pre_shared_key extension was received then the key
1785 * exchange mode is PSK-only.
1786 * 3) If only the key_share extension was received then the key
1787 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001788 */
Jerry Yub85277e2021-10-13 13:36:05 +08001789 switch( handshake->extensions_present &
1790 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001791 {
Jerry Yu745bb612021-10-13 22:01:04 +08001792 /* Only the pre_shared_key extension was received */
1793 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Ronald Cron79907712022-07-20 17:05:29 +02001794 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001795 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001796
Jerry Yu745bb612021-10-13 22:01:04 +08001797 /* Only the key_share extension was received */
1798 case MBEDTLS_SSL_EXT_KEY_SHARE:
Ronald Cron79907712022-07-20 17:05:29 +02001799 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001800 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001801
Jerry Yu745bb612021-10-13 22:01:04 +08001802 /* Both the pre_shared_key and key_share extensions were received */
1803 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Ronald Cron79907712022-07-20 17:05:29 +02001804 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001805 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001806
Jerry Yu745bb612021-10-13 22:01:04 +08001807 /* Neither pre_shared_key nor key_share extension was received */
1808 default:
1809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1810 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1811 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001812 }
1813
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001814 if( !mbedtls_ssl_conf_tls13_check_kex_modes( ssl, handshake->key_exchange_mode ) )
1815 {
1816 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1817 MBEDTLS_SSL_DEBUG_MSG( 2,
Xiaokang Qianca343ae2022-09-28 02:07:54 +00001818 ( "Key exchange mode(%s) is not supported.",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001819 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001820 goto cleanup;
1821 }
1822
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001823 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaokang Qianca343ae2022-09-28 02:07:54 +00001824 ( "Selected key exchange mode: %s",
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001825 ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001826
Jerry Yu0b177842021-09-05 19:41:30 +08001827 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1828 *
1829 * TODO: We don't have to do this in case we offered 0-RTT and the
1830 * server accepted it. In this case, we could skip generating
1831 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001832 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001833 if( ret != 0 )
1834 {
Jerry Yue110d252022-05-05 10:19:22 +08001835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early",
Jerry Yu0b177842021-09-05 19:41:30 +08001836 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001837 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001838 }
1839
Jerry Yuf86eb752022-05-06 11:16:55 +08001840 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001841 if( ret != 0 )
1842 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001843 MBEDTLS_SSL_DEBUG_RET( 1,
1844 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yuc068b662021-10-11 22:30:19 +08001845 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001846 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001847 }
1848
Jerry Yue110d252022-05-05 10:19:22 +08001849 mbedtls_ssl_set_inbound_transform( ssl, handshake->transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1851 ssl->session_in = ssl->session_negotiate;
1852
Jerry Yu4a173382021-10-11 21:45:31 +08001853cleanup:
Jerry Yu4a173382021-10-11 21:45:31 +08001854 if( ret != 0 )
1855 {
Jerry Yu4a173382021-10-11 21:45:31 +08001856 MBEDTLS_SSL_PEND_FATAL_ALERT(
1857 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1858 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1859 }
Jerry Yue110d252022-05-05 10:19:22 +08001860
Jerry Yu4a173382021-10-11 21:45:31 +08001861 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001862}
1863
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001864MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian355e09a2022-01-20 11:14:50 +00001865static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001866{
1867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1868
XiaokangQian78b1fa72022-01-19 06:56:30 +00001869 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001870
XiaokangQian78b1fa72022-01-19 06:56:30 +00001871 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001872 * We are going to re-generate a shared secret corresponding to the group
1873 * selected by the server, which is different from the group for which we
1874 * generated a shared secret in the first client hello.
1875 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001876 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001877 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001878 if( ret != 0 )
1879 return( ret );
1880
1881 return( 0 );
1882}
1883
Jerry Yue1b9c292021-09-10 10:08:31 +08001884/*
Jerry Yu4a173382021-10-11 21:45:31 +08001885 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001886 * Handler for MBEDTLS_SSL_SERVER_HELLO
1887 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001888MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00001889static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001890{
Jerry Yu4a173382021-10-11 21:45:31 +08001891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001892 unsigned char *buf = NULL;
1893 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001894 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001895
1896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1897
Ronald Crondb5dfa12022-05-31 11:44:38 +02001898 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1899 MBEDTLS_SSL_HS_SERVER_HELLO,
1900 &buf, &buf_len ) );
1901
Ronald Cron828aff62022-05-31 12:04:31 +02001902 ret = ssl_tls13_preprocess_server_hello( ssl, buf, buf + buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001903 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001904 goto cleanup;
1905 else
Ronald Cron828aff62022-05-31 12:04:31 +02001906 is_hrr = ( ret == SSL_SERVER_HELLO_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001907
Ronald Cron828aff62022-05-31 12:04:31 +02001908 if( ret == SSL_SERVER_HELLO_TLS1_2 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01001909 {
1910 ret = 0;
1911 goto cleanup;
1912 }
1913
XiaokangQianb851da82022-01-14 04:03:11 +00001914 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001915 buf + buf_len,
1916 is_hrr ) );
1917 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001918 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1919
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001920 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1921 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001922
XiaokangQiand9e068e2022-01-18 06:23:32 +00001923 if( is_hrr )
Ronald Cronfb508b82022-05-31 14:49:55 +02001924 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001925 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001926#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1927 /* If not offering early data, the client sends a dummy CCS record
1928 * immediately before its second flight. This may either be before
1929 * its second ClientHello or before its encrypted handshake flight.
1930 */
1931 mbedtls_ssl_handshake_set_state( ssl,
1932 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1933#else
1934 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1935#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1936 }
XiaokangQiand9e068e2022-01-18 06:23:32 +00001937 else
Ronald Cronfb508b82022-05-31 14:49:55 +02001938 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001939 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Ronald Cronfb508b82022-05-31 14:49:55 +02001940 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1941 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001942
1943cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1945 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001946 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001947}
1948
1949/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001950 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001951 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001952 *
1953 * The EncryptedExtensions message contains any extensions which
1954 * should be protected, i.e., any which are not needed to establish
1955 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001956 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001957
XiaokangQian08da26c2021-10-09 10:12:11 +00001958/* Parse EncryptedExtensions message
1959 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001960 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001961 * } EncryptedExtensions;
1962 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001963MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian97799ac2021-10-11 10:05:54 +00001964static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1965 const unsigned char *buf,
1966 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001967{
1968 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001969 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001970 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001971 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001972
XiaokangQian08da26c2021-10-09 10:12:11 +00001973 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001974 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001975 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001976
XiaokangQian97799ac2021-10-11 10:05:54 +00001977 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001978 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
Ronald Cronc8083592022-05-31 16:24:05 +02001979 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001980
XiaokangQian8db25ff2021-10-13 05:56:18 +00001981 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001982 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001983 unsigned int extension_type;
1984 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001985
XiaokangQian08da26c2021-10-09 10:12:11 +00001986 /*
1987 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001988 * ExtensionType extension_type; (2 bytes)
1989 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001990 * } Extension;
1991 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001992 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001993 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1994 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1995 p += 4;
1996
XiaokangQian8db25ff2021-10-13 05:56:18 +00001997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001998
XiaokangQian97799ac2021-10-11 10:05:54 +00001999 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002000 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00002001 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002002 */
XiaokangQian08da26c2021-10-09 10:12:11 +00002003 switch( extension_type )
2004 {
Jerry Yu661dd942022-08-03 14:50:01 +08002005 case MBEDTLS_TLS_EXT_SERVERNAME:
2006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found server_name extension" ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002007
Jerry Yu661dd942022-08-03 14:50:01 +08002008 /* The server_name extension should be an empty extension */
2009
2010 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00002011 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
2012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
2013 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002014
lhuang0486cacac2022-01-21 07:34:27 -08002015#if defined(MBEDTLS_SSL_ALPN)
2016 case MBEDTLS_TLS_EXT_ALPN:
2017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
2018
2019 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
2020 {
2021 return( ret );
2022 }
2023
2024 break;
2025#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00002026 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00002027 MBEDTLS_SSL_DEBUG_MSG(
2028 3, ( "unsupported extension found: %u ", extension_type) );
2029 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08002030 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00002031 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
2032 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00002033 }
2034
XiaokangQian08da26c2021-10-09 10:12:11 +00002035 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00002036 }
XiaokangQian08da26c2021-10-09 10:12:11 +00002037
XiaokangQian97799ac2021-10-11 10:05:54 +00002038 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00002039 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00002040 {
2041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08002042 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00002043 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00002044 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002045 }
2046
2047 return( ret );
2048}
2049
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002050MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9d6a5452022-05-30 16:05:38 +02002051static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
2052{
2053 int ret;
2054 unsigned char *buf;
2055 size_t buf_len;
2056
2057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
2058
2059 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2060 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2061 &buf, &buf_len ) );
2062
2063 /* Process the message contents */
2064 MBEDTLS_SSL_PROC_CHK(
2065 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
2066
2067 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2068 buf, buf_len );
2069
Ronald Cron928cbd32022-10-04 16:14:26 +02002070#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron79907712022-07-20 17:05:29 +02002071 if( mbedtls_ssl_tls13_key_exchange_mode_with_psk( ssl ) )
Ronald Cronfb508b82022-05-31 14:49:55 +02002072 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2073 else
2074 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
2075#else
2076 ((void) ssl);
2077 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2078#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02002079
2080cleanup:
2081
2082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
2083 return( ret );
2084
2085}
2086
Ronald Cron928cbd32022-10-04 16:14:26 +02002087#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08002088/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002089 * STATE HANDLING: CertificateRequest
2090 *
Jerry Yud2674312021-10-29 10:08:19 +08002091 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002092#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2093#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002094/* Coordination:
2095 * Deals with the ambiguity of not knowing if a CertificateRequest
2096 * will be sent. Returns a negative code on failure, or
2097 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2098 * - SSL_CERTIFICATE_REQUEST_SKIP
2099 * indicating if a Certificate Request is expected or not.
2100 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002101MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002102static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08002103{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08002105
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002106 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08002107 {
2108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2109 return( ret );
2110 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002111 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002112
2113 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
2114 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
2115 {
Ronald Cron19385882022-06-15 16:26:13 +02002116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got a certificate request" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002117 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08002118 }
2119
Ronald Cron19385882022-06-15 16:26:13 +02002120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got no certificate request" ) );
2121
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002122 return( SSL_CERTIFICATE_REQUEST_SKIP );
2123}
2124
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002125/*
2126 * ssl_tls13_parse_certificate_request()
2127 * Parse certificate request
2128 * struct {
2129 * opaque certificate_request_context<0..2^8-1>;
2130 * Extension extensions<2..2^16-1>;
2131 * } CertificateRequest;
2132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002134static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
2135 const unsigned char *buf,
2136 const unsigned char *end )
2137{
2138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2139 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002140 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002141 size_t extensions_len = 0;
2142 const unsigned char *extensions_end;
2143 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002144
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002145 /* ...
2146 * opaque certificate_request_context<0..2^8-1>
2147 * ...
2148 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002149 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
2150 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002151 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002152
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002153 if( certificate_request_context_len > 0 )
2154 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002155 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002156 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
2157 p, certificate_request_context_len );
2158
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002159 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002160 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002161 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002162 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002163 {
2164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2165 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2166 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002167 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002168 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002169 p += certificate_request_context_len;
2170 }
2171
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002172 /* ...
2173 * Extension extensions<2..2^16-1>;
2174 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002175 */
2176 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2177 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2178 p += 2;
2179
2180 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2181 extensions_end = p + extensions_len;
2182
2183 while( p < extensions_end )
2184 {
2185 unsigned int extension_type;
2186 size_t extension_data_len;
2187
2188 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
2189 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2190 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2191 p += 4;
2192
2193 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
2194
2195 switch( extension_type )
2196 {
2197 case MBEDTLS_TLS_EXT_SIG_ALG:
2198 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002199 ( "found signature algorithms extension" ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02002200 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, p,
Gabor Mezei696956d2022-05-13 16:27:29 +02002201 p + extension_data_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002202 if( ret != 0 )
2203 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002204 if( ! sig_alg_ext_found )
2205 sig_alg_ext_found = 1;
2206 else
2207 {
2208 MBEDTLS_SSL_DEBUG_MSG( 3,
2209 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002210 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002211 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002212 break;
2213
2214 default:
2215 MBEDTLS_SSL_DEBUG_MSG(
2216 3,
2217 ( "unknown extension found: %u ( ignoring )",
2218 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002219 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002220 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002221 p += extension_data_len;
2222 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002223 /* Check that we consumed all the message. */
2224 if( p != end )
2225 {
2226 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002227 ( "CertificateRequest misaligned" ) );
2228 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002229 }
2230 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002231 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002232 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00002233 MBEDTLS_SSL_DEBUG_MSG( 3,
2234 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002235 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002236 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002237
Jerry Yu7840f812022-01-29 10:26:51 +08002238 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002239 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002240
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002241decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002242 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2243 MBEDTLS_ERR_SSL_DECODE_ERROR );
2244 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002245}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002246
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002247/*
2248 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2249 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002250MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002251static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002252{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002254
2255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2256
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002257 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
2258
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002259 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
2260 {
2261 unsigned char *buf;
2262 size_t buf_len;
2263
2264 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
2265 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2266 &buf, &buf_len ) );
2267
2268 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
2269 buf, buf + buf_len ) );
2270
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002271 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2272 buf, buf_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002273 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002274 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002275 {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002276 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002277 }
2278 else
2279 {
2280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002281 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2282 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002283 }
2284
Jerry Yud2674312021-10-29 10:08:19 +08002285 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
2286
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002287cleanup:
2288
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2290 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002291}
2292
2293/*
Jerry Yu687101b2021-09-14 16:03:56 +08002294 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2295 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002296MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002297static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002298{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002299 int ret;
2300
2301 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002302 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002303 return( ret );
2304
Jerry Yu687101b2021-09-14 16:03:56 +08002305 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2306 return( 0 );
2307}
2308
2309/*
2310 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2311 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002312MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002313static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002314{
Jerry Yu30b071c2021-09-12 20:16:03 +08002315 int ret;
2316
2317 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2318 if( ret != 0 )
2319 return( ret );
2320
Jerry Yu687101b2021-09-14 16:03:56 +08002321 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2322 return( 0 );
2323}
Ronald Cron928cbd32022-10-04 16:14:26 +02002324#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002325
Jerry Yu687101b2021-09-14 16:03:56 +08002326/*
2327 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2328 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002329MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002330static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002331{
XiaokangQianac0385c2021-11-03 06:40:11 +00002332 int ret;
2333
XiaokangQianc5c39d52021-11-09 11:55:10 +00002334 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002335 if( ret != 0 )
2336 return( ret );
2337
Jerry Yue3d67cb2022-05-19 15:33:10 +08002338 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
2339 if( ret != 0 )
2340 {
2341 MBEDTLS_SSL_PEND_FATAL_ALERT(
2342 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2343 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2344 return( ret );
2345 }
2346
Ronald Cron49ad6192021-11-24 16:25:31 +01002347#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2348 mbedtls_ssl_handshake_set_state(
2349 ssl,
2350 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2351#else
Jerry Yuca133a32022-02-15 14:22:05 +08002352 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu566c7812022-01-26 15:41:22 +08002353#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002354
XiaokangQianac0385c2021-11-03 06:40:11 +00002355 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002356}
2357
2358/*
Jerry Yu566c7812022-01-26 15:41:22 +08002359 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2360 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002361MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002362static int ssl_tls13_write_client_certificate( mbedtls_ssl_context *ssl )
2363{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002364 int non_empty_certificate_msg = 0;
2365
Jerry Yu5cc35062022-01-28 16:16:08 +08002366 MBEDTLS_SSL_DEBUG_MSG( 1,
2367 ( "Switch to handshake traffic keys for outbound traffic" ) );
Jerry Yu566c7812022-01-26 15:41:22 +08002368 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
Jerry Yu5cc35062022-01-28 16:16:08 +08002369
Ronald Cron928cbd32022-10-04 16:14:26 +02002370#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002371 if( ssl->handshake->client_auth )
Ronald Cron7a94aca2022-03-09 07:44:27 +01002372 {
2373 int ret = mbedtls_ssl_tls13_write_certificate( ssl );
2374 if( ret != 0 )
2375 return( ret );
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002376
Ronald Cron7a94aca2022-03-09 07:44:27 +01002377 if( mbedtls_ssl_own_cert( ssl ) != NULL )
2378 non_empty_certificate_msg = 1;
2379 }
2380 else
2381 {
XiaokangQian23c5be62022-06-07 02:04:34 +00002382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002383 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002384#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002385
Ronald Cron7a94aca2022-03-09 07:44:27 +01002386 if( non_empty_certificate_msg )
2387 {
2388 mbedtls_ssl_handshake_set_state( ssl,
2389 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
2390 }
2391 else
Ronald Cron19385882022-06-15 16:26:13 +02002392 {
2393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip write certificate verify" ) );
Ronald Cron7a94aca2022-03-09 07:44:27 +01002394 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02002395 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002396
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002397 return( 0 );
Jerry Yu566c7812022-01-26 15:41:22 +08002398}
2399
Ronald Cron928cbd32022-10-04 16:14:26 +02002400#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002401/*
2402 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2403 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002404MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu566c7812022-01-26 15:41:22 +08002405static int ssl_tls13_write_client_certificate_verify( mbedtls_ssl_context *ssl )
2406{
Ronald Crona8b38872022-03-09 07:59:25 +01002407 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
2408
2409 if( ret == 0 )
2410 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
2411
2412 return( ret );
Jerry Yu566c7812022-01-26 15:41:22 +08002413}
Ronald Cron928cbd32022-10-04 16:14:26 +02002414#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002415
2416/*
Jerry Yu687101b2021-09-14 16:03:56 +08002417 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2418 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002419MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian74af2a82021-09-22 07:40:30 +00002420static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002421{
XiaokangQian0fa66432021-11-15 03:33:57 +00002422 int ret;
2423
2424 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2425 if( ret != 0 )
2426 return( ret );
2427
Jerry Yu466dda82022-09-13 11:20:20 +08002428 ret = mbedtls_ssl_tls13_compute_resumption_master_secret( ssl );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002429 if( ret != 0 )
2430 {
2431 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu466dda82022-09-13 11:20:20 +08002432 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret );
Jerry Yue3d67cb2022-05-19 15:33:10 +08002433 return ( ret );
2434 }
2435
XiaokangQian0fa66432021-11-15 03:33:57 +00002436 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2437 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002438}
2439
2440/*
2441 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2442 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002443MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002444static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002445{
Jerry Yu378254d2021-10-30 21:44:47 +08002446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002447 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002448 return( 0 );
2449}
2450
2451/*
2452 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2453 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002454MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +00002455static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002456{
Jerry Yu378254d2021-10-30 21:44:47 +08002457
2458 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2459
2460 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2461 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002462}
2463
Jerry Yuf8a49942022-07-07 11:32:32 +00002464#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2465
Jerry Yua0446a02022-07-13 11:22:55 +08002466MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002467static int ssl_tls13_parse_new_session_ticket_exts( mbedtls_ssl_context *ssl,
2468 const unsigned char *buf,
2469 const unsigned char *end )
Jerry Yuf8a49942022-07-07 11:32:32 +00002470{
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002471 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002472
2473 ((void) ssl);
2474
2475 while( p < end )
2476 {
2477 unsigned int extension_type;
2478 size_t extension_data_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002479
Jerry Yuf8a49942022-07-07 11:32:32 +00002480 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
2481 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
2482 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
2483 p += 4;
2484
2485 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extension_data_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002486
2487 switch( extension_type )
2488 {
2489 case MBEDTLS_TLS_EXT_EARLY_DATA:
2490 MBEDTLS_SSL_DEBUG_MSG( 4, ( "early_data extension received" ) );
2491 break;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002492
Jerry Yuf8a49942022-07-07 11:32:32 +00002493 default:
2494 break;
2495 }
2496 p += extension_data_len;
2497 }
2498
2499 return( 0 );
2500}
2501
2502/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002503 * From RFC8446, page 74
2504 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002505 * struct {
2506 * uint32 ticket_lifetime;
2507 * uint32 ticket_age_add;
2508 * opaque ticket_nonce<0..255>;
2509 * opaque ticket<1..2^16-1>;
2510 * Extension extensions<0..2^16-2>;
2511 * } NewSessionTicket;
2512 *
2513 */
Jerry Yua0446a02022-07-13 11:22:55 +08002514MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002515static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
2516 unsigned char *buf,
Jerry Yucb3b1392022-07-12 06:09:38 +00002517 unsigned char *end,
2518 unsigned char **ticket_nonce,
2519 size_t *ticket_nonce_len )
Jerry Yuf8a49942022-07-07 11:32:32 +00002520{
2521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2522 unsigned char *p = buf;
2523 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002524 size_t ticket_len;
2525 unsigned char *ticket;
2526 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002527
Jerry Yucb3b1392022-07-12 06:09:38 +00002528 *ticket_nonce = NULL;
2529 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002530 /*
2531 * ticket_lifetime 4 bytes
2532 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002533 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002534 */
Jerry Yucb3b1392022-07-12 06:09:38 +00002535 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 9 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002536
2537 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002538 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002539 ( "ticket_lifetime: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002540 ( unsigned int )session->ticket_lifetime ) );
2541
Jerry Yucb3b1392022-07-12 06:09:38 +00002542 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 4 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002543 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002544 ( "ticket_age_add: %u",
Jerry Yuf8a49942022-07-07 11:32:32 +00002545 ( unsigned int )session->ticket_age_add ) );
2546
Jerry Yucb3b1392022-07-12 06:09:38 +00002547 *ticket_nonce_len = p[8];
2548 p += 9;
2549
2550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, *ticket_nonce_len );
2551 *ticket_nonce = p;
2552 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len );
2553 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002554
2555 /* Ticket */
Jerry Yucb3b1392022-07-12 06:09:38 +00002556 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002557 ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2558 p += 2;
2559 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ticket_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002560 MBEDTLS_SSL_DEBUG_BUF( 3, "received ticket", p, ticket_len ) ;
2561
2562 /* Check if we previously received a ticket already. */
2563 if( session->ticket != NULL || session->ticket_len > 0 )
2564 {
2565 mbedtls_free( session->ticket );
2566 session->ticket = NULL;
2567 session->ticket_len = 0;
2568 }
2569
2570 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
2573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2574 }
2575 memcpy( ticket, p, ticket_len );
2576 p += ticket_len;
2577 session->ticket = ticket;
2578 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002579
Jerry Yucb3b1392022-07-12 06:09:38 +00002580 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yuf8a49942022-07-07 11:32:32 +00002581 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2582 p += 2;
2583 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
2584
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002585 MBEDTLS_SSL_DEBUG_BUF( 3, "ticket extension", p, extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002586
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002587 ret = ssl_tls13_parse_new_session_ticket_exts( ssl, p, p + extensions_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002588 if( ret != 0 )
2589 {
2590 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002591 "ssl_tls13_parse_new_session_ticket_exts",
Jerry Yuf8a49942022-07-07 11:32:32 +00002592 ret );
2593 return( ret );
2594 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002595
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002596 /* session has been updated, allow export */
2597 session->exported = 0;
2598
Jerry Yucb3b1392022-07-12 06:09:38 +00002599 return( 0 );
2600}
2601
Jerry Yua0446a02022-07-13 11:22:55 +08002602MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yucb3b1392022-07-12 06:09:38 +00002603static int ssl_tls13_postprocess_new_session_ticket( mbedtls_ssl_context *ssl,
2604 unsigned char *ticket_nonce,
2605 size_t ticket_nonce_len )
2606{
2607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2608 mbedtls_ssl_session *session = ssl->session;
2609 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2610 psa_algorithm_t psa_hash_alg;
2611 int hash_length;
2612
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002613#if defined(MBEDTLS_HAVE_TIME)
2614 /* Store ticket creation time */
Jerry Yu08aed4d2022-07-20 10:36:12 +08002615 session->ticket_received = mbedtls_time( NULL );
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002616#endif
2617
Jerry Yuf8a49942022-07-07 11:32:32 +00002618 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
2619 if( ciphersuite_info == NULL )
2620 {
2621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2622 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2623 }
2624
2625 psa_hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
2626 hash_length = PSA_HASH_LENGTH( psa_hash_alg );
Jerry Yu3afdf362022-07-20 17:34:14 +08002627 if( hash_length == -1 ||
2628 ( size_t )hash_length > sizeof( session->resumption_key ) )
2629 {
Jerry Yuf8a49942022-07-07 11:32:32 +00002630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu3afdf362022-07-20 17:34:14 +08002631 }
2632
Jerry Yuf8a49942022-07-07 11:32:32 +00002633
2634 MBEDTLS_SSL_DEBUG_BUF( 3, "resumption_master_secret",
2635 session->app_secrets.resumption_master_secret,
2636 hash_length );
2637
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002638 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002639 *
2640 * HKDF-Expand-Label( resumption_master_secret,
2641 * "resumption", ticket_nonce, Hash.length )
2642 */
2643 ret = mbedtls_ssl_tls13_hkdf_expand_label(
2644 psa_hash_alg,
2645 session->app_secrets.resumption_master_secret,
2646 hash_length,
2647 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( resumption ),
2648 ticket_nonce,
2649 ticket_nonce_len,
Jerry Yu0a430c82022-07-20 11:02:48 +08002650 session->resumption_key,
Jerry Yuf8a49942022-07-07 11:32:32 +00002651 hash_length );
2652
2653 if( ret != 0 )
2654 {
2655 MBEDTLS_SSL_DEBUG_RET( 2,
2656 "Creating the ticket-resumed PSK failed",
2657 ret );
2658 return( ret );
2659 }
2660
Jerry Yu0a430c82022-07-20 11:02:48 +08002661 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002662
2663 MBEDTLS_SSL_DEBUG_BUF( 3, "Ticket-resumed PSK",
Jerry Yu0a430c82022-07-20 11:02:48 +08002664 session->resumption_key,
2665 session->resumption_key_len );
Jerry Yuf8a49942022-07-07 11:32:32 +00002666
Jerry Yuf8a49942022-07-07 11:32:32 +00002667 return( 0 );
2668}
2669
2670/*
Jerry Yua357cf42022-07-12 05:36:45 +00002671 * Handler for MBEDTLS_SSL_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002672 */
Jerry Yua0446a02022-07-13 11:22:55 +08002673MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf8a49942022-07-07 11:32:32 +00002674static int ssl_tls13_process_new_session_ticket( mbedtls_ssl_context *ssl )
2675{
2676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2677 unsigned char *buf;
2678 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002679 unsigned char *ticket_nonce;
2680 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002681
2682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2683
2684 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
2685 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2686 &buf, &buf_len ) );
2687
Jerry Yucb3b1392022-07-12 06:09:38 +00002688 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_new_session_ticket(
2689 ssl, buf, buf + buf_len,
2690 &ticket_nonce, &ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002691
Jerry Yucb3b1392022-07-12 06:09:38 +00002692 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_new_session_ticket(
2693 ssl, ticket_nonce, ticket_nonce_len ) );
Jerry Yuf8a49942022-07-07 11:32:32 +00002694
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002695 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2696
Jerry Yuf8a49942022-07-07 11:32:32 +00002697cleanup:
2698
2699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2700 return( ret );
2701}
2702#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2703
Jerry Yu92c6b402021-08-27 16:59:09 +08002704int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002705{
Jerry Yu92c6b402021-08-27 16:59:09 +08002706 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002707
Jerry Yu92c6b402021-08-27 16:59:09 +08002708 switch( ssl->state )
2709 {
2710 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002711 * ssl->state is initialized as HELLO_REQUEST. It is the same
2712 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002713 */
2714 case MBEDTLS_SSL_HELLO_REQUEST:
2715 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron3d580bf2022-02-18 17:24:56 +01002716 ret = mbedtls_ssl_write_client_hello( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002717 break;
2718
2719 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002720 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002721 break;
2722
2723 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002724 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002725 break;
2726
Ronald Cron928cbd32022-10-04 16:14:26 +02002727#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002728 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2729 ret = ssl_tls13_process_certificate_request( ssl );
2730 break;
2731
Jerry Yu687101b2021-09-14 16:03:56 +08002732 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002733 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002734 break;
2735
2736 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002737 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002738 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002739#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002740
2741 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002742 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002743 break;
2744
Jerry Yu566c7812022-01-26 15:41:22 +08002745 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
2746 ret = ssl_tls13_write_client_certificate( ssl );
2747 break;
2748
Ronald Cron928cbd32022-10-04 16:14:26 +02002749#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002750 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
2751 ret = ssl_tls13_write_client_certificate_verify( ssl );
2752 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002753#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002754
Jerry Yu687101b2021-09-14 16:03:56 +08002755 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002756 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002757 break;
2758
2759 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002760 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002761 break;
2762
2763 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002764 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002765 break;
2766
Ronald Cron49ad6192021-11-24 16:25:31 +01002767 /*
2768 * Injection of dummy-CCS's for middlebox compatibility
2769 */
2770#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002771 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Cron9f55f632022-02-02 16:02:47 +01002772 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2773 if( ret == 0 )
2774 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
2775 break;
2776
2777 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
2778 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2779 if( ret == 0 )
2780 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Ronald Cron49ad6192021-11-24 16:25:31 +01002781 break;
2782#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2783
Jerry Yuf8a49942022-07-07 11:32:32 +00002784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00002785 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Jerry Yuf8a49942022-07-07 11:32:32 +00002786 ret = ssl_tls13_process_new_session_ticket( ssl );
2787 if( ret != 0 )
2788 break;
2789 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2790 break;
2791#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2792
Jerry Yu92c6b402021-08-27 16:59:09 +08002793 default:
2794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2796 }
2797
2798 return( ret );
2799}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002800
Jerry Yufb4b6472022-01-27 15:03:26 +08002801#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002802
Jerry Yufb4b6472022-01-27 15:03:26 +08002803