blob: d5a41ce5ec5361477b905374b84849e04a14e95d [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)
Pengyu Lv93566782022-12-07 12:10:05 +0800675static int ssl_tls13_has_compat_ticket_flags(mbedtls_ssl_context *ssl)
676{
677 mbedtls_ssl_session *session = ssl->session_negotiate;
678 return session != NULL &&
679 mbedtls_ssl_conf_tls13_check_kex_modes(ssl,
680 mbedtls_ssl_tls13_session_get_ticket_flags(
681 session,
682 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL));
683}
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
Xiaokang Qianb781a232022-11-01 07:39:46 +0000686{
687 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return ssl->handshake->resume &&
Pengyu Lv93566782022-12-07 12:10:05 +0800689 session != NULL && session->ticket != NULL &&
690 ssl_tls13_has_compat_ticket_flags(ssl);
Xiaokang Qianb781a232022-11-01 07:39:46 +0000691}
692
Xiaokang Qian01323a42022-11-03 02:27:35 +0000693#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
Xiaokang Qian01323a42022-11-03 02:27:35 +0000695{
696 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return ssl->handshake->resume &&
698 session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
699 (session->ticket_flags &
700 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA) &&
701 mbedtls_ssl_tls13_cipher_suite_is_offered(
702 ssl, session->ciphersuite);
Xiaokang Qian01323a42022-11-03 02:27:35 +0000703}
704#endif
705
Xiaokang Qianb781a232022-11-01 07:39:46 +0000706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100707static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
708 psa_algorithm_t *hash_alg,
709 const unsigned char **identity,
710 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800711{
712 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu8b41e892022-09-30 10:00:20 +0800713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (!ssl_tls13_has_configured_ticket(ssl)) {
715 return -1;
716 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yuf7c12592022-09-28 22:09:38 +0800719 *identity = session->ticket;
720 *identity_len = session->ticket_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800722}
723
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800724MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100725static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
726 psa_algorithm_t *hash_alg,
727 const unsigned char **psk,
728 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800729{
730
731 mbedtls_ssl_session *session = ssl->session_negotiate;
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (!ssl_tls13_has_configured_ticket(ssl)) {
734 return -1;
735 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800738 *psk = session->resumption_key;
739 *psk_len = session->resumption_key_len;
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800742}
Jerry Yuf7c12592022-09-28 22:09:38 +0800743#endif /* MBEDTLS_SSL_SESSION_TICKETS */
744
745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100746static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
747 psa_algorithm_t *hash_alg,
748 const unsigned char **identity,
749 size_t *identity_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800750{
Jerry Yu8b41e892022-09-30 10:00:20 +0800751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
753 return -1;
754 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800755
Jerry Yua99cbfa2022-10-08 11:17:14 +0800756 *hash_alg = PSA_ALG_SHA_256;
Jerry Yuf7c12592022-09-28 22:09:38 +0800757 *identity = ssl->conf->psk_identity;
758 *identity_len = ssl->conf->psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800760}
761
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800762MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100763static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
764 psa_algorithm_t *hash_alg,
765 const unsigned char **psk,
766 size_t *psk_len)
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800767{
Jerry Yu8b41e892022-09-30 10:00:20 +0800768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
770 return -1;
771 }
Jerry Yu8b41e892022-09-30 10:00:20 +0800772
Jerry Yua99cbfa2022-10-08 11:17:14 +0800773 *hash_alg = PSA_ALG_SHA_256;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800774 *psk = ssl->conf->psk;
775 *psk_len = ssl->conf->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 return 0;
Jerry Yu1a0a0f42022-09-28 22:11:02 +0800777}
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
Jerry Yuf75364b2022-09-30 10:30:31 +0800780{
781 int configured_psk_count = 0;
782#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ssl_tls13_has_configured_ticket(ssl)) {
784 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800785 configured_psk_count++;
786 }
787#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
789 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
Jerry Yuf75364b2022-09-30 10:30:31 +0800790 configured_psk_count++;
791 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return configured_psk_count;
Jerry Yuf75364b2022-09-30 10:30:31 +0800793}
794
Jerry Yuf7c12592022-09-28 22:09:38 +0800795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
797 unsigned char *buf,
798 unsigned char *end,
799 const unsigned char *identity,
800 size_t identity_len,
801 uint32_t obfuscated_ticket_age,
802 size_t *out_len)
Jerry Yuf7c12592022-09-28 22:09:38 +0800803{
Jerry Yuf75364b2022-09-30 10:30:31 +0800804 ((void) ssl);
Jerry Yuf7c12592022-09-28 22:09:38 +0800805 *out_len = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800806
807 /*
808 * - identity_len (2 bytes)
809 * - identity (psk_identity_len bytes)
810 * - obfuscated_ticket_age (4 bytes)
811 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
815 memcpy(buf + 2, identity, identity_len);
816 MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
Jerry Yuf7c12592022-09-28 22:09:38 +0800819
820 *out_len = 6 + identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800823}
824
Jerry Yu8b41e892022-09-30 10:00:20 +0800825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100826static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
827 unsigned char *buf,
828 unsigned char *end,
829 int psk_type,
830 psa_algorithm_t hash_alg,
831 const unsigned char *psk,
832 size_t psk_len,
833 size_t *out_len)
Jerry Yu8b41e892022-09-30 10:00:20 +0800834{
835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8b41e892022-09-30 10:00:20 +0800836 unsigned char binder_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8b41e892022-09-30 10:00:20 +0800838 size_t transcript_len = 0;
839
840 *out_len = 0;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 binder_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu8b41e892022-09-30 10:00:20 +0800843
844 /*
845 * - binder_len (1 bytes)
846 * - binder (binder_len bytes)
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800849
Jerry Yua99cbfa2022-10-08 11:17:14 +0800850 buf[0] = binder_len;
Jerry Yu8b41e892022-09-30 10:00:20 +0800851
852 /* Get current state of handshake transcript. */
853 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 ssl, mbedtls_hash_info_md_from_psa(hash_alg),
855 transcript, sizeof(transcript), &transcript_len);
856 if (ret != 0) {
857 return ret;
Jerry Yu8b41e892022-09-30 10:00:20 +0800858 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100859
860 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
861 psk, psk_len, psk_type,
862 transcript, buf + 1);
863 if (ret != 0) {
864 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
865 return ret;
866 }
867 MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
Jerry Yu8b41e892022-09-30 10:00:20 +0800868
869 *out_len = 1 + binder_len;
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return 0;
Jerry Yu8b41e892022-09-30 10:00:20 +0800872}
873
874/*
875 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
876 *
877 * struct {
878 * opaque identity<1..2^16-1>;
879 * uint32 obfuscated_ticket_age;
880 * } PskIdentity;
881 *
882 * opaque PskBinderEntry<32..255>;
883 *
884 * struct {
885 * PskIdentity identities<7..2^16-1>;
886 * PskBinderEntry binders<33..2^16-1>;
887 * } OfferedPsks;
888 *
889 * struct {
890 * select (Handshake.msg_type) {
891 * case client_hello: OfferedPsks;
892 * ...
893 * };
894 * } PreSharedKeyExtension;
895 *
896 */
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000897int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
899 size_t *out_len, size_t *binders_len)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000900{
Jerry Yuf7c12592022-09-28 22:09:38 +0800901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf75364b2022-09-30 10:30:31 +0800902 int configured_psk_count = 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800903 unsigned char *p = buf;
Jerry Yuf75364b2022-09-30 10:30:31 +0800904 psa_algorithm_t hash_alg;
905 const unsigned char *identity;
906 size_t identity_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800907 size_t l_binders_len = 0;
Jerry Yuf75364b2022-09-30 10:30:31 +0800908 size_t output_len;
Jerry Yuf7c12592022-09-28 22:09:38 +0800909
910 *out_len = 0;
911 *binders_len = 0;
912
913 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
915 if (configured_psk_count == 0) {
916 MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
917 return 0;
Jerry Yuf7c12592022-09-28 22:09:38 +0800918 }
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
921 configured_psk_count));
Jerry Yuf75364b2022-09-30 10:30:31 +0800922
Jerry Yuf7c12592022-09-28 22:09:38 +0800923 /* Check if we have space to write the extension, binders included.
924 * - extension_type (2 bytes)
925 * - extension_data_len (2 bytes)
926 * - identities_len (2 bytes)
927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yuf7c12592022-09-28 22:09:38 +0800929 p += 6;
930
Jerry Yuf75364b2022-09-30 10:30:31 +0800931#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (ssl_tls13_ticket_get_identity(
933 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800934#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_time_t now = mbedtls_time(NULL);
Jerry Yuf75364b2022-09-30 10:30:31 +0800936 mbedtls_ssl_session *session = ssl->session_negotiate;
Jerry Yu6916e702022-10-10 21:33:51 +0800937 uint32_t obfuscated_ticket_age =
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 (uint32_t) (now - session->ticket_received);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800939
Jerry Yu3e60cad2023-01-10 14:58:08 +0800940 /*
941 * The ticket timestamp is in seconds but the ticket age is in
942 * milliseconds. If the ticket was received at the end of a second and
943 * re-used here just at the beginning of the next second, the computed
944 * age `now - session->ticket_received` is equal to 1s thus 1000 ms
945 * while the actual age could be just a few milliseconds or tens of
946 * milliseconds. If the server has more accurate ticket timestamps
947 * (typically timestamps in milliseconds), as part of the processing of
948 * the ClientHello, it may compute a ticket lifetime smaller than the
949 * one computed here and potentially reject the ticket. To avoid that,
950 * remove one second to the ticket age if possible.
Jerry Yubdb936b2023-01-07 16:07:46 +0800951 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (obfuscated_ticket_age > 0) {
Jerry Yubdb936b2023-01-07 16:07:46 +0800953 obfuscated_ticket_age -= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Jerry Yubdb936b2023-01-07 16:07:46 +0800955
Jerry Yuf75364b2022-09-30 10:30:31 +0800956 obfuscated_ticket_age *= 1000;
957 obfuscated_ticket_age += session->ticket_age_add;
Jerry Yua99cbfa2022-10-08 11:17:14 +0800958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 ret = ssl_tls13_write_identity(ssl, p, end,
960 identity, identity_len,
961 obfuscated_ticket_age,
962 &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800963#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
965 0, &output_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800966#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (ret != 0) {
968 return ret;
969 }
Jerry Yuf7c12592022-09-28 22:09:38 +0800970
Jerry Yuf75364b2022-09-30 10:30:31 +0800971 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf75364b2022-09-30 10:30:31 +0800973 }
974#endif /* MBEDTLS_SSL_SESSION_TICKETS */
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (ssl_tls13_psk_get_identity(
977 ssl, &hash_alg, &identity, &identity_len) == 0) {
Jerry Yuf75364b2022-09-30 10:30:31 +0800978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
980 &output_len);
981 if (ret != 0) {
982 return ret;
983 }
Jerry Yuf75364b2022-09-30 10:30:31 +0800984
Jerry Yuf7c12592022-09-28 22:09:38 +0800985 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
Jerry Yuf7c12592022-09-28 22:09:38 +0800987 }
988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 MBEDTLS_SSL_DEBUG_MSG(3,
990 ("client hello, adding pre_shared_key extension, "
991 "omitting PSK binder list"));
Jerry Yua99cbfa2022-10-08 11:17:14 +0800992
993 /* Take into account the two bytes for the length of the binders. */
994 l_binders_len += 2;
Jerry Yu6916e702022-10-10 21:33:51 +0800995 /* Check if there is enough space for binders */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
Jerry Yua99cbfa2022-10-08 11:17:14 +0800997
Jerry Yuf7c12592022-09-28 22:09:38 +0800998 /*
999 * - extension_type (2 bytes)
1000 * - extension_data_len (2 bytes)
1001 * - identities_len (2 bytes)
1002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
1004 MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
1005 MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
Jerry Yuf7c12592022-09-28 22:09:38 +08001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 *out_len = (p - buf) + l_binders_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001008 *binders_len = l_binders_len;
Jerry Yuf7c12592022-09-28 22:09:38 +08001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
Jerry Yuf7c12592022-09-28 22:09:38 +08001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001013}
1014
XiaokangQian86981952022-07-19 09:51:50 +00001015int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001017{
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1019 unsigned char *p = buf;
Jerry Yu6183cc72022-09-30 11:08:57 +08001020 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1021 const unsigned char *psk;
1022 size_t psk_len;
1023 size_t output_len;
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001024
1025 /* Check if we have space to write binders_len.
1026 * - binders_len (2 bytes)
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001029 p += 2;
1030
Jerry Yu6183cc72022-09-30 11:08:57 +08001031#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 ret = ssl_tls13_write_binder(ssl, p, end,
1035 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
1036 hash_alg, psk, psk_len,
1037 &output_len);
1038 if (ret != 0) {
1039 return ret;
1040 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001041 p += output_len;
1042 }
Jerry Yu6183cc72022-09-30 11:08:57 +08001043#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
Jerry Yu6183cc72022-09-30 11:08:57 +08001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ret = ssl_tls13_write_binder(ssl, p, end,
1048 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
1049 hash_alg, psk, psk_len,
1050 &output_len);
1051 if (ret != 0) {
1052 return ret;
1053 }
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001054 p += output_len;
1055 }
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001058
1059 /*
1060 * - binders_len (2 bytes)
1061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
Jerry Yu1a0a0f42022-09-28 22:11:02 +08001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
XiaokangQianeb69aee2022-07-05 08:21:43 +00001065
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001066 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yu0c354a22022-08-29 15:25:36 +08001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001070}
Jerry Yu0c6105b2022-08-12 17:26:40 +08001071
1072/*
1073 * struct {
1074 * opaque identity<1..2^16-1>;
1075 * uint32 obfuscated_ticket_age;
1076 * } PskIdentity;
1077 *
1078 * opaque PskBinderEntry<32..255>;
1079 *
1080 * struct {
1081 *
1082 * select (Handshake.msg_type) {
1083 * ...
1084 * case server_hello: uint16 selected_identity;
1085 * };
1086 *
1087 * } PreSharedKeyExtension;
1088 *
1089 */
1090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001091static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
1092 const unsigned char *buf,
1093 const unsigned char *end)
Jerry Yu0c6105b2022-08-12 17:26:40 +08001094{
Jerry Yua99cbfa2022-10-08 11:17:14 +08001095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub300e3c2022-09-28 22:12:07 +08001096 int selected_identity;
Jerry Yub300e3c2022-09-28 22:12:07 +08001097 const unsigned char *psk;
1098 size_t psk_len;
Jerry Yua99cbfa2022-10-08 11:17:14 +08001099 psa_algorithm_t hash_alg;
Jerry Yub300e3c2022-09-28 22:12:07 +08001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
1102 selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
Jerry Yua99cbfa2022-10-08 11:17:14 +08001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
Jerry Yua99cbfa2022-10-08 11:17:14 +08001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
Jerry Yu4a698342022-09-30 12:22:01 +08001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1111 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yub300e3c2022-09-28 22:12:07 +08001112 }
Jerry Yua99cbfa2022-10-08 11:17:14 +08001113
Jerry Yu4a698342022-09-30 12:22:01 +08001114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
1116 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1117 } else
Jerry Yu4a698342022-09-30 12:22:01 +08001118#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
1120 ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
1121 } else {
1122 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1123 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yub300e3c2022-09-28 22:12:07 +08001124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (ret != 0) {
1126 return ret;
Jerry Yub300e3c2022-09-28 22:12:07 +08001127 }
Jerry Yub300e3c2022-09-28 22:12:07 +08001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1130 if (ret != 0) {
1131 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1132 return ret;
1133 }
1134
1135 return 0;
Jerry Yu0c6105b2022-08-12 17:26:40 +08001136}
Ronald Cron41a443a2022-10-04 16:38:25 +02001137#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1140 unsigned char *buf,
1141 unsigned char *end,
1142 size_t *out_len)
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001143{
1144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1145 unsigned char *p = buf;
1146 size_t ext_len;
1147
1148 *out_len = 0;
1149
1150 /* Write supported_versions extension
1151 *
1152 * Supported Versions Extension is mandatory with TLS 1.3.
1153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
1155 if (ret != 0) {
1156 return ret;
1157 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001158 p += ext_len;
1159
1160 /* Echo the cookie if the server provided one in its preceding
1161 * HelloRetryRequest message.
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
1164 if (ret != 0) {
1165 return ret;
1166 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001167 p += ext_len;
1168
Ronald Cron766c0cd2022-10-18 12:17:11 +02001169#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1171 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
1172 if (ret != 0) {
1173 return ret;
1174 }
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001175 p += ext_len;
1176 }
Ronald Cron766c0cd2022-10-18 12:17:11 +02001177#endif
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001178
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001179#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1181 ssl_tls13_early_data_has_valid_ticket(ssl) &&
1182 ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
1183 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &ext_len);
1184 if (ret != 0) {
1185 return ret;
1186 }
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001187 p += ext_len;
1188
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001189 /* Initializes the status to `rejected`. It will be updated to
1190 * `accepted` if the EncryptedExtension message contain an early data
1191 * indication extension.
Xiaokang Qiana042b842022-11-09 01:59:33 +00001192 */
Ronald Cron4a8c9e22022-10-26 18:49:09 +02001193 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 } else {
1195 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write early_data extension"));
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00001196 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001197 }
1198#endif /* MBEDTLS_SSL_EARLY_DATA */
1199
Ronald Cron41a443a2022-10-04 16:38:25 +02001200#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +00001201 /* For PSK-based key exchange we need the pre_shared_key extension
1202 * and the psk_key_exchange_modes extension.
1203 *
1204 * The pre_shared_key extension MUST be the last extension in the
1205 * ClientHello. Servers MUST check that it is the last extension and
1206 * otherwise fail the handshake with an "illegal_parameter" alert.
1207 *
1208 * Add the psk_key_exchange_modes extension.
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
1211 if (ret != 0) {
1212 return ret;
1213 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001214 p += ext_len;
Ronald Cron41a443a2022-10-04 16:38:25 +02001215#endif
XiaokangQianeb69aee2022-07-05 08:21:43 +00001216
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001217 *out_len = p - buf;
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return 0;
Ronald Cron04fbd2b2022-02-18 12:06:07 +01001220}
1221
Jerry Yu1bc2c1f2021-09-01 12:57:29 +08001222/*
Jerry Yu4a173382021-10-11 21:45:31 +08001223 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001224 */
Ronald Cronfd6193c2022-04-05 11:04:20 +02001225
Ronald Cronda41b382022-03-30 09:57:11 +02001226/**
1227 * \brief Detect if the ServerHello contains a supported_versions extension
1228 * or not.
1229 *
1230 * \param[in] ssl SSL context
1231 * \param[in] buf Buffer containing the ServerHello message
1232 * \param[in] end End of the buffer containing the ServerHello message
1233 *
1234 * \return 0 if the ServerHello does not contain a supported_versions extension
1235 * \return 1 if the ServerHello contains a supported_versions extension
1236 * \return A negative value if an error occurred while parsing the ServerHello.
1237 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001238MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron9f0fba32022-02-10 16:45:15 +01001239static int ssl_tls13_is_supported_versions_ext_present(
1240 mbedtls_ssl_context *ssl,
1241 const unsigned char *buf,
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 const unsigned char *end)
Ronald Cron9f0fba32022-02-10 16:45:15 +01001243{
1244 const unsigned char *p = buf;
1245 size_t legacy_session_id_echo_len;
1246 size_t extensions_len;
1247 const unsigned char *extensions_end;
1248
1249 /*
1250 * Check there is enough data to access the legacy_session_id_echo vector
Ronald Cronda41b382022-03-30 09:57:11 +02001251 * length:
1252 * - legacy_version 2 bytes
1253 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1254 * - legacy_session_id_echo length 1 byte
Ronald Cron9f0fba32022-02-10 16:45:15 +01001255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001257 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1258 legacy_session_id_echo_len = *p;
1259
1260 /*
1261 * Jump to the extensions, jumping over:
1262 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1263 * - cipher_suite 2 bytes
1264 * - legacy_compression_method 1 byte
1265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
1267 p += legacy_session_id_echo_len + 4;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001268
1269 /* Case of no extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (p == end) {
1271 return 0;
1272 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001273
1274 /* ...
1275 * Extension extensions<6..2^16-1>;
1276 * ...
1277 * struct {
1278 * ExtensionType extension_type; (2 bytes)
1279 * opaque extension_data<0..2^16-1>;
1280 * } Extension;
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1283 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001284 p += 2;
1285
1286 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001288 extensions_end = p + extensions_len;
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 while (p < extensions_end) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01001291 unsigned int extension_type;
1292 size_t extension_data_len;
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1295 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1296 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001297 p += 4;
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
1300 return 1;
1301 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Ronald Cron9f0fba32022-02-10 16:45:15 +01001304 p += extension_data_len;
1305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 return 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001308}
1309
Jerry Yu7a186a02021-10-15 18:46:14 +08001310/* Returns a negative value on failure, and otherwise
Ronald Cronfd6193c2022-04-05 11:04:20 +02001311 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1312 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1313 * - 0 otherwise
1314 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001316static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
1317 const unsigned char *buf,
1318 const unsigned char *end)
Ronald Cronfd6193c2022-04-05 11:04:20 +02001319{
1320 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1321 static const unsigned char magic_downgrade_string[] =
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
Ronald Cronfd6193c2022-04-05 11:04:20 +02001323 const unsigned char *last_eight_bytes_of_random;
1324 unsigned char last_byte_of_random;
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
Ronald Cronfd6193c2022-04-05 11:04:20 +02001327 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (memcmp(last_eight_bytes_of_random,
1330 magic_downgrade_string,
1331 sizeof(magic_downgrade_string)) == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001332 last_byte_of_random = last_eight_bytes_of_random[7];
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return last_byte_of_random == 0 ||
1334 last_byte_of_random == 1;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001335 }
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return 0;
Ronald Cronfd6193c2022-04-05 11:04:20 +02001338}
1339
1340/* Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001341 * - SSL_SERVER_HELLO or
1342 * - SSL_SERVER_HELLO_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001343 * to indicate which message is expected and to be parsed next.
1344 */
Ronald Cron828aff62022-05-31 12:04:31 +02001345#define SSL_SERVER_HELLO 0
1346#define SSL_SERVER_HELLO_HRR 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001348static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
1349 const unsigned char *buf,
1350 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001351{
Jerry Yue1b9c292021-09-10 10:08:31 +08001352
1353 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1354 *
Jerry Yu4a173382021-10-11 21:45:31 +08001355 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001356 * special value of the SHA-256 of "HelloRetryRequest".
1357 *
1358 * struct {
1359 * ProtocolVersion legacy_version = 0x0303;
1360 * Random random;
1361 * opaque legacy_session_id_echo<0..32>;
1362 * CipherSuite cipher_suite;
1363 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001364 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001365 * } ServerHello;
1366 *
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end,
1369 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
Jerry Yu4a173382021-10-11 21:45:31 +08001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1372 sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
1373 return SSL_SERVER_HELLO_HRR;
Jerry Yue1b9c292021-09-10 10:08:31 +08001374 }
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return SSL_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001377}
1378
Ronald Cron828aff62022-05-31 12:04:31 +02001379/*
Jerry Yu745bb612021-10-13 22:01:04 +08001380 * Returns a negative value on failure, and otherwise
Ronald Cron828aff62022-05-31 12:04:31 +02001381 * - SSL_SERVER_HELLO or
1382 * - SSL_SERVER_HELLO_HRR or
1383 * - SSL_SERVER_HELLO_TLS1_2
Jerry Yu745bb612021-10-13 22:01:04 +08001384 */
Ronald Cron828aff62022-05-31 12:04:31 +02001385#define SSL_SERVER_HELLO_TLS1_2 2
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001386MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001387static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
1388 const unsigned char *buf,
1389 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001390{
Jerry Yu4a173382021-10-11 21:45:31 +08001391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5afb9042022-05-31 12:11:39 +02001392 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yue1b9c292021-09-10 10:08:31 +08001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
1395 ssl, buf, end));
Jerry Yua66fece2022-07-13 14:30:29 +08001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (ret == 0) {
Ronald Cronfd6193c2022-04-05 11:04:20 +02001398 MBEDTLS_SSL_PROC_CHK_NEG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
Ronald Cronfd6193c2022-04-05 11:04:20 +02001400
1401 /* If the server is negotiating TLS 1.2 or below and:
1402 * . we did not propose TLS 1.2 or
1403 * . the server responded it is TLS 1.3 capable but negotiating a lower
1404 * version of the protocol and thus we are under downgrade attack
1405 * abort the handshake with an "illegal parameter" alert.
Ronald Cron9f0fba32022-02-10 16:45:15 +01001406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
1408 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1409 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1410 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001411 }
1412
1413 ssl->keep_current_message = 1;
Glenn Strauss60bfe602022-03-14 19:04:24 -04001414 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1416 buf, (size_t) (end - buf));
Ronald Cron9f0fba32022-02-10 16:45:15 +01001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1419 ret = ssl_tls13_reset_key_share(ssl);
1420 if (ret != 0) {
1421 return ret;
1422 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01001423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return SSL_SERVER_HELLO_TLS1_2;
Ronald Cron9f0fba32022-02-10 16:45:15 +01001426 }
1427
Jerry Yua66fece2022-07-13 14:30:29 +08001428#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1429 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1430 ssl->session_negotiate->tls_version = ssl->tls_version;
1431#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1432
Jerry Yu50e00e32022-10-31 14:45:01 +08001433 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron5afb9042022-05-31 12:11:39 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 ret = ssl_server_hello_is_hrr(ssl, buf, end);
1436 switch (ret) {
Ronald Cron828aff62022-05-31 12:04:31 +02001437 case SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
Jerry Yu745bb612021-10-13 22:01:04 +08001439 break;
Ronald Cron828aff62022-05-31 12:04:31 +02001440 case SSL_SERVER_HELLO_HRR:
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
1442 /* If a client receives a second
1443 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1444 * was itself in response to a HelloRetryRequest), it MUST abort the
1445 * handshake with an "unexpected_message" alert.
1446 */
1447 if (handshake->hello_retry_request_count > 0) {
1448 MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
1449 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1450 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1451 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001452 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453 /*
1454 * Clients must abort the handshake with an "illegal_parameter"
1455 * alert if the HelloRetryRequest would not result in any change
1456 * in the ClientHello.
1457 * In a PSK only key exchange that what we expect.
1458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
1460 MBEDTLS_SSL_DEBUG_MSG(1,
1461 ("Unexpected HRR in pure PSK key exchange."));
XiaokangQian2b01dc32022-01-21 02:53:13 +00001462 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1464 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1465 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian2b01dc32022-01-21 02:53:13 +00001466 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001467
Ronald Cron5afb9042022-05-31 12:11:39 +02001468 handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001469
Jerry Yu745bb612021-10-13 22:01:04 +08001470 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001471 }
1472
1473cleanup:
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001476}
1477
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001479static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
1480 const unsigned char **buf,
1481 const unsigned char *end)
Jerry Yue1b9c292021-09-10 10:08:31 +08001482{
1483 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001484 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1487 legacy_session_id_echo_len = *p++;
Jerry Yue1b9c292021-09-10 10:08:31 +08001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001490
1491 /* legacy_session_id_echo */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1493 memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
1494 MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
1495 ssl->session_negotiate->id,
1496 ssl->session_negotiate->id_len);
1497 MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
1498 legacy_session_id_echo_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1501 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
Jerry Yu4a173382021-10-11 21:45:31 +08001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001504 }
1505
Jerry Yu4a173382021-10-11 21:45:31 +08001506 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001507 *buf = p;
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
1510 ssl->session_negotiate->id_len);
1511 return 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001512}
1513
Jerry Yue1b9c292021-09-10 10:08:31 +08001514/* Parse ServerHello message and configure context
1515 *
1516 * struct {
1517 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1518 * Random random;
1519 * opaque legacy_session_id_echo<0..32>;
1520 * CipherSuite cipher_suite;
1521 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001522 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001523 * } ServerHello;
1524 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001525MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001526static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
1527 const unsigned char *buf,
1528 const unsigned char *end,
1529 int is_hrr)
Jerry Yue1b9c292021-09-10 10:08:31 +08001530{
Jerry Yub85277e2021-10-13 13:36:05 +08001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001532 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001533 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001534 size_t extensions_len;
1535 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001536 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001537 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQianb119a352022-01-26 03:29:10 +00001538 int fatal_alert = 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001539 uint32_t allowed_extensions_mask;
Jerry Yu50e00e32022-10-31 14:45:01 +08001540 int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_SSL_HS_SERVER_HELLO;
Jerry Yue1b9c292021-09-10 10:08:31 +08001542
1543 /*
1544 * Check there is space for minimal fields
1545 *
1546 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001547 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001548 * - legacy_session_id_echo ( 1 byte ), minimum size
1549 * - cipher_suite ( 2 bytes)
1550 * - legacy_compression_method ( 1 byte )
1551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
Jerry Yue1b9c292021-09-10 10:08:31 +08001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
1555 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001556
Jerry Yu4a173382021-10-11 21:45:31 +08001557 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001558 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001559 * ...
1560 * with ProtocolVersion defined as:
1561 * uint16 ProtocolVersion;
1562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1564 MBEDTLS_SSL_VERSION_TLS1_2) {
1565 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1566 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1567 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001568 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1569 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001570 }
1571 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001572
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001573 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001574 * Random random;
1575 * ...
1576 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001577 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (!is_hrr) {
1580 memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1581 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1582 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
1583 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
XiaokangQian53f20b72022-01-18 10:47:33 +00001584 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001585 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001586
Jerry Yu4a173382021-10-11 21:45:31 +08001587 /* ...
1588 * opaque legacy_session_id_echo<0..32>;
1589 * ...
1590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
XiaokangQian52da5582022-01-26 09:49:29 +00001592 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001593 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001594 }
1595
Jerry Yu4a173382021-10-11 21:45:31 +08001596 /* ...
1597 * CipherSuite cipher_suite;
1598 * ...
1599 * with CipherSuite defined as:
1600 * uint8 CipherSuite[2];
1601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1603 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001604 p += 2;
1605
Jerry Yu4a173382021-10-11 21:45:31 +08001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Jerry Yu4a173382021-10-11 21:45:31 +08001608 /*
Ronald Cronba120bb2022-03-30 22:09:48 +02001609 * Check whether this ciphersuite is valid and offered.
Jerry Yu4a173382021-10-11 21:45:31 +08001610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
1612 ssl->tls_version,
1613 ssl->tls_version) != 0) ||
1614 !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001615 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001616 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001617 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001618 * If we received an HRR before and that the proposed selected
1619 * ciphersuite in this server hello is not the same as the one
1620 * proposed in the HRR, we abort the handshake and send an
1621 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 else if ((!is_hrr) && (handshake->hello_retry_request_count > 0) &&
1624 (cipher_suite != ssl->session_negotiate->ciphersuite)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001625 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001626 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1629 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
1630 cipher_suite));
XiaokangQiand59be772022-01-24 10:12:51 +00001631 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001632 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001633
Jerry Yu4a173382021-10-11 21:45:31 +08001634 /* Configure ciphersuites */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
Jerry Yu4a173382021-10-11 21:45:31 +08001636
XiaokangQian355e09a2022-01-20 11:14:50 +00001637 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001638 ssl->session_negotiate->ciphersuite = cipher_suite;
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
1641 cipher_suite, ciphersuite_info->name));
Jerry Yue1b9c292021-09-10 10:08:31 +08001642
1643#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ssl->session_negotiate->start = time(NULL);
Jerry Yue1b9c292021-09-10 10:08:31 +08001645#endif /* MBEDTLS_HAVE_TIME */
1646
Jerry Yu4a173382021-10-11 21:45:31 +08001647 /* ...
1648 * uint8 legacy_compression_method = 0;
1649 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1652 if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
1653 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
XiaokangQian52da5582022-01-26 09:49:29 +00001654 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001655 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001656 }
1657 p++;
1658
Jerry Yub85277e2021-10-13 13:36:05 +08001659 /* ...
1660 * Extension extensions<6..2^16-1>;
1661 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001662 * struct {
1663 * ExtensionType extension_type; (2 bytes)
1664 * opaque extension_data<0..2^16-1>;
1665 * } Extension;
1666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1668 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yue1b9c292021-09-10 10:08:31 +08001669 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001670
Jerry Yu4a173382021-10-11 21:45:31 +08001671 /* Check extensions do not go beyond the buffer of data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yu4a173382021-10-11 21:45:31 +08001673 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
Jerry Yue1b9c292021-09-10 10:08:31 +08001676
Jerry Yu50e00e32022-10-31 14:45:01 +08001677 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001678 allowed_extensions_mask = is_hrr ?
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
1680 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
Jerry Yu2c5363e2022-08-04 17:42:49 +08001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 while (p < extensions_end) {
Jerry Yue1b9c292021-09-10 10:08:31 +08001683 unsigned int extension_type;
1684 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001685 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1688 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1689 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001690 p += 4;
1691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian43550bd2022-01-21 04:32:58 +00001693 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001694
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001695 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 ssl, hs_msg_type, extension_type, allowed_extensions_mask);
1697 if (ret != 0) {
1698 return ret;
1699 }
Jerry Yu2c5363e2022-08-04 17:42:49 +08001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 switch (extension_type) {
XiaokangQianb851da82022-01-14 04:03:11 +00001702 case MBEDTLS_TLS_EXT_COOKIE:
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 ret = ssl_tls13_parse_cookie_ext(ssl,
1705 p, extension_data_end);
1706 if (ret != 0) {
1707 MBEDTLS_SSL_DEBUG_RET(1,
1708 "ssl_tls13_parse_cookie_ext",
1709 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001710 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001711 }
XiaokangQianb851da82022-01-14 04:03:11 +00001712 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001713
Jerry Yue1b9c292021-09-10 10:08:31 +08001714 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 ret = ssl_tls13_parse_supported_versions_ext(ssl,
1716 p,
1717 extension_data_end);
1718 if (ret != 0) {
XiaokangQiand59be772022-01-24 10:12:51 +00001719 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001721 break;
1722
Ronald Cron41a443a2022-10-04 16:38:25 +02001723#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue1b9c292021-09-10 10:08:31 +08001724 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
Jerry Yu4a173382021-10-11 21:45:31 +08001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
1728 ssl, p, extension_data_end)) != 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +00001729 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
1731 return ret;
XiaokangQianeb69aee2022-07-05 08:21:43 +00001732 }
1733 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001734#endif
Jerry Yue1b9c292021-09-10 10:08:31 +08001735
Jerry Yue1b9c292021-09-10 10:08:31 +08001736 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
1738 if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
XiaokangQian52da5582022-01-26 09:49:29 +00001739 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001740 goto cleanup;
1741 }
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (is_hrr) {
1744 ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
1745 p, extension_data_end);
1746 } else {
1747 ret = ssl_tls13_parse_key_share_ext(ssl,
1748 p, extension_data_end);
1749 }
1750 if (ret != 0) {
1751 MBEDTLS_SSL_DEBUG_RET(1,
1752 "ssl_tls13_parse_key_share_ext",
1753 ret);
XiaokangQiand59be772022-01-24 10:12:51 +00001754 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001755 }
1756 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001757
1758 default:
Jerry Yu9872eb22022-08-29 13:42:01 +08001759 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1760 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001761 }
1762
1763 p += extension_data_len;
1764 }
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
Jerry Yu2c5363e2022-08-04 17:42:49 +08001767
XiaokangQiand59be772022-01-24 10:12:51 +00001768cleanup:
1769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
1771 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1772 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
XiaokangQiand59be772022-01-24 10:12:51 +00001773 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1775 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1776 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
XiaokangQiand59be772022-01-24 10:12:51 +00001777 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001780}
1781
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001782#if defined(MBEDTLS_DEBUG_C)
1783static const char *ssl_tls13_get_kex_mode_str(int mode)
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001784{
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 switch (mode) {
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001786 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1787 return "psk";
1788 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1789 return "ephemeral";
1790 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1791 return "psk_ephemeral";
1792 default:
1793 return "unknown mode";
1794 }
1795}
Xiaokang Qiancb6e9632022-09-26 11:59:32 +00001796#endif /* MBEDTLS_DEBUG_C */
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001797
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001798MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001799static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue1b9c292021-09-10 10:08:31 +08001800{
Jerry Yub85277e2021-10-13 13:36:05 +08001801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub85277e2021-10-13 13:36:05 +08001802 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001803
Jerry Yub85277e2021-10-13 13:36:05 +08001804 /* Determine the key exchange mode:
1805 * 1) If both the pre_shared_key and key_share extensions were received
1806 * then the key exchange mode is PSK with EPHEMERAL.
1807 * 2) If only the pre_shared_key extension was received then the key
1808 * exchange mode is PSK-only.
1809 * 3) If only the key_share extension was received then the key
1810 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 switch (handshake->received_extensions &
1813 (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
Jerry Yu745bb612021-10-13 22:01:04 +08001814 /* Only the pre_shared_key extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
Ronald Cron79907712022-07-20 17:05:29 +02001816 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001817 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001818
Jerry Yu745bb612021-10-13 22:01:04 +08001819 /* Only the key_share extension was received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
Ronald Cron79907712022-07-20 17:05:29 +02001821 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001822 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001823
Jerry Yu745bb612021-10-13 22:01:04 +08001824 /* Both the pre_shared_key and key_share extensions were received */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
Ronald Cron79907712022-07-20 17:05:29 +02001826 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001827 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001828
Jerry Yu745bb612021-10-13 22:01:04 +08001829 /* Neither pre_shared_key nor key_share extension was received */
1830 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
Jerry Yu745bb612021-10-13 22:01:04 +08001832 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1833 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001834 }
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (!mbedtls_ssl_conf_tls13_check_kex_modes(ssl, handshake->key_exchange_mode)) {
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001837 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 MBEDTLS_SSL_DEBUG_MSG(2,
1839 ("Key exchange mode(%s) is not supported.",
1840 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qianac8195f2022-09-26 04:01:06 +00001841 goto cleanup;
1842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 MBEDTLS_SSL_DEBUG_MSG(3,
1845 ("Selected key exchange mode: %s",
1846 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
Xiaokang Qian5beec4b2022-09-26 08:23:45 +00001847
Jerry Yu0b177842021-09-05 19:41:30 +08001848 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1849 *
1850 * TODO: We don't have to do this in case we offered 0-RTT and the
1851 * server accepted it. In this case, we could skip generating
1852 * the early secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1854 if (ret != 0) {
1855 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_key_schedule_stage_early",
1856 ret);
Jerry Yu4a173382021-10-11 21:45:31 +08001857 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001858 }
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
1861 if (ret != 0) {
1862 MBEDTLS_SSL_DEBUG_RET(1,
1863 "mbedtls_ssl_tls13_compute_handshake_transform",
1864 ret);
Jerry Yu4a173382021-10-11 21:45:31 +08001865 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001866 }
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
1869 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
Jerry Yu0b177842021-09-05 19:41:30 +08001870 ssl->session_in = ssl->session_negotiate;
1871
Jerry Yu4a173382021-10-11 21:45:31 +08001872cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (ret != 0) {
Jerry Yu4a173382021-10-11 21:45:31 +08001874 MBEDTLS_SSL_PEND_FATAL_ALERT(
1875 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yu4a173382021-10-11 21:45:31 +08001877 }
Jerry Yue110d252022-05-05 10:19:22 +08001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return ret;
Jerry Yue1b9c292021-09-10 10:08:31 +08001880}
1881
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001882MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001883static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
XiaokangQian647719a2021-12-07 09:16:29 +00001884{
1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
XiaokangQian647719a2021-12-07 09:16:29 +00001888
XiaokangQian78b1fa72022-01-19 06:56:30 +00001889 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001890 * We are going to re-generate a shared secret corresponding to the group
1891 * selected by the server, which is different from the group for which we
1892 * generated a shared secret in the first client hello.
1893 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 ret = ssl_tls13_reset_key_share(ssl);
1896 if (ret != 0) {
1897 return ret;
1898 }
XiaokangQian647719a2021-12-07 09:16:29 +00001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return 0;
XiaokangQian647719a2021-12-07 09:16:29 +00001901}
1902
Jerry Yue1b9c292021-09-10 10:08:31 +08001903/*
Jerry Yu4a173382021-10-11 21:45:31 +08001904 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001905 * Handler for MBEDTLS_SSL_SERVER_HELLO
1906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001908static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08001909{
Jerry Yu4a173382021-10-11 21:45:31 +08001910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001911 unsigned char *buf = NULL;
1912 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001913 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
Jerry Yue1b9c292021-09-10 10:08:31 +08001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
1918 MBEDTLS_SSL_HS_SERVER_HELLO,
1919 &buf, &buf_len));
Ronald Crondb5dfa12022-05-31 11:44:38 +02001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
1922 if (ret < 0) {
XiaokangQian16acd4b2022-01-14 07:35:47 +00001923 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 } else {
1925 is_hrr = (ret == SSL_SERVER_HELLO_HRR);
1926 }
XiaokangQiand59be772022-01-24 10:12:51 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (ret == SSL_SERVER_HELLO_TLS1_2) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01001929 ret = 0;
1930 goto cleanup;
1931 }
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
1934 buf + buf_len,
1935 is_hrr));
1936 if (is_hrr) {
1937 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
Ronald Cronfb508b82022-05-31 14:49:55 +02001938 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001939
1940 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1941 buf, buf_len);
1942
1943 if (is_hrr) {
1944 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
1945#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1946 /* If not offering early data, the client sends a dummy CCS record
1947 * immediately before its second flight. This may either be before
1948 * its second ClientHello or before its encrypted handshake flight.
1949 */
1950 mbedtls_ssl_handshake_set_state(ssl,
1951 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
1952#else
1953 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
1954#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1955 } else {
1956 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
1957 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Ronald Cronfb508b82022-05-31 14:49:55 +02001958 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001959
1960cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
1962 is_hrr ? "HelloRetryRequest" : "ServerHello"));
1963 return ret;
Jerry Yu687101b2021-09-14 16:03:56 +08001964}
1965
1966/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001967 *
Ronald Cron9d6a5452022-05-30 16:05:38 +02001968 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001969 *
1970 * The EncryptedExtensions message contains any extensions which
1971 * should be protected, i.e., any which are not needed to establish
1972 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001973 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001974
XiaokangQian08da26c2021-10-09 10:12:11 +00001975/* Parse EncryptedExtensions message
1976 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001977 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001978 * } EncryptedExtensions;
1979 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001981static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
1982 const unsigned char *buf,
1983 const unsigned char *end)
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001984{
1985 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001986 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001987 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001988 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001989 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1992 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08da26c2021-10-09 10:12:11 +00001993 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
1996 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Ronald Cronc8083592022-05-31 16:24:05 +02001997 extensions_end = p + extensions_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001998
Jerry Yu9eba7502022-10-31 13:46:16 +08001999 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yucbd082f2022-08-04 16:55:10 +08002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 while (p < extensions_end) {
XiaokangQian08da26c2021-10-09 10:12:11 +00002002 unsigned int extension_type;
2003 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002004
XiaokangQian08da26c2021-10-09 10:12:11 +00002005 /*
2006 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00002007 * ExtensionType extension_type; (2 bytes)
2008 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00002009 * } Extension;
2010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2012 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2013 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian08da26c2021-10-09 10:12:11 +00002014 p += 4;
2015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002017
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002018 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
2020 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
2021 if (ret != 0) {
2022 return ret;
2023 }
Jerry Yucbd082f2022-08-04 16:55:10 +08002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 switch (extension_type) {
lhuang0486cacac2022-01-21 07:34:27 -08002026#if defined(MBEDTLS_SSL_ALPN)
2027 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
lhuang0486cacac2022-01-21 07:34:27 -08002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if ((ret = ssl_tls13_parse_alpn_ext(ssl, p, (size_t) extension_data_len)) != 0) {
2031 return ret;
lhuang0486cacac2022-01-21 07:34:27 -08002032 }
2033
2034 break;
2035#endif /* MBEDTLS_SSL_ALPN */
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002036
2037#if defined(MBEDTLS_SSL_EARLY_DATA)
2038 case MBEDTLS_TLS_EXT_EARLY_DATA:
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (extension_data_len != 0) {
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002041 /* The message must be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2043 MBEDTLS_ERR_SSL_DECODE_ERROR);
2044 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qianca09afc2022-11-22 10:05:19 +00002045 }
2046
Xiaokang Qian8bee8992022-10-27 10:21:05 +00002047 break;
2048#endif /* MBEDTLS_SSL_EARLY_DATA */
2049
XiaokangQian08da26c2021-10-09 10:12:11 +00002050 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002051 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu9eba7502022-10-31 13:46:16 +08002052 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 extension_type, "( ignored )");
Jerry Yucbd082f2022-08-04 16:55:10 +08002054 break;
XiaokangQian08da26c2021-10-09 10:12:11 +00002055 }
2056
XiaokangQian08da26c2021-10-09 10:12:11 +00002057 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00002058 }
XiaokangQian08da26c2021-10-09 10:12:11 +00002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2061 handshake->received_extensions);
Jerry Yucbd082f2022-08-04 16:55:10 +08002062
XiaokangQian97799ac2021-10-11 10:05:54 +00002063 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (p != end) {
2065 MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
2066 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2067 MBEDTLS_ERR_SSL_DECODE_ERROR);
2068 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002069 }
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return ret;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00002072}
2073
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002075static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
Ronald Cron9d6a5452022-05-30 16:05:38 +02002076{
2077 int ret;
2078 unsigned char *buf;
2079 size_t buf_len;
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2084 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2085 &buf, &buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002086
2087 /* Process the message contents */
2088 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
Ronald Cron9d6a5452022-05-30 16:05:38 +02002090
Xiaokang Qianb157e912022-11-23 08:12:26 +00002091#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (ssl->handshake->received_extensions &
2093 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Xiaokang Qianb157e912022-11-23 08:12:26 +00002094 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
2095 }
2096#endif
2097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2099 buf, buf_len);
Ronald Cron9d6a5452022-05-30 16:05:38 +02002100
Ronald Cron928cbd32022-10-04 16:14:26 +02002101#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2103 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2104 } else {
2105 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2106 }
Ronald Cronfb508b82022-05-31 14:49:55 +02002107#else
2108 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Ronald Cronfb508b82022-05-31 14:49:55 +02002110#endif
Ronald Cron9d6a5452022-05-30 16:05:38 +02002111
2112cleanup:
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
2115 return ret;
Ronald Cron9d6a5452022-05-30 16:05:38 +02002116
2117}
2118
Ronald Cron928cbd32022-10-04 16:14:26 +02002119#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08002120/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002121 * STATE HANDLING: CertificateRequest
2122 *
Jerry Yud2674312021-10-29 10:08:19 +08002123 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002124#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2125#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002126/* Coordination:
2127 * Deals with the ambiguity of not knowing if a CertificateRequest
2128 * will be sent. Returns a negative code on failure, or
2129 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2130 * - SSL_CERTIFICATE_REQUEST_SKIP
2131 * indicating if a Certificate Request is expected or not.
2132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
Jerry Yud2674312021-10-29 10:08:19 +08002135{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2139 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2140 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002141 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002142 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
2145 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
2146 MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
2147 return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
Jerry Yud2674312021-10-29 10:08:19 +08002148 }
2149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
Ronald Cron19385882022-06-15 16:26:13 +02002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 return SSL_CERTIFICATE_REQUEST_SKIP;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002153}
2154
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002155/*
2156 * ssl_tls13_parse_certificate_request()
2157 * Parse certificate request
2158 * struct {
2159 * opaque certificate_request_context<0..2^8-1>;
2160 * Extension extensions<2..2^16-1>;
2161 * } CertificateRequest;
2162 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002163MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002164static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
2165 const unsigned char *buf,
2166 const unsigned char *end)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002167{
2168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2169 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002170 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002171 size_t extensions_len = 0;
2172 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002173 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002174
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002175 /* ...
2176 * opaque certificate_request_context<0..2^8-1>
2177 * ...
2178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002180 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002181 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (certificate_request_context_len > 0) {
2184 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
2185 MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
2186 p, certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002187
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002188 handshake->certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 mbedtls_calloc(1, certificate_request_context_len);
2190 if (handshake->certificate_request_context == NULL) {
2191 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2192 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002193 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 memcpy(handshake->certificate_request_context, p,
2195 certificate_request_context_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002196 p += certificate_request_context_len;
2197 }
2198
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002199 /* ...
2200 * Extension extensions<2..2^16-1>;
2201 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2204 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002205 p += 2;
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002208 extensions_end = p + extensions_len;
2209
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002210 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yuc55a6af2022-08-04 17:01:21 +08002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 while (p < extensions_end) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002213 unsigned int extension_type;
2214 size_t extension_data_len;
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2217 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2218 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002219 p += 4;
2220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002222
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002223 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
2225 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
2226 if (ret != 0) {
2227 return ret;
2228 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 switch (extension_type) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002231 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 MBEDTLS_SSL_DEBUG_MSG(3,
2233 ("found signature algorithms extension"));
2234 ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
2235 p + extension_data_len);
2236 if (ret != 0) {
2237 return ret;
2238 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002239
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002240 break;
2241
2242 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002243 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu6d0e78b2022-10-31 14:13:25 +08002244 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 extension_type, "( ignored )");
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00002246 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002247 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002248
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002249 p += extension_data_len;
2250 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2253 handshake->received_extensions);
Jerry Yuc55a6af2022-08-04 17:01:21 +08002254
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002255 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (p != end) {
2257 MBEDTLS_SSL_DEBUG_MSG(1,
2258 ("CertificateRequest misaligned"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002259 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002260 }
Jerry Yuc55a6af2022-08-04 17:01:21 +08002261
Jerry Yu0c354a22022-08-29 15:25:36 +08002262 /* RFC 8446 section 4.3.2
Jerry Yuc55a6af2022-08-04 17:01:21 +08002263 *
2264 * The "signature_algorithms" extension MUST be specified
2265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
2267 MBEDTLS_SSL_DEBUG_MSG(3,
2268 ("no signature algorithms extension found"));
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002269 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00002270 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002271
Jerry Yu7840f812022-01-29 10:26:51 +08002272 ssl->handshake->client_auth = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 return 0;
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002274
Xiaofei Baic234ecf2022-02-08 09:59:23 +00002275decode_error:
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2277 MBEDTLS_ERR_SSL_DECODE_ERROR);
2278 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002279}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002280
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002281/*
2282 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002285static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002286{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00002287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002294 unsigned char *buf;
2295 size_t buf_len;
2296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
2298 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2299 &buf, &buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(ssl,
2302 buf, buf + buf_len));
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2305 buf, buf_len);
2306 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
Xiaofei Baif6d36962022-01-16 14:54:35 +00002307 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 } else {
2309 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Xiaofei Bai51f515a2022-02-08 07:28:04 +00002310 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2311 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002312 }
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
Jerry Yud2674312021-10-29 10:08:19 +08002315
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002316cleanup:
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2319 return ret;
Jerry Yud2674312021-10-29 10:08:19 +08002320}
2321
2322/*
Jerry Yu687101b2021-09-14 16:03:56 +08002323 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2324 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002325MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002326static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002327{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002328 int ret;
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 ret = mbedtls_ssl_tls13_process_certificate(ssl);
2331 if (ret != 0) {
2332 return ret;
2333 }
Xiaofei Bai947571e2021-09-29 09:12:03 +00002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2336 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002337}
2338
2339/*
2340 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2341 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002342MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002343static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002344{
Jerry Yu30b071c2021-09-12 20:16:03 +08002345 int ret;
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
2348 if (ret != 0) {
2349 return ret;
2350 }
Jerry Yu30b071c2021-09-12 20:16:03 +08002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2353 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002354}
Ronald Cron928cbd32022-10-04 16:14:26 +02002355#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002356
Jerry Yu687101b2021-09-14 16:03:56 +08002357/*
2358 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2359 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002360MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002361static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002362{
XiaokangQianac0385c2021-11-03 06:40:11 +00002363 int ret;
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2366 if (ret != 0) {
2367 return ret;
2368 }
XiaokangQianac0385c2021-11-03 06:40:11 +00002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2371 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002372 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2374 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2375 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002376 }
2377
Ronald Cron49ad6192021-11-24 16:25:31 +01002378#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2379 mbedtls_ssl_handshake_set_state(
2380 ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
Ronald Cron49ad6192021-11-24 16:25:31 +01002382#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
Jerry Yu566c7812022-01-26 15:41:22 +08002384#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Ronald Cron49ad6192021-11-24 16:25:31 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002387}
2388
2389/*
Jerry Yu566c7812022-01-26 15:41:22 +08002390 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2391 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002392MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002393static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002394{
Ronald Cron7a94aca2022-03-09 07:44:27 +01002395 int non_empty_certificate_msg = 0;
2396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_SSL_DEBUG_MSG(1,
2398 ("Switch to handshake traffic keys for outbound traffic"));
2399 mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
Jerry Yu5cc35062022-01-28 16:16:08 +08002400
Ronald Cron928cbd32022-10-04 16:14:26 +02002401#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (ssl->handshake->client_auth) {
2403 int ret = mbedtls_ssl_tls13_write_certificate(ssl);
2404 if (ret != 0) {
2405 return ret;
2406 }
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (mbedtls_ssl_own_cert(ssl) != NULL) {
Ronald Cron7a94aca2022-03-09 07:44:27 +01002409 non_empty_certificate_msg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 }
2411 } else {
2412 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
Ronald Cron7a94aca2022-03-09 07:44:27 +01002413 }
Ronald Cron9df7c802022-03-08 18:38:54 +01002414#endif
Ronald Cron5bb8fc82022-03-09 07:00:13 +01002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (non_empty_certificate_msg) {
2417 mbedtls_ssl_handshake_set_state(ssl,
2418 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2419 } else {
2420 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
2421 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2422 }
Ronald Cron7a94aca2022-03-09 07:44:27 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return 0;
Jerry Yu566c7812022-01-26 15:41:22 +08002425}
2426
Ronald Cron928cbd32022-10-04 16:14:26 +02002427#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002428/*
2429 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2430 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002431MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002432static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu566c7812022-01-26 15:41:22 +08002433{
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
Ronald Crona8b38872022-03-09 07:59:25 +01002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (ret == 0) {
2437 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2438 }
Ronald Crona8b38872022-03-09 07:59:25 +01002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 return ret;
Jerry Yu566c7812022-01-26 15:41:22 +08002441}
Ronald Cron928cbd32022-10-04 16:14:26 +02002442#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002443
2444/*
Jerry Yu687101b2021-09-14 16:03:56 +08002445 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2446 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002447MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002448static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002449{
XiaokangQian0fa66432021-11-15 03:33:57 +00002450 int ret;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2453 if (ret != 0) {
2454 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002455 }
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2458 if (ret != 0) {
2459 MBEDTLS_SSL_DEBUG_RET(1,
2460 "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
2461 return ret;
2462 }
2463
2464 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
2465 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002466}
2467
2468/*
2469 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
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_flush_buffers(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002473{
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
2475 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2476 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002477}
2478
2479/*
2480 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2481 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002482MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002483static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu687101b2021-09-14 16:03:56 +08002484{
Jerry Yu378254d2021-10-30 21:44:47 +08002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yu378254d2021-10-30 21:44:47 +08002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2489 return 0;
Jerry Yu687101b2021-09-14 16:03:56 +08002490}
2491
Jerry Yuf8a49942022-07-07 11:32:32 +00002492#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2493
Jerry Yua0446a02022-07-13 11:22:55 +08002494MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002495static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
2496 const unsigned char *buf,
2497 const unsigned char *end)
Jerry Yuf8a49942022-07-07 11:32:32 +00002498{
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002499 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yuaf2c0c82022-07-12 05:47:21 +00002500 const unsigned char *p = buf;
Jerry Yuf8a49942022-07-07 11:32:32 +00002501
Jerry Yuf8a49942022-07-07 11:32:32 +00002502
Jerry Yuedab6372022-10-31 14:37:31 +08002503 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 while (p < end) {
Jerry Yuf8a49942022-07-07 11:32:32 +00002506 unsigned int extension_type;
2507 size_t extension_data_len;
Jerry Yu0c354a22022-08-29 15:25:36 +08002508 int ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
2511 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2512 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuf8a49942022-07-07 11:32:32 +00002513 p += 4;
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002516
Jerry Yuc4bf5d62022-10-29 09:08:47 +08002517 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
2519 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
2520 if (ret != 0) {
2521 return ret;
2522 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 switch (extension_type) {
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002525#if defined(MBEDTLS_SSL_EARLY_DATA)
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002526 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (extension_data_len != 4) {
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002528 MBEDTLS_SSL_PEND_FATAL_ALERT(
2529 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 MBEDTLS_ERR_SSL_DECODE_ERROR);
2531 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (ssl->session != NULL) {
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002534 ssl->session->ticket_flags |=
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA;
Xiaokang Qian2d87a9e2022-11-09 07:55:48 +00002536 }
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002537 break;
Xiaokang Qian0cc43202022-11-16 08:43:50 +00002538#endif /* MBEDTLS_SSL_EARLY_DATA */
Xiaokang Qianf447e8a2022-11-08 07:02:27 +00002539
Jerry Yuf8a49942022-07-07 11:32:32 +00002540 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08002541 MBEDTLS_SSL_PRINT_EXT(
Jerry Yuedab6372022-10-31 14:37:31 +08002542 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 extension_type, "( ignored )");
Jerry Yuf8a49942022-07-07 11:32:32 +00002544 break;
2545 }
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002546
Jerry Yuf8a49942022-07-07 11:32:32 +00002547 p += extension_data_len;
2548 }
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2551 handshake->received_extensions);
Jerry Yu6ba9f1c2022-08-04 17:53:25 +08002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002554}
2555
2556/*
Jerry Yu08aed4d2022-07-20 10:36:12 +08002557 * From RFC8446, page 74
2558 *
Jerry Yuf8a49942022-07-07 11:32:32 +00002559 * struct {
2560 * uint32 ticket_lifetime;
2561 * uint32 ticket_age_add;
2562 * opaque ticket_nonce<0..255>;
2563 * opaque ticket<1..2^16-1>;
2564 * Extension extensions<0..2^16-2>;
2565 * } NewSessionTicket;
2566 *
2567 */
Jerry Yua0446a02022-07-13 11:22:55 +08002568MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002569static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
2570 unsigned char *buf,
2571 unsigned char *end,
2572 unsigned char **ticket_nonce,
2573 size_t *ticket_nonce_len)
Jerry Yuf8a49942022-07-07 11:32:32 +00002574{
2575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2576 unsigned char *p = buf;
2577 mbedtls_ssl_session *session = ssl->session;
Jerry Yuf8a49942022-07-07 11:32:32 +00002578 size_t ticket_len;
2579 unsigned char *ticket;
2580 size_t extensions_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002581
Jerry Yucb3b1392022-07-12 06:09:38 +00002582 *ticket_nonce = NULL;
2583 *ticket_nonce_len = 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002584 /*
2585 * ticket_lifetime 4 bytes
2586 * ticket_age_add 4 bytes
Jerry Yu08aed4d2022-07-20 10:36:12 +08002587 * ticket_nonce_len 1 byte
Jerry Yuf8a49942022-07-07 11:32:32 +00002588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
Jerry Yuf8a49942022-07-07 11:32:32 +00002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
2592 MBEDTLS_SSL_DEBUG_MSG(3,
2593 ("ticket_lifetime: %u",
2594 (unsigned int) session->ticket_lifetime));
Jerry Yuf8a49942022-07-07 11:32:32 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
2597 MBEDTLS_SSL_DEBUG_MSG(3,
2598 ("ticket_age_add: %u",
2599 (unsigned int) session->ticket_age_add));
Jerry Yuf8a49942022-07-07 11:32:32 +00002600
Jerry Yucb3b1392022-07-12 06:09:38 +00002601 *ticket_nonce_len = p[8];
2602 p += 9;
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002605 *ticket_nonce = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
Jerry Yucb3b1392022-07-12 06:09:38 +00002607 p += *ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002608
2609 /* Ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2611 ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002612 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
2614 MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002615
2616 /* Check if we previously received a ticket already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (session->ticket != NULL || session->ticket_len > 0) {
2618 mbedtls_free(session->ticket);
Jerry Yuf8a49942022-07-07 11:32:32 +00002619 session->ticket = NULL;
2620 session->ticket_len = 0;
2621 }
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2624 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2625 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yuf8a49942022-07-07 11:32:32 +00002626 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 memcpy(ticket, p, ticket_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002628 p += ticket_len;
2629 session->ticket = ticket;
2630 session->ticket_len = ticket_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002631
Pengyu Lv9f926952022-11-17 15:22:33 +08002632 /* Clear all flags in ticket_flags */
2633 mbedtls_ssl_tls13_session_clear_ticket_flags(session,
2634 MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2637 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuf8a49942022-07-07 11:32:32 +00002638 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
2644 if (ret != 0) {
2645 MBEDTLS_SSL_DEBUG_RET(1,
2646 "ssl_tls13_parse_new_session_ticket_exts",
2647 ret);
2648 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002649 }
Jerry Yucb3b1392022-07-12 06:09:38 +00002650
Jerry Yudb8c5fa2022-08-03 12:10:13 +08002651 /* session has been updated, allow export */
2652 session->exported = 0;
2653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 return 0;
Jerry Yucb3b1392022-07-12 06:09:38 +00002655}
2656
Jerry Yua0446a02022-07-13 11:22:55 +08002657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002658static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
2659 unsigned char *ticket_nonce,
2660 size_t ticket_nonce_len)
Jerry Yucb3b1392022-07-12 06:09:38 +00002661{
2662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2663 mbedtls_ssl_session *session = ssl->session;
2664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2665 psa_algorithm_t psa_hash_alg;
2666 int hash_length;
2667
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002668#if defined(MBEDTLS_HAVE_TIME)
2669 /* Store ticket creation time */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 session->ticket_received = mbedtls_time(NULL);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002671#endif
2672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
2674 if (ciphersuite_info == NULL) {
2675 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2676 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf8a49942022-07-07 11:32:32 +00002677 }
2678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2680 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2681 if (hash_length == -1 ||
2682 (size_t) hash_length > sizeof(session->resumption_key)) {
2683 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu3afdf362022-07-20 17:34:14 +08002684 }
2685
Jerry Yuf8a49942022-07-07 11:32:32 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2688 session->app_secrets.resumption_master_secret,
2689 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002690
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002691 /* Compute resumption key
Jerry Yuf8a49942022-07-07 11:32:32 +00002692 *
2693 * HKDF-Expand-Label( resumption_master_secret,
2694 * "resumption", ticket_nonce, Hash.length )
2695 */
2696 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 psa_hash_alg,
2698 session->app_secrets.resumption_master_secret,
2699 hash_length,
2700 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2701 ticket_nonce,
2702 ticket_nonce_len,
2703 session->resumption_key,
2704 hash_length);
Jerry Yuf8a49942022-07-07 11:32:32 +00002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (ret != 0) {
2707 MBEDTLS_SSL_DEBUG_RET(2,
2708 "Creating the ticket-resumed PSK failed",
2709 ret);
2710 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002711 }
2712
Jerry Yu0a430c82022-07-20 11:02:48 +08002713 session->resumption_key_len = hash_length;
Jerry Yuf8a49942022-07-07 11:32:32 +00002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2716 session->resumption_key,
2717 session->resumption_key_len);
Jerry Yuf8a49942022-07-07 11:32:32 +00002718
Pengyu Lv9f926952022-11-17 15:22:33 +08002719 /* Set ticket_flags depends on the selected key exchange modes */
2720 mbedtls_ssl_tls13_session_set_ticket_flags(session,
2721 ssl->conf->tls13_kex_modes);
2722 MBEDTLS_SSL_DEBUG_TICKET_FLAGS(4, session->ticket_flags);
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 return 0;
Jerry Yuf8a49942022-07-07 11:32:32 +00002725}
2726
2727/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002728 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yuf8a49942022-07-07 11:32:32 +00002729 */
Jerry Yua0446a02022-07-13 11:22:55 +08002730MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002731static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuf8a49942022-07-07 11:32:32 +00002732{
2733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2734 unsigned char *buf;
2735 size_t buf_len;
Jerry Yucb3b1392022-07-12 06:09:38 +00002736 unsigned char *ticket_nonce;
2737 size_t ticket_nonce_len;
Jerry Yuf8a49942022-07-07 11:32:32 +00002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
Jerry Yuf8a49942022-07-07 11:32:32 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2742 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2743 &buf, &buf_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
2746 ssl, buf, buf + buf_len,
2747 &ticket_nonce, &ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_new_session_ticket(
2750 ssl, ticket_nonce, ticket_nonce_len));
Jerry Yuf8a49942022-07-07 11:32:32 +00002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yu4e6c42a2022-07-13 11:16:51 +08002753
Jerry Yuf8a49942022-07-07 11:32:32 +00002754cleanup:
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
2757 return ret;
Jerry Yuf8a49942022-07-07 11:32:32 +00002758}
2759#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
Jerry Yubc20bdd2021-08-24 15:59:48 +08002762{
Jerry Yu92c6b402021-08-27 16:59:09 +08002763 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 switch (ssl->state) {
Jerry Yu92c6b402021-08-27 16:59:09 +08002766 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Jerry Yuddda0502022-12-01 19:43:12 +08002768 break;
2769
Jerry Yu92c6b402021-08-27 16:59:09 +08002770 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 ret = mbedtls_ssl_write_client_hello(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002772 break;
2773
2774 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 ret = ssl_tls13_process_server_hello(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002776 break;
2777
2778 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 ret = ssl_tls13_process_encrypted_extensions(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002780 break;
2781
Ronald Cron928cbd32022-10-04 16:14:26 +02002782#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002783 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 ret = ssl_tls13_process_certificate_request(ssl);
Jerry Yud2674312021-10-29 10:08:19 +08002785 break;
2786
Jerry Yu687101b2021-09-14 16:03:56 +08002787 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 ret = ssl_tls13_process_server_certificate(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002789 break;
2790
2791 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 ret = ssl_tls13_process_certificate_verify(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002793 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002794#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002795
2796 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 ret = ssl_tls13_process_server_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002798 break;
2799
Jerry Yu566c7812022-01-26 15:41:22 +08002800 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 ret = ssl_tls13_write_client_certificate(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002802 break;
2803
Ronald Cron928cbd32022-10-04 16:14:26 +02002804#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu566c7812022-01-26 15:41:22 +08002805 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 ret = ssl_tls13_write_client_certificate_verify(ssl);
Jerry Yu566c7812022-01-26 15:41:22 +08002807 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002808#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu566c7812022-01-26 15:41:22 +08002809
Jerry Yu687101b2021-09-14 16:03:56 +08002810 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 ret = ssl_tls13_write_client_finished(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002812 break;
2813
2814 case MBEDTLS_SSL_FLUSH_BUFFERS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 ret = ssl_tls13_flush_buffers(ssl);
Jerry Yu687101b2021-09-14 16:03:56 +08002816 break;
2817
2818 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu92c6b402021-08-27 16:59:09 +08002820 break;
2821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 /*
2823 * Injection of dummy-CCS's for middlebox compatibility
2824 */
Ronald Cron49ad6192021-11-24 16:25:31 +01002825#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002826 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2828 if (ret == 0) {
2829 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2830 }
Ronald Cron9f55f632022-02-02 16:02:47 +01002831 break;
2832
2833 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2835 if (ret == 0) {
2836 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2837 }
Ronald Cron49ad6192021-11-24 16:25:31 +01002838 break;
2839#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2840
Jerry Yuf8a49942022-07-07 11:32:32 +00002841#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08002842 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 ret = ssl_tls13_process_new_session_ticket(ssl);
2844 if (ret != 0) {
Jerry Yuf8a49942022-07-07 11:32:32 +00002845 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 }
Jerry Yuf8a49942022-07-07 11:32:32 +00002847 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
2848 break;
2849#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2850
Jerry Yu92c6b402021-08-27 16:59:09 +08002851 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
2853 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu92c6b402021-08-27 16:59:09 +08002854 }
2855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 return ret;
Jerry Yu92c6b402021-08-27 16:59:09 +08002857}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002858
Jerry Yufb4b6472022-01-27 15:03:26 +08002859#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */