blob: e824a5431a5077b2488c5274a7259576709d88e6 [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 &&
679 session != NULL && session->ticket != NULL;
Xiaokang Qianb781a232022-11-01 07:39:46 +0000680}
681
Xiaokang Qian01323a42022-11-03 02:27:35 +0000682#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
Xiaokang Qian01323a42022-11-03 02:27:35 +0000684{
685 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return ssl->handshake->resume &&
687 session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
688 (session->ticket_flags &
689 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA) &&
690 mbedtls_ssl_tls13_cipher_suite_is_offered(
691 ssl, session->ciphersuite);
Xiaokang Qian01323a42022-11-03 02:27:35 +0000692}
693#endif
694
Xiaokang Qianb781a232022-11-01 07:39:46 +0000695MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
697 psa_algorithm_t *hash_alg,
698 const unsigned char **identity,
699 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800700{
701 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu8b41e892022-09-30 10:00:20 +0800702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (!ssl_tls13_has_configured_ticket(ssl)) {
704 return -1;
705 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yuf7c12592022-09-28 22:09:38 +0800708 *identity = session->ticket;
709 *identity_len = session->ticket_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800711}
712
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800713MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100714static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
715 psa_algorithm_t *hash_alg,
716 const unsigned char **psk,
717 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800718{
719
720 mbedtls_ssl_session *session = ssl->session_negotiate;
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (!ssl_tls13_has_configured_ticket(ssl)) {
723 return -1;
724 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800727 *psk = session->resumption_key;
728 *psk_len = session->resumption_key_len;
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800731}
Jerry Yuf7c12592022-09-28 22:09:38 +0800732#endif /* MBEDTLS_SSL_SESSION_TICKETS */
733
734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100735static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
736 psa_algorithm_t *hash_alg,
737 const unsigned char **identity,
738 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800739{
Jerry Yu8b41e892022-09-30 10:00:20 +0800740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
742 return -1;
743 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800744
Jerry Yua99cbfa2022-10-08 11:17:14 +0800745 *hash_alg = PSA_ALG_SHA_256;
Jerry Yuf7c12592022-09-28 22:09:38 +0800746 *identity = ssl->conf->psk_identity;
747 *identity_len = ssl->conf->psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800749}
750
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800751MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100752static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
753 psa_algorithm_t *hash_alg,
754 const unsigned char **psk,
755 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800756{
Jerry Yu8b41e892022-09-30 10:00:20 +0800757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
759 return -1;
760 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800761
Jerry Yua99cbfa2022-10-08 11:17:14 +0800762 *hash_alg = PSA_ALG_SHA_256;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800763 *psk = ssl->conf->psk;
764 *psk_len = ssl->conf->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800766}
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
Jerry Yuf75364b2022-09-30 10:30:31 +0800769{
770 int configured_psk_count = 0;
771#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (ssl_tls13_has_configured_ticket(ssl)) {
773 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800774 configured_psk_count++;
775 }
776#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
778 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800779 configured_psk_count++;
780 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return configured_psk_count;
Jerry Yuf75364b2022-09-30 10:30:31 +0800782}
783
Jerry Yuf7c12592022-09-28 22:09:38 +0800784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100785static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
786 unsigned char *buf,
787 unsigned char *end,
788 const unsigned char *identity,
789 size_t identity_len,
790 uint32_t obfuscated_ticket_age,
791 size_t *out_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800792{
Jerry Yuf75364b2022-09-30 10:30:31 +0800793 ((void) ssl);
Jerry Yuf7c12592022-09-28 22:09:38 +0800794 *out_len = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800795
796 /*
797 * - identity_len (2 bytes)
798 * - identity (psk_identity_len bytes)
799 * - obfuscated_ticket_age (4 bytes)
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
804 memcpy(buf + 2, identity, identity_len);
805 MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800808
809 *out_len = 6 + identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800812}
813
Jerry Yu8b41e892022-09-30 10:00:20 +0800814MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100815static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
816 unsigned char *buf,
817 unsigned char *end,
818 int psk_type,
819 psa_algorithm_t hash_alg,
820 const unsigned char *psk,
821 size_t psk_len,
822 size_t *out_len)
Jerry Yu8b41e892022-09-30 10:00:20 +0800823{
824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8b41e892022-09-30 10:00:20 +0800825 unsigned char binder_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8b41e892022-09-30 10:00:20 +0800827 size_t transcript_len = 0;
828
829 *out_len = 0;
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 binder_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu8b41e892022-09-30 10:00:20 +0800832
833 /*
834 * - binder_len (1 bytes)
835 * - binder (binder_len bytes)
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800838
Jerry Yua99cbfa2022-10-08 11:17:14 +0800839 buf[0] = binder_len;
Jerry Yu8b41e892022-09-30 10:00:20 +0800840
841 /* Get current state of handshake transcript. */
842 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 ssl, mbedtls_hash_info_md_from_psa(hash_alg),
844 transcript, sizeof(transcript), &transcript_len);
845 if (ret != 0) {
846 return ret;
Jerry Yu8b41e892022-09-30 10:00:20 +0800847 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100848
849 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
850 psk, psk_len, psk_type,
851 transcript, buf + 1);
852 if (ret != 0) {
853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
854 return ret;
855 }
856 MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800857
858 *out_len = 1 + binder_len;
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return 0;
Jerry Yu8b41e892022-09-30 10:00:20 +0800861}
862
863/*
864 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
865 *
866 * struct {
867 * opaque identity<1..2^16-1>;
868 * uint32 obfuscated_ticket_age;
869 * } PskIdentity;
870 *
871 * opaque PskBinderEntry<32..255>;
872 *
873 * struct {
874 * PskIdentity identities<7..2^16-1>;
875 * PskBinderEntry binders<33..2^16-1>;
876 * } OfferedPsks;
877 *
878 * struct {
879 * select (Handshake.msg_type) {
880 * case client_hello: OfferedPsks;
881 * ...
882 * };
883 * } PreSharedKeyExtension;
884 *
885 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000886int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
888 size_t *out_len, size_t *binders_len)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000889{
Jerry Yuf7c12592022-09-28 22:09:38 +0800890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf75364b2022-09-30 10:30:31 +0800891 int configured_psk_count = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800892 unsigned char *p = buf;
Jerry Yuf75364b2022-09-30 10:30:31 +0800893 psa_algorithm_t hash_alg;
894 const unsigned char *identity;
895 size_t identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800896 size_t l_binders_len = 0;
Jerry Yuf75364b2022-09-30 10:30:31 +0800897 size_t output_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800898
899 *out_len = 0;
900 *binders_len = 0;
901
902 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
904 if (configured_psk_count == 0) {
905 MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
906 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
910 configured_psk_count));
Jerry Yuf75364b2022-09-30 10:30:31 +0800911
Jerry Yuf7c12592022-09-28 22:09:38 +0800912 /* Check if we have space to write the extension, binders included.
913 * - extension_type (2 bytes)
914 * - extension_data_len (2 bytes)
915 * - identities_len (2 bytes)
916 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yuf7c12592022-09-28 22:09:38 +0800918 p += 6;
919
Jerry Yuf75364b2022-09-30 10:30:31 +0800920#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (ssl_tls13_ticket_get_identity(
922 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800923#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 mbedtls_time_t now = mbedtls_time(NULL);
Jerry Yuf75364b2022-09-30 10:30:31 +0800925 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu6916e702022-10-10 21:33:51 +0800926 uint32_t obfuscated_ticket_age =
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 (uint32_t) (now - session->ticket_received);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800928
Jerry Yu3e60cad2023-01-10 14:58:08 +0800929 /*
930 * The ticket timestamp is in seconds but the ticket age is in
931 * milliseconds. If the ticket was received at the end of a second and
932 * re-used here just at the beginning of the next second, the computed
933 * age `now - session->ticket_received` is equal to 1s thus 1000 ms
934 * while the actual age could be just a few milliseconds or tens of
935 * milliseconds. If the server has more accurate ticket timestamps
936 * (typically timestamps in milliseconds), as part of the processing of
937 * the ClientHello, it may compute a ticket lifetime smaller than the
938 * one computed here and potentially reject the ticket. To avoid that,
939 * remove one second to the ticket age if possible.
Jerry Yubdb936b2023-01-07 16:07:46 +0800940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (obfuscated_ticket_age > 0) {
Jerry Yubdb936b2023-01-07 16:07:46 +0800942 obfuscated_ticket_age -= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 }
Jerry Yubdb936b2023-01-07 16:07:46 +0800944
Jerry Yuf75364b2022-09-30 10:30:31 +0800945 obfuscated_ticket_age *= 1000;
946 obfuscated_ticket_age += session->ticket_age_add;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 ret = ssl_tls13_write_identity(ssl, p, end,
949 identity, identity_len,
950 obfuscated_ticket_age,
951 &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800952#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
954 0, &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800955#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (ret != 0) {
957 return ret;
958 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800959
Jerry Yuf75364b2022-09-30 10:30:31 +0800960 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf75364b2022-09-30 10:30:31 +0800962 }
963#endif /* MBEDTLS_SSL_SESSION_TICKETS */
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (ssl_tls13_psk_get_identity(
966 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
969 &output_len);
970 if (ret != 0) {
971 return ret;
972 }
Jerry Yuf75364b2022-09-30 10:30:31 +0800973
Jerry Yuf7c12592022-09-28 22:09:38 +0800974 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf7c12592022-09-28 22:09:38 +0800976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 MBEDTLS_SSL_DEBUG_MSG(3,
979 ("client hello, adding pre_shared_key extension, "
980 "omitting PSK binder list"));
Jerry Yua99cbfa2022-10-08 11:17:14 +0800981
982 /* Take into account the two bytes for the length of the binders. */
983 l_binders_len += 2;
Jerry Yu6916e702022-10-10 21:33:51 +0800984 /* Check if there is enough space for binders */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800986
Jerry Yuf7c12592022-09-28 22:09:38 +0800987 /*
988 * - extension_type (2 bytes)
989 * - extension_data_len (2 bytes)
990 * - identities_len (2 bytes)
991 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
993 MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
994 MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
Jerry Yuf7c12592022-09-28 22:09:38 +0800995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 *out_len = (p - buf) + l_binders_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800997 *binders_len = l_binders_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
Jerry Yuf7c12592022-09-28 22:09:38 +08001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001002}
1003
XiaokangQian86981952022-07-19 09:51:50 +00001004int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001006{
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1008 unsigned char *p = buf;
Jerry Yu6183cc72022-09-30 11:08:57 +08001009 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1010 const unsigned char *psk;
1011 size_t psk_len;
1012 size_t output_len;
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001013
1014 /* Check if we have space to write binders_len.
1015 * - binders_len (2 bytes)
1016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001018 p += 2;
1019
Jerry Yu6183cc72022-09-30 11:08:57 +08001020#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 ret = ssl_tls13_write_binder(ssl, p, end,
1024 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
1025 hash_alg, psk, psk_len,
1026 &output_len);
1027 if (ret != 0) {
1028 return ret;
1029 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001030 p += output_len;
1031 }
Jerry Yu6183cc72022-09-30 11:08:57 +08001032#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 ret = ssl_tls13_write_binder(ssl, p, end,
1037 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
1038 hash_alg, psk, psk_len,
1039 &output_len);
1040 if (ret != 0) {
1041 return ret;
1042 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001043 p += output_len;
1044 }
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001047
1048 /*
1049 * - binders_len (2 bytes)
1050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
XiaokangQianeb69aee2022-07-05 08:21:43 +00001054
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001055 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yu0c354a22022-08-29 15:25:36 +08001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001059}
Jerry Yu0c6105b2022-08-12 17:26:40 +08001060
1061/*
1062 * struct {
1063 * opaque identity<1..2^16-1>;
1064 * uint32 obfuscated_ticket_age;
1065 * } PskIdentity;
1066 *
1067 * opaque PskBinderEntry<32..255>;
1068 *
1069 * struct {
1070 *
1071 * select (Handshake.msg_type) {
1072 * ...
1073 * case server_hello: uint16 selected_identity;
1074 * };
1075 *
1076 * } PreSharedKeyExtension;
1077 *
1078 */
1079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001080static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
1081 const unsigned char *buf,
1082 const unsigned char *end)
Jerry Yu0c6105b2022-08-12 17:26:40 +08001083{
Jerry Yua99cbfa2022-10-08 11:17:14 +08001084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub300e3c2022-09-28 22:12:07 +08001085 int selected_identity;
Jerry Yub300e3c2022-09-28 22:12:07 +08001086 const unsigned char *psk;
1087 size_t psk_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001088 psa_algorithm_t hash_alg;
Jerry Yub300e3c2022-09-28 22:12:07 +08001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
1091 selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
Jerry Yua99cbfa2022-10-08 11:17:14 +08001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
Jerry Yua99cbfa2022-10-08 11:17:14 +08001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
1096 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
Jerry Yu4a698342022-09-30 12:22:01 +08001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1099 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1100 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yub300e3c2022-09-28 22:12:07 +08001101 }
Jerry Yua99cbfa2022-10-08 11:17:14 +08001102
Jerry Yu4a698342022-09-30 12:22:01 +08001103#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
1105 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1106 } else
Jerry Yu4a698342022-09-30 12:22:01 +08001107#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
1109 ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
1110 } else {
1111 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1112 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yub300e3c2022-09-28 22:12:07 +08001113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (ret != 0) {
1115 return ret;
Jerry Yub300e3c2022-09-28 22:12:07 +08001116 }
Jerry Yub300e3c2022-09-28 22:12:07 +08001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1119 if (ret != 0) {
1120 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1121 return ret;
1122 }
1123
1124 return 0;
Jerry Yu0c6105b2022-08-12 17:26:40 +08001125}
Ronald Cron41a443a2022-10-04 16:38:25 +02001126#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1129 unsigned char *buf,
1130 unsigned char *end,
1131 size_t *out_len)
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001132{
1133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1134 unsigned char *p = buf;
1135 size_t ext_len;
1136
1137 *out_len = 0;
1138
1139 /* Write supported_versions extension
1140 *
1141 * Supported Versions Extension is mandatory with TLS 1.3.
1142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
1144 if (ret != 0) {
1145 return ret;
1146 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001147 p += ext_len;
1148
1149 /* Echo the cookie if the server provided one in its preceding
1150 * HelloRetryRequest message.
1151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
1153 if (ret != 0) {
1154 return ret;
1155 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001156 p += ext_len;
1157
Ronald Cron766c0cd2022-10-18 12:17:11 +02001158#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1160 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
1161 if (ret != 0) {
1162 return ret;
1163 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001164 p += ext_len;
1165 }
Ronald Cron766c0cd2022-10-18 12:17:11 +02001166#endif
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001167
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001168#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1170 ssl_tls13_early_data_has_valid_ticket(ssl) &&
1171 ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
1172 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &ext_len);
1173 if (ret != 0) {
1174 return ret;
1175 }
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001176 p += ext_len;
1177
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001178 /* Initializes the status to `rejected`. It will be updated to
1179 * `accepted` if the EncryptedExtension message contain an early data
1180 * indication extension.
Xiaokang Qiana042b842022-11-09 01:59:33 +00001181 */
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001182 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 } else {
1184 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write early_data extension"));
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00001185 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001186 }
1187#endif /* MBEDTLS_SSL_EARLY_DATA */
1188
Ronald Cron41a443a2022-10-04 16:38:25 +02001189#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001190 /* For PSK-based key exchange we need the pre_shared_key extension
1191 * and the psk_key_exchange_modes extension.
1192 *
1193 * The pre_shared_key extension MUST be the last extension in the
1194 * ClientHello. Servers MUST check that it is the last extension and
1195 * otherwise fail the handshake with an "illegal_parameter" alert.
1196 *
1197 * Add the psk_key_exchange_modes extension.
1198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
1200 if (ret != 0) {
1201 return ret;
1202 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001203 p += ext_len;
Ronald Cron41a443a2022-10-04 16:38:25 +02001204#endif
XiaokangQianeb69aee2022-07-05 08:21:43 +00001205
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001206 *out_len = p - buf;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return 0;
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001209}
1210
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001211/*
Jerry Yu4a173382021-10-11 21:45:31 +08001212 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001213 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001214
Ronald Cronda41b382022-03-30 09:57:11 +02001215/**
1216 * \brief Detect if the ServerHello contains a supported_versions extension
1217 * or not.
1218 *
1219 * \param[in] ssl SSL context
1220 * \param[in] buf Buffer containing the ServerHello message
1221 * \param[in] end End of the buffer containing the ServerHello message
1222 *
1223 * \return 0 if the ServerHello does not contain a supported_versions extension
1224 * \return 1 if the ServerHello contains a supported_versions extension
1225 * \return A negative value if an error occurred while parsing the ServerHello.
1226 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001227MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001228static int ssl_tls13_is_supported_versions_ext_present(
1229 mbedtls_ssl_context *ssl,
1230 const unsigned char *buf,
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 const unsigned char *end)
Ronald Cron9f0fba32022-02-10 16:45:15 +01001232{
1233 const unsigned char *p = buf;
1234 size_t legacy_session_id_echo_len;
1235 size_t extensions_len;
1236 const unsigned char *extensions_end;
1237
1238 /*
1239 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001240 * length:
1241 * - legacy_version 2 bytes
1242 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1243 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001246 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1247 legacy_session_id_echo_len = *p;
1248
1249 /*
1250 * Jump to the extensions, jumping over:
1251 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1252 * - cipher_suite 2 bytes
1253 * - legacy_compression_method 1 byte
1254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
1256 p += legacy_session_id_echo_len + 4;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001257
1258 /* Case of no extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (p == end) {
1260 return 0;
1261 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001262
1263 /* ...
1264 * Extension extensions<6..2^16-1>;
1265 * ...
1266 * struct {
1267 * ExtensionType extension_type; (2 bytes)
1268 * opaque extension_data<0..2^16-1>;
1269 * } Extension;
1270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1272 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001273 p += 2;
1274
1275 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001277 extensions_end = p + extensions_len;
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 while (p < extensions_end) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01001280 unsigned int extension_type;
1281 size_t extension_data_len;
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1284 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1285 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001286 p += 4;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
1289 return 1;
1290 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001293 p += extension_data_len;
1294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 return 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001297}
1298
Jerry Yu7a186a02021-10-15 18:46:14 +08001299/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001300 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1301 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1302 * - 0 otherwise
1303 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001305static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
1306 const unsigned char *buf,
1307 const unsigned char *end)
Ronald Cronfd6193c2022-04-05 11:04:20 +02001308{
1309 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1310 static const unsigned char magic_downgrade_string[] =
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
Ronald Cronfd6193c2022-04-05 11:04:20 +02001312 const unsigned char *last_eight_bytes_of_random;
1313 unsigned char last_byte_of_random;
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
Ronald Cronfd6193c2022-04-05 11:04:20 +02001316 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (memcmp(last_eight_bytes_of_random,
1319 magic_downgrade_string,
1320 sizeof(magic_downgrade_string)) == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001321 last_byte_of_random = last_eight_bytes_of_random[7];
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return last_byte_of_random == 0 ||
1323 last_byte_of_random == 1;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001324 }
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 return 0;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001327}
1328
1329/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001330 * - SSL_SERVER_HELLO or
1331 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001332 * to indicate which message is expected and to be parsed next.
1333 */
Ronald Cron828aff62022-05-31 12:04:31 +02001334#define SSL_SERVER_HELLO 0
1335#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001337static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
1338 const unsigned char *buf,
1339 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001340{
Jerry Yue1b9c292021-09-10 10:08:31 +08001341
1342 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1343 *
Jerry Yu4a173382021-10-11 21:45:31 +08001344 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001345 * special value of the SHA-256 of "HelloRetryRequest".
1346 *
1347 * struct {
1348 * ProtocolVersion legacy_version = 0x0303;
1349 * Random random;
1350 * opaque legacy_session_id_echo<0..32>;
1351 * CipherSuite cipher_suite;
1352 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001353 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001354 * } ServerHello;
1355 *
1356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end,
1358 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
Jerry Yu4a173382021-10-11 21:45:31 +08001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1361 sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
1362 return SSL_SERVER_HELLO_HRR;
Jerry Yue1b9c292021-09-10 10:08:31 +08001363 }
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return SSL_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001366}
1367
Ronald Cron828aff62022-05-31 12:04:31 +02001368/*
Jerry Yu745bb612021-10-13 22:01:04 +08001369 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001370 * - SSL_SERVER_HELLO or
1371 * - SSL_SERVER_HELLO_HRR or
1372 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001373 */
Ronald Cron828aff62022-05-31 12:04:31 +02001374#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001375MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001376static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
1377 const unsigned char *buf,
1378 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001379{
Jerry Yu4a173382021-10-11 21:45:31 +08001380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001381 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
1384 ssl, buf, end));
Jerry Yua66fece2022-07-13 14:30:29 +08001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (ret == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001387 MBEDTLS_SSL_PROC_CHK_NEG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
Ronald Cronfd6193c2022-04-05 11:04:20 +02001389
1390 /* If the server is negotiating TLS 1.2 or below and:
1391 * . we did not propose TLS 1.2 or
1392 * . the server responded it is TLS 1.3 capable but negotiating a lower
1393 * version of the protocol and thus we are under downgrade attack
1394 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
1397 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1398 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1399 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001400 }
1401
1402 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001403 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1405 buf, (size_t) (end - buf));
Ronald Cron9f0fba32022-02-10 16:45:15 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1408 ret = ssl_tls13_reset_key_share(ssl);
1409 if (ret != 0) {
1410 return ret;
1411 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001412 }
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return SSL_SERVER_HELLO_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001415 }
1416
Jerry Yua66fece2022-07-13 14:30:29 +08001417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1418 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1419 ssl->session_negotiate->tls_version = ssl->tls_version;
1420#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1421
Jerry Yu50e00e32022-10-31 14:45:01 +08001422 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron5afb9042022-05-31 12:11:39 +02001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 ret = ssl_server_hello_is_hrr(ssl, buf, end);
1425 switch (ret) {
Ronald Cron828aff62022-05-31 12:04:31 +02001426 case SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
Jerry Yu745bb612021-10-13 22:01:04 +08001428 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001429 case SSL_SERVER_HELLO_HRR:
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
1431 /* If a client receives a second
1432 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1433 * was itself in response to a HelloRetryRequest), it MUST abort the
1434 * handshake with an "unexpected_message" alert.
1435 */
1436 if (handshake->hello_retry_request_count > 0) {
1437 MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
1438 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1439 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1440 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001441 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001442 /*
1443 * Clients must abort the handshake with an "illegal_parameter"
1444 * alert if the HelloRetryRequest would not result in any change
1445 * in the ClientHello.
1446 * In a PSK only key exchange that what we expect.
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1449 MBEDTLS_SSL_DEBUG_MSG(1,
1450 ("Unexpected HRR in pure PSK key exchange."));
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1453 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1454 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001456
Ronald Cron5afb9042022-05-31 12:11:39 +02001457 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001458
Jerry Yu745bb612021-10-13 22:01:04 +08001459 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001460 }
1461
1462cleanup:
1463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001465}
1466
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001468static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
1469 const unsigned char **buf,
1470 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001471{
1472 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001473 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1476 legacy_session_id_echo_len = *p++;
Jerry Yue1b9c292021-09-10 10:08:31 +08001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001479
1480 /* legacy_session_id_echo */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1482 memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
1483 MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
1484 ssl->session_negotiate->id,
1485 ssl->session_negotiate->id_len);
1486 MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
1487 legacy_session_id_echo_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1490 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
Jerry Yu4a173382021-10-11 21:45:31 +08001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001493 }
1494
Jerry Yu4a173382021-10-11 21:45:31 +08001495 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001496 *buf = p;
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
1499 ssl->session_negotiate->id_len);
1500 return 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001501}
1502
Jerry Yue1b9c292021-09-10 10:08:31 +08001503/* Parse ServerHello message and configure context
1504 *
1505 * struct {
1506 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1507 * Random random;
1508 * opaque legacy_session_id_echo<0..32>;
1509 * CipherSuite cipher_suite;
1510 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001511 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001512 * } ServerHello;
1513 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001515static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
1516 const unsigned char *buf,
1517 const unsigned char *end,
1518 int is_hrr)
Jerry Yue1b9c292021-09-10 10:08:31 +08001519{
Jerry Yub85277e2021-10-13 13:36:05 +08001520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001521 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001522 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001523 size_t extensions_len;
1524 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001525 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001526 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001527 int fatal_alert = 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001528 uint32_t allowed_extensions_mask;
Jerry Yu50e00e32022-10-31 14:45:01 +08001529 int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_HS_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001531
1532 /*
1533 * Check there is space for minimal fields
1534 *
1535 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001536 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001537 * - legacy_session_id_echo ( 1 byte ), minimum size
1538 * - cipher_suite ( 2 bytes)
1539 * - legacy_compression_method ( 1 byte )
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
Jerry Yue1b9c292021-09-10 10:08:31 +08001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
1544 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001545
Jerry Yu4a173382021-10-11 21:45:31 +08001546 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001547 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001548 * ...
1549 * with ProtocolVersion defined as:
1550 * uint16 ProtocolVersion;
1551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1553 MBEDTLS_SSL_VERSION_TLS1_2) {
1554 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1555 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1556 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001557 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1558 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001559 }
1560 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001561
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001562 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001563 * Random random;
1564 * ...
1565 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001566 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001567 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (!is_hrr) {
1569 memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1570 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1571 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
1572 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
XiaokangQian53f20b72022-01-18 10:47:33 +00001573 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001574 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001575
Jerry Yu4a173382021-10-11 21:45:31 +08001576 /* ...
1577 * opaque legacy_session_id_echo<0..32>;
1578 * ...
1579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
XiaokangQian52da5582022-01-26 09:49:29 +00001581 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001582 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001583 }
1584
Jerry Yu4a173382021-10-11 21:45:31 +08001585 /* ...
1586 * CipherSuite cipher_suite;
1587 * ...
1588 * with CipherSuite defined as:
1589 * uint8 CipherSuite[2];
1590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1592 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001593 p += 2;
1594
Jerry Yu4a173382021-10-11 21:45:31 +08001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Jerry Yu4a173382021-10-11 21:45:31 +08001597 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001598 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
1601 ssl->tls_version,
1602 ssl->tls_version) != 0) ||
1603 !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001604 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001605 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001606 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001607 * If we received an HRR before and that the proposed selected
1608 * ciphersuite in this server hello is not the same as the one
1609 * proposed in the HRR, we abort the handshake and send an
1610 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 else if ((!is_hrr) && (handshake->hello_retry_request_count > 0) &&
1613 (cipher_suite != ssl->session_negotiate->ciphersuite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001614 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001615 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1618 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
1619 cipher_suite));
XiaokangQiand59be772022-01-24 10:12:51 +00001620 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001621 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001622
Jerry Yu4a173382021-10-11 21:45:31 +08001623 /* Configure ciphersuites */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
Jerry Yu4a173382021-10-11 21:45:31 +08001625
XiaokangQian355e09a2022-01-20 11:14:50 +00001626 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001627 ssl->session_negotiate->ciphersuite = cipher_suite;
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
1630 cipher_suite, ciphersuite_info->name));
Jerry Yue1b9c292021-09-10 10:08:31 +08001631
1632#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 ssl->session_negotiate->start = time(NULL);
Jerry Yue1b9c292021-09-10 10:08:31 +08001634#endif /* MBEDTLS_HAVE_TIME */
1635
Jerry Yu4a173382021-10-11 21:45:31 +08001636 /* ...
1637 * uint8 legacy_compression_method = 0;
1638 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001639 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1641 if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
1642 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
XiaokangQian52da5582022-01-26 09:49:29 +00001643 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001644 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001645 }
1646 p++;
1647
Jerry Yub85277e2021-10-13 13:36:05 +08001648 /* ...
1649 * Extension extensions<6..2^16-1>;
1650 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001651 * struct {
1652 * ExtensionType extension_type; (2 bytes)
1653 * opaque extension_data<0..2^16-1>;
1654 * } Extension;
1655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1657 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001658 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001659
Jerry Yu4a173382021-10-11 21:45:31 +08001660 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001662 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001665
Jerry Yu50e00e32022-10-31 14:45:01 +08001666 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001667 allowed_extensions_mask = is_hrr ?
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
1669 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
Jerry Yu2c5363e2022-08-04 17:42:49 +08001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 while (p < extensions_end) {
Jerry Yue1b9c292021-09-10 10:08:31 +08001672 unsigned int extension_type;
1673 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001674 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1677 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1678 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001679 p += 4;
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian43550bd2022-01-21 04:32:58 +00001682 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001683
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001684 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 ssl, hs_msg_type, extension_type, allowed_extensions_mask);
1686 if (ret != 0) {
1687 return ret;
1688 }
Jerry Yu2c5363e2022-08-04 17:42:49 +08001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 switch (extension_type) {
XiaokangQianb851da82022-01-14 04:03:11 +00001691 case MBEDTLS_TLS_EXT_COOKIE:
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 ret = ssl_tls13_parse_cookie_ext(ssl,
1694 p, extension_data_end);
1695 if (ret != 0) {
1696 MBEDTLS_SSL_DEBUG_RET(1,
1697 "ssl_tls13_parse_cookie_ext",
1698 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001699 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001700 }
XiaokangQianb851da82022-01-14 04:03:11 +00001701 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001702
Jerry Yue1b9c292021-09-10 10:08:31 +08001703 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 ret = ssl_tls13_parse_supported_versions_ext(ssl,
1705 p,
1706 extension_data_end);
1707 if (ret != 0) {
XiaokangQiand59be772022-01-24 10:12:51 +00001708 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001710 break;
1711
Ronald Cron41a443a2022-10-04 16:38:25 +02001712#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001713 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
Jerry Yu4a173382021-10-11 21:45:31 +08001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
1717 ssl, p, extension_data_end)) != 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +00001718 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
1720 return ret;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001721 }
1722 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001723#endif
Jerry Yue1b9c292021-09-10 10:08:31 +08001724
Jerry Yue1b9c292021-09-10 10:08:31 +08001725 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
1727 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001728 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001729 goto cleanup;
1730 }
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (is_hrr) {
1733 ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
1734 p, extension_data_end);
1735 } else {
1736 ret = ssl_tls13_parse_key_share_ext(ssl,
1737 p, extension_data_end);
1738 }
1739 if (ret != 0) {
1740 MBEDTLS_SSL_DEBUG_RET(1,
1741 "ssl_tls13_parse_key_share_ext",
1742 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001743 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001744 }
1745 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001746
1747 default:
Jerry Yu9872eb22022-08-29 13:42:01 +08001748 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1749 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001750 }
1751
1752 p += extension_data_len;
1753 }
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
Jerry Yu2c5363e2022-08-04 17:42:49 +08001756
XiaokangQiand59be772022-01-24 10:12:51 +00001757cleanup:
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
1760 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1761 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001762 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1764 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1765 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
XiaokangQiand59be772022-01-24 10:12:51 +00001766 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001769}
1770
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001771#if defined(MBEDTLS_DEBUG_C)
1772static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001773{
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 switch (mode) {
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001775 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1776 return "psk";
1777 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1778 return "ephemeral";
1779 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1780 return "psk_ephemeral";
1781 default:
1782 return "unknown mode";
1783 }
1784}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001785#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001786
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001787MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001788static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue1b9c292021-09-10 10:08:31 +08001789{
Jerry Yub85277e2021-10-13 13:36:05 +08001790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001791 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001792
Jerry Yub85277e2021-10-13 13:36:05 +08001793 /* Determine the key exchange mode:
1794 * 1) If both the pre_shared_key and key_share extensions were received
1795 * then the key exchange mode is PSK with EPHEMERAL.
1796 * 2) If only the pre_shared_key extension was received then the key
1797 * exchange mode is PSK-only.
1798 * 3) If only the key_share extension was received then the key
1799 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 switch (handshake->received_extensions &
1802 (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
Jerry Yu745bb612021-10-13 22:01:04 +08001803 /* Only the pre_shared_key extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
Ronald Cron79907712022-07-20 17:05:29 +02001805 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001806 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001807
Jerry Yu745bb612021-10-13 22:01:04 +08001808 /* Only the key_share extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
Ronald Cron79907712022-07-20 17:05:29 +02001810 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001811 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001812
Jerry Yu745bb612021-10-13 22:01:04 +08001813 /* Both the pre_shared_key and key_share extensions were received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
Ronald Cron79907712022-07-20 17:05:29 +02001815 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001816 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001817
Jerry Yu745bb612021-10-13 22:01:04 +08001818 /* Neither pre_shared_key nor key_share extension was received */
1819 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
Jerry Yu745bb612021-10-13 22:01:04 +08001821 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1822 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001823 }
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (!mbedtls_ssl_conf_tls13_check_kex_modes(ssl, handshake->key_exchange_mode)) {
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001826 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 MBEDTLS_SSL_DEBUG_MSG(2,
1828 ("Key exchange mode(%s) is not supported.",
1829 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001830 goto cleanup;
1831 }
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 MBEDTLS_SSL_DEBUG_MSG(3,
1834 ("Selected key exchange mode: %s",
1835 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001836
Jerry Yu0b177842021-09-05 19:41:30 +08001837 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1838 *
1839 * TODO: We don't have to do this in case we offered 0-RTT and the
1840 * server accepted it. In this case, we could skip generating
1841 * the early secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1843 if (ret != 0) {
1844 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_key_schedule_stage_early",
1845 ret);
Jerry Yu4a173382021-10-11 21:45:31 +08001846 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001847 }
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
1850 if (ret != 0) {
1851 MBEDTLS_SSL_DEBUG_RET(1,
1852 "mbedtls_ssl_tls13_compute_handshake_transform",
1853 ret);
Jerry Yu4a173382021-10-11 21:45:31 +08001854 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001855 }
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
1858 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
Jerry Yu0b177842021-09-05 19:41:30 +08001859 ssl->session_in = ssl->session_negotiate;
1860
Jerry Yu4a173382021-10-11 21:45:31 +08001861cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (ret != 0) {
Jerry Yu4a173382021-10-11 21:45:31 +08001863 MBEDTLS_SSL_PEND_FATAL_ALERT(
1864 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yu4a173382021-10-11 21:45:31 +08001866 }
Jerry Yue110d252022-05-05 10:19:22 +08001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001869}
1870
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001871MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001872static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
XiaokangQian647719a2021-12-07 09:16:29 +00001873{
1874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
XiaokangQian647719a2021-12-07 09:16:29 +00001877
XiaokangQian78b1fa72022-01-19 06:56:30 +00001878 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001879 * We are going to re-generate a shared secret corresponding to the group
1880 * selected by the server, which is different from the group for which we
1881 * generated a shared secret in the first client hello.
1882 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 ret = ssl_tls13_reset_key_share(ssl);
1885 if (ret != 0) {
1886 return ret;
1887 }
XiaokangQian647719a2021-12-07 09:16:29 +00001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 return 0;
XiaokangQian647719a2021-12-07 09:16:29 +00001890}
1891
Jerry Yue1b9c292021-09-10 10:08:31 +08001892/*
Jerry Yu4a173382021-10-11 21:45:31 +08001893 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001894 * Handler for MBEDTLS_SSL_SERVER_HELLO
1895 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001896MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001897static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08001898{
Jerry Yu4a173382021-10-11 21:45:31 +08001899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001900 unsigned char *buf = NULL;
1901 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001902 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
Jerry Yue1b9c292021-09-10 10:08:31 +08001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
1907 MBEDTLS_SSL_HS_SERVER_HELLO,
1908 &buf, &buf_len));
Ronald Crondb5dfa12022-05-31 11:44:38 +02001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
1911 if (ret < 0) {
XiaokangQian16acd4b2022-01-14 07:35:47 +00001912 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 } else {
1914 is_hrr = (ret == SSL_SERVER_HELLO_HRR);
1915 }
XiaokangQiand59be772022-01-24 10:12:51 +00001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (ret == SSL_SERVER_HELLO_TLS1_2) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01001918 ret = 0;
1919 goto cleanup;
1920 }
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
1923 buf + buf_len,
1924 is_hrr));
1925 if (is_hrr) {
1926 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
Ronald Cronfb508b82022-05-31 14:49:55 +02001927 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001928
1929 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1930 buf, buf_len);
1931
1932 if (is_hrr) {
1933 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
1934#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1935 /* If not offering early data, the client sends a dummy CCS record
1936 * immediately before its second flight. This may either be before
1937 * its second ClientHello or before its encrypted handshake flight.
1938 */
1939 mbedtls_ssl_handshake_set_state(ssl,
1940 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
1941#else
1942 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
1943#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1944 } else {
1945 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
1946 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Ronald Cronfb508b82022-05-31 14:49:55 +02001947 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001948
1949cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
1951 is_hrr ? "HelloRetryRequest" : "ServerHello"));
1952 return ret;
Jerry Yu687101b2021-09-14 16:03:56 +08001953}
1954
1955/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001956 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001957 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001958 *
1959 * The EncryptedExtensions message contains any extensions which
1960 * should be protected, i.e., any which are not needed to establish
1961 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001962 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001963
XiaokangQian08da26c2021-10-09 10:12:11 +00001964/* Parse EncryptedExtensions message
1965 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001966 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001967 * } EncryptedExtensions;
1968 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001969MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001970static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
1971 const unsigned char *buf,
1972 const unsigned char *end)
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001973{
1974 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001975 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001976 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001977 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001978 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1981 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08da26c2021-10-09 10:12:11 +00001982 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
1985 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cronc8083592022-05-31 16:24:05 +02001986 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001987
Jerry Yu9eba7502022-10-31 13:46:16 +08001988 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yucbd082f2022-08-04 16:55:10 +08001989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 while (p < extensions_end) {
XiaokangQian08da26c2021-10-09 10:12:11 +00001991 unsigned int extension_type;
1992 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001993
XiaokangQian08da26c2021-10-09 10:12:11 +00001994 /*
1995 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001996 * ExtensionType extension_type; (2 bytes)
1997 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001998 * } Extension;
1999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2001 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2002 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian08da26c2021-10-09 10:12:11 +00002003 p += 4;
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002006
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002007 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
2009 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
2010 if (ret != 0) {
2011 return ret;
2012 }
Jerry Yucbd082f2022-08-04 16:55:10 +08002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 switch (extension_type) {
lhuang0486cacac2022-01-21 07:34:27 -08002015#if defined(MBEDTLS_SSL_ALPN)
2016 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
lhuang0486cacac2022-01-21 07:34:27 -08002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if ((ret = ssl_tls13_parse_alpn_ext(ssl, p, (size_t) extension_data_len)) != 0) {
2020 return ret;
lhuang0486cacac2022-01-21 07:34:27 -08002021 }
2022
2023 break;
2024#endif /* MBEDTLS_SSL_ALPN */
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002025
2026#if defined(MBEDTLS_SSL_EARLY_DATA)
2027 case MBEDTLS_TLS_EXT_EARLY_DATA:
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (extension_data_len != 0) {
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002030 /* The message must be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2032 MBEDTLS_ERR_SSL_DECODE_ERROR);
2033 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002034 }
2035
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002036 break;
2037#endif /* MBEDTLS_SSL_EARLY_DATA */
2038
XiaokangQian08da26c2021-10-09 10:12:11 +00002039 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002040 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu9eba7502022-10-31 13:46:16 +08002041 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 extension_type, "( ignored )");
Jerry Yucbd082f2022-08-04 16:55:10 +08002043 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00002044 }
2045
XiaokangQian08da26c2021-10-09 10:12:11 +00002046 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00002047 }
XiaokangQian08da26c2021-10-09 10:12:11 +00002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2050 handshake->received_extensions);
Jerry Yucbd082f2022-08-04 16:55:10 +08002051
XiaokangQian97799ac2021-10-11 10:05:54 +00002052 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (p != end) {
2054 MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
2055 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2056 MBEDTLS_ERR_SSL_DECODE_ERROR);
2057 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002058 }
2059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 return ret;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002061}
2062
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002064static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
Ronald Cron9d6a5452022-05-30 16:05:38 +02002065{
2066 int ret;
2067 unsigned char *buf;
2068 size_t buf_len;
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2073 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2074 &buf, &buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002075
2076 /* Process the message contents */
2077 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002079
Xiaokang Qianb157e912022-11-23 08:12:26 +00002080#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (ssl->handshake->received_extensions &
2082 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Xiaokang Qianb157e912022-11-23 08:12:26 +00002083 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
2084 }
2085#endif
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2088 buf, buf_len);
Ronald Cron9d6a5452022-05-30 16:05:38 +02002089
Ronald Cron928cbd32022-10-04 16:14:26 +02002090#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2092 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2093 } else {
2094 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2095 }
Ronald Cronfb508b82022-05-31 14:49:55 +02002096#else
2097 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Ronald Cronfb508b82022-05-31 14:49:55 +02002099#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02002100
2101cleanup:
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
2104 return ret;
Ronald Cron9d6a5452022-05-30 16:05:38 +02002105
2106}
2107
Ronald Cron928cbd32022-10-04 16:14:26 +02002108#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08002109/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002110 * STATE HANDLING: CertificateRequest
2111 *
Jerry Yud2674312021-10-29 10:08:19 +08002112 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002113#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2114#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002115/* Coordination:
2116 * Deals with the ambiguity of not knowing if a CertificateRequest
2117 * will be sent. Returns a negative code on failure, or
2118 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2119 * - SSL_CERTIFICATE_REQUEST_SKIP
2120 * indicating if a Certificate Request is expected or not.
2121 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
Jerry Yud2674312021-10-29 10:08:19 +08002124{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2128 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2129 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002130 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002131 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
2134 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
2135 MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
2136 return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
Jerry Yud2674312021-10-29 10:08:19 +08002137 }
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
Ronald Cron19385882022-06-15 16:26:13 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return SSL_CERTIFICATE_REQUEST_SKIP;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002142}
2143
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002144/*
2145 * ssl_tls13_parse_certificate_request()
2146 * Parse certificate request
2147 * struct {
2148 * opaque certificate_request_context<0..2^8-1>;
2149 * Extension extensions<2..2^16-1>;
2150 * } CertificateRequest;
2151 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002152MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002153static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
2154 const unsigned char *buf,
2155 const unsigned char *end)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002156{
2157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2158 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002159 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002160 size_t extensions_len = 0;
2161 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002162 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002163
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002164 /* ...
2165 * opaque certificate_request_context<0..2^8-1>
2166 * ...
2167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002169 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002170 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (certificate_request_context_len > 0) {
2173 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
2174 MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
2175 p, certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002176
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002177 handshake->certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 mbedtls_calloc(1, certificate_request_context_len);
2179 if (handshake->certificate_request_context == NULL) {
2180 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2181 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002182 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 memcpy(handshake->certificate_request_context, p,
2184 certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002185 p += certificate_request_context_len;
2186 }
2187
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002188 /* ...
2189 * Extension extensions<2..2^16-1>;
2190 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2193 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002194 p += 2;
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002197 extensions_end = p + extensions_len;
2198
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002199 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yuc55a6af2022-08-04 17:01:21 +08002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 while (p < extensions_end) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002202 unsigned int extension_type;
2203 size_t extension_data_len;
2204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2206 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2207 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002208 p += 4;
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002211
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002212 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
2214 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
2215 if (ret != 0) {
2216 return ret;
2217 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 switch (extension_type) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002220 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_DEBUG_MSG(3,
2222 ("found signature algorithms extension"));
2223 ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
2224 p + extension_data_len);
2225 if (ret != 0) {
2226 return ret;
2227 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002228
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002229 break;
2230
2231 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002232 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002233 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 extension_type, "( ignored )");
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002235 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002236 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002237
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002238 p += extension_data_len;
2239 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2242 handshake->received_extensions);
Jerry Yuc55a6af2022-08-04 17:01:21 +08002243
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002244 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (p != end) {
2246 MBEDTLS_SSL_DEBUG_MSG(1,
2247 ("CertificateRequest misaligned"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002248 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002249 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002250
Jerry Yu0c354a22022-08-29 15:25:36 +08002251 /* RFC 8446 section 4.3.2
Jerry Yuc55a6af2022-08-04 17:01:21 +08002252 *
2253 * The "signature_algorithms" extension MUST be specified
2254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
2256 MBEDTLS_SSL_DEBUG_MSG(3,
2257 ("no signature algorithms extension found"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002258 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002259 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002260
Jerry Yu7840f812022-01-29 10:26:51 +08002261 ssl->handshake->client_auth = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return 0;
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002263
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002264decode_error:
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2266 MBEDTLS_ERR_SSL_DECODE_ERROR);
2267 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002268}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002269
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002270/*
2271 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2272 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002274static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002275{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002283 unsigned char *buf;
2284 size_t buf_len;
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2287 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2288 &buf, &buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(ssl,
2291 buf, buf + buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2294 buf, buf_len);
2295 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002296 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 } else {
2298 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002299 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2300 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002301 }
2302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
Jerry Yud2674312021-10-29 10:08:19 +08002304
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002305cleanup:
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2308 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002309}
2310
2311/*
Jerry Yu687101b2021-09-14 16:03:56 +08002312 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2313 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002314MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002315static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002316{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002317 int ret;
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 ret = mbedtls_ssl_tls13_process_certificate(ssl);
2320 if (ret != 0) {
2321 return ret;
2322 }
Xiaofei Bai947571e2021-09-29 09:12:03 +00002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2325 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002326}
2327
2328/*
2329 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2330 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002331MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002332static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002333{
Jerry Yu30b071c2021-09-12 20:16:03 +08002334 int ret;
2335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
2337 if (ret != 0) {
2338 return ret;
2339 }
Jerry Yu30b071c2021-09-12 20:16:03 +08002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2342 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002343}
Ronald Cron928cbd32022-10-04 16:14:26 +02002344#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002345
Jerry Yu687101b2021-09-14 16:03:56 +08002346/*
2347 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2348 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002349MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002350static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002351{
XiaokangQianac0385c2021-11-03 06:40:11 +00002352 int ret;
2353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2355 if (ret != 0) {
2356 return ret;
2357 }
XiaokangQianac0385c2021-11-03 06:40:11 +00002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2360 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002361 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2363 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2364 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002365 }
2366
Ronald Cron49ad6192021-11-24 16:25:31 +01002367#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2368 mbedtls_ssl_handshake_set_state(
2369 ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
Ronald Cron49ad6192021-11-24 16:25:31 +01002371#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
Jerry Yu566c7812022-01-26 15:41:22 +08002373#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002376}
2377
2378/*
Jerry Yu566c7812022-01-26 15:41:22 +08002379 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2380 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002382static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002383{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002384 int non_empty_certificate_msg = 0;
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_DEBUG_MSG(1,
2387 ("Switch to handshake traffic keys for outbound traffic"));
2388 mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
Jerry Yu5cc35062022-01-28 16:16:08 +08002389
Ronald Cron928cbd32022-10-04 16:14:26 +02002390#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (ssl->handshake->client_auth) {
2392 int ret = mbedtls_ssl_tls13_write_certificate(ssl);
2393 if (ret != 0) {
2394 return ret;
2395 }
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (mbedtls_ssl_own_cert(ssl) != NULL) {
Ronald Cron7a94aca2022-03-09 07:44:27 +01002398 non_empty_certificate_msg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 }
2400 } else {
2401 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
Ronald Cron7a94aca2022-03-09 07:44:27 +01002402 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002403#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (non_empty_certificate_msg) {
2406 mbedtls_ssl_handshake_set_state(ssl,
2407 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2408 } else {
2409 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
2410 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2411 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return 0;
Jerry Yu566c7812022-01-26 15:41:22 +08002414}
2415
Ronald Cron928cbd32022-10-04 16:14:26 +02002416#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002417/*
2418 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2419 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002421static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002422{
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
Ronald Crona8b38872022-03-09 07:59:25 +01002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ret == 0) {
2426 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2427 }
Ronald Crona8b38872022-03-09 07:59:25 +01002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return ret;
Jerry Yu566c7812022-01-26 15:41:22 +08002430}
Ronald Cron928cbd32022-10-04 16:14:26 +02002431#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002432
2433/*
Jerry Yu687101b2021-09-14 16:03:56 +08002434 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2435 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002436MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002437static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002438{
XiaokangQian0fa66432021-11-15 03:33:57 +00002439 int ret;
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2442 if (ret != 0) {
2443 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002444 }
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2447 if (ret != 0) {
2448 MBEDTLS_SSL_DEBUG_RET(1,
2449 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
2450 return ret;
2451 }
2452
2453 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
2454 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002455}
2456
2457/*
2458 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2459 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002460MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002461static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002462{
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
2464 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2465 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002466}
2467
2468/*
2469 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2470 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002471MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002472static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002473{
Jerry Yu378254d2021-10-30 21:44:47 +08002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yu378254d2021-10-30 21:44:47 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2478 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002479}
2480
Jerry Yuf8a49942022-07-07 11:32:32 +00002481#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2482
Jerry Yua0446a02022-07-13 11:22:55 +08002483MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002484static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
2485 const unsigned char *buf,
2486 const unsigned char *end)
Jerry Yuf8a49942022-07-07 11:32:32 +00002487{
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002488 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002489 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002490
Jerry Yuf8a49942022-07-07 11:32:32 +00002491
Jerry Yuedab6372022-10-31 14:37:31 +08002492 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 while (p < end) {
Jerry Yuf8a49942022-07-07 11:32:32 +00002495 unsigned int extension_type;
2496 size_t extension_data_len;
Jerry Yu0c354a22022-08-29 15:25:36 +08002497 int ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
2500 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2501 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuf8a49942022-07-07 11:32:32 +00002502 p += 4;
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002505
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002506 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
2508 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
2509 if (ret != 0) {
2510 return ret;
2511 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 switch (extension_type) {
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002514#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002515 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (extension_data_len != 4) {
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002517 MBEDTLS_SSL_PEND_FATAL_ALERT(
2518 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 MBEDTLS_ERR_SSL_DECODE_ERROR);
2520 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002521 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (ssl->session != NULL) {
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002523 ssl->session->ticket_flags |=
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002525 }
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002526 break;
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002527#endif /* MBEDTLS_SSL_EARLY_DATA */
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002528
Jerry Yuf8a49942022-07-07 11:32:32 +00002529 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002530 MBEDTLS_SSL_PRINT_EXT(
Jerry Yuedab6372022-10-31 14:37:31 +08002531 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 extension_type, "( ignored )");
Jerry Yuf8a49942022-07-07 11:32:32 +00002533 break;
2534 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002535
Jerry Yuf8a49942022-07-07 11:32:32 +00002536 p += extension_data_len;
2537 }
2538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2540 handshake->received_extensions);
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002543}
2544
2545/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002546 * From RFC8446, page 74
2547 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002548 * struct {
2549 * uint32 ticket_lifetime;
2550 * uint32 ticket_age_add;
2551 * opaque ticket_nonce<0..255>;
2552 * opaque ticket<1..2^16-1>;
2553 * Extension extensions<0..2^16-2>;
2554 * } NewSessionTicket;
2555 *
2556 */
Jerry Yua0446a02022-07-13 11:22:55 +08002557MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002558static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
2559 unsigned char *buf,
2560 unsigned char *end,
2561 unsigned char **ticket_nonce,
2562 size_t *ticket_nonce_len)
Jerry Yuf8a49942022-07-07 11:32:32 +00002563{
2564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2565 unsigned char *p = buf;
2566 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002567 size_t ticket_len;
2568 unsigned char *ticket;
2569 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002570
Jerry Yucb3b1392022-07-12 06:09:38 +00002571 *ticket_nonce = NULL;
2572 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002573 /*
2574 * ticket_lifetime 4 bytes
2575 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002576 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
Jerry Yuf8a49942022-07-07 11:32:32 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
2581 MBEDTLS_SSL_DEBUG_MSG(3,
2582 ("ticket_lifetime: %u",
2583 (unsigned int) session->ticket_lifetime));
Jerry Yuf8a49942022-07-07 11:32:32 +00002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
2586 MBEDTLS_SSL_DEBUG_MSG(3,
2587 ("ticket_age_add: %u",
2588 (unsigned int) session->ticket_age_add));
Jerry Yuf8a49942022-07-07 11:32:32 +00002589
Jerry Yucb3b1392022-07-12 06:09:38 +00002590 *ticket_nonce_len = p[8];
2591 p += 9;
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002594 *ticket_nonce = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002596 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002597
2598 /* Ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2600 ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002601 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
2603 MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002604
2605 /* Check if we previously received a ticket already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (session->ticket != NULL || session->ticket_len > 0) {
2607 mbedtls_free(session->ticket);
Jerry Yuf8a49942022-07-07 11:32:32 +00002608 session->ticket = NULL;
2609 session->ticket_len = 0;
2610 }
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2613 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2614 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yuf8a49942022-07-07 11:32:32 +00002615 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 memcpy(ticket, p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002617 p += ticket_len;
2618 session->ticket = ticket;
2619 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002620
Pengyu Lv9f926952022-11-17 15:22:33 +08002621 /* Clear all flags in ticket_flags */
2622 mbedtls_ssl_tls13_session_clear_ticket_flags(session,
2623 MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2626 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002627 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
2633 if (ret != 0) {
2634 MBEDTLS_SSL_DEBUG_RET(1,
2635 "ssl_tls13_parse_new_session_ticket_exts",
2636 ret);
2637 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002638 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002639
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002640 /* session has been updated, allow export */
2641 session->exported = 0;
2642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return 0;
Jerry Yucb3b1392022-07-12 06:09:38 +00002644}
2645
Jerry Yua0446a02022-07-13 11:22:55 +08002646MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002647static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
2648 unsigned char *ticket_nonce,
2649 size_t ticket_nonce_len)
Jerry Yucb3b1392022-07-12 06:09:38 +00002650{
2651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2652 mbedtls_ssl_session *session = ssl->session;
2653 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2654 psa_algorithm_t psa_hash_alg;
2655 int hash_length;
2656
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002657#if defined(MBEDTLS_HAVE_TIME)
2658 /* Store ticket creation time */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 session->ticket_received = mbedtls_time(NULL);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002660#endif
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
2663 if (ciphersuite_info == NULL) {
2664 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2665 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf8a49942022-07-07 11:32:32 +00002666 }
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2669 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2670 if (hash_length == -1 ||
2671 (size_t) hash_length > sizeof(session->resumption_key)) {
2672 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu3afdf362022-07-20 17:34:14 +08002673 }
2674
Jerry Yuf8a49942022-07-07 11:32:32 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2677 session->app_secrets.resumption_master_secret,
2678 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002679
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002680 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002681 *
2682 * HKDF-Expand-Label( resumption_master_secret,
2683 * "resumption", ticket_nonce, Hash.length )
2684 */
2685 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 psa_hash_alg,
2687 session->app_secrets.resumption_master_secret,
2688 hash_length,
2689 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2690 ticket_nonce,
2691 ticket_nonce_len,
2692 session->resumption_key,
2693 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (ret != 0) {
2696 MBEDTLS_SSL_DEBUG_RET(2,
2697 "Creating the ticket-resumed PSK failed",
2698 ret);
2699 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002700 }
2701
Jerry Yu0a430c82022-07-20 11:02:48 +08002702 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2705 session->resumption_key,
2706 session->resumption_key_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002707
Pengyu Lv9f926952022-11-17 15:22:33 +08002708 /* Set ticket_flags depends on the selected key exchange modes */
2709 mbedtls_ssl_tls13_session_set_ticket_flags(session,
2710 ssl->conf->tls13_kex_modes);
2711 MBEDTLS_SSL_DEBUG_TICKET_FLAGS(4, session->ticket_flags);
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002714}
2715
2716/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002717 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002718 */
Jerry Yua0446a02022-07-13 11:22:55 +08002719MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002720static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuf8a49942022-07-07 11:32:32 +00002721{
2722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2723 unsigned char *buf;
2724 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002725 unsigned char *ticket_nonce;
2726 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Jerry Yuf8a49942022-07-07 11:32:32 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2731 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2732 &buf, &buf_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
2735 ssl, buf, buf + buf_len,
2736 &ticket_nonce, &ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_new_session_ticket(
2739 ssl, ticket_nonce, ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002742
Jerry Yuf8a49942022-07-07 11:32:32 +00002743cleanup:
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
2746 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002747}
2748#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
Jerry Yubc20bdd2021-08-24 15:59:48 +08002751{
Jerry Yu92c6b402021-08-27 16:59:09 +08002752 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 switch (ssl->state) {
Jerry Yu92c6b402021-08-27 16:59:09 +08002755 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Jerry Yuddda0502022-12-01 19:43:12 +08002757 break;
2758
Jerry Yu92c6b402021-08-27 16:59:09 +08002759 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 ret = mbedtls_ssl_write_client_hello(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002761 break;
2762
2763 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 ret = ssl_tls13_process_server_hello(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002765 break;
2766
2767 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 ret = ssl_tls13_process_encrypted_extensions(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002769 break;
2770
Ronald Cron928cbd32022-10-04 16:14:26 +02002771#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002772 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 ret = ssl_tls13_process_certificate_request(ssl);
Jerry Yud2674312021-10-29 10:08:19 +08002774 break;
2775
Jerry Yu687101b2021-09-14 16:03:56 +08002776 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 ret = ssl_tls13_process_server_certificate(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002778 break;
2779
2780 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 ret = ssl_tls13_process_certificate_verify(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002782 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002783#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002784
2785 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 ret = ssl_tls13_process_server_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002787 break;
2788
Jerry Yu566c7812022-01-26 15:41:22 +08002789 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 ret = ssl_tls13_write_client_certificate(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002791 break;
2792
Ronald Cron928cbd32022-10-04 16:14:26 +02002793#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002794 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 ret = ssl_tls13_write_client_certificate_verify(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002796 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002797#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002798
Jerry Yu687101b2021-09-14 16:03:56 +08002799 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 ret = ssl_tls13_write_client_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002801 break;
2802
2803 case MBEDTLS_SSL_FLUSH_BUFFERS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 ret = ssl_tls13_flush_buffers(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002805 break;
2806
2807 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002809 break;
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 /*
2812 * Injection of dummy-CCS's for middlebox compatibility
2813 */
Ronald Cron49ad6192021-11-24 16:25:31 +01002814#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002815 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2817 if (ret == 0) {
2818 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2819 }
Ronald Cron9f55f632022-02-02 16:02:47 +01002820 break;
2821
2822 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2824 if (ret == 0) {
2825 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2826 }
Ronald Cron49ad6192021-11-24 16:25:31 +01002827 break;
2828#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2829
Jerry Yuf8a49942022-07-07 11:32:32 +00002830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08002831 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 ret = ssl_tls13_process_new_session_ticket(ssl);
2833 if (ret != 0) {
Jerry Yuf8a49942022-07-07 11:32:32 +00002834 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 }
Jerry Yuf8a49942022-07-07 11:32:32 +00002836 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2837 break;
2838#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2839
Jerry Yu92c6b402021-08-27 16:59:09 +08002840 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
2842 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu92c6b402021-08-27 16:59:09 +08002843 }
2844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 return ret;
Jerry Yu92c6b402021-08-27 16:59:09 +08002846}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002847
Jerry Yufb4b6472022-01-27 15:03:26 +08002848#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */