blob: 65a56b3bcb221982539a70a83251985b622480a5 [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 Yucbd082f2022-08-04 16:55:10 +080035#include "ssl_debug_helpers.h"
Jerry Yubdc71882021-09-14 19:30:36 +080036
Jerry Yubc20bdd2021-08-24 15:59:48 +080037/* Write extensions */
38
Jerry Yu92c6b402021-08-27 16:59:09 +080039/*
40 * ssl_tls13_write_supported_versions_ext():
41 *
42 * struct {
43 * ProtocolVersion versions<2..254>;
44 * } SupportedVersions;
45 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020046MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010047static int ssl_tls13_write_supported_versions_ext(mbedtls_ssl_context *ssl,
48 unsigned char *buf,
49 unsigned char *end,
50 size_t *out_len)
Jerry Yu92c6b402021-08-27 16:59:09 +080051{
52 unsigned char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +010053 unsigned char versions_len = (ssl->handshake->min_tls_version <=
54 MBEDTLS_SSL_VERSION_TLS1_2) ? 4 : 2;
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Xiaofei Baid25fab62021-12-02 06:36:27 +000056 *out_len = 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported versions extension"));
Jerry Yu92c6b402021-08-27 16:59:09 +080059
Jerry Yu388bd0d2021-09-15 18:41:02 +080060 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080061 * - extension_type (2 bytes)
62 * - extension_data_length (2 bytes)
63 * - versions_length (1 byte )
Ronald Crona77fc272022-03-30 17:20:47 +020064 * - versions (2 or 4 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + versions_len);
Ronald Crondbe87f02022-02-10 14:35:27 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
69 MBEDTLS_PUT_UINT16_BE(versions_len + 1, p, 2);
Jerry Yueecfbf02021-08-30 18:32:07 +080070 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Length of versions */
Ronald Crondbe87f02022-02-10 14:35:27 +010073 *p++ = versions_len;
Jerry Yu92c6b402021-08-27 16:59:09 +080074
Jerry Yu0c63af62021-09-02 12:59:12 +080075 /* Write values of supported versions.
Jerry Yu0c63af62021-09-02 12:59:12 +080076 * They are defined by the configuration.
Ronald Crondbe87f02022-02-10 14:35:27 +010077 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
Jerry Yu92c6b402021-08-27 16:59:09 +080078 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_ssl_write_version(p, MBEDTLS_SSL_TRANSPORT_STREAM,
80 MBEDTLS_SSL_VERSION_TLS1_3);
81 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:4]"));
Jerry Yu92c6b402021-08-27 16:59:09 +080082
Jerry Yu92c6b402021-08-27 16:59:09 +080083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
85 mbedtls_ssl_write_version(p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
86 MBEDTLS_SSL_VERSION_TLS1_2);
87 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:3]"));
Ronald Crondbe87f02022-02-10 14:35:27 +010088 }
89
90 *out_len = 5 + versions_len;
Jerry Yu4b8f2f72022-10-31 13:31:22 +080091
Jerry Yuc4bf5d62022-10-29 09:08:47 +080092 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +010093 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yu4b8f2f72022-10-31 13:31:22 +080094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return 0;
Jerry Yu92c6b402021-08-27 16:59:09 +080096}
Jerry Yubc20bdd2021-08-24 15:59:48 +080097
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020098MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
100 const unsigned char *buf,
101 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +0800102{
Jerry Yue1b9c292021-09-10 10:08:31 +0800103 ((void) ssl);
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
106 if (mbedtls_ssl_read_version(buf, ssl->conf->transport) !=
107 MBEDTLS_SSL_VERSION_TLS1_3) {
108 MBEDTLS_SSL_DEBUG_MSG(1, ("unexpected version"));
Jerry Yu4a173382021-10-11 21:45:31 +0800109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
111 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
112 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +0800113 }
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (&buf[2] != end) {
116 MBEDTLS_SSL_DEBUG_MSG(1, ("supported_versions ext data length incorrect"));
117 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
118 MBEDTLS_ERR_SSL_DECODE_ERROR);
119 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Ronald Cron98473382022-03-30 20:04:10 +0200120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return 0;
Jerry Yue1b9c292021-09-10 10:08:31 +0800123}
124
lhuang0486cacac2022-01-21 07:34:27 -0800125#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200126MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100127static int ssl_tls13_parse_alpn_ext(mbedtls_ssl_context *ssl,
128 const unsigned char *buf, size_t len)
lhuang0486cacac2022-01-21 07:34:27 -0800129{
lhuang0486cacac2022-01-21 07:34:27 -0800130 const unsigned char *p = buf;
131 const unsigned char *end = buf + len;
Ronald Cron81a334f2022-05-31 16:04:11 +0200132 size_t protocol_name_list_len, protocol_name_len;
133 const unsigned char *protocol_name_list_end;
lhuang0486cacac2022-01-21 07:34:27 -0800134
135 /* If we didn't send it, the server shouldn't send it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (ssl->conf->alpn_list == NULL) {
137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
138 }
lhuang0486cacac2022-01-21 07:34:27 -0800139
140 /*
141 * opaque ProtocolName<1..2^8-1>;
142 *
143 * struct {
144 * ProtocolName protocol_name_list<2..2^16-1>
145 * } ProtocolNameList;
146 *
147 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
148 */
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
151 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
lhuang0486cacac2022-01-21 07:34:27 -0800152 p += 2;
lhuang0486cacac2022-01-21 07:34:27 -0800153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
Ronald Cron81a334f2022-05-31 16:04:11 +0200155 protocol_name_list_end = p + protocol_name_list_len;
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, 1);
Ronald Cron81a334f2022-05-31 16:04:11 +0200158 protocol_name_len = *p++;
lhuang0486cacac2022-01-21 07:34:27 -0800159
160 /* Check that the server chosen protocol was in our list and save it */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
162 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
163 if (protocol_name_len == strlen(*alpn) &&
164 memcmp(p, *alpn, protocol_name_len) == 0) {
lhuang0486cacac2022-01-21 07:34:27 -0800165 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return 0;
lhuang0486cacac2022-01-21 07:34:27 -0800167 }
168 }
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
lhuang0486cacac2022-01-21 07:34:27 -0800171}
172#endif /* MBEDTLS_SSL_ALPN */
173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (group_id == 0) {
180 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
181 }
XiaokangQian647719a2021-12-07 09:16:29 +0000182
XiaokangQian355e09a2022-01-20 11:14:50 +0000183#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id)) {
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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 status = psa_destroy_key(ssl->handshake->ecdh_psa_privkey);
190 if (status != PSA_SUCCESS) {
191 ret = psa_ssl_status_to_mbedtls(status);
192 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
193 return ret;
Ronald Cron5b98ac92022-03-15 10:19:18 +0100194 }
195
196 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return 0;
198 } else
XiaokangQian355e09a2022-01-20 11:14:50 +0000199#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (0 /* other KEMs? */) {
XiaokangQian647719a2021-12-07 09:16:29 +0000201 /* Do something */
202 }
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQian647719a2021-12-07 09:16:29 +0000205}
206
207/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208 * Functions for writing key_share extension.
209 */
Ronald Cron766c0cd2022-10-18 12:17:11 +0200210#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200211MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100212static int ssl_tls13_get_default_group_id(mbedtls_ssl_context *ssl,
213 uint16_t *group_id)
Jerry Yu56fc07f2021-09-01 17:48:49 +0800214{
215 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
216
Jerry Yu56fc07f2021-09-01 17:48:49 +0800217
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Jerry Yu388bd0d2021-09-15 18:41:02 +0800220 /* Pick first available ECDHE group compatible with TLS 1.3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (group_list == NULL) {
222 return MBEDTLS_ERR_SSL_BAD_CONFIG;
223 }
Jerry Yu388bd0d2021-09-15 18:41:02 +0800224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 for (; *group_list != 0; group_list++) {
226 if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(*group_list,
227 NULL, NULL) == PSA_SUCCESS) &&
228 mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
Brett Warren14efd332021-10-06 09:32:11 +0100229 *group_id = *group_list;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800231 }
232 }
233#else
234 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800235 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800236#endif /* MBEDTLS_ECDH_C */
237
238 /*
239 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800240 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 */
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return ret;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800244}
245
246/*
247 * ssl_tls13_write_key_share_ext
248 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800249 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250 *
251 * struct {
252 * NamedGroup group;
253 * opaque key_exchange<1..2^16-1>;
254 * } KeyShareEntry;
255 * struct {
256 * KeyShareEntry client_shares<0..2^16-1>;
257 * } KeyShareClientHello;
258 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200259MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
261 unsigned char *buf,
262 unsigned char *end,
263 size_t *out_len)
Jerry Yu56fc07f2021-09-01 17:48:49 +0800264{
265 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000266 unsigned char *client_shares; /* Start of client_shares */
267 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800268 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800269 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
270
Xiaofei Baid25fab62021-12-02 06:36:27 +0000271 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800272
Jerry Yub60e3cf2021-09-08 16:41:02 +0800273 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800274 * - extension_type (2 bytes)
275 * - extension_data_length (2 bytes)
276 * - client_shares_length (2 bytes)
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800279 p += 6;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello: adding key share extension"));
Jerry Yu56fc07f2021-09-01 17:48:49 +0800282
283 /* HRR could already have requested something else. */
284 group_id = ssl->handshake->offered_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (!mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) &&
286 !mbedtls_ssl_tls13_named_group_is_dhe(group_id)) {
287 MBEDTLS_SSL_PROC_CHK(ssl_tls13_get_default_group_id(ssl,
288 &group_id));
Jerry Yu56fc07f2021-09-01 17:48:49 +0800289 }
290
291 /*
292 * Dispatch to type-specific key generation function.
293 *
294 * So far, we're only supporting ECDHE. With the introduction
295 * of PQC KEMs, we'll want to have multiple branches, one per
296 * type of KEM, and dispatch to the corresponding crypto. And
297 * only one key share entry is allowed.
298 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000299 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id)) {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800302 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000303 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304 /* Length of key_exchange */
Przemyslaw Stekiel4f419e52022-02-10 15:56:26 +0100305 size_t key_exchange_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306
307 /* Check there is space for header of KeyShareEntry
308 * - group (2 bytes)
309 * - key_exchange_length (2 bytes)
310 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312 p += 4;
Jerry Yu89e103c2022-03-30 22:43:29 +0800313 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 ssl, group_id, p, end, &key_exchange_len);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800315 p += key_exchange_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (ret != 0) {
317 return ret;
318 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800319
320 /* Write group */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_PUT_UINT16_BE(group_id, group, 0);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322 /* Write key_exchange_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(key_exchange_len, group, 2);
324 } else
Jerry Yu56fc07f2021-09-01 17:48:49 +0800325#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (0 /* other KEMs? */) {
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 /* Do something */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 } else {
329 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800330 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800331
Jerry Yub60e3cf2021-09-08 16:41:02 +0800332 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000333 client_shares_len = p - client_shares;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (client_shares_len == 0) {
335 MBEDTLS_SSL_DEBUG_MSG(1, ("No key share defined."));
336 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu7c522d42021-09-08 17:55:09 +0800337 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 MBEDTLS_PUT_UINT16_BE(client_shares_len + 2, buf, 2);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 /* Write client_shares_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_PUT_UINT16_BE(client_shares_len, buf, 4);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800344
345 /* Update offered_group_id field */
346 ssl->handshake->offered_group_id = group_id;
347
348 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000349 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, key_share extension", buf, *out_len);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354
355cleanup:
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return ret;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800358}
Ronald Cron766c0cd2022-10-18 12:17:11 +0200359#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yue1b9c292021-09-10 10:08:31 +0800360
XiaokangQiand59be772022-01-24 10:12:51 +0000361/*
362 * ssl_tls13_parse_hrr_key_share_ext()
363 * Parse key_share extension in Hello Retry Request
364 *
365 * struct {
366 * NamedGroup selected_group;
367 * } KeyShareHelloRetryRequest;
368 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200369MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100370static int ssl_tls13_parse_hrr_key_share_ext(mbedtls_ssl_context *ssl,
371 const unsigned char *buf,
372 const unsigned char *end)
XiaokangQianb851da82022-01-14 04:03:11 +0000373{
Andrzej Kurekc19fb082022-10-03 10:52:24 -0400374#if defined(MBEDTLS_ECDH_C)
XiaokangQianb851da82022-01-14 04:03:11 +0000375 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000376 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000377 int found = 0;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
380 if (group_list == NULL) {
381 return MBEDTLS_ERR_SSL_BAD_CONFIG;
382 }
XiaokangQianb851da82022-01-14 04:03:11 +0000383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 MBEDTLS_SSL_DEBUG_BUF(3, "key_share extension", p, end - buf);
XiaokangQianb851da82022-01-14 04:03:11 +0000385
386 /* Read selected_group */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
388 selected_group = MBEDTLS_GET_UINT16_BE(p, 0);
389 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_group ( %d )", selected_group));
XiaokangQianb851da82022-01-14 04:03:11 +0000390
391 /* Upon receipt of this extension in a HelloRetryRequest, the client
392 * MUST first verify that the selected_group field corresponds to a
393 * group which was provided in the "supported_groups" extension in the
394 * original ClientHello.
395 * The supported_group was based on the info in ssl->conf->group_list.
396 *
397 * If the server provided a key share that was not sent in the ClientHello
398 * then the client MUST abort the handshake with an "illegal_parameter" alert.
399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 for (; *group_list != 0; group_list++) {
401 if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(*group_list,
402 NULL, NULL) == PSA_ERROR_NOT_SUPPORTED) ||
403 *group_list != selected_group) {
XiaokangQianb851da82022-01-14 04:03:11 +0000404 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 }
XiaokangQianb851da82022-01-14 04:03:11 +0000406
407 /* We found a match */
408 found = 1;
409 break;
410 }
411
412 /* Client MUST verify that the selected_group field does not
413 * correspond to a group which was provided in the "key_share"
414 * extension in the original ClientHello. If the server sent an
415 * HRR message with a key share already provided in the
416 * ClientHello then the client MUST abort the handshake with
417 * an "illegal_parameter" alert.
418 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (found == 0 || selected_group == ssl->handshake->offered_group_id) {
420 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid key share in HRR"));
XiaokangQianb851da82022-01-14 04:03:11 +0000421 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
423 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
424 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQianb851da82022-01-14 04:03:11 +0000425 }
426
427 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000428 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return 0;
Andrzej Kurekc19fb082022-10-03 10:52:24 -0400431#else
432 (void) ssl;
433 (void) buf;
434 (void) end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Andrzej Kurekc19fb082022-10-03 10:52:24 -0400436#endif
XiaokangQianb851da82022-01-14 04:03:11 +0000437}
438
Jerry Yue1b9c292021-09-10 10:08:31 +0800439/*
Jerry Yub85277e2021-10-13 13:36:05 +0800440 * ssl_tls13_parse_key_share_ext()
441 * Parse key_share extension in Server Hello
442 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800443 * struct {
444 * KeyShareEntry server_share;
445 * } KeyShareServerHello;
446 * struct {
447 * NamedGroup group;
448 * opaque key_exchange<1..2^16-1>;
449 * } KeyShareEntry;
450 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200451MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100452static int ssl_tls13_parse_key_share_ext(mbedtls_ssl_context *ssl,
453 const unsigned char *buf,
454 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +0800455{
Jerry Yub85277e2021-10-13 13:36:05 +0800456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800457 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800458 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800459
Jerry Yu4a173382021-10-11 21:45:31 +0800460 /* ...
461 * NamedGroup group; (2 bytes)
462 * ...
463 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
465 group = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +0800466 p += 2;
467
Jerry Yu4a173382021-10-11 21:45:31 +0800468 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800469 offered_group = ssl->handshake->offered_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (offered_group != group) {
471 MBEDTLS_SSL_DEBUG_MSG(1,
472 ("Invalid server key share, our group %u, their group %u",
473 (unsigned) offered_group, (unsigned) group));
474 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
475 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
476 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +0800477 }
478
479#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
481 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(group, NULL, NULL)
482 == PSA_ERROR_NOT_SUPPORTED) {
483 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid TLS curve group id"));
484 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100485 }
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
488 mbedtls_ssl_get_curve_name_from_tls_id(group)));
Przemyslaw Stekiel9e23ddb2022-02-10 10:32:02 +0100489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(ssl, p, end - p);
491 if (ret != 0) {
492 return ret;
493 }
494 } else
Jerry Yue1b9c292021-09-10 10:08:31 +0800495#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (0 /* other KEMs? */) {
Jerry Yue1b9c292021-09-10 10:08:31 +0800497 /* Do something */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else {
499 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yue1b9c292021-09-10 10:08:31 +0800500 }
Jerry Yue1b9c292021-09-10 10:08:31 +0800501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +0800503}
504
XiaokangQiand59be772022-01-24 10:12:51 +0000505/*
506 * ssl_tls13_parse_cookie_ext()
507 * Parse cookie extension in Hello Retry Request
508 *
509 * struct {
510 * opaque cookie<1..2^16-1>;
511 * } Cookie;
512 *
513 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
514 * extension to the client (this is an exception to the usual rule that
515 * the only extensions that may be sent are those that appear in the
516 * ClientHello). When sending the new ClientHello, the client MUST copy
517 * the contents of the extension received in the HelloRetryRequest into
518 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
519 * cookies in their initial ClientHello in subsequent connections.
520 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200521MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100522static int ssl_tls13_parse_cookie_ext(mbedtls_ssl_context *ssl,
523 const unsigned char *buf,
524 const unsigned char *end)
XiaokangQian43550bd2022-01-21 04:32:58 +0000525{
XiaokangQian25c9c902022-02-08 10:49:53 +0000526 uint16_t cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000527 const unsigned char *p = buf;
528 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
529
530 /* Retrieve length field of cookie */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
532 cookie_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian43550bd2022-01-21 04:32:58 +0000533 p += 2;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cookie_len);
536 MBEDTLS_SSL_DEBUG_BUF(3, "cookie extension", p, cookie_len);
XiaokangQian43550bd2022-01-21 04:32:58 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 mbedtls_free(handshake->cookie);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800539 handshake->cookie_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 handshake->cookie = mbedtls_calloc(1, cookie_len);
541 if (handshake->cookie == NULL) {
542 MBEDTLS_SSL_DEBUG_MSG(1,
543 ("alloc failed ( %ud bytes )",
544 cookie_len));
545 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
XiaokangQian43550bd2022-01-21 04:32:58 +0000546 }
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 memcpy(handshake->cookie, p, cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800549 handshake->cookie_len = cookie_len;
XiaokangQian43550bd2022-01-21 04:32:58 +0000550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000552}
XiaokangQian43550bd2022-01-21 04:32:58 +0000553
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200554MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100555static int ssl_tls13_write_cookie_ext(mbedtls_ssl_context *ssl,
556 unsigned char *buf,
557 unsigned char *end,
558 size_t *out_len)
XiaokangQian0b64eed2022-01-27 10:36:51 +0000559{
560 unsigned char *p = buf;
XiaokangQian9deb90f2022-02-08 10:31:07 +0000561 *out_len = 0;
XiaokangQianc02768a2022-02-10 07:31:25 +0000562 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (handshake->cookie == NULL) {
565 MBEDTLS_SSL_DEBUG_MSG(3, ("no cookie to send; skip extension"));
566 return 0;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000567 }
568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
570 handshake->cookie,
571 handshake->cookie_len);
XiaokangQian0b64eed2022-01-27 10:36:51 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_PTR(p, end, handshake->cookie_len + 6);
XiaokangQian0b64eed2022-01-27 10:36:51 +0000574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding cookie extension"));
XiaokangQian0b64eed2022-01-27 10:36:51 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_COOKIE, p, 0);
578 MBEDTLS_PUT_UINT16_BE(handshake->cookie_len + 2, p, 2);
579 MBEDTLS_PUT_UINT16_BE(handshake->cookie_len, p, 4);
XiaokangQian233397e2022-02-07 08:32:16 +0000580 p += 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000581
582 /* Cookie */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 memcpy(p, handshake->cookie, handshake->cookie_len);
XiaokangQian0b64eed2022-01-27 10:36:51 +0000584
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800585 *out_len = handshake->cookie_len + 6;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_COOKIE);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return 0;
XiaokangQian0b64eed2022-01-27 10:36:51 +0000590}
591
Ronald Cron41a443a2022-10-04 16:38:25 +0200592#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000593/*
594 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
595 *
596 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
597 *
598 * struct {
599 * PskKeyExchangeMode ke_modes<1..255>;
600 * } PskKeyExchangeModes;
601 */
XiaokangQian86981952022-07-19 09:51:50 +0000602MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100603static int ssl_tls13_write_psk_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
604 unsigned char *buf,
605 unsigned char *end,
606 size_t *out_len)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000607{
XiaokangQianeb69aee2022-07-05 08:21:43 +0000608 unsigned char *p = buf;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000609 int ke_modes_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 ((void) ke_modes_len);
XiaokangQianeb69aee2022-07-05 08:21:43 +0000612 *out_len = 0;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000613
XiaokangQianeb69aee2022-07-05 08:21:43 +0000614 /* Skip writing extension if no PSK key exchange mode
XiaokangQian7c12d312022-07-20 07:25:43 +0000615 * is enabled in the config.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (!mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
618 MBEDTLS_SSL_DEBUG_MSG(3, ("skip psk_key_exchange_modes extension"));
619 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000620 }
621
622 /* Require 7 bytes of data, otherwise fail,
623 * even if extension might be shorter.
624 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7);
XiaokangQianeb69aee2022-07-05 08:21:43 +0000626 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 3, ("client hello, adding psk_key_exchange_modes extension"));
XiaokangQianeb69aee2022-07-05 08:21:43 +0000628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0);
XiaokangQianeb69aee2022-07-05 08:21:43 +0000630
XiaokangQian008d2bf2022-07-14 07:54:01 +0000631 /* Skip extension length (2 bytes) and
632 * ke_modes length (1 byte) for now.
XiaokangQianeb69aee2022-07-05 08:21:43 +0000633 */
634 p += 5;
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl)) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000637 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
XiaokangQian008d2bf2022-07-14 07:54:01 +0000638 ke_modes_len++;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 MBEDTLS_SSL_DEBUG_MSG(4, ("Adding PSK-ECDHE key exchange mode"));
XiaokangQianeb69aee2022-07-05 08:21:43 +0000641 }
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (mbedtls_ssl_conf_tls13_psk_enabled(ssl)) {
Ronald Crona709a0f2022-09-27 16:46:11 +0200644 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
645 ke_modes_len++;
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 MBEDTLS_SSL_DEBUG_MSG(4, ("Adding pure PSK key exchange mode"));
Ronald Crona709a0f2022-09-27 16:46:11 +0200648 }
649
XiaokangQian008d2bf2022-07-14 07:54:01 +0000650 /* Now write the extension and ke_modes length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 MBEDTLS_PUT_UINT16_BE(ke_modes_len + 1, buf, 2);
XiaokangQian008d2bf2022-07-14 07:54:01 +0000652 buf[4] = ke_modes_len;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000653
654 *out_len = p - buf;
Jerry Yu0c354a22022-08-29 15:25:36 +0800655
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800656 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 ssl, MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000660}
Jerry Yudb8c5fa2022-08-03 12:10:13 +0800661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg(int ciphersuite)
Jerry Yuf7c12592022-09-28 22:09:38 +0800663{
Jerry Yu21f90952022-10-08 10:30:53 +0800664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
Jerry Yuf7c12592022-09-28 22:09:38 +0800666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (ciphersuite_info != NULL) {
668 return mbedtls_psa_translate_md(ciphersuite_info->mac);
669 }
Jerry Yua99cbfa2022-10-08 11:17:14 +0800670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return PSA_ALG_NONE;
Jerry Yuf7c12592022-09-28 22:09:38 +0800672}
673
Jerry Yuf75364b2022-09-30 10:30:31 +0800674#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100675static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
Xiaokang Qianb781a232022-11-01 07:39:46 +0000676{
677 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return ssl->handshake->resume &&
Pengyu Lv93566782022-12-07 12:10:05 +0800679 session != NULL && session->ticket != NULL &&
Pengyu Lv9b84ea72023-01-16 14:08:23 +0800680 mbedtls_ssl_conf_tls13_check_kex_modes(
681 ssl, mbedtls_ssl_session_get_ticket_flags(
682 session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL));
Xiaokang Qianb781a232022-11-01 07:39:46 +0000683}
684
Xiaokang Qian01323a42022-11-03 02:27:35 +0000685#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
Xiaokang Qian01323a42022-11-03 02:27:35 +0000687{
688 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return ssl->handshake->resume &&
690 session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
691 (session->ticket_flags &
692 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA) &&
693 mbedtls_ssl_tls13_cipher_suite_is_offered(
694 ssl, session->ciphersuite);
Xiaokang Qian01323a42022-11-03 02:27:35 +0000695}
696#endif
697
Xiaokang Qianb781a232022-11-01 07:39:46 +0000698MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
700 psa_algorithm_t *hash_alg,
701 const unsigned char **identity,
702 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800703{
704 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu8b41e892022-09-30 10:00:20 +0800705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (!ssl_tls13_has_configured_ticket(ssl)) {
707 return -1;
708 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yuf7c12592022-09-28 22:09:38 +0800711 *identity = session->ticket;
712 *identity_len = session->ticket_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800714}
715
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800716MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100717static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
718 psa_algorithm_t *hash_alg,
719 const unsigned char **psk,
720 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800721{
722
723 mbedtls_ssl_session *session = ssl->session_negotiate;
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (!ssl_tls13_has_configured_ticket(ssl)) {
726 return -1;
727 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800730 *psk = session->resumption_key;
731 *psk_len = session->resumption_key_len;
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800734}
Jerry Yuf7c12592022-09-28 22:09:38 +0800735#endif /* MBEDTLS_SSL_SESSION_TICKETS */
736
737MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100738static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
739 psa_algorithm_t *hash_alg,
740 const unsigned char **identity,
741 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800742{
Jerry Yu8b41e892022-09-30 10:00:20 +0800743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
745 return -1;
746 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800747
Jerry Yua99cbfa2022-10-08 11:17:14 +0800748 *hash_alg = PSA_ALG_SHA_256;
Jerry Yuf7c12592022-09-28 22:09:38 +0800749 *identity = ssl->conf->psk_identity;
750 *identity_len = ssl->conf->psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800752}
753
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800754MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100755static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
756 psa_algorithm_t *hash_alg,
757 const unsigned char **psk,
758 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800759{
Jerry Yu8b41e892022-09-30 10:00:20 +0800760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
762 return -1;
763 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800764
Jerry Yua99cbfa2022-10-08 11:17:14 +0800765 *hash_alg = PSA_ALG_SHA_256;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800766 *psk = ssl->conf->psk;
767 *psk_len = ssl->conf->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800769}
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
Jerry Yuf75364b2022-09-30 10:30:31 +0800772{
773 int configured_psk_count = 0;
774#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (ssl_tls13_has_configured_ticket(ssl)) {
776 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800777 configured_psk_count++;
778 }
779#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
781 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800782 configured_psk_count++;
783 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return configured_psk_count;
Jerry Yuf75364b2022-09-30 10:30:31 +0800785}
786
Jerry Yuf7c12592022-09-28 22:09:38 +0800787MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100788static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
789 unsigned char *buf,
790 unsigned char *end,
791 const unsigned char *identity,
792 size_t identity_len,
793 uint32_t obfuscated_ticket_age,
794 size_t *out_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800795{
Jerry Yuf75364b2022-09-30 10:30:31 +0800796 ((void) ssl);
Jerry Yuf7c12592022-09-28 22:09:38 +0800797 *out_len = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800798
799 /*
800 * - identity_len (2 bytes)
801 * - identity (psk_identity_len bytes)
802 * - obfuscated_ticket_age (4 bytes)
803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
807 memcpy(buf + 2, identity, identity_len);
808 MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800811
812 *out_len = 6 + identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800815}
816
Jerry Yu8b41e892022-09-30 10:00:20 +0800817MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100818static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
819 unsigned char *buf,
820 unsigned char *end,
821 int psk_type,
822 psa_algorithm_t hash_alg,
823 const unsigned char *psk,
824 size_t psk_len,
825 size_t *out_len)
Jerry Yu8b41e892022-09-30 10:00:20 +0800826{
827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8b41e892022-09-30 10:00:20 +0800828 unsigned char binder_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8b41e892022-09-30 10:00:20 +0800830 size_t transcript_len = 0;
831
832 *out_len = 0;
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 binder_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu8b41e892022-09-30 10:00:20 +0800835
836 /*
837 * - binder_len (1 bytes)
838 * - binder (binder_len bytes)
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800841
Jerry Yua99cbfa2022-10-08 11:17:14 +0800842 buf[0] = binder_len;
Jerry Yu8b41e892022-09-30 10:00:20 +0800843
844 /* Get current state of handshake transcript. */
845 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 ssl, mbedtls_hash_info_md_from_psa(hash_alg),
847 transcript, sizeof(transcript), &transcript_len);
848 if (ret != 0) {
849 return ret;
Jerry Yu8b41e892022-09-30 10:00:20 +0800850 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100851
852 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
853 psk, psk_len, psk_type,
854 transcript, buf + 1);
855 if (ret != 0) {
856 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
857 return ret;
858 }
859 MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800860
861 *out_len = 1 + binder_len;
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return 0;
Jerry Yu8b41e892022-09-30 10:00:20 +0800864}
865
866/*
867 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
868 *
869 * struct {
870 * opaque identity<1..2^16-1>;
871 * uint32 obfuscated_ticket_age;
872 * } PskIdentity;
873 *
874 * opaque PskBinderEntry<32..255>;
875 *
876 * struct {
877 * PskIdentity identities<7..2^16-1>;
878 * PskBinderEntry binders<33..2^16-1>;
879 * } OfferedPsks;
880 *
881 * struct {
882 * select (Handshake.msg_type) {
883 * case client_hello: OfferedPsks;
884 * ...
885 * };
886 * } PreSharedKeyExtension;
887 *
888 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000889int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
891 size_t *out_len, size_t *binders_len)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000892{
Jerry Yuf7c12592022-09-28 22:09:38 +0800893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf75364b2022-09-30 10:30:31 +0800894 int configured_psk_count = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800895 unsigned char *p = buf;
Xiaokang Qian854db282022-12-19 07:31:27 +0000896 psa_algorithm_t hash_alg = PSA_ALG_NONE;
Jerry Yuf75364b2022-09-30 10:30:31 +0800897 const unsigned char *identity;
898 size_t identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800899 size_t l_binders_len = 0;
Jerry Yuf75364b2022-09-30 10:30:31 +0800900 size_t output_len;
Xiaokang Qian7179f812023-02-03 03:38:44 +0000901
Jerry Yuf7c12592022-09-28 22:09:38 +0800902 *out_len = 0;
903 *binders_len = 0;
904
905 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
907 if (configured_psk_count == 0) {
908 MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
909 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
913 configured_psk_count));
Jerry Yuf75364b2022-09-30 10:30:31 +0800914
Jerry Yuf7c12592022-09-28 22:09:38 +0800915 /* Check if we have space to write the extension, binders included.
916 * - extension_type (2 bytes)
917 * - extension_data_len (2 bytes)
918 * - identities_len (2 bytes)
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yuf7c12592022-09-28 22:09:38 +0800921 p += 6;
922
Jerry Yuf75364b2022-09-30 10:30:31 +0800923#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (ssl_tls13_ticket_get_identity(
925 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800926#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 mbedtls_time_t now = mbedtls_time(NULL);
Jerry Yuf75364b2022-09-30 10:30:31 +0800928 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu6916e702022-10-10 21:33:51 +0800929 uint32_t obfuscated_ticket_age =
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 (uint32_t) (now - session->ticket_received);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800931
Jerry Yu3e60cad2023-01-10 14:58:08 +0800932 /*
933 * The ticket timestamp is in seconds but the ticket age is in
934 * milliseconds. If the ticket was received at the end of a second and
935 * re-used here just at the beginning of the next second, the computed
936 * age `now - session->ticket_received` is equal to 1s thus 1000 ms
937 * while the actual age could be just a few milliseconds or tens of
938 * milliseconds. If the server has more accurate ticket timestamps
939 * (typically timestamps in milliseconds), as part of the processing of
940 * the ClientHello, it may compute a ticket lifetime smaller than the
941 * one computed here and potentially reject the ticket. To avoid that,
942 * remove one second to the ticket age if possible.
Jerry Yubdb936b2023-01-07 16:07:46 +0800943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (obfuscated_ticket_age > 0) {
Jerry Yubdb936b2023-01-07 16:07:46 +0800945 obfuscated_ticket_age -= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 }
Jerry Yubdb936b2023-01-07 16:07:46 +0800947
Jerry Yuf75364b2022-09-30 10:30:31 +0800948 obfuscated_ticket_age *= 1000;
949 obfuscated_ticket_age += session->ticket_age_add;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = ssl_tls13_write_identity(ssl, p, end,
952 identity, identity_len,
953 obfuscated_ticket_age,
954 &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800955#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
957 0, &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800958#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ret != 0) {
960 return ret;
961 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800962
Jerry Yuf75364b2022-09-30 10:30:31 +0800963 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf75364b2022-09-30 10:30:31 +0800965 }
966#endif /* MBEDTLS_SSL_SESSION_TICKETS */
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ssl_tls13_psk_get_identity(
969 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
972 &output_len);
973 if (ret != 0) {
974 return ret;
975 }
Jerry Yuf75364b2022-09-30 10:30:31 +0800976
Jerry Yuf7c12592022-09-28 22:09:38 +0800977 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf7c12592022-09-28 22:09:38 +0800979 }
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 MBEDTLS_SSL_DEBUG_MSG(3,
982 ("client hello, adding pre_shared_key extension, "
983 "omitting PSK binder list"));
Jerry Yua99cbfa2022-10-08 11:17:14 +0800984
985 /* Take into account the two bytes for the length of the binders. */
986 l_binders_len += 2;
Jerry Yu6916e702022-10-10 21:33:51 +0800987 /* Check if there is enough space for binders */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800989
Jerry Yuf7c12592022-09-28 22:09:38 +0800990 /*
991 * - extension_type (2 bytes)
992 * - extension_data_len (2 bytes)
993 * - identities_len (2 bytes)
994 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
996 MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
997 MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
Jerry Yuf7c12592022-09-28 22:09:38 +0800998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 *out_len = (p - buf) + l_binders_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001000 *binders_len = l_binders_len;
Jerry Yuf7c12592022-09-28 22:09:38 +08001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
Jerry Yuf7c12592022-09-28 22:09:38 +08001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001005}
1006
XiaokangQian86981952022-07-19 09:51:50 +00001007int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001009{
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1011 unsigned char *p = buf;
Jerry Yu6183cc72022-09-30 11:08:57 +08001012 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1013 const unsigned char *psk;
1014 size_t psk_len;
1015 size_t output_len;
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001016
1017 /* Check if we have space to write binders_len.
1018 * - binders_len (2 bytes)
1019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001021 p += 2;
1022
Jerry Yu6183cc72022-09-30 11:08:57 +08001023#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 ret = ssl_tls13_write_binder(ssl, p, end,
1027 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
1028 hash_alg, psk, psk_len,
1029 &output_len);
1030 if (ret != 0) {
1031 return ret;
1032 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001033 p += output_len;
1034 }
Jerry Yu6183cc72022-09-30 11:08:57 +08001035#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 ret = ssl_tls13_write_binder(ssl, p, end,
1040 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
1041 hash_alg, psk, psk_len,
1042 &output_len);
1043 if (ret != 0) {
1044 return ret;
1045 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001046 p += output_len;
1047 }
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001050
1051 /*
1052 * - binders_len (2 bytes)
1053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
XiaokangQianeb69aee2022-07-05 08:21:43 +00001057
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001058 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yu0c354a22022-08-29 15:25:36 +08001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001062}
Jerry Yu0c6105b2022-08-12 17:26:40 +08001063
1064/*
1065 * struct {
1066 * opaque identity<1..2^16-1>;
1067 * uint32 obfuscated_ticket_age;
1068 * } PskIdentity;
1069 *
1070 * opaque PskBinderEntry<32..255>;
1071 *
1072 * struct {
1073 *
1074 * select (Handshake.msg_type) {
1075 * ...
1076 * case server_hello: uint16 selected_identity;
1077 * };
1078 *
1079 * } PreSharedKeyExtension;
1080 *
1081 */
1082MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001083static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
1084 const unsigned char *buf,
1085 const unsigned char *end)
Jerry Yu0c6105b2022-08-12 17:26:40 +08001086{
Jerry Yua99cbfa2022-10-08 11:17:14 +08001087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub300e3c2022-09-28 22:12:07 +08001088 int selected_identity;
Jerry Yub300e3c2022-09-28 22:12:07 +08001089 const unsigned char *psk;
1090 size_t psk_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001091 psa_algorithm_t hash_alg;
Jerry Yub300e3c2022-09-28 22:12:07 +08001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
1094 selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
Xiaokang Qian2a674932023-01-04 03:15:09 +00001095 ssl->handshake->selected_identity = (uint16_t) selected_identity;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
Jerry Yua99cbfa2022-10-08 11:17:14 +08001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
1100 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
Jerry Yu4a698342022-09-30 12:22:01 +08001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1103 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1104 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yub300e3c2022-09-28 22:12:07 +08001105 }
Jerry Yua99cbfa2022-10-08 11:17:14 +08001106
Jerry Yu4a698342022-09-30 12:22:01 +08001107#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
1109 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
Xiaokang Qianac4c6252023-02-06 10:15:00 +00001110 if (mbedtls_psa_translate_md(ssl->handshake->ciphersuite_info->mac)
1111 != hash_alg) {
1112 MBEDTLS_SSL_DEBUG_MSG(
1113 1, ("Invalid ciphersuite for ticket psk."));
1114
1115 MBEDTLS_SSL_PEND_FATAL_ALERT(
1116 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1117 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1118 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1119 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 } else
Jerry Yu4a698342022-09-30 12:22:01 +08001121#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
1123 ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
Xiaokang Qian1d8e86c2023-01-12 03:28:18 +00001124 if (mbedtls_psa_translate_md(ssl->handshake->ciphersuite_info->mac)
1125 != hash_alg) {
Xiaokang Qianf10f4742023-01-06 03:43:56 +00001126 MBEDTLS_SSL_DEBUG_MSG(
1127 1, ("Invalid ciphersuite for external psk."));
1128
1129 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1130 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1131 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
1134 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1135 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yub300e3c2022-09-28 22:12:07 +08001136 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (ret != 0) {
1138 return ret;
Jerry Yub300e3c2022-09-28 22:12:07 +08001139 }
Jerry Yub300e3c2022-09-28 22:12:07 +08001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1142 if (ret != 0) {
1143 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1144 return ret;
1145 }
1146
1147 return 0;
Jerry Yu0c6105b2022-08-12 17:26:40 +08001148}
Ronald Cron41a443a2022-10-04 16:38:25 +02001149#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1152 unsigned char *buf,
1153 unsigned char *end,
1154 size_t *out_len)
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001155{
1156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1157 unsigned char *p = buf;
1158 size_t ext_len;
1159
1160 *out_len = 0;
1161
1162 /* Write supported_versions extension
1163 *
1164 * Supported Versions Extension is mandatory with TLS 1.3.
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
1167 if (ret != 0) {
1168 return ret;
1169 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001170 p += ext_len;
1171
1172 /* Echo the cookie if the server provided one in its preceding
1173 * HelloRetryRequest message.
1174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
1176 if (ret != 0) {
1177 return ret;
1178 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001179 p += ext_len;
1180
Ronald Cron766c0cd2022-10-18 12:17:11 +02001181#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1183 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
1184 if (ret != 0) {
1185 return ret;
1186 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001187 p += ext_len;
1188 }
Ronald Cron766c0cd2022-10-18 12:17:11 +02001189#endif
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001190
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001191#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1193 ssl_tls13_early_data_has_valid_ticket(ssl) &&
1194 ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
1195 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &ext_len);
1196 if (ret != 0) {
1197 return ret;
1198 }
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001199 p += ext_len;
1200
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001201 /* Initializes the status to `rejected`. It will be updated to
1202 * `accepted` if the EncryptedExtension message contain an early data
1203 * indication extension.
Xiaokang Qiana042b842022-11-09 01:59:33 +00001204 */
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001205 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 } else {
1207 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write early_data extension"));
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00001208 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001209 }
1210#endif /* MBEDTLS_SSL_EARLY_DATA */
1211
Ronald Cron41a443a2022-10-04 16:38:25 +02001212#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001213 /* For PSK-based key exchange we need the pre_shared_key extension
1214 * and the psk_key_exchange_modes extension.
1215 *
1216 * The pre_shared_key extension MUST be the last extension in the
1217 * ClientHello. Servers MUST check that it is the last extension and
1218 * otherwise fail the handshake with an "illegal_parameter" alert.
1219 *
1220 * Add the psk_key_exchange_modes extension.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
1223 if (ret != 0) {
1224 return ret;
1225 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001226 p += ext_len;
Ronald Cron41a443a2022-10-04 16:38:25 +02001227#endif
XiaokangQianeb69aee2022-07-05 08:21:43 +00001228
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001229 *out_len = p - buf;
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return 0;
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001232}
1233
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001234int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
Xiaokang Qian126929f2023-01-03 10:29:41 +00001235{
Xiaokang Qian90746132023-01-06 05:54:59 +00001236 ((void) ssl);
Xiaokang Qian79f77522023-01-28 10:35:29 +00001237
Xiaokang Qian126929f2023-01-03 10:29:41 +00001238#if defined(MBEDTLS_SSL_EARLY_DATA)
1239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1240 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1241 const unsigned char *psk;
1242 size_t psk_len;
1243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Xiaokang Qian592021a2023-01-04 10:47:05 +00001244
Xiaokang Qian126929f2023-01-03 10:29:41 +00001245 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
Xiaokang Qian6be82902023-02-03 06:04:43 +00001246#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1247 mbedtls_ssl_handshake_set_state(
1248 ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO);
1249#endif
Xiaokang Qian126929f2023-01-03 10:29:41 +00001250 MBEDTLS_SSL_DEBUG_MSG(
1251 1, ("Set hs psk for early data when writing the first psk"));
1252
1253 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1254 if (ret != 0) {
1255 MBEDTLS_SSL_DEBUG_RET(
1256 1, "ssl_tls13_ticket_get_psk", ret);
1257 return ret;
1258 }
1259
1260 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1261 if (ret != 0) {
1262 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1263 return ret;
1264 }
1265
1266 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
1267 ssl->session_negotiate->ciphersuite);
1268 ssl->handshake->ciphersuite_info = ciphersuite_info;
Xiaokang Qian42242442023-01-12 02:26:17 +00001269 /* Enable psk and psk_ephermal to make stage early happy */
Xiaokang Qian126929f2023-01-03 10:29:41 +00001270 ssl->handshake->key_exchange_mode =
1271 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
1272
1273 /* Start the TLS 1.3 key schedule:
1274 * Set the PSK and derive early secret.
1275 */
1276 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1277 if (ret != 0) {
1278 MBEDTLS_SSL_DEBUG_RET(1,
1279 "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1280 return ret;
1281 }
1282
1283 /* Derive early data key material */
1284 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1285 if (ret != 0) {
1286 MBEDTLS_SSL_DEBUG_RET(1,
1287 "mbedtls_ssl_tls13_compute_early_transform", ret);
1288 return ret;
1289 }
1290
Xiaokang Qian126929f2023-01-03 10:29:41 +00001291 }
1292#endif /* MBEDTLS_SSL_EARLY_DATA */
1293 return 0;
1294}
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001295/*
Jerry Yu4a173382021-10-11 21:45:31 +08001296 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001297 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001298
Ronald Cronda41b382022-03-30 09:57:11 +02001299/**
1300 * \brief Detect if the ServerHello contains a supported_versions extension
1301 * or not.
1302 *
1303 * \param[in] ssl SSL context
1304 * \param[in] buf Buffer containing the ServerHello message
1305 * \param[in] end End of the buffer containing the ServerHello message
1306 *
1307 * \return 0 if the ServerHello does not contain a supported_versions extension
1308 * \return 1 if the ServerHello contains a supported_versions extension
1309 * \return A negative value if an error occurred while parsing the ServerHello.
1310 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001311MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001312static int ssl_tls13_is_supported_versions_ext_present(
1313 mbedtls_ssl_context *ssl,
1314 const unsigned char *buf,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 const unsigned char *end)
Ronald Cron9f0fba32022-02-10 16:45:15 +01001316{
1317 const unsigned char *p = buf;
1318 size_t legacy_session_id_echo_len;
1319 size_t extensions_len;
1320 const unsigned char *extensions_end;
1321
1322 /*
1323 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001324 * length:
1325 * - legacy_version 2 bytes
1326 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1327 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001330 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1331 legacy_session_id_echo_len = *p;
1332
1333 /*
1334 * Jump to the extensions, jumping over:
1335 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1336 * - cipher_suite 2 bytes
1337 * - legacy_compression_method 1 byte
1338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
1340 p += legacy_session_id_echo_len + 4;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001341
1342 /* Case of no extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (p == end) {
1344 return 0;
1345 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001346
1347 /* ...
1348 * Extension extensions<6..2^16-1>;
1349 * ...
1350 * struct {
1351 * ExtensionType extension_type; (2 bytes)
1352 * opaque extension_data<0..2^16-1>;
1353 * } Extension;
1354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1356 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001357 p += 2;
1358
1359 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001361 extensions_end = p + extensions_len;
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 while (p < extensions_end) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01001364 unsigned int extension_type;
1365 size_t extension_data_len;
1366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1368 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1369 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001370 p += 4;
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
1373 return 1;
1374 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001377 p += extension_data_len;
1378 }
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001381}
1382
Jerry Yu7a186a02021-10-15 18:46:14 +08001383/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001384 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1385 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1386 * - 0 otherwise
1387 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001388MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001389static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
1390 const unsigned char *buf,
1391 const unsigned char *end)
Ronald Cronfd6193c2022-04-05 11:04:20 +02001392{
1393 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1394 static const unsigned char magic_downgrade_string[] =
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
Ronald Cronfd6193c2022-04-05 11:04:20 +02001396 const unsigned char *last_eight_bytes_of_random;
1397 unsigned char last_byte_of_random;
1398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
Ronald Cronfd6193c2022-04-05 11:04:20 +02001400 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (memcmp(last_eight_bytes_of_random,
1403 magic_downgrade_string,
1404 sizeof(magic_downgrade_string)) == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001405 last_byte_of_random = last_eight_bytes_of_random[7];
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return last_byte_of_random == 0 ||
1407 last_byte_of_random == 1;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001408 }
1409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return 0;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001411}
1412
1413/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001414 * - SSL_SERVER_HELLO or
1415 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001416 * to indicate which message is expected and to be parsed next.
1417 */
Ronald Cron828aff62022-05-31 12:04:31 +02001418#define SSL_SERVER_HELLO 0
1419#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001421static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
1422 const unsigned char *buf,
1423 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001424{
Jerry Yue1b9c292021-09-10 10:08:31 +08001425
1426 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1427 *
Jerry Yu4a173382021-10-11 21:45:31 +08001428 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001429 * special value of the SHA-256 of "HelloRetryRequest".
1430 *
1431 * struct {
1432 * ProtocolVersion legacy_version = 0x0303;
1433 * Random random;
1434 * opaque legacy_session_id_echo<0..32>;
1435 * CipherSuite cipher_suite;
1436 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001437 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001438 * } ServerHello;
1439 *
1440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end,
1442 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
Jerry Yu4a173382021-10-11 21:45:31 +08001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1445 sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
1446 return SSL_SERVER_HELLO_HRR;
Jerry Yue1b9c292021-09-10 10:08:31 +08001447 }
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return SSL_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001450}
1451
Ronald Cron828aff62022-05-31 12:04:31 +02001452/*
Jerry Yu745bb612021-10-13 22:01:04 +08001453 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001454 * - SSL_SERVER_HELLO or
1455 * - SSL_SERVER_HELLO_HRR or
1456 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001457 */
Ronald Cron828aff62022-05-31 12:04:31 +02001458#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001459MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001460static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
1461 const unsigned char *buf,
1462 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001463{
Jerry Yu4a173382021-10-11 21:45:31 +08001464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001465 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
1468 ssl, buf, end));
Jerry Yua66fece2022-07-13 14:30:29 +08001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (ret == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001471 MBEDTLS_SSL_PROC_CHK_NEG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
Ronald Cronfd6193c2022-04-05 11:04:20 +02001473
1474 /* If the server is negotiating TLS 1.2 or below and:
1475 * . we did not propose TLS 1.2 or
1476 * . the server responded it is TLS 1.3 capable but negotiating a lower
1477 * version of the protocol and thus we are under downgrade attack
1478 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
1481 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1482 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1483 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001484 }
1485
1486 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001487 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1489 buf, (size_t) (end - buf));
Ronald Cron9f0fba32022-02-10 16:45:15 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1492 ret = ssl_tls13_reset_key_share(ssl);
1493 if (ret != 0) {
1494 return ret;
1495 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001496 }
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return SSL_SERVER_HELLO_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001499 }
1500
Jerry Yua66fece2022-07-13 14:30:29 +08001501#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1502 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1503 ssl->session_negotiate->tls_version = ssl->tls_version;
1504#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1505
Jerry Yu50e00e32022-10-31 14:45:01 +08001506 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron5afb9042022-05-31 12:11:39 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 ret = ssl_server_hello_is_hrr(ssl, buf, end);
1509 switch (ret) {
Ronald Cron828aff62022-05-31 12:04:31 +02001510 case SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
Jerry Yu745bb612021-10-13 22:01:04 +08001512 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001513 case SSL_SERVER_HELLO_HRR:
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
1515 /* If a client receives a second
1516 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1517 * was itself in response to a HelloRetryRequest), it MUST abort the
1518 * handshake with an "unexpected_message" alert.
1519 */
1520 if (handshake->hello_retry_request_count > 0) {
1521 MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
1522 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1523 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1524 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001525 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526 /*
1527 * Clients must abort the handshake with an "illegal_parameter"
1528 * alert if the HelloRetryRequest would not result in any change
1529 * in the ClientHello.
1530 * In a PSK only key exchange that what we expect.
1531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1533 MBEDTLS_SSL_DEBUG_MSG(1,
1534 ("Unexpected HRR in pure PSK key exchange."));
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1537 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1538 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian2b01dc32022-01-21 02:53:13 +00001539 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001540
Ronald Cron5afb9042022-05-31 12:11:39 +02001541 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001542
Jerry Yu745bb612021-10-13 22:01:04 +08001543 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001544 }
1545
1546cleanup:
1547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001549}
1550
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001551MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001552static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
1553 const unsigned char **buf,
1554 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001555{
1556 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001557 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1560 legacy_session_id_echo_len = *p++;
Jerry Yue1b9c292021-09-10 10:08:31 +08001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001563
1564 /* legacy_session_id_echo */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1566 memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
1567 MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
1568 ssl->session_negotiate->id,
1569 ssl->session_negotiate->id_len);
1570 MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
1571 legacy_session_id_echo_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1574 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
Jerry Yu4a173382021-10-11 21:45:31 +08001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001577 }
1578
Jerry Yu4a173382021-10-11 21:45:31 +08001579 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001580 *buf = p;
1581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
1583 ssl->session_negotiate->id_len);
1584 return 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001585}
1586
Jerry Yue1b9c292021-09-10 10:08:31 +08001587/* Parse ServerHello message and configure context
1588 *
1589 * struct {
1590 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1591 * Random random;
1592 * opaque legacy_session_id_echo<0..32>;
1593 * CipherSuite cipher_suite;
1594 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001595 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001596 * } ServerHello;
1597 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001598MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001599static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
1600 const unsigned char *buf,
1601 const unsigned char *end,
1602 int is_hrr)
Jerry Yue1b9c292021-09-10 10:08:31 +08001603{
Jerry Yub85277e2021-10-13 13:36:05 +08001604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001605 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001606 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001607 size_t extensions_len;
1608 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001609 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001610 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001611 int fatal_alert = 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001612 uint32_t allowed_extensions_mask;
Jerry Yu50e00e32022-10-31 14:45:01 +08001613 int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 MBEDTLS_SSL_HS_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001615
1616 /*
1617 * Check there is space for minimal fields
1618 *
1619 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001620 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001621 * - legacy_session_id_echo ( 1 byte ), minimum size
1622 * - cipher_suite ( 2 bytes)
1623 * - legacy_compression_method ( 1 byte )
1624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
Jerry Yue1b9c292021-09-10 10:08:31 +08001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
1628 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001629
Jerry Yu4a173382021-10-11 21:45:31 +08001630 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001631 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001632 * ...
1633 * with ProtocolVersion defined as:
1634 * uint16 ProtocolVersion;
1635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1637 MBEDTLS_SSL_VERSION_TLS1_2) {
1638 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1639 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1640 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001641 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1642 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001643 }
1644 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001645
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001646 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001647 * Random random;
1648 * ...
1649 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001650 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (!is_hrr) {
1653 memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1654 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1655 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
1656 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
XiaokangQian53f20b72022-01-18 10:47:33 +00001657 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001658 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001659
Jerry Yu4a173382021-10-11 21:45:31 +08001660 /* ...
1661 * opaque legacy_session_id_echo<0..32>;
1662 * ...
1663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
XiaokangQian52da5582022-01-26 09:49:29 +00001665 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001666 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001667 }
1668
Jerry Yu4a173382021-10-11 21:45:31 +08001669 /* ...
1670 * CipherSuite cipher_suite;
1671 * ...
1672 * with CipherSuite defined as:
1673 * uint8 CipherSuite[2];
1674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1676 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001677 p += 2;
1678
Jerry Yu4a173382021-10-11 21:45:31 +08001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Jerry Yu4a173382021-10-11 21:45:31 +08001681 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001682 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
1685 ssl->tls_version,
1686 ssl->tls_version) != 0) ||
1687 !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001688 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001689 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001690 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001691 * If we received an HRR before and that the proposed selected
1692 * ciphersuite in this server hello is not the same as the one
1693 * proposed in the HRR, we abort the handshake and send an
1694 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001695 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 else if ((!is_hrr) && (handshake->hello_retry_request_count > 0) &&
1697 (cipher_suite != ssl->session_negotiate->ciphersuite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001698 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001699 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1702 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
1703 cipher_suite));
XiaokangQiand59be772022-01-24 10:12:51 +00001704 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001705 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001706
Jerry Yu4a173382021-10-11 21:45:31 +08001707 /* Configure ciphersuites */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
Jerry Yu4a173382021-10-11 21:45:31 +08001709
XiaokangQian355e09a2022-01-20 11:14:50 +00001710 handshake->ciphersuite_info = ciphersuite_info;
Xiaokang Qian33ff8682023-01-10 06:32:12 +00001711#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianea28a782023-01-12 03:18:31 +00001712 if (handshake->resume == 0)
Xiaokang Qian33ff8682023-01-10 06:32:12 +00001713#endif
Jerry Yue1b9c292021-09-10 10:08:31 +08001714 ssl->session_negotiate->ciphersuite = cipher_suite;
1715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
1717 cipher_suite, ciphersuite_info->name));
Jerry Yue1b9c292021-09-10 10:08:31 +08001718
1719#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 ssl->session_negotiate->start = time(NULL);
Jerry Yue1b9c292021-09-10 10:08:31 +08001721#endif /* MBEDTLS_HAVE_TIME */
1722
Jerry Yu4a173382021-10-11 21:45:31 +08001723 /* ...
1724 * uint8 legacy_compression_method = 0;
1725 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001726 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1728 if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
1729 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
XiaokangQian52da5582022-01-26 09:49:29 +00001730 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001731 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001732 }
1733 p++;
1734
Jerry Yub85277e2021-10-13 13:36:05 +08001735 /* ...
1736 * Extension extensions<6..2^16-1>;
1737 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001738 * struct {
1739 * ExtensionType extension_type; (2 bytes)
1740 * opaque extension_data<0..2^16-1>;
1741 * } Extension;
1742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1744 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001745 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001746
Jerry Yu4a173382021-10-11 21:45:31 +08001747 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001749 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001752
Jerry Yu50e00e32022-10-31 14:45:01 +08001753 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001754 allowed_extensions_mask = is_hrr ?
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
1756 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
Jerry Yu2c5363e2022-08-04 17:42:49 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 while (p < extensions_end) {
Jerry Yue1b9c292021-09-10 10:08:31 +08001759 unsigned int extension_type;
1760 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001761 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1764 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1765 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001766 p += 4;
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian43550bd2022-01-21 04:32:58 +00001769 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001770
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001771 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ssl, hs_msg_type, extension_type, allowed_extensions_mask);
1773 if (ret != 0) {
1774 return ret;
1775 }
Jerry Yu2c5363e2022-08-04 17:42:49 +08001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 switch (extension_type) {
XiaokangQianb851da82022-01-14 04:03:11 +00001778 case MBEDTLS_TLS_EXT_COOKIE:
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = ssl_tls13_parse_cookie_ext(ssl,
1781 p, extension_data_end);
1782 if (ret != 0) {
1783 MBEDTLS_SSL_DEBUG_RET(1,
1784 "ssl_tls13_parse_cookie_ext",
1785 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001786 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001787 }
XiaokangQianb851da82022-01-14 04:03:11 +00001788 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001789
Jerry Yue1b9c292021-09-10 10:08:31 +08001790 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 ret = ssl_tls13_parse_supported_versions_ext(ssl,
1792 p,
1793 extension_data_end);
1794 if (ret != 0) {
XiaokangQiand59be772022-01-24 10:12:51 +00001795 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001797 break;
1798
Ronald Cron41a443a2022-10-04 16:38:25 +02001799#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001800 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
Jerry Yu4a173382021-10-11 21:45:31 +08001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
1804 ssl, p, extension_data_end)) != 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +00001805 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
1807 return ret;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001808 }
1809 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001810#endif
Jerry Yue1b9c292021-09-10 10:08:31 +08001811
Jerry Yue1b9c292021-09-10 10:08:31 +08001812 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
1814 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001815 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001816 goto cleanup;
1817 }
1818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (is_hrr) {
1820 ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
1821 p, extension_data_end);
1822 } else {
1823 ret = ssl_tls13_parse_key_share_ext(ssl,
1824 p, extension_data_end);
1825 }
1826 if (ret != 0) {
1827 MBEDTLS_SSL_DEBUG_RET(1,
1828 "ssl_tls13_parse_key_share_ext",
1829 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001830 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001831 }
1832 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001833
1834 default:
Jerry Yu9872eb22022-08-29 13:42:01 +08001835 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1836 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001837 }
1838
1839 p += extension_data_len;
1840 }
1841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
Jerry Yu2c5363e2022-08-04 17:42:49 +08001843
XiaokangQiand59be772022-01-24 10:12:51 +00001844cleanup:
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
1847 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1848 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001849 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1851 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1852 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
XiaokangQiand59be772022-01-24 10:12:51 +00001853 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001856}
1857
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001858#if defined(MBEDTLS_DEBUG_C)
1859static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001860{
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 switch (mode) {
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001862 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1863 return "psk";
1864 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1865 return "ephemeral";
1866 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1867 return "psk_ephemeral";
1868 default:
1869 return "unknown mode";
1870 }
1871}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001872#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001873
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001874MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001875static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue1b9c292021-09-10 10:08:31 +08001876{
Jerry Yub85277e2021-10-13 13:36:05 +08001877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001878 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001879
Jerry Yub85277e2021-10-13 13:36:05 +08001880 /* Determine the key exchange mode:
1881 * 1) If both the pre_shared_key and key_share extensions were received
1882 * then the key exchange mode is PSK with EPHEMERAL.
1883 * 2) If only the pre_shared_key extension was received then the key
1884 * exchange mode is PSK-only.
1885 * 3) If only the key_share extension was received then the key
1886 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 switch (handshake->received_extensions &
1889 (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
Jerry Yu745bb612021-10-13 22:01:04 +08001890 /* Only the pre_shared_key extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
Ronald Cron79907712022-07-20 17:05:29 +02001892 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001893 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001894
Jerry Yu745bb612021-10-13 22:01:04 +08001895 /* Only the key_share extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
Ronald Cron79907712022-07-20 17:05:29 +02001897 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001898 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001899
Jerry Yu745bb612021-10-13 22:01:04 +08001900 /* Both the pre_shared_key and key_share extensions were received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
Ronald Cron79907712022-07-20 17:05:29 +02001902 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001903 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001904
Jerry Yu745bb612021-10-13 22:01:04 +08001905 /* Neither pre_shared_key nor key_share extension was received */
1906 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
Jerry Yu745bb612021-10-13 22:01:04 +08001908 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1909 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001910 }
Xiaokang Qian3f616c22023-01-12 03:36:31 +00001911#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qian02f5e142023-02-06 10:44:17 +00001912 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA) &&
1913 (handshake->selected_identity != 0 ||
1914 handshake->ciphersuite_info->id !=
1915 ssl->session_negotiate->ciphersuite)) {
Xiaokang Qian3f616c22023-01-12 03:36:31 +00001916 /* RFC8446 4.2.11
1917 * If the server supplies an "early_data" extension, the
1918 * client MUST verify that the server's selected_identity
1919 * is 0. If any other value is returned, the client MUST
1920 * abort the handshake with an "illegal_parameter" alert.
Xiaokang Qian02f5e142023-02-06 10:44:17 +00001921 *
1922 * Clients MUST verify that the server selected a cipher suite
1923 * indicating a Hash associated with the PSK, If this value are
1924 * not consistent, the client MUST abort the handshake with an
1925 * "illegal_parameter" alert.
Xiaokang Qian3f616c22023-01-12 03:36:31 +00001926 */
1927 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1928 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1929 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1930 }
1931#endif
Jerry Yu0b177842021-09-05 19:41:30 +08001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (!mbedtls_ssl_conf_tls13_check_kex_modes(ssl, handshake->key_exchange_mode)) {
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001934 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 MBEDTLS_SSL_DEBUG_MSG(2,
1936 ("Key exchange mode(%s) is not supported.",
1937 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001938 goto cleanup;
1939 }
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 MBEDTLS_SSL_DEBUG_MSG(3,
1942 ("Selected key exchange mode: %s",
1943 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001944
Xiaokang Qian7892b6c2023-02-02 06:05:48 +00001945 /* Start the TLS 1.3 key scheduling if not already done.
Jerry Yu0b177842021-09-05 19:41:30 +08001946 *
Xiaokang Qian7892b6c2023-02-02 06:05:48 +00001947 * If we proposed early data then we have already derived an
1948 * early secret using the selected PSK and its associated hash.
1949 * It means that if the negotiated key exchange mode is psk or
1950 * psk_ephemeral, we have already correctly computed the
1951 * early secret and thus we do not do it again. In all other
1952 * cases we compute it here.
Xiaokang Qian303f82c52023-01-04 08:43:46 +00001953 */
Xiaokang Qian90746132023-01-06 05:54:59 +00001954#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qian42242442023-01-12 02:26:17 +00001955 if ((ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT) ||
1956 (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED &&
1957 handshake->key_exchange_mode ==
1958 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL))
Xiaokang Qian90746132023-01-06 05:54:59 +00001959#endif
1960 {
Xiaokang Qian303f82c52023-01-04 08:43:46 +00001961 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1962 if (ret != 0) {
1963 MBEDTLS_SSL_DEBUG_RET(
1964 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1965 goto cleanup;
1966 }
Jerry Yu0b177842021-09-05 19:41:30 +08001967 }
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
1970 if (ret != 0) {
1971 MBEDTLS_SSL_DEBUG_RET(1,
1972 "mbedtls_ssl_tls13_compute_handshake_transform",
1973 ret);
Jerry Yu4a173382021-10-11 21:45:31 +08001974 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001975 }
1976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
1978 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
Xiaokang Qian4ef8ba22023-02-06 11:06:16 +00001979 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
Jerry Yu0b177842021-09-05 19:41:30 +08001980 ssl->session_in = ssl->session_negotiate;
1981
Jerry Yu4a173382021-10-11 21:45:31 +08001982cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (ret != 0) {
Jerry Yu4a173382021-10-11 21:45:31 +08001984 MBEDTLS_SSL_PEND_FATAL_ALERT(
1985 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yu4a173382021-10-11 21:45:31 +08001987 }
Jerry Yue110d252022-05-05 10:19:22 +08001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001990}
1991
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001992MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001993static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
XiaokangQian647719a2021-12-07 09:16:29 +00001994{
1995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
XiaokangQian647719a2021-12-07 09:16:29 +00001998
XiaokangQian78b1fa72022-01-19 06:56:30 +00001999 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00002000 * We are going to re-generate a shared secret corresponding to the group
2001 * selected by the server, which is different from the group for which we
2002 * generated a shared secret in the first client hello.
2003 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00002004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 ret = ssl_tls13_reset_key_share(ssl);
2006 if (ret != 0) {
2007 return ret;
2008 }
XiaokangQian647719a2021-12-07 09:16:29 +00002009
Xiaokang Qian4ef8ba22023-02-06 11:06:16 +00002010 ssl->session_negotiate->ciphersuite = ssl->handshake->ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return 0;
XiaokangQian647719a2021-12-07 09:16:29 +00002012}
2013
Jerry Yue1b9c292021-09-10 10:08:31 +08002014/*
Jerry Yu4a173382021-10-11 21:45:31 +08002015 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08002016 * Handler for MBEDTLS_SSL_SERVER_HELLO
2017 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002018MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002019static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002020{
Jerry Yu4a173382021-10-11 21:45:31 +08002021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00002022 unsigned char *buf = NULL;
2023 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00002024 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
Jerry Yue1b9c292021-09-10 10:08:31 +08002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2029 MBEDTLS_SSL_HS_SERVER_HELLO,
2030 &buf, &buf_len));
Ronald Crondb5dfa12022-05-31 11:44:38 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
2033 if (ret < 0) {
XiaokangQian16acd4b2022-01-14 07:35:47 +00002034 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 } else {
2036 is_hrr = (ret == SSL_SERVER_HELLO_HRR);
2037 }
XiaokangQiand59be772022-01-24 10:12:51 +00002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (ret == SSL_SERVER_HELLO_TLS1_2) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01002040 ret = 0;
2041 goto cleanup;
2042 }
2043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
2045 buf + buf_len,
2046 is_hrr));
2047 if (is_hrr) {
2048 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
Ronald Cronfb508b82022-05-31 14:49:55 +02002049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002050
2051 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2052 buf, buf_len);
2053
2054 if (is_hrr) {
2055 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
2056#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2057 /* If not offering early data, the client sends a dummy CCS record
2058 * immediately before its second flight. This may either be before
2059 * its second ClientHello or before its encrypted handshake flight.
2060 */
2061 mbedtls_ssl_handshake_set_state(ssl,
2062 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
2063#else
2064 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2065#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2066 } else {
2067 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
2068 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Ronald Cronfb508b82022-05-31 14:49:55 +02002069 }
Jerry Yue1b9c292021-09-10 10:08:31 +08002070
2071cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
2073 is_hrr ? "HelloRetryRequest" : "ServerHello"));
2074 return ret;
Jerry Yu687101b2021-09-14 16:03:56 +08002075}
2076
2077/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002078 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02002079 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002080 *
2081 * The EncryptedExtensions message contains any extensions which
2082 * should be protected, i.e., any which are not needed to establish
2083 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08002084 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002085
XiaokangQian08da26c2021-10-09 10:12:11 +00002086/* Parse EncryptedExtensions message
2087 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00002088 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00002089 * } EncryptedExtensions;
2090 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002092static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
2093 const unsigned char *buf,
2094 const unsigned char *end)
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002095{
2096 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00002097 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00002098 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00002099 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002100 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2103 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08da26c2021-10-09 10:12:11 +00002104 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
2107 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cronc8083592022-05-31 16:24:05 +02002108 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002109
Jerry Yu9eba7502022-10-31 13:46:16 +08002110 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yucbd082f2022-08-04 16:55:10 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 while (p < extensions_end) {
XiaokangQian08da26c2021-10-09 10:12:11 +00002113 unsigned int extension_type;
2114 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002115
XiaokangQian08da26c2021-10-09 10:12:11 +00002116 /*
2117 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00002118 * ExtensionType extension_type; (2 bytes)
2119 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00002120 * } Extension;
2121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2123 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2124 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian08da26c2021-10-09 10:12:11 +00002125 p += 4;
2126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002128
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002129 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
2131 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
2132 if (ret != 0) {
2133 return ret;
2134 }
Jerry Yucbd082f2022-08-04 16:55:10 +08002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 switch (extension_type) {
lhuang0486cacac2022-01-21 07:34:27 -08002137#if defined(MBEDTLS_SSL_ALPN)
2138 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
lhuang0486cacac2022-01-21 07:34:27 -08002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if ((ret = ssl_tls13_parse_alpn_ext(ssl, p, (size_t) extension_data_len)) != 0) {
2142 return ret;
lhuang0486cacac2022-01-21 07:34:27 -08002143 }
2144
2145 break;
2146#endif /* MBEDTLS_SSL_ALPN */
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002147
2148#if defined(MBEDTLS_SSL_EARLY_DATA)
2149 case MBEDTLS_TLS_EXT_EARLY_DATA:
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (extension_data_len != 0) {
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002152 /* The message must be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2154 MBEDTLS_ERR_SSL_DECODE_ERROR);
2155 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002156 }
2157
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002158 break;
2159#endif /* MBEDTLS_SSL_EARLY_DATA */
2160
XiaokangQian08da26c2021-10-09 10:12:11 +00002161 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002162 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu9eba7502022-10-31 13:46:16 +08002163 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 extension_type, "( ignored )");
Jerry Yucbd082f2022-08-04 16:55:10 +08002165 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00002166 }
2167
XiaokangQian08da26c2021-10-09 10:12:11 +00002168 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00002169 }
XiaokangQian08da26c2021-10-09 10:12:11 +00002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2172 handshake->received_extensions);
Jerry Yucbd082f2022-08-04 16:55:10 +08002173
XiaokangQian97799ac2021-10-11 10:05:54 +00002174 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (p != end) {
2176 MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
2177 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2178 MBEDTLS_ERR_SSL_DECODE_ERROR);
2179 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002180 }
2181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 return ret;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002183}
2184
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002185MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002186static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
Ronald Cron9d6a5452022-05-30 16:05:38 +02002187{
2188 int ret;
2189 unsigned char *buf;
2190 size_t buf_len;
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2195 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2196 &buf, &buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002197
2198 /* Process the message contents */
2199 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002201
Xiaokang Qianb157e912022-11-23 08:12:26 +00002202#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (ssl->handshake->received_extensions &
2204 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Xiaokang Qianb157e912022-11-23 08:12:26 +00002205 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
2206 }
2207#endif
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2210 buf, buf_len);
Ronald Cron9d6a5452022-05-30 16:05:38 +02002211
Ronald Cron928cbd32022-10-04 16:14:26 +02002212#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2214 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2215 } else {
2216 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2217 }
Ronald Cronfb508b82022-05-31 14:49:55 +02002218#else
2219 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Ronald Cronfb508b82022-05-31 14:49:55 +02002221#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02002222
2223cleanup:
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
2226 return ret;
Ronald Cron9d6a5452022-05-30 16:05:38 +02002227
2228}
2229
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002230/*
2231 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
2232 *
Xiaokang Qianc81a15a2022-12-19 02:43:33 +00002233 * RFC 8446 section 4.5
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002234 *
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002235 * struct {} EndOfEarlyData;
Xiaokang Qianc81a15a2022-12-19 02:43:33 +00002236 *
2237 * If the server sent an "early_data" extension in EncryptedExtensions, the
2238 * client MUST send an EndOfEarlyData message after receiving the server
2239 * Finished. Otherwise, the client MUST NOT send an EndOfEarlyData message.
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002240 */
2241
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002242MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002243static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
2244{
2245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaokang Qian742578c2022-12-19 06:34:44 +00002246 unsigned char *buf = NULL;
2247 size_t buf_len;
Xiaokang Qian57a138d2022-12-19 06:40:47 +00002248 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write EndOfEarlyData"));
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002249
Xiaokang Qian742578c2022-12-19 06:34:44 +00002250 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2251 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, &buf, &buf_len));
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002252
Xiaokang Qian742578c2022-12-19 06:34:44 +00002253 mbedtls_ssl_add_hs_hdr_to_checksum(
2254 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0);
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002255
Xiaokang Qian742578c2022-12-19 06:34:44 +00002256 MBEDTLS_SSL_PROC_CHK(
2257 mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
Xiaokang Qianda8402d2022-12-15 14:55:35 +00002258
Xiaokang Qian19d44162023-01-03 03:39:50 +00002259 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002260
2261cleanup:
2262
2263 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write EndOfEarlyData"));
2264 return ret;
2265}
2266
Ronald Cron928cbd32022-10-04 16:14:26 +02002267#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08002268/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002269 * STATE HANDLING: CertificateRequest
2270 *
Jerry Yud2674312021-10-29 10:08:19 +08002271 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002272#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2273#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002274/* Coordination:
2275 * Deals with the ambiguity of not knowing if a CertificateRequest
2276 * will be sent. Returns a negative code on failure, or
2277 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2278 * - SSL_CERTIFICATE_REQUEST_SKIP
2279 * indicating if a Certificate Request is expected or not.
2280 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002282static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
Jerry Yud2674312021-10-29 10:08:19 +08002283{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2287 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2288 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002289 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002290 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
2293 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
2294 MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
2295 return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
Jerry Yud2674312021-10-29 10:08:19 +08002296 }
2297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
Ronald Cron19385882022-06-15 16:26:13 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return SSL_CERTIFICATE_REQUEST_SKIP;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002301}
2302
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002303/*
2304 * ssl_tls13_parse_certificate_request()
2305 * Parse certificate request
2306 * struct {
2307 * opaque certificate_request_context<0..2^8-1>;
2308 * Extension extensions<2..2^16-1>;
2309 * } CertificateRequest;
2310 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002311MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002312static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
2313 const unsigned char *buf,
2314 const unsigned char *end)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002315{
2316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2317 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002318 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002319 size_t extensions_len = 0;
2320 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002321 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002322
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002323 /* ...
2324 * opaque certificate_request_context<0..2^8-1>
2325 * ...
2326 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002328 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002329 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (certificate_request_context_len > 0) {
2332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
2333 MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
2334 p, certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002335
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002336 handshake->certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 mbedtls_calloc(1, certificate_request_context_len);
2338 if (handshake->certificate_request_context == NULL) {
2339 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2340 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002341 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 memcpy(handshake->certificate_request_context, p,
2343 certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002344 p += certificate_request_context_len;
2345 }
2346
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002347 /* ...
2348 * Extension extensions<2..2^16-1>;
2349 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2352 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002353 p += 2;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002356 extensions_end = p + extensions_len;
2357
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002358 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yuc55a6af2022-08-04 17:01:21 +08002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 while (p < extensions_end) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002361 unsigned int extension_type;
2362 size_t extension_data_len;
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2365 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2366 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002367 p += 4;
2368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002370
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002371 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
2373 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
2374 if (ret != 0) {
2375 return ret;
2376 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 switch (extension_type) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002379 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_DEBUG_MSG(3,
2381 ("found signature algorithms extension"));
2382 ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
2383 p + extension_data_len);
2384 if (ret != 0) {
2385 return ret;
2386 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002387
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002388 break;
2389
2390 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002391 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002392 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 extension_type, "( ignored )");
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002394 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002395 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002396
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002397 p += extension_data_len;
2398 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2401 handshake->received_extensions);
Jerry Yuc55a6af2022-08-04 17:01:21 +08002402
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002403 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (p != end) {
2405 MBEDTLS_SSL_DEBUG_MSG(1,
2406 ("CertificateRequest misaligned"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002407 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002408 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002409
Jerry Yu0c354a22022-08-29 15:25:36 +08002410 /* RFC 8446 section 4.3.2
Jerry Yuc55a6af2022-08-04 17:01:21 +08002411 *
2412 * The "signature_algorithms" extension MUST be specified
2413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
2415 MBEDTLS_SSL_DEBUG_MSG(3,
2416 ("no signature algorithms extension found"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002417 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002418 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002419
Jerry Yu7840f812022-01-29 10:26:51 +08002420 ssl->handshake->client_auth = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return 0;
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002422
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002423decode_error:
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2425 MBEDTLS_ERR_SSL_DECODE_ERROR);
2426 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002427}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002428
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002429/*
2430 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2431 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002432MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002433static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002434{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002442 unsigned char *buf;
2443 size_t buf_len;
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2446 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2447 &buf, &buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(ssl,
2450 buf, buf + buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2453 buf, buf_len);
2454 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002455 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 } else {
2457 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002458 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2459 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002460 }
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
Jerry Yud2674312021-10-29 10:08:19 +08002463
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002464cleanup:
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2467 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002468}
2469
2470/*
Jerry Yu687101b2021-09-14 16:03:56 +08002471 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2472 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002473MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002474static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002475{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002476 int ret;
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 ret = mbedtls_ssl_tls13_process_certificate(ssl);
2479 if (ret != 0) {
2480 return ret;
2481 }
Xiaofei Bai947571e2021-09-29 09:12:03 +00002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2484 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002485}
2486
2487/*
2488 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2489 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002490MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002491static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002492{
Jerry Yu30b071c2021-09-12 20:16:03 +08002493 int ret;
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
2496 if (ret != 0) {
2497 return ret;
2498 }
Jerry Yu30b071c2021-09-12 20:16:03 +08002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2501 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002502}
Ronald Cron928cbd32022-10-04 16:14:26 +02002503#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002504
Jerry Yu687101b2021-09-14 16:03:56 +08002505/*
2506 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2507 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002508MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002509static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002510{
XiaokangQianac0385c2021-11-03 06:40:11 +00002511 int ret;
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2514 if (ret != 0) {
2515 return ret;
2516 }
XiaokangQianac0385c2021-11-03 06:40:11 +00002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2519 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002520 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2522 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2523 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002524 }
2525
Xiaokang Qianbc75bc02022-12-19 06:16:42 +00002526#if defined(MBEDTLS_SSL_EARLY_DATA)
2527 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
2528 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
Xiaokang Qianbd0ab062023-02-02 05:56:30 +00002529 } else if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
2530 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
Xiaokang Qianbc75bc02022-12-19 06:16:42 +00002531 } else
2532#endif /* MBEDTLS_SSL_EARLY_DATA */
2533 {
2534#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2535 mbedtls_ssl_handshake_set_state(
2536 ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
2537#else
2538 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2539#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2540 }
Ronald Cron49ad6192021-11-24 16:25:31 +01002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002543}
2544
2545/*
Jerry Yu566c7812022-01-26 15:41:22 +08002546 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2547 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002548MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002549static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002550{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002551 int non_empty_certificate_msg = 0;
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_SSL_DEBUG_MSG(1,
2554 ("Switch to handshake traffic keys for outbound traffic"));
2555 mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
Jerry Yu5cc35062022-01-28 16:16:08 +08002556
Ronald Cron928cbd32022-10-04 16:14:26 +02002557#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ssl->handshake->client_auth) {
2559 int ret = mbedtls_ssl_tls13_write_certificate(ssl);
2560 if (ret != 0) {
2561 return ret;
2562 }
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (mbedtls_ssl_own_cert(ssl) != NULL) {
Ronald Cron7a94aca2022-03-09 07:44:27 +01002565 non_empty_certificate_msg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 }
2567 } else {
2568 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
Ronald Cron7a94aca2022-03-09 07:44:27 +01002569 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002570#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (non_empty_certificate_msg) {
2573 mbedtls_ssl_handshake_set_state(ssl,
2574 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2575 } else {
2576 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
2577 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2578 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 return 0;
Jerry Yu566c7812022-01-26 15:41:22 +08002581}
2582
Ronald Cron928cbd32022-10-04 16:14:26 +02002583#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002584/*
2585 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2586 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002587MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002588static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002589{
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
Ronald Crona8b38872022-03-09 07:59:25 +01002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (ret == 0) {
2593 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2594 }
Ronald Crona8b38872022-03-09 07:59:25 +01002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 return ret;
Jerry Yu566c7812022-01-26 15:41:22 +08002597}
Ronald Cron928cbd32022-10-04 16:14:26 +02002598#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002599
2600/*
Jerry Yu687101b2021-09-14 16:03:56 +08002601 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2602 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002603MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002604static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002605{
XiaokangQian0fa66432021-11-15 03:33:57 +00002606 int ret;
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2609 if (ret != 0) {
2610 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002611 }
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2614 if (ret != 0) {
2615 MBEDTLS_SSL_DEBUG_RET(1,
2616 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
2617 return ret;
2618 }
2619
2620 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
2621 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002622}
2623
2624/*
2625 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2626 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002628static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002629{
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
2631 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2632 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002633}
2634
2635/*
2636 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2637 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002638MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002639static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002640{
Jerry Yu378254d2021-10-30 21:44:47 +08002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yu378254d2021-10-30 21:44:47 +08002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2645 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002646}
2647
Jerry Yuf8a49942022-07-07 11:32:32 +00002648#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2649
Jerry Yua0446a02022-07-13 11:22:55 +08002650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
2652 const unsigned char *buf,
2653 const unsigned char *end)
Jerry Yuf8a49942022-07-07 11:32:32 +00002654{
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002655 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002656 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002657
Jerry Yuf8a49942022-07-07 11:32:32 +00002658
Jerry Yuedab6372022-10-31 14:37:31 +08002659 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 while (p < end) {
Jerry Yuf8a49942022-07-07 11:32:32 +00002662 unsigned int extension_type;
2663 size_t extension_data_len;
Jerry Yu0c354a22022-08-29 15:25:36 +08002664 int ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
2667 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2668 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuf8a49942022-07-07 11:32:32 +00002669 p += 4;
2670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002672
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002673 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
2675 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
2676 if (ret != 0) {
2677 return ret;
2678 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 switch (extension_type) {
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002681#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002682 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (extension_data_len != 4) {
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002684 MBEDTLS_SSL_PEND_FATAL_ALERT(
2685 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 MBEDTLS_ERR_SSL_DECODE_ERROR);
2687 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002688 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (ssl->session != NULL) {
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002690 ssl->session->ticket_flags |=
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002692 }
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002693 break;
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002694#endif /* MBEDTLS_SSL_EARLY_DATA */
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002695
Jerry Yuf8a49942022-07-07 11:32:32 +00002696 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002697 MBEDTLS_SSL_PRINT_EXT(
Jerry Yuedab6372022-10-31 14:37:31 +08002698 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 extension_type, "( ignored )");
Jerry Yuf8a49942022-07-07 11:32:32 +00002700 break;
2701 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002702
Jerry Yuf8a49942022-07-07 11:32:32 +00002703 p += extension_data_len;
2704 }
2705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2707 handshake->received_extensions);
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002710}
2711
2712/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002713 * From RFC8446, page 74
2714 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002715 * struct {
2716 * uint32 ticket_lifetime;
2717 * uint32 ticket_age_add;
2718 * opaque ticket_nonce<0..255>;
2719 * opaque ticket<1..2^16-1>;
2720 * Extension extensions<0..2^16-2>;
2721 * } NewSessionTicket;
2722 *
2723 */
Jerry Yua0446a02022-07-13 11:22:55 +08002724MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002725static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
2726 unsigned char *buf,
2727 unsigned char *end,
2728 unsigned char **ticket_nonce,
2729 size_t *ticket_nonce_len)
Jerry Yuf8a49942022-07-07 11:32:32 +00002730{
2731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2732 unsigned char *p = buf;
2733 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002734 size_t ticket_len;
2735 unsigned char *ticket;
2736 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002737
Jerry Yucb3b1392022-07-12 06:09:38 +00002738 *ticket_nonce = NULL;
2739 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002740 /*
2741 * ticket_lifetime 4 bytes
2742 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002743 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
Jerry Yuf8a49942022-07-07 11:32:32 +00002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
2748 MBEDTLS_SSL_DEBUG_MSG(3,
2749 ("ticket_lifetime: %u",
2750 (unsigned int) session->ticket_lifetime));
Jerry Yuf8a49942022-07-07 11:32:32 +00002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
2753 MBEDTLS_SSL_DEBUG_MSG(3,
2754 ("ticket_age_add: %u",
2755 (unsigned int) session->ticket_age_add));
Jerry Yuf8a49942022-07-07 11:32:32 +00002756
Jerry Yucb3b1392022-07-12 06:09:38 +00002757 *ticket_nonce_len = p[8];
2758 p += 9;
2759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002761 *ticket_nonce = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002763 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002764
2765 /* Ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2767 ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002768 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
2770 MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002771
2772 /* Check if we previously received a ticket already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (session->ticket != NULL || session->ticket_len > 0) {
2774 mbedtls_free(session->ticket);
Jerry Yuf8a49942022-07-07 11:32:32 +00002775 session->ticket = NULL;
2776 session->ticket_len = 0;
2777 }
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2780 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2781 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yuf8a49942022-07-07 11:32:32 +00002782 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 memcpy(ticket, p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002784 p += ticket_len;
2785 session->ticket = ticket;
2786 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002787
Pengyu Lv9f926952022-11-17 15:22:33 +08002788 /* Clear all flags in ticket_flags */
Pengyu Lv80270b22023-01-12 11:54:04 +08002789 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002790 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lv9f926952022-11-17 15:22:33 +08002791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2793 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002794 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
2800 if (ret != 0) {
2801 MBEDTLS_SSL_DEBUG_RET(1,
2802 "ssl_tls13_parse_new_session_ticket_exts",
2803 ret);
2804 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002805 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002806
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002807 /* session has been updated, allow export */
2808 session->exported = 0;
2809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return 0;
Jerry Yucb3b1392022-07-12 06:09:38 +00002811}
2812
Jerry Yua0446a02022-07-13 11:22:55 +08002813MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002814static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
2815 unsigned char *ticket_nonce,
2816 size_t ticket_nonce_len)
Jerry Yucb3b1392022-07-12 06:09:38 +00002817{
2818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2819 mbedtls_ssl_session *session = ssl->session;
2820 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2821 psa_algorithm_t psa_hash_alg;
2822 int hash_length;
2823
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002824#if defined(MBEDTLS_HAVE_TIME)
2825 /* Store ticket creation time */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 session->ticket_received = mbedtls_time(NULL);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002827#endif
2828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
2830 if (ciphersuite_info == NULL) {
2831 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2832 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf8a49942022-07-07 11:32:32 +00002833 }
2834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2836 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2837 if (hash_length == -1 ||
2838 (size_t) hash_length > sizeof(session->resumption_key)) {
2839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu3afdf362022-07-20 17:34:14 +08002840 }
2841
Jerry Yuf8a49942022-07-07 11:32:32 +00002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2844 session->app_secrets.resumption_master_secret,
2845 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002846
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002847 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002848 *
2849 * HKDF-Expand-Label( resumption_master_secret,
2850 * "resumption", ticket_nonce, Hash.length )
2851 */
2852 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 psa_hash_alg,
2854 session->app_secrets.resumption_master_secret,
2855 hash_length,
2856 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2857 ticket_nonce,
2858 ticket_nonce_len,
2859 session->resumption_key,
2860 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (ret != 0) {
2863 MBEDTLS_SSL_DEBUG_RET(2,
2864 "Creating the ticket-resumed PSK failed",
2865 ret);
2866 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002867 }
2868
Jerry Yu0a430c82022-07-20 11:02:48 +08002869 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2872 session->resumption_key,
2873 session->resumption_key_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002874
Pengyu Lv9f926952022-11-17 15:22:33 +08002875 /* Set ticket_flags depends on the selected key exchange modes */
Pengyu Lv80270b22023-01-12 11:54:04 +08002876 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002877 session, ssl->conf->tls13_kex_modes);
Pengyu Lv4938a562023-01-16 11:28:49 +08002878 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002881}
2882
2883/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002884 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002885 */
Jerry Yua0446a02022-07-13 11:22:55 +08002886MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002887static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuf8a49942022-07-07 11:32:32 +00002888{
2889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2890 unsigned char *buf;
2891 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002892 unsigned char *ticket_nonce;
2893 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Jerry Yuf8a49942022-07-07 11:32:32 +00002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2898 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2899 &buf, &buf_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
2902 ssl, buf, buf + buf_len,
2903 &ticket_nonce, &ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_new_session_ticket(
2906 ssl, ticket_nonce, ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002909
Jerry Yuf8a49942022-07-07 11:32:32 +00002910cleanup:
2911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
2913 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002914}
2915#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
Jerry Yubc20bdd2021-08-24 15:59:48 +08002918{
Jerry Yu92c6b402021-08-27 16:59:09 +08002919 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 switch (ssl->state) {
Jerry Yu92c6b402021-08-27 16:59:09 +08002922 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Jerry Yuddda0502022-12-01 19:43:12 +08002924 break;
2925
Jerry Yu92c6b402021-08-27 16:59:09 +08002926 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 ret = mbedtls_ssl_write_client_hello(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002928 break;
2929
2930 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 ret = ssl_tls13_process_server_hello(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002932 break;
2933
2934 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 ret = ssl_tls13_process_encrypted_extensions(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002936 break;
2937
Ronald Cron928cbd32022-10-04 16:14:26 +02002938#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002939 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 ret = ssl_tls13_process_certificate_request(ssl);
Jerry Yud2674312021-10-29 10:08:19 +08002941 break;
2942
Jerry Yu687101b2021-09-14 16:03:56 +08002943 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 ret = ssl_tls13_process_server_certificate(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002945 break;
2946
2947 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 ret = ssl_tls13_process_certificate_verify(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002949 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002950#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002951
2952 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 ret = ssl_tls13_process_server_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002954 break;
2955
Xiaokang Qian125afcb2022-10-28 06:04:06 +00002956 case MBEDTLS_SSL_END_OF_EARLY_DATA:
2957 ret = ssl_tls13_write_end_of_early_data(ssl);
2958 break;
2959
Jerry Yu566c7812022-01-26 15:41:22 +08002960 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 ret = ssl_tls13_write_client_certificate(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002962 break;
2963
Ronald Cron928cbd32022-10-04 16:14:26 +02002964#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002965 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 ret = ssl_tls13_write_client_certificate_verify(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002967 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002968#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002969
Jerry Yu687101b2021-09-14 16:03:56 +08002970 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 ret = ssl_tls13_write_client_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002972 break;
2973
2974 case MBEDTLS_SSL_FLUSH_BUFFERS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 ret = ssl_tls13_flush_buffers(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002976 break;
2977
2978 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002980 break;
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 /*
2983 * Injection of dummy-CCS's for middlebox compatibility
2984 */
Ronald Cron49ad6192021-11-24 16:25:31 +01002985#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002986 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2988 if (ret == 0) {
2989 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2990 }
Ronald Cron9f55f632022-02-02 16:02:47 +01002991 break;
2992
2993 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2995 if (ret == 0) {
2996 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2997 }
Ronald Cron49ad6192021-11-24 16:25:31 +01002998 break;
Xiaokang Qianf6d8fd32023-02-02 02:46:26 +00002999
Xiaokang Qian592021a2023-01-04 10:47:05 +00003000 case MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO:
3001 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3002 if (ret == 0) {
3003 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
3004
Xiaokang Qian33ff8682023-01-10 06:32:12 +00003005#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qian592021a2023-01-04 10:47:05 +00003006 MBEDTLS_SSL_DEBUG_MSG(
3007 1, ("Switch to early data keys for outbound traffic"));
3008 mbedtls_ssl_set_outbound_transform(
3009 ssl, ssl->handshake->transform_earlydata);
Xiaokang Qian33ff8682023-01-10 06:32:12 +00003010#endif
Xiaokang Qian592021a2023-01-04 10:47:05 +00003011 }
3012 break;
Ronald Cron49ad6192021-11-24 16:25:31 +01003013#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3014
Jerry Yuf8a49942022-07-07 11:32:32 +00003015#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003016 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 ret = ssl_tls13_process_new_session_ticket(ssl);
3018 if (ret != 0) {
Jerry Yuf8a49942022-07-07 11:32:32 +00003019 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 }
Jerry Yuf8a49942022-07-07 11:32:32 +00003021 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
3022 break;
3023#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3024
Jerry Yu92c6b402021-08-27 16:59:09 +08003025 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu92c6b402021-08-27 16:59:09 +08003028 }
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return ret;
Jerry Yu92c6b402021-08-27 16:59:09 +08003031}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08003032
Jerry Yufb4b6472022-01-27 15:03:26 +08003033#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */