blob: 9c949bd0b181badd59364e231e37d433d2b41ef4 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
Ronald Cron38117652024-03-05 09:05:46 +010053 * In a compliant ClientHello the byte-length of the list of ciphersuites
54 * is even and this function relies on this fact. This should have been
55 * checked in the main ClientHello parsing function. Double check here.
Ronald Cron89089cc2024-02-14 18:18:08 +010056 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
Ronald Cron7cab4f82024-03-07 15:09:09 +010077 * If a valid PSK ciphersuite identifier has been passed in, we want
78 * an exact match.
Ronald Cron89089cc2024-02-14 18:18:08 +010079 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
Gilles Peskinea9d4ef02024-06-03 22:16:23 +020095 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
96 (unsigned) psk_ciphersuite_id,
97 (unsigned long) psk_hash_alg));
Ronald Cron89089cc2024-02-14 18:18:08 +010098}
99
Ronald Cron41a443a2022-10-04 16:38:25 +0200100#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +0000101/* From RFC 8446:
102 *
103 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
104 * struct {
105 * PskKeyExchangeMode ke_modes<1..255>;
106 * } PskKeyExchangeModes;
107 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800108MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100109static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
110 const unsigned char *buf,
111 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000112{
Jerry Yu299e31f2022-07-13 23:06:36 +0800113 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000114 size_t ke_modes_len;
115 int ke_modes = 0;
116
Jerry Yu854dd9e2022-07-15 14:28:27 +0800117 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800119 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000120 /* Currently, there are only two PSK modes, so even without looking
121 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (ke_modes_len > 2) {
123 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
124 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
125 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800126 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 while (ke_modes_len-- != 0) {
131 switch (*p++) {
132 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
133 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
134 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
135 break;
136 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
137 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
138 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
139 break;
140 default:
141 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
142 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
143 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000144 }
145 }
146
147 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000149}
Jerry Yu1c105562022-07-10 06:32:38 +0000150
Ronald Cron38117652024-03-05 09:05:46 +0100151/*
152 * Non-error return values of
153 * ssl_tls13_offered_psks_check_identity_match_ticket() and
154 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
155 * not collide with error codes that are negative. Zero
156 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
157 * up by the callers of this function as a generic success condition.
158 *
159 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
Ronald Cron7cab4f82024-03-07 15:09:09 +0100160 * that the pre-shared-key identity matches that of a ticket or an externally-
Ronald Cron38117652024-03-05 09:05:46 +0100161 * provisioned pre-shared-key. We have thus been able to retrieve the
162 * attributes of the pre-shared-key but at least one of them does not meet
163 * some criteria and the pre-shared-key cannot be used. For example, a ticket
164 * is expired or its version is not TLS 1.3. Note eventually that the return
165 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
166 * anything to do with binder check. A binder check is done only when a
167 * suitable pre-shared-key has been selected and only for that selected
168 * pre-shared-key: if the binder check fails, we fail the handshake and we do
169 * not try to find another pre-shared-key for which the binder check would
170 * succeed as recommended by the specification.
171 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100172#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
173#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
174#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800175
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800176MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800177static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800178MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800179static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800180
Ronald Cron1f045f32024-03-25 13:37:07 +0100181#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu95699e72022-08-21 19:22:23 +0800182MBEDTLS_CHECK_RETURN_CRITICAL
183static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_ssl_context *ssl,
185 const unsigned char *identity,
186 size_t identity_len,
187 uint32_t obfuscated_ticket_age,
188 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800189{
Jerry Yufd310eb2022-09-06 09:16:35 +0800190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800191 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800192#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800193 mbedtls_ms_time_t now;
194 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800195 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800196 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800197#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800198
199 ((void) obfuscated_ticket_age);
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800202
Jerry Yufd310eb2022-09-06 09:16:35 +0800203 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 }
Jerry Yu95699e72022-08-21 19:22:23 +0800207
Jerry Yu4746b102022-09-13 11:11:48 +0800208 /* We create a copy of the encrypted ticket since the ticket parsing
209 * function is allowed to use its input buffer as an output buffer
210 * (in-place decryption). We do, however, need the original buffer for
211 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ticket_buffer = mbedtls_calloc(1, identity_len);
214 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800216 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
220 session,
221 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100222 switch (ret) {
223 case 0:
224 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
225 break;
226
227 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100229 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100230 break;
231
232 case MBEDTLS_ERR_SSL_INVALID_MAC:
233 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100234 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100235 break;
236
237 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100239 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800240 }
241
242 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800244
Ronald Cronfbae94a2023-12-05 18:15:14 +0100245 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800246 goto exit;
247 }
Jerry Yu95699e72022-08-21 19:22:23 +0800248
Ronald Cron38117652024-03-05 09:05:46 +0100249 /*
250 * The identity matches that of a ticket. Now check that it has suitable
251 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800252 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100253 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800254
Ronald Cronfbae94a2023-12-05 18:15:14 +0100255 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
256 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800257 goto exit;
258 }
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800261 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100262
Jerry Yu25ba4d42023-11-10 14:12:20 +0800263 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800265 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
266 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800267 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 goto exit;
269 }
270
Jerry Yu25ba4d42023-11-10 14:12:20 +0800271 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800272
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800273 /* RFC 8446 section 4.6.1
274 *
275 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
276 *
277 * RFC 8446 section 4.2.11.1
278 *
279 * Clients MUST NOT attempt to use tickets which have ages greater than
280 * the "ticket_lifetime" value which was provided with the ticket.
281 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800282 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800283 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800284 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800285 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800286 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800287 goto exit;
288 }
Jerry Yu95699e72022-08-21 19:22:23 +0800289
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800290 /* RFC 8446 section 4.2.10
291 *
292 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
293 * the ticket age for the selected PSK identity (computed by subtracting
294 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
295 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800296 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800297 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
298 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800299 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800300 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800301 */
Jerry Yucebffc32022-12-15 18:00:05 +0800302 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800303 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800304 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800305 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800306 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800307 3, ("Ticket age outside tolerance window ( diff = %"
308 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800309 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800310 goto exit;
311 }
Jerry Yu95699e72022-08-21 19:22:23 +0800312#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800313
Ronald Cron38117652024-03-05 09:05:46 +0100314 /*
315 * All good, we have found a suitable ticket.
316 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100317 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
318
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800319exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100320 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 mbedtls_ssl_session_free(session);
322 }
Jerry Yu95699e72022-08-21 19:22:23 +0800323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
325 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800326}
327#endif /* MBEDTLS_SSL_SESSION_TICKETS */
328
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800329MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000330static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_ssl_context *ssl,
332 const unsigned char *identity,
333 size_t identity_len,
334 uint32_t obfuscated_ticket_age,
335 int *psk_type,
336 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000337{
Ronald Cron606671e2023-02-17 11:36:33 +0100338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
339
Jerry Yu95699e72022-08-21 19:22:23 +0800340 ((void) session);
341 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800342 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800345
Jerry Yu95699e72022-08-21 19:22:23 +0800346#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100347 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
348 ssl, identity, identity_len, obfuscated_ticket_age, session);
349 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800350 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100351 ret = mbedtls_ssl_set_hs_psk(ssl,
352 session->resumption_key,
353 session->resumption_key_len);
354 if (ret != 0) {
355 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
356 return ret;
357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358
359 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
360 session->resumption_key,
361 session->resumption_key_len);
362 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
363 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100364 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100365 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
366 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800367 }
368#endif /* MBEDTLS_SSL_SESSION_TICKETS */
369
Jerry Yu1c105562022-07-10 06:32:38 +0000370 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->conf->f_psk != NULL) {
372 if (ssl->conf->f_psk(
373 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100374 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000375 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100376 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000380 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800382 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_ct_memcmp(ssl->conf->psk_identity,
384 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100385 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
386 if (ret != 0) {
387 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
388 return ret;
389 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100390 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000391 }
392
Ronald Cronfbae94a2023-12-05 18:15:14 +0100393 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000394}
395
Ronald Cron38117652024-03-05 09:05:46 +0100396/*
397 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
398 * They are positive to not collide with error codes that are negative. Zero
399 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
400 * by the callers of this function as a generic success condition.
401 */
Ronald Cron6e311272023-12-05 17:57:01 +0100402#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
403#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800404MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000405static int ssl_tls13_offered_psks_check_binder_match(
406 mbedtls_ssl_context *ssl,
407 const unsigned char *binder, size_t binder_len,
408 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000409{
410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800411
Jerry Yudaf375a2022-07-20 21:31:43 +0800412 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000413 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800414 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800415 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800416 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000417
Ronald Crona5c5c582024-03-19 13:54:15 +0100418 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
419 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
420 }
421
Jerry Yu1c105562022-07-10 06:32:38 +0000422 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800423 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200424 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 transcript, sizeof(transcript), &transcript_len);
426 if (ret != 0) {
427 return ret;
428 }
Jerry Yu1c105562022-07-10 06:32:38 +0000429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
431 if (ret != 0) {
432 return ret;
433 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
436 psk, psk_len, psk_type,
437 transcript,
438 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800439#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800441#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ret != 0) {
443 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
444 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000445 }
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
448 server_computed_binder, transcript_len);
449 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000450
Ronald Crona5c5c582024-03-19 13:54:15 +0100451 if (mbedtls_ct_memcmp(server_computed_binder,
452 binder,
453 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100454 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000455 }
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 mbedtls_platform_zeroize(server_computed_binder,
458 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100459 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800460}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800461
Jerry Yu82534862022-08-30 10:42:33 +0800462#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800463MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100464static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
465 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800466{
Jerry Yu82534862022-08-30 10:42:33 +0800467 dst->ticket_age_add = src->ticket_age_add;
468 dst->ticket_flags = src->ticket_flags;
469 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (src->resumption_key_len == 0) {
471 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
472 }
473 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800474
Jerry Yu33bf2402022-12-12 16:01:43 +0800475#if defined(MBEDTLS_SSL_EARLY_DATA)
476 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000477
478#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000479 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000480 if (ret != 0) {
481 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000482 }
483#endif /* MBEDTLS_SSL_ALPN */
484#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800487}
488#endif /* MBEDTLS_SSL_SESSION_TICKETS */
489
Ronald Cron79cdd412024-02-20 17:19:28 +0100490struct psk_attributes {
491 int type;
492 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100493 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100494};
Ronald Cron16cc3702024-03-07 15:04:56 +0100495#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100496
Jerry Yu1c105562022-07-10 06:32:38 +0000497/* Parser for pre_shared_key extension in client hello
498 * struct {
499 * opaque identity<1..2^16-1>;
500 * uint32 obfuscated_ticket_age;
501 * } PskIdentity;
502 *
503 * opaque PskBinderEntry<32..255>;
504 *
505 * struct {
506 * PskIdentity identities<7..2^16-1>;
507 * PskBinderEntry binders<33..2^16-1>;
508 * } OfferedPsks;
509 *
510 * struct {
511 * select (Handshake.msg_type) {
512 * case client_hello: OfferedPsks;
513 * ....
514 * };
515 * } PreSharedKeyExtension;
516 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800517MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000518static int ssl_tls13_parse_pre_shared_key_ext(
519 mbedtls_ssl_context *ssl,
520 const unsigned char *pre_shared_key_ext,
521 const unsigned char *pre_shared_key_ext_end,
522 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100523 const unsigned char *ciphersuites_end,
524 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000525{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800527 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800528 const unsigned char *p_identity_len;
529 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000530 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800531 const unsigned char *binders;
532 const unsigned char *p_binder_len;
533 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000534 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800535 int matched_identity = -1;
536 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
539 pre_shared_key_ext,
540 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000541
Jerry Yu96a2e362022-07-21 15:11:34 +0800542 /* identities_len 2 bytes
543 * identities_data >= 7 bytes
544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
546 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800547 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
549 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800550 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000551
Jerry Yu96a2e362022-07-21 15:11:34 +0800552 /* binders_len 2 bytes
553 * binders >= 33 bytes
554 */
Jerry Yu568ec252022-07-22 21:27:34 +0800555 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
557 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800558 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800560 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000561
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100562 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
563 identities_end - pre_shared_key_ext);
564 if (0 != ret) {
565 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
566 return ret;
567 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000570 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800571 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800572 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800573 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800574 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100575 int psk_ciphersuite_id;
576 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100577 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100578
Jerry Yu82534862022-08-30 10:42:33 +0800579 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
583 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
586 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800587 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800590 binder_len = *p_binder_len;
591 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800593 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800594
Jerry Yu96a2e362022-07-21 15:11:34 +0800595 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000597 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Jerry Yu1c105562022-07-10 06:32:38 +0000599
Jerry Yu96a2e362022-07-21 15:11:34 +0800600 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100602 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100603 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800604 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100608
Ronald Cron79cdd412024-02-20 17:19:28 +0100609 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100611 psk_ciphersuite_id = 0;
612 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100613 allowed_key_exchange_modes =
614 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800615 break;
Jerry Yu82534862022-08-30 10:42:33 +0800616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100617 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100618 psk_ciphersuite_id = session.ciphersuite;
619 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100620 ssl->session_negotiate->ticket_flags = session.ticket_flags;
621 allowed_key_exchange_modes =
622 session.ticket_flags &
623 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800624 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100625#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800626 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800628 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100629
Ronald Cron79cdd412024-02-20 17:19:28 +0100630 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
631
Ronald Croncf284562024-02-16 18:54:10 +0100632 if ((allowed_key_exchange_modes &
633 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
634 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100635 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100636 } else if ((allowed_key_exchange_modes &
637 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
638 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100639 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100640 }
641
Ronald Cron79cdd412024-02-20 17:19:28 +0100642 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100643 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
644 continue;
645 }
646
Ronald Cron89089cc2024-02-14 18:18:08 +0100647 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
648 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100649 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100650
Ronald Cron74a16292024-02-23 17:07:41 +0100651 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100652#if defined(MBEDTLS_SSL_SESSION_TICKETS)
653 mbedtls_ssl_session_free(&session);
654#endif
655 /*
656 * We consider finding a ciphersuite suitable for the PSK as part
657 * of the validation of its binder. Thus if we do not find one, we
658 * abort the handshake with a decrypt_error alert.
659 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800660 MBEDTLS_SSL_PEND_FATAL_ALERT(
661 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100663 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800664 }
665
Jerry Yu96a2e362022-07-21 15:11:34 +0800666 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100667 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100668 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100669 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800670 /* For security reasons, the handshake should be aborted when we
671 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
672 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800673#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800675#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000677 MBEDTLS_SSL_DEBUG_RET(
678 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800679 MBEDTLS_SSL_PEND_FATAL_ALERT(
680 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
682 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800683 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800684
685 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800686
Jerry Yu82534862022-08-30 10:42:33 +0800687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100688 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
690 &session);
691 mbedtls_ssl_session_free(&session);
692 if (ret != 0) {
693 return ret;
694 }
Jerry Yu82534862022-08-30 10:42:33 +0800695 }
696#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (p_identity_len != identities_end || p_binder_len != binders_end) {
700 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
701 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
702 MBEDTLS_ERR_SSL_DECODE_ERROR);
703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000704 }
705
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800706 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000707 ret = ssl->handshake->update_checksum(
708 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100709 if (0 != ret) {
710 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
711 return ret;
712 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100714 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 ssl->handshake->selected_identity = (uint16_t) matched_identity;
719 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000722}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
724/*
725 * struct {
726 * select ( Handshake.msg_type ) {
727 * ....
728 * case server_hello:
729 * uint16 selected_identity;
730 * }
731 * } PreSharedKeyExtension;
732 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
734 unsigned char *buf,
735 unsigned char *end,
736 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000737{
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000739
740 *olen = 0;
741
David Horstmann21b89762022-10-06 18:34:28 +0100742 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000747#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000749 /* We shouldn't have called this extension writer unless we've
750 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000752 }
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
755 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
758 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000761
762 *olen = 6;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
765 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000770}
771
Ronald Cron41a443a2022-10-04 16:38:25 +0200772#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000773
XiaokangQian7807f9f2022-02-15 10:04:37 +0000774/* From RFC 8446:
775 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000776 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 * } SupportedVersions;
778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100780static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
781 const unsigned char *buf,
782 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000785 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000786 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000787 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000791 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792 p += 1;
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000795 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 while (p < versions_end) {
797 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
798 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000799 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800
Ronald Cron3bd2b022023-04-03 16:45:39 +0200801 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100802 found_supported_version = 1;
803 break;
804 }
805
Ronald Cron3bd2b022023-04-03 16:45:39 +0200806 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
807 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100808 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809 break;
810 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811 }
812
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100813 if (!found_supported_version) {
814 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
817 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
818 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819 }
820
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100821 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100824 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825}
826
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200827#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000828/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829 *
830 * From RFC 8446:
831 * enum {
832 * ... (0xFFFF)
833 * } NamedGroup;
834 * struct {
835 * NamedGroup named_group_list<2..2^16-1>;
836 * } NamedGroupList;
837 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200838MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100839static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
840 const unsigned char *buf,
841 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000844 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000845 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
848 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
849 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000850 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000852 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000853 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000856 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
858 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000859 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 MBEDTLS_SSL_DEBUG_MSG(2,
862 ("got named group: %s(%04x)",
863 mbedtls_ssl_named_group_to_str(named_group),
864 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
867 !mbedtls_ssl_named_group_is_supported(named_group) ||
868 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000869 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870 }
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_DEBUG_MSG(2,
873 ("add named group %s(%04x) into received list.",
874 mbedtls_ssl_named_group_to_str(named_group),
875 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800876
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000877 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878 }
879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881
882}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200883#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
XiaokangQian08037552022-04-20 07:16:41 +0000885#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
886
Valerio Settic9ae8622023-07-25 11:23:50 +0200887#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888/*
889 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000890 * extension is correct and stores the first acceptable key share and its
891 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892 *
893 * Possible return values are:
894 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000895 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
896 * the client does not match a group supported by the server. A
897 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000898 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800899 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200900MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100901static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
902 const unsigned char *buf,
903 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904{
XiaokangQianb67384d2022-04-19 00:02:38 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000907 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800908 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909
910 /* From RFC 8446:
911 *
912 * struct {
913 * KeyShareEntry client_shares<0..2^16-1>;
914 * } KeyShareClientHello;
915 *
916 */
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
919 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000922
923 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000924 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925
926 /* We try to find a suitable key share entry and copy it to the
927 * handshake context. Later, we have to find out whether we can do
928 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000929 * dismiss it and send a HelloRetryRequest message.
930 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000933 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800934 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800935 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000936
937 /*
938 * struct {
939 * NamedGroup group;
940 * opaque key_exchange<1..2^16-1>;
941 * } KeyShareEntry;
942 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
944 group = MBEDTLS_GET_UINT16_BE(p, 0);
945 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800946 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800947 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800949 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950
951 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000952 * for input validation purposes.
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
955 !mbedtls_ssl_named_group_is_supported(group) ||
956 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000957 continue;
958 }
XiaokangQian060d8672022-04-21 09:24:56 +0000959
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200961 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200963 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200964 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200965 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_named_group_to_str(group),
967 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200968 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 ssl, key_exchange - 2, key_exchange_len + 2);
970 if (ret != 0) {
971 return ret;
972 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
976 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977 continue;
978 }
979
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000980 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000981 }
982
Jerry Yuc1be19f2022-04-23 16:11:39 +0800983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (ssl->handshake->offered_group_id == 0) {
985 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
986 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000987 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000989}
Valerio Settic9ae8622023-07-25 11:23:50 +0200990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000991
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200992MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100993static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
994 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000995{
Jerry Yu0c354a22022-08-29 15:25:36 +0800996 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000998}
999
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001001MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +00001002static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ssl_tls13_client_hello_has_exts(
1006 ssl,
1007 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1008 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1009 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001010}
Jerry Yue5991322022-11-07 14:03:44 +08001011#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001012
Jerry Yue5991322022-11-07 14:03:44 +08001013#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001014MBEDTLS_CHECK_RETURN_CRITICAL
1015static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return ssl_tls13_client_hello_has_exts(
1019 ssl,
1020 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1021 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001022}
Jerry Yue5991322022-11-07 14:03:44 +08001023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001024
Jerry Yue5991322022-11-07 14:03:44 +08001025#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001026MBEDTLS_CHECK_RETURN_CRITICAL
1027static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ssl_tls13_client_hello_has_exts(
1031 ssl,
1032 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1033 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1034 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1035 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001036}
Jerry Yue5991322022-11-07 14:03:44 +08001037#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001038
Pengyu Lv306a01d2023-02-02 16:35:47 +08001039#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1040MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001041static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001042{
Jerry Yue5991322022-11-07 14:03:44 +08001043#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001044 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001045 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001047#else
1048 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001050#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001051}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052
Jerry Yu77f01482022-07-11 07:03:24 +00001053MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001054static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
Jerry Yue5991322022-11-07 14:03:44 +08001056#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001057 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001058 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001060#else
1061 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001063#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001064}
1065#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1066
1067MBEDTLS_CHECK_RETURN_CRITICAL
1068static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1069{
1070#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1071 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1072 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1073#else
1074 ((void) ssl);
1075 return 0;
1076#endif
1077}
1078
XiaokangQian81802f42022-06-10 13:25:22 +00001079#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001080 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001081
1082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001083static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001084{
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001096 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001098 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001100 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001102 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001104 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001106 }
1107}
1108#endif /* MBEDTLS_USE_PSA_CRYPTO */
1109
XiaokangQian23c5be62022-06-07 02:04:34 +00001110/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001111 * Pick best ( private key, certificate chain ) pair based on the signature
1112 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001113 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001115static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001116{
XiaokangQianfb665a82022-06-15 03:57:21 +00001117 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001118 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001119
1120#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001122 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001124#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (key_cert_list == NULL) {
1128 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1129 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1133 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 for (key_cert = key_cert_list; key_cert != NULL;
1142 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001143#if defined(MBEDTLS_USE_PSA_CRYPTO)
1144 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1145#endif /* MBEDTLS_USE_PSA_CRYPTO */
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1148 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001149
1150 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 * This avoids sending the client a cert it'll reject based on
1152 * keyUsage or other extensions.
1153 */
1154 if (mbedtls_x509_crt_check_key_usage(
1155 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001156 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001157 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1159 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1160 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001161 continue;
1162 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_SSL_DEBUG_MSG(3,
1165 ("ssl_tls13_pick_key_cert:"
1166 "check signature algorithm %s [%04x]",
1167 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1168 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001171#endif /* MBEDTLS_USE_PSA_CRYPTO */
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1174 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001175#if defined(MBEDTLS_USE_PSA_CRYPTO)
1176 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1178 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001179#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001181 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 MBEDTLS_SSL_DEBUG_MSG(3,
1183 ("ssl_tls13_pick_key_cert:"
1184 "selected signature algorithm"
1185 " %s [%04x]",
1186 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1187 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001188 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 3, "selected certificate (chain)",
1190 ssl->handshake->key_cert->cert);
1191 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001192 }
XiaokangQian81802f42022-06-10 13:25:22 +00001193 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001194 }
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1197 "no suitable certificate found"));
1198 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001199}
XiaokangQian81802f42022-06-10 13:25:22 +00001200#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001201 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001202
XiaokangQian4080a7f2022-04-11 09:55:18 +00001203/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001204 *
1205 * STATE HANDLING: ClientHello
1206 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001207 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001208 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001209 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 *
1211 * In this case, the server progresses to sending its ServerHello.
1212 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001213 * 2) The ClientHello was well-formed but didn't match the server's
1214 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001215 *
1216 * For example, the client might not have offered a key share which
1217 * the server supports, or the server might require a cookie.
1218 *
1219 * In this case, the server sends a HelloRetryRequest.
1220 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001221 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001222 *
1223 * In this case, we abort the handshake.
1224 *
1225 */
1226
1227/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001228 * Structure of this message:
1229 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001230 * uint16 ProtocolVersion;
1231 * opaque Random[32];
1232 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001233 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001234 * struct {
1235 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1236 * Random random;
1237 * opaque legacy_session_id<0..32>;
1238 * CipherSuite cipher_suites<2..2^16-2>;
1239 * opaque legacy_compression_methods<1..2^8-1>;
1240 * Extension extensions<8..2^16-1>;
1241 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001242 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001243
1244#define SSL_CLIENT_HELLO_OK 0
1245#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001246#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001247
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001249static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1250 const unsigned char *buf,
1251 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001252{
XiaokangQianb67384d2022-04-19 00:02:38 +00001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1254 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001255 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001256 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001257 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001258 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001259 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001260 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001261 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001262 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001263 const unsigned char *supported_versions_data;
1264 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001265 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001266 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001267 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268
Ronald Cron41a443a2022-10-04 16:38:25 +02001269#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001270 int got_psk = 0;
1271 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001272 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001273 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001274#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001277 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001278 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001279 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001280 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001282 * .. . .. ciphersuite list length ( 2 bytes )
1283 * .. . .. ciphersuite list
1284 * .. . .. compression alg. list length ( 1 byte )
1285 * .. . .. compression alg. list
1286 * .. . .. extensions length ( 2 bytes, optional )
1287 * .. . .. extensions ( optional )
1288 */
1289
XiaokangQianb67384d2022-04-19 00:02:38 +00001290 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001291 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001292 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1293 * read at least up to session id length without worrying.
1294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296
1297 /* ...
1298 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1299 * ...
1300 * with ProtocolVersion defined as:
1301 * uint16 ProtocolVersion;
1302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1304 MBEDTLS_SSL_VERSION_TLS1_2) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1306 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1307 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1308 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 }
1310 p += 2;
1311
Jerry Yuc1be19f2022-04-23 16:11:39 +08001312 /* ...
1313 * Random random;
1314 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 * with Random defined as:
1316 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 */
Ronald Crond540d992023-03-07 09:41:48 +01001318 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001319 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320
Jerry Yuc1be19f2022-04-23 16:11:39 +08001321 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001323 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 */
Ronald Croncada4102023-03-07 09:51:39 +01001325 legacy_session_id_len = *(p++);
1326 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
XiaokangQianb67384d2022-04-19 00:02:38 +00001328 /*
1329 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001330 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001333 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001335 /* ...
1336 * CipherSuite cipher_suites<2..2^16-2>;
1337 * ...
1338 * with CipherSuite defined as:
1339 * uint8 CipherSuite[2];
1340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001343 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344
Ronald Cronfc7ae872023-02-16 15:32:19 +01001345 /*
1346 * The length of the ciphersuite list has to be even.
1347 */
1348 if (cipher_suites_len & 1) {
1349 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1350 MBEDTLS_ERR_SSL_DECODE_ERROR);
1351 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1352 }
1353
XiaokangQianb67384d2022-04-19 00:02:38 +00001354 /* Check we have enough data for the ciphersuite list, the legacy
1355 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001356 *
1357 * cipher_suites cipher_suites_len bytes
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001358 * legacy_compression_methods length 1 byte
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001360 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 1);
Ronald Cron8c527d02023-03-07 15:47:47 +01001361 p += cipher_suites_len;
1362 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001363
Waleed Elmelegy0a9e8a32024-06-25 10:22:49 +01001364 /* Check if we have enough data for legacy_compression_methods
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001365 * and the length of the extensions (2 bytes).
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001366 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001367 MBEDTLS_SSL_CHK_BUF_READ_PTR(p + 1, end, p[0] + 2);
Waleed Elmelegyb6e73312024-06-11 18:44:48 +01001368
Jerry Yu5725f1c2022-08-21 17:27:16 +08001369 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001370 * Search for the supported versions extension and parse it to determine
1371 * if the client supports TLS 1.3.
1372 */
1373 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001374 ssl, p + 1 + p[0], end,
Ronald Croneff56732023-04-03 17:36:31 +02001375 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001376 if (ret < 0) {
1377 MBEDTLS_SSL_DEBUG_RET(1,
1378 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1379 return ret;
1380 }
1381
1382 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001383 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001384 }
1385
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001386 if (ret == 1) {
1387 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001388 supported_versions_data,
1389 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001390 if (ret < 0) {
1391 MBEDTLS_SSL_DEBUG_RET(1,
1392 ("ssl_tls13_parse_supported_versions_ext"), ret);
1393 return ret;
1394 }
1395
Ronald Cronb828c7d2023-04-03 16:37:22 +02001396 /*
1397 * The supported versions extension was parsed successfully as the
1398 * value returned by ssl_tls13_parse_supported_versions_ext() is
1399 * positive. The return value is then equal to
1400 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1401 * the TLS version to negotiate.
1402 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001403 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1404 return SSL_CLIENT_HELLO_TLS1_2;
1405 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 }
1407
1408 /*
1409 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001410 */
1411 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001412 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1413 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001414
1415 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001416 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001417 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001418 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001419 */
1420 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1421 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1422 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1423
Ronald Croncada4102023-03-07 09:51:39 +01001424 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1425 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1426 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1427 }
1428 ssl->session_negotiate->id_len = legacy_session_id_len;
1429 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1430 legacy_session_id, legacy_session_id_len);
1431 memcpy(&ssl->session_negotiate->id[0],
1432 legacy_session_id, legacy_session_id_len);
1433
Ronald Crond540d992023-03-07 09:41:48 +01001434 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001435 * Search for a matching ciphersuite
1436 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001437 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1438 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001439
Ronald Cron89089cc2024-02-14 18:18:08 +01001440 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1441 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (handshake->ciphersuite_info == NULL) {
1444 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1445 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1446 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001447 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001448 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1449
1450 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1451 ((unsigned) handshake->ciphersuite_info->id),
1452 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001453
XiaokangQian4080a7f2022-04-11 09:55:18 +00001454 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001455 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001456 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1459 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1460 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1461 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1462 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001464 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465
Jerry Yuc1be19f2022-04-23 16:11:39 +08001466 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001467 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001468 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001469 * with Extension defined as:
1470 * struct {
1471 * ExtensionType extension_type;
1472 * opaque extension_data<0..2^16-1>;
1473 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001476 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001478 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001481 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001484 unsigned int extension_type;
1485 size_t extension_data_len;
1486 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001487 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1488
Ronald Cron5fbd2702024-02-14 10:03:36 +01001489 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001490 /* Do not accept early data extension in 2nd ClientHello */
1491 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1492 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001493
Jerry Yu0c354a22022-08-29 15:25:36 +08001494 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001495 *
1496 * The "pre_shared_key" extension MUST be the last extension in the
1497 * ClientHello (this facilitates implementation as described below).
1498 * Servers MUST check that it is the last extension and otherwise fail
1499 * the handshake with an "illegal_parameter" alert.
1500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001502 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001504 MBEDTLS_SSL_PEND_FATAL_ALERT(
1505 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1507 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001508 }
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1511 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1512 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513 p += 4;
1514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516 extension_data_end = p + extension_data_len;
1517
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001518 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001520 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (ret != 0) {
1522 return ret;
1523 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001526#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1527 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1529 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1530 extension_data_end);
1531 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001532 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 1, "mbedtls_ssl_parse_servername_ext", ret);
1534 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001535 }
XiaokangQian40a35232022-05-07 09:02:40 +00001536 break;
1537#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1538
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001539#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001540 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542
1543 /* Supported Groups Extension
1544 *
1545 * When sent by the client, the "supported_groups" extension
1546 * indicates the named groups which the client supports,
1547 * ordered from most preferred to least preferred.
1548 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001549 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 ssl, p, extension_data_end);
1551 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001552 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001553 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001555 }
1556
XiaokangQian7807f9f2022-02-15 10:04:37 +00001557 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001558#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559
Valerio Settic9ae8622023-07-25 11:23:50 +02001560#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563
1564 /*
1565 * Key Share Extension
1566 *
1567 * When sent by the client, the "key_share" extension
1568 * contains the endpoint's cryptographic parameters for
1569 * ECDHE/DHE key establishment methods.
1570 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001571 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 ssl, p, extension_data_end);
1573 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001574 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1575 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001576 }
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001579 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 1, "ssl_tls13_parse_key_shares_ext", ret);
1581 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001582 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001583
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001585#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586
1587 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001588 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589 break;
1590
Ronald Cron41a443a2022-10-04 16:38:25 +02001591#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001592 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001593 MBEDTLS_SSL_DEBUG_MSG(
1594 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001595
1596 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 ssl, p, extension_data_end);
1598 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001599 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1601 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001602 }
1603
Jerry Yue19e3b92022-07-08 12:04:51 +00001604 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001605#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001606
Jerry Yu1c105562022-07-10 06:32:38 +00001607 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1609 if ((handshake->received_extensions &
1610 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001611 MBEDTLS_SSL_PEND_FATAL_ALERT(
1612 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1614 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001615 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001616#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001617 /* Delay processing of the PSK identity once we have
1618 * found out which algorithms to use. We keep a pointer
1619 * to the buffer and the size for later processing.
1620 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001621 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001622 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001623#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001624 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001625
XiaokangQianacb39922022-06-17 10:18:48 +00001626#if defined(MBEDTLS_SSL_ALPN)
1627 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1631 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001632 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1634 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001635 }
XiaokangQianacb39922022-06-17 10:18:48 +00001636 break;
1637#endif /* MBEDTLS_SSL_ALPN */
1638
Ronald Cron928cbd32022-10-04 16:14:26 +02001639#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001640 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001642
Gabor Mezei078e8032022-04-27 21:17:56 +02001643 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ssl, p, extension_data_end);
1645 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001646 MBEDTLS_SSL_DEBUG_RET(
1647 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001649 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001650 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001651#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001652
Jan Bruckner151f6422023-02-10 12:45:19 +01001653#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1654 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1655 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1656
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001657 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1658 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001659 if (ret != 0) {
1660 MBEDTLS_SSL_DEBUG_RET(
1661 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1662 return ret;
1663 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001664 break;
1665#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1666
XiaokangQian7807f9f2022-02-15 10:04:37 +00001667 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001668 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001669 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001671 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001672 }
1673
1674 p += extension_data_len;
1675 }
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1678 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001679
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001680 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1681 MBEDTLS_SSL_HS_CLIENT_HELLO,
1682 p - buf);
1683 if (0 != ret) {
1684 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1685 return ret;
1686 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001687
Ronald Cron41a443a2022-10-04 16:38:25 +02001688#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001689 /* Update checksum with either
1690 * - The entire content of the CH message, if no PSK extension is present
1691 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001692 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001693 * ClientHello even if some pre-requisites for PSK key exchange modes are
1694 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001696 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001697 ret = handshake->update_checksum(ssl, buf,
1698 pre_shared_key_ext - buf);
1699 if (0 != ret) {
1700 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1701 return ret;
1702 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1704 pre_shared_key_ext,
1705 pre_shared_key_ext_end,
1706 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001707 cipher_suites_end,
1708 &psk);
1709 if (ret == 0) {
1710 got_psk = 1;
1711 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001712 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1714 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001717#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001718 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001719 ret = handshake->update_checksum(ssl, buf, p - buf);
1720 if (0 != ret) {
1721 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1722 return ret;
1723 }
Jerry Yu1c105562022-07-10 06:32:38 +00001724 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001725
Ronald Cron79cdd412024-02-20 17:19:28 +01001726 /*
1727 * Determine the key exchange algorithm to use.
1728 * There are three types of key exchanges supported in TLS 1.3:
1729 * - (EC)DH with ECDSA,
1730 * - (EC)DH with PSK,
1731 * - plain PSK.
1732 *
1733 * The PSK-based key exchanges may additionally be used with 0-RTT.
1734 *
1735 * Our built-in order of preference is
1736 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1737 * 2 ) Certificate Mode ( ephemeral )
1738 * 3 ) Plain PSK Mode ( psk )
1739 */
1740#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1741 if (got_psk && (psk.key_exchange_mode ==
1742 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1743 handshake->key_exchange_mode =
1744 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1745 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1746
1747 } else
1748#endif
1749 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1750 handshake->key_exchange_mode =
1751 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1752 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1753
1754 }
1755#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1756 else if (got_psk && (psk.key_exchange_mode ==
1757 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1758 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1759 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1760 }
1761#endif
1762 else {
1763 MBEDTLS_SSL_DEBUG_MSG(
1764 1,
1765 ("ClientHello message misses mandatory extensions."));
1766 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1767 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1768 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001770
Ronald Cron3e47eec2024-02-21 10:29:24 +01001771#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001772 if (handshake->key_exchange_mode &
1773 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1774 handshake->ciphersuite_info = psk.ciphersuite_info;
1775 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1776
1777 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1778 ((unsigned) psk.ciphersuite_info->id),
1779 psk.ciphersuite_info->name));
1780
1781 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1782 handshake->resume = 1;
1783 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001784 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001785#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001786
1787 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001788 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1789 hrr_required = (no_usable_share_for_key_agreement != 0);
1790 }
1791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001795}
1796
Jerry Yuab0da372023-02-08 13:55:24 +08001797#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001798static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001799{
1800 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1801
Jerry Yu985c9672022-12-04 14:06:30 +08001802 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1803 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001804 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001805 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001806 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001807 }
1808
Jerry Yu454dda32023-10-31 15:13:54 +08001809 if (!handshake->resume) {
1810 /* We currently support early data only in the case of PSKs established
1811 via a NewSessionTicket message thus in the case of a session
1812 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001813 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001814 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001815 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001816 }
1817
Jerry Yu82fd6c12023-10-31 16:32:19 +08001818 /* RFC 8446 4.2.10
1819 *
1820 * In order to accept early data, the server MUST have accepted a PSK cipher
1821 * suite and selected the first key offered in the client's "pre_shared_key"
1822 * extension. In addition, it MUST verify that the following values are the
1823 * same as those associated with the selected PSK:
1824 * - The TLS version number
1825 * - The selected cipher suite
1826 * - The selected ALPN [RFC7301] protocol, if any
1827 *
1828 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001829 * - The TLS version number is checked in
1830 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001831 */
1832
1833 if (handshake->selected_identity != 0) {
1834 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001835 1, ("EarlyData: rejected, the selected key in "
1836 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001837 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001838 }
1839
1840 if (handshake->ciphersuite_info->id !=
1841 ssl->session_negotiate->ciphersuite) {
1842 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001843 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1844 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001845 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001846
1847 }
Jerry Yu985c9672022-12-04 14:06:30 +08001848
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001849 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001850 MBEDTLS_SSL_DEBUG_MSG(
1851 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001852 ("EarlyData: rejected, early_data not allowed in ticket "
1853 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001854 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001855 }
Jerry Yu985c9672022-12-04 14:06:30 +08001856
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001857#if defined(MBEDTLS_SSL_ALPN)
1858 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1859 size_t alpn_len;
1860
1861 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1862 return 0;
1863 }
1864
1865 if (alpn != NULL) {
1866 alpn_len = strlen(alpn);
1867 }
1868
1869 if (alpn == NULL ||
1870 ssl->session_negotiate->ticket_alpn == NULL ||
1871 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1872 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1873 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1874 "from the one associated with the pre-shared key."));
1875 return -1;
1876 }
1877#endif
1878
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001879 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001880}
1881#endif /* MBEDTLS_SSL_EARLY_DATA */
1882
XiaokangQian75fe8c72022-06-15 09:42:45 +00001883/* Update the handshake state machine */
1884
Ronald Cronce7d76e2022-07-08 18:56:49 +02001885MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001886static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1887 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001888{
1889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1890
XiaokangQian7807f9f2022-02-15 10:04:37 +00001891 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001892 * Server certificate selection
1893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1895 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1896 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001897 }
1898#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1899 ssl->handshake->sni_name = NULL;
1900 ssl->handshake->sni_name_len = 0;
1901#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1904 if (ret != 0) {
1905 MBEDTLS_SSL_DEBUG_RET(1,
1906 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1907 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001908 }
1909
Jerry Yuab0da372023-02-08 13:55:24 +08001910#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001911 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001912 ssl->handshake->early_data_accepted =
1913 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001914
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001915 if (ssl->handshake->early_data_accepted) {
1916 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1917 if (ret != 0) {
1918 MBEDTLS_SSL_DEBUG_RET(
1919 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1920 return ret;
1921 }
1922 } else {
1923 ssl->discard_early_data_record =
1924 hrr_required ?
1925 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1926 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001927 }
1928 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001929#else
1930 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001931#endif /* MBEDTLS_SSL_EARLY_DATA */
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001934}
1935
1936/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001937 * Main entry point from the state machine; orchestrates the otherfunctions.
1938 */
1939
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001940MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001941static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001942{
1943
XiaokangQian08037552022-04-20 07:16:41 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001946 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001947 int parse_client_hello_ret;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1952 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1953 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1956 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001957 parse_client_hello_ret = ret; /* Store positive return value of
1958 * parse_client_hello,
1959 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001960 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001961
Ronald Cronb828c7d2023-04-03 16:37:22 +02001962 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001963 * Version 1.2 of the protocol has to be used for the handshake.
1964 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001965 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1966 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1967 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1968 * will dispatch to the TLS 1.2 state machine.
1969 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001970 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001971 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001972 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001973 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001974 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001975 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001976 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1977 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1978 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001979 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001980 ssl->keep_current_message = 1;
1981 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1982 return 0;
1983 }
1984
Ronald Cron7b6ee942024-01-12 10:29:55 +01001985 MBEDTLS_SSL_PROC_CHK(
1986 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1987 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001988
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001989 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1991 } else {
1992 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1993 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001994
1995cleanup:
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1998 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001999}
2000
2001/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08002002 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08002003 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002004MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002005static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002006{
Jerry Yu637a3f12022-04-20 21:37:58 +08002007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2008 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2012 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2013 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2014 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002015 }
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2018 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002019
2020#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002021 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002022#endif /* MBEDTLS_HAVE_TIME */
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002025}
2026
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002028 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029 *
2030 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002031 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032 * } SupportedVersions;
2033 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002034MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002035static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 mbedtls_ssl_context *ssl,
2037 unsigned char *buf,
2038 unsigned char *end,
2039 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041 *out_len = 0;
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
2045 /* Check if we have space to write the extension:
2046 * - extension_type (2 bytes)
2047 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002048 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 mbedtls_ssl_write_version(buf + 4,
2057 ssl->conf->transport,
2058 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2061 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062
Jerry Yu349a6132022-04-14 20:52:56 +08002063 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064
Jerry Yub95dd362022-11-08 21:19:34 +08002065 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069}
2070
Jerry Yud9436a12022-04-20 22:28:09 +08002071
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072
2073/* Generate and export a single key share. For hybrid KEMs, this can
2074 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002075MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002076static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2077 uint16_t named_group,
2078 unsigned char *buf,
2079 unsigned char *end,
2080 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081{
2082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002083
2084 *out_len = 0;
2085
Valerio Settic9ae8622023-07-25 11:23:50 +02002086#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002087 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002088 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002089 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 ssl, named_group, buf, end, out_len);
2091 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002092 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002093 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 ret);
2095 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002098#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (0 /* Other kinds of KEMs */) {
2100 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002101 ((void) ssl);
2102 ((void) named_group);
2103 ((void) buf);
2104 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2106 }
2107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109}
2110
2111/*
2112 * ssl_tls13_write_key_share_ext
2113 *
2114 * Structure of key_share extension in ServerHello:
2115 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002116 * struct {
2117 * NamedGroup group;
2118 * opaque key_exchange<1..2^16-1>;
2119 * } KeyShareEntry;
2120 * struct {
2121 * KeyShareEntry server_share;
2122 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002123 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002125static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2126 unsigned char *buf,
2127 unsigned char *end,
2128 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129{
Jerry Yu955ddd72022-04-22 22:27:33 +08002130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002132 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135
2136 *out_len = 0;
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2141 mbedtls_ssl_named_group_to_str(group),
2142 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002143
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144 /* Check if we have space for header and length fields:
2145 * - extension_type (2 bytes)
2146 * - extension_data_length (2 bytes)
2147 * - group (2 bytes)
2148 * - key_exchange_length (2 bytes)
2149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2151 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2152 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002154
Jerry Yu3bf2c642022-03-30 22:02:12 +08002155 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2156 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002157 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 ssl, group, server_share + 4, end, &key_exchange_length);
2159 if (ret != 0) {
2160 return ret;
2161 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002162 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002167
Jerry Yu57d48412022-04-20 21:50:42 +08002168 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002173}
Jerry Yud9436a12022-04-20 22:28:09 +08002174
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002175MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002176static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2177 unsigned char *buf,
2178 unsigned char *end,
2179 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002180{
2181 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2182 /* key_share Extension
2183 *
2184 * struct {
2185 * select (Handshake.msg_type) {
2186 * ...
2187 * case hello_retry_request:
2188 * NamedGroup selected_group;
2189 * ...
2190 * };
2191 * } KeyShare;
2192 */
2193
2194 *out_len = 0;
2195
Jerry Yub0ac10b2022-05-05 11:10:08 +08002196 /*
2197 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2198 * of the HRR is then to transmit a cookie to force the client to demonstrate
2199 * reachability at their apparent network address (primarily useful for DTLS).
2200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2202 return 0;
2203 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002204
2205 /* We should only send the key_share extension if the client's initial
2206 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (ssl->handshake->offered_group_id != 0) {
2208 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2209 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002210 }
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (selected_group == 0) {
2213 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2214 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002215 }
2216
Jerry Yub0ac10b2022-05-05 11:10:08 +08002217 /* Check if we have enough space:
2218 * - extension_type (2 bytes)
2219 * - extension_data_length (2 bytes)
2220 * - selected_group (2 bytes)
2221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2225 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2226 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 MBEDTLS_SSL_DEBUG_MSG(3,
2229 ("HRR selected_group: %s (%x)",
2230 mbedtls_ssl_named_group_to_str(selected_group),
2231 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002232
2233 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002238}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002239
2240/*
2241 * Structure of ServerHello message:
2242 *
2243 * struct {
2244 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2245 * Random random;
2246 * opaque legacy_session_id_echo<0..32>;
2247 * CipherSuite cipher_suite;
2248 * uint8 legacy_compression_method = 0;
2249 * Extension extensions<6..2^16-1>;
2250 * } ServerHello;
2251 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002253static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2254 unsigned char *buf,
2255 unsigned char *end,
2256 size_t *out_len,
2257 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002258{
Jerry Yu955ddd72022-04-22 22:27:33 +08002259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002261 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002262 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263
2264 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002265 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002266
Jerry Yucfc04b32022-04-21 09:31:58 +08002267 /* ...
2268 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2269 * ...
2270 * with ProtocolVersion defined as:
2271 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2274 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275 p += 2;
2276
Jerry Yu1c3e6882022-04-20 21:23:40 +08002277 /* ...
2278 * Random random;
2279 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002280 * with Random defined as:
2281 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2284 if (is_hrr) {
2285 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2286 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2287 } else {
2288 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2289 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002290 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2292 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2294
Jerry Yucfc04b32022-04-21 09:31:58 +08002295 /* ...
2296 * opaque legacy_session_id_echo<0..32>;
2297 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2300 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2301 if (ssl->session_negotiate->id_len > 0) {
2302 memcpy(p, &ssl->session_negotiate->id[0],
2303 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002304 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2307 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002308 }
2309
Jerry Yucfc04b32022-04-21 09:31:58 +08002310 /* ...
2311 * CipherSuite cipher_suite;
2312 * ...
2313 * with CipherSuite defined as:
2314 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2317 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002318 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_DEBUG_MSG(3,
2320 ("server hello, chosen ciphersuite: %s ( id=%d )",
2321 mbedtls_ssl_get_ciphersuite_name(
2322 ssl->session_negotiate->ciphersuite),
2323 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Jerry Yucfc04b32022-04-21 09:31:58 +08002325 /* ...
2326 * uint8 legacy_compression_method = 0;
2327 * ...
2328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002330 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002331
Jerry Yucfc04b32022-04-21 09:31:58 +08002332 /* ...
2333 * Extension extensions<6..2^16-1>;
2334 * ...
2335 * struct {
2336 * ExtensionType extension_type; (2 bytes)
2337 * opaque extension_data<0..2^16-1>;
2338 * } Extension;
2339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002341 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002342 p += 2;
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2345 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002346 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2348 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002349 }
2350 p += output_len;
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2353 if (is_hrr) {
2354 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2355 } else {
2356 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2357 }
2358 if (ret != 0) {
2359 return ret;
2360 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002361 p += output_len;
2362 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002363
Ronald Cron41a443a2022-10-04 16:38:25 +02002364#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2366 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2367 if (ret != 0) {
2368 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2369 ret);
2370 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002371 }
2372 p += output_len;
2373 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002374#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2379 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002380
Jerry Yud9436a12022-04-20 22:28:09 +08002381 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002384
Jerry Yu7de2ff02022-11-08 21:43:46 +08002385 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002386 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_HS_SERVER_HELLO,
2388 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002391}
2392
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002393MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002394static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002395{
2396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2398 if (ret != 0) {
2399 MBEDTLS_SSL_DEBUG_RET(1,
2400 "mbedtls_ssl_tls13_compute_handshake_transform",
2401 ret);
2402 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002403 }
2404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002406}
2407
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002408MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002409static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002410{
Jerry Yu637a3f12022-04-20 21:37:58 +08002411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002412 unsigned char *buf;
2413 size_t buf_len, msg_len;
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002418
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002419 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2420 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2423 buf + buf_len,
2424 &msg_len,
2425 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002426
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002427 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002428 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2431 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002432
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002433 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002434
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002435#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2436 /* The server sends a dummy change_cipher_spec record immediately
2437 * after its first handshake message. This may either be after
2438 * a ServerHello or a HelloRetryRequest.
2439 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002440 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002442#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002444#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002445
Jerry Yuf4b27e42022-03-30 17:32:21 +08002446cleanup:
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2449 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002450}
2451
Jerry Yu23d1a252022-05-12 18:08:59 +08002452
2453/*
2454 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2455 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002457static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002458{
2459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002460 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2462 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2463 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2464 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002465 }
2466
2467 /*
2468 * Create stateless transcript hash for HRR
2469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2471 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2472 if (ret != 0) {
2473 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2474 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002479}
2480
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002481MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002482static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002483{
2484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2485 unsigned char *buf;
2486 size_t buf_len, msg_len;
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2493 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2494 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2497 buf + buf_len,
2498 &msg_len,
2499 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002500 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002501 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002502
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2505 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002506
Ronald Cron5fbd2702024-02-14 10:03:36 +01002507 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002508
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002509#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2510 /* The server sends a dummy change_cipher_spec record immediately
2511 * after its first handshake message. This may either be after
2512 * a ServerHello or a HelloRetryRequest.
2513 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002514 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002516#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002518#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002519
2520cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2522 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002523}
2524
Jerry Yu5b64ae92022-03-30 17:15:02 +08002525/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002526 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2527 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002528
2529/*
2530 * struct {
2531 * Extension extensions<0..2 ^ 16 - 1>;
2532 * } EncryptedExtensions;
2533 *
2534 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2537 unsigned char *buf,
2538 unsigned char *end,
2539 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002540{
XiaokangQianacb39922022-06-17 10:18:48 +00002541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542 unsigned char *p = buf;
2543 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002544 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002545 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002546
Jerry Yu4d3841a2022-04-16 12:37:19 +08002547 *out_len = 0;
2548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002550 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002551 p += 2;
2552
Jerry Yu8937eb42022-05-03 12:12:14 +08002553 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002554 ((void) ret);
2555 ((void) output_len);
2556
2557#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2559 if (ret != 0) {
2560 return ret;
2561 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002562 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002563#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564
Jerry Yu71c14f12022-12-12 11:10:35 +08002565#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002566 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002567 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002568 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002569 if (ret != 0) {
2570 return ret;
2571 }
2572 p += output_len;
2573 }
2574#endif /* MBEDTLS_SSL_EARLY_DATA */
2575
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002576#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002577 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002578 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2579 ssl, p, end, &output_len);
2580 if (ret != 0) {
2581 return ret;
2582 }
2583 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002584 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002585#endif
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 extensions_len = (p - p_extensions_len) - 2;
2588 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002589
2590 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593
Jerry Yu7de2ff02022-11-08 21:43:46 +08002594 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598}
2599
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002600MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002601static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602{
2603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2604 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002605 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 mbedtls_ssl_set_outbound_transform(ssl,
2608 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002609 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002613
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002614 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2615 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2616 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2619 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002620
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002621 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002622 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2623 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2626 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002627
Ronald Cron928cbd32022-10-04 16:14:26 +02002628#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2630 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2631 } else {
2632 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2633 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002634#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002636#endif
2637
Jerry Yu4d3841a2022-04-16 12:37:19 +08002638cleanup:
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2641 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002642}
2643
Ronald Cron928cbd32022-10-04 16:14:26 +02002644#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002645#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2646#define SSL_CERTIFICATE_REQUEST_SKIP 1
2647/* Coordination:
2648 * Check whether a CertificateRequest message should be written.
2649 * Returns a negative code on failure, or
2650 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2651 * - SSL_CERTIFICATE_REQUEST_SKIP
2652 * indicating if the writing of the CertificateRequest
2653 * should be skipped or not.
2654 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002655MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002656static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002657{
2658 int authmode;
2659
XiaokangQian40a35232022-05-07 09:02:40 +00002660#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002662 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002664#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002665 authmode = ssl->conf->authmode;
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002668 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002670 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002671
XiaokangQianc3017f62022-05-13 05:55:41 +00002672 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002675}
2676
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677/*
2678 * struct {
2679 * opaque certificate_request_context<0..2^8-1>;
2680 * Extension extensions<2..2^16-1>;
2681 * } CertificateRequest;
2682 *
2683 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002684MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002685static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2686 unsigned char *buf,
2687 const unsigned char *end,
2688 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689{
2690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2691 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002692 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693 unsigned char *p_extensions_len;
2694
2695 *out_len = 0;
2696
2697 /* Check if we have enough space:
2698 * - certificate_request_context (1 byte)
2699 * - extensions length (2 bytes)
2700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702
2703 /*
2704 * Write certificate_request_context
2705 */
2706 /*
2707 * We use a zero length context for the normal handshake
2708 * messages. For post-authentication handshake messages
2709 * this request context would be set to a non-zero value.
2710 */
2711 *p++ = 0x0;
2712
2713 /*
2714 * Write extensions
2715 */
2716 /* The extensions must contain the signature_algorithms. */
2717 p_extensions_len = p;
2718 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2720 if (ret != 0) {
2721 return ret;
2722 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723
XiaokangQianec6efb92022-05-06 09:53:10 +00002724 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726
2727 *out_len = p - buf;
2728
Jerry Yu7de2ff02022-11-08 21:43:46 +08002729 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002733}
2734
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002736static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002737{
2738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002745 unsigned char *buf;
2746 size_t buf_len, msg_len;
2747
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002748 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2749 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2750 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2753 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002755 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002756 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2757 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2760 ssl, buf_len, msg_len));
2761 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2762 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002763 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 } else {
2765 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002766 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2767 goto cleanup;
2768 }
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002771cleanup:
2772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2774 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002775}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002776
Jerry Yu4d3841a2022-04-16 12:37:19 +08002777/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002778 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002779 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002780MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002781static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002782{
Jerry Yu5a26f302022-05-10 20:46:40 +08002783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002784
2785#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2787 mbedtls_ssl_own_cert(ssl) == NULL) {
2788 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2789 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2790 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2791 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002792 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002793#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2796 if (ret != 0) {
2797 return ret;
2798 }
2799 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2800 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002801}
2802
2803/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002804 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002805 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002806MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002807static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002808{
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2810 if (ret != 0) {
2811 return ret;
2812 }
2813 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2814 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002815}
Ronald Cron928cbd32022-10-04 16:14:26 +02002816#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002817
Jerry Yu9b72e392023-12-01 16:27:08 +08002818/*
2819 * RFC 8446 section A.2
2820 *
2821 * | Send ServerHello
2822 * | K_send = handshake
2823 * | Send EncryptedExtensions
2824 * | [Send CertificateRequest]
2825 * Can send | [Send Certificate + CertificateVerify]
2826 * app data | Send Finished
2827 * after --> | K_send = application
2828 * here +--------+--------+
2829 * No 0-RTT | | 0-RTT
2830 * | |
2831 * K_recv = handshake | | K_recv = early data
2832 * [Skip decrypt errors] | +------> WAIT_EOED -+
2833 * | | Recv | | Recv EndOfEarlyData
2834 * | | early data | | K_recv = handshake
2835 * | +------------+ |
2836 * | |
2837 * +> WAIT_FLIGHT2 <--------+
2838 * |
2839 * +--------+--------+
2840 * No auth | | Client auth
2841 * | |
2842 * | v
2843 * | WAIT_CERT
2844 * | Recv | | Recv Certificate
2845 * | empty | v
2846 * | Certificate | WAIT_CV
2847 * | | | Recv
2848 * | v | CertificateVerify
2849 * +-> WAIT_FINISHED <---+
2850 * | Recv Finished
2851 *
2852 *
2853 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002854 * above diagram. We are not going to receive early data related messages
2855 * anymore, prepare to receive the first handshake message of the client
2856 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002857 */
Jerry Yu3be85072023-12-04 09:58:54 +08002858static void ssl_tls13_prepare_for_handshake_second_flight(
2859 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002860{
Jerry Yu9b72e392023-12-01 16:27:08 +08002861 if (ssl->handshake->certificate_request_sent) {
2862 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2863 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002864 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002865 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002866
Jerry Yu9b72e392023-12-01 16:27:08 +08002867 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2868 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002869}
2870
Jerry Yu83da34e2022-04-16 13:59:52 +08002871/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002872 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002873 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002874MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002875static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002876{
Jerry Yud6e253d2022-05-18 13:59:24 +08002877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2880 if (ret != 0) {
2881 return ret;
2882 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2885 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002886 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2888 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2889 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002890 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002891
Jerry Yu87b5ed42022-12-12 13:07:07 +08002892#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002893 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002894 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002895 MBEDTLS_SSL_DEBUG_MSG(
2896 1, ("Switch to early keys for inbound traffic. "
2897 "( K_recv = early data )"));
2898 mbedtls_ssl_set_inbound_transform(
2899 ssl, ssl->handshake->transform_earlydata);
2900 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2901 return 0;
2902 }
2903#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002904 MBEDTLS_SSL_DEBUG_MSG(
2905 1, ("Switch to handshake keys for inbound traffic "
2906 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002908
Jerry Yu3be85072023-12-04 09:58:54 +08002909 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002910
2911 return 0;
2912}
2913
Jerry Yu87b5ed42022-12-12 13:07:07 +08002914#if defined(MBEDTLS_SSL_EARLY_DATA)
2915/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002916 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002917 */
Jerry Yu3be85072023-12-04 09:58:54 +08002918#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002919#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002920/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002921 * Deals with the ambiguity of not knowing if the next message is an
2922 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002923 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002924 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002925 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002926 * indicating which message is received.
2927 */
2928MBEDTLS_CHECK_RETURN_CRITICAL
2929static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2930{
2931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002932
2933 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2934 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2935 return ret;
2936 }
2937 ssl->keep_current_message = 1;
2938
2939 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2940 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002941 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002942 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002943 }
2944
2945 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002946 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002947 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002948 /* Set the reading pointer */
2949 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002950 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2951 if (ret != 0) {
2952 return ret;
2953 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002954 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002955 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002956 }
2957
Jerry Yu7bb40a32023-12-04 10:04:15 +08002958 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2959 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002960 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002961}
2962
2963MBEDTLS_CHECK_RETURN_CRITICAL
2964static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2965 const unsigned char *buf,
2966 const unsigned char *end)
2967{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002968 /* RFC 8446 section 4.5
2969 *
2970 * struct {} EndOfEarlyData;
2971 */
Jerry Yufbf03992023-12-04 10:00:37 +08002972 if (buf != end) {
2973 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2974 MBEDTLS_ERR_SSL_DECODE_ERROR);
2975 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2976 }
2977 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002978}
2979
Jerry Yud5c34962023-12-01 16:32:31 +08002980/*
2981 * RFC 8446 section A.2
2982 *
2983 * | Send ServerHello
2984 * | K_send = handshake
2985 * | Send EncryptedExtensions
2986 * | [Send CertificateRequest]
2987 * Can send | [Send Certificate + CertificateVerify]
2988 * app data | Send Finished
2989 * after --> | K_send = application
2990 * here +--------+--------+
2991 * No 0-RTT | | 0-RTT
2992 * | |
2993 * K_recv = handshake | | K_recv = early data
2994 * [Skip decrypt errors] | +------> WAIT_EOED -+
2995 * | | Recv | | Recv EndOfEarlyData
2996 * | | early data | | K_recv = handshake
2997 * | +------------+ |
2998 * | |
2999 * +> WAIT_FLIGHT2 <--------+
3000 * |
3001 * +--------+--------+
3002 * No auth | | Client auth
3003 * | |
3004 * | v
3005 * | WAIT_CERT
3006 * | Recv | | Recv Certificate
3007 * | empty | v
3008 * | Certificate | WAIT_CV
3009 * | | | Recv
3010 * | v | CertificateVerify
3011 * +-> WAIT_FINISHED <---+
3012 * | Recv Finished
3013 *
3014 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3015 * the above diagram.
3016 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003017MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003018static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003019{
3020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003021
3022 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3023
3024 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3025
Jerry Yu3be85072023-12-04 09:58:54 +08003026 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003027 unsigned char *buf;
3028 size_t buf_len;
3029
3030 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3031 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3032 &buf, &buf_len));
3033
3034 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3035 ssl, buf, buf + buf_len));
3036
Jerry Yue9655122023-12-01 16:44:40 +08003037 MBEDTLS_SSL_DEBUG_MSG(
3038 1, ("Switch to handshake keys for inbound traffic"
3039 "( K_recv = handshake )"));
3040 mbedtls_ssl_set_inbound_transform(
3041 ssl, ssl->handshake->transform_handshake);
3042
Jerry Yud5c34962023-12-01 16:32:31 +08003043 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3044 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3045 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003046
Jerry Yu3be85072023-12-04 09:58:54 +08003047 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003048
Jerry Yub55f9eb2023-12-05 10:27:17 +08003049 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003050 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3051 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003052 } else {
3053 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3054 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3055 goto cleanup;
3056 }
3057
Jerry Yud5c34962023-12-01 16:32:31 +08003058cleanup:
3059 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003060 return ret;
3061}
3062#endif /* MBEDTLS_SSL_EARLY_DATA */
3063
Jerry Yu69dd8d42022-04-16 12:51:26 +08003064/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003065 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003066 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003067MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003068static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003069{
Jerry Yud6e253d2022-05-18 13:59:24 +08003070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3073 if (ret != 0) {
3074 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003075 }
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3078 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003079 MBEDTLS_SSL_DEBUG_RET(
3080 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 }
3082
3083 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3084 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003085}
3086
3087/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003088 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003089 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003091static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003092{
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003096
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003097#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3098 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003099/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3100 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3101 * expected to be resolved with issue#6395.
3102 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003103 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003104 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003105 mbedtls_ssl_handshake_set_state(
3106 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003107 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003108#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003109 {
3110 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003113}
3114
Norbert Fabritius96eed722023-01-23 15:24:59 +01003115#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003116/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003117 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003118 */
3119#define SSL_NEW_SESSION_TICKET_SKIP 0
3120#define SSL_NEW_SESSION_TICKET_WRITE 1
3121MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003122static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003123{
3124 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (ssl->conf->f_ticket_write == NULL) {
3126 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3127 " callback is not set"));
3128 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (ssl->conf->new_session_tickets_count == 0) {
3131 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3132 " configured count is zero"));
3133 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003134 }
3135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 if (ssl->handshake->new_session_tickets_count == 0) {
3137 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3138 "been sent."));
3139 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003140 }
3141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003143}
3144
Jerry Yue67bef42022-07-07 07:29:42 +00003145MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003146static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3147 unsigned char *ticket_nonce,
3148 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003149{
3150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3151 mbedtls_ssl_session *session = ssl->session;
3152 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3153 psa_algorithm_t psa_hash_alg;
3154 int hash_length;
3155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003157
Pengyu Lv9f926952022-11-17 15:22:33 +08003158 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003159 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003160 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003161#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003162 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003163 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003164#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003165
3166#if defined(MBEDTLS_SSL_EARLY_DATA)
3167 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3168 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003169 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003170 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003171 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003172 }
3173#endif /* MBEDTLS_SSL_EARLY_DATA */
3174
Pengyu Lv4938a562023-01-16 11:28:49 +08003175 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003176
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003177#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003178 if (session->ticket_alpn == NULL) {
3179 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3180 if (ret != 0) {
3181 return ret;
3182 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003183 }
3184#endif
3185
Jerry Yue67bef42022-07-07 07:29:42 +00003186 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3188 (unsigned char *) &session->ticket_age_add,
3189 sizeof(session->ticket_age_add)) != 0)) {
3190 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3191 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3194 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003195
3196 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3198 if (ret != 0) {
3199 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3200 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3203 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003204
3205 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003207 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3209 if (hash_length == -1 ||
3210 (size_t) hash_length > sizeof(session->resumption_key)) {
3211 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003212 }
3213
Jerry Yue67bef42022-07-07 07:29:42 +00003214 /* In this code the psk key length equals the length of the hash */
3215 session->resumption_key_len = hash_length;
3216 session->ciphersuite = ciphersuite_info->id;
3217
3218 /* Compute resumption key
3219 *
3220 * HKDF-Expand-Label( resumption_master_secret,
3221 * "resumption", ticket_nonce, Hash.length )
3222 */
3223 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 psa_hash_alg,
3225 session->app_secrets.resumption_master_secret,
3226 hash_length,
3227 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3228 ticket_nonce,
3229 ticket_nonce_size,
3230 session->resumption_key,
3231 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 if (ret != 0) {
3234 MBEDTLS_SSL_DEBUG_RET(2,
3235 "Creating the ticket-resumed PSK failed",
3236 ret);
3237 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3240 session->resumption_key,
3241 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3244 session->app_secrets.resumption_master_secret,
3245 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003248}
3249
3250/* This function creates a NewSessionTicket message in the following format:
3251 *
3252 * struct {
3253 * uint32 ticket_lifetime;
3254 * uint32 ticket_age_add;
3255 * opaque ticket_nonce<0..255>;
3256 * opaque ticket<1..2^16-1>;
3257 * Extension extensions<0..2^16-2>;
3258 * } NewSessionTicket;
3259 *
3260 * The ticket inside the NewSessionTicket message is an encrypted container
3261 * carrying the necessary information so that the server is later able to
3262 * re-start the communication.
3263 *
3264 * The following fields are placed inside the ticket by the
3265 * f_ticket_write() function:
3266 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003267 * - creation time (ticket_creation_time)
3268 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003269 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003270 * - key (resumption_key)
3271 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003272 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003273 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003274 */
3275MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003276static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3277 unsigned char *buf,
3278 unsigned char *end,
3279 size_t *out_len,
3280 unsigned char *ticket_nonce,
3281 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003282{
3283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3284 unsigned char *p = buf;
3285 mbedtls_ssl_session *session = ssl->session;
3286 size_t ticket_len;
3287 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003288 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003289
3290 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003292
3293 /*
3294 * ticket_lifetime 4 bytes
3295 * ticket_age_add 4 bytes
3296 * ticket_nonce 1 + ticket_nonce_size bytes
3297 * ticket >=2 bytes
3298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003300
3301 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003302#if defined(MBEDTLS_HAVE_TIME)
3303 session->ticket_creation_time = mbedtls_ms_time();
3304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3306 session,
3307 p + 9 + ticket_nonce_size + 2,
3308 end,
3309 &ticket_len,
3310 &ticket_lifetime);
3311 if (ret != 0) {
3312 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3313 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003314 }
Jerry Yuce794882023-11-22 15:01:18 +08003315
3316 /* RFC 8446 section 4.6.1
3317 *
Jerry Yue67bef42022-07-07 07:29:42 +00003318 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003319 * unsigned integer in network byte order from the time of ticket
3320 * issuance. Servers MUST NOT use any value greater than
3321 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003322 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003323 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003324 MBEDTLS_SSL_DEBUG_MSG(
3325 1, ("Ticket lifetime (%u) is greater than 7 days.",
3326 (unsigned int) ticket_lifetime));
3327 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 }
Jerry Yuce794882023-11-22 15:01:18 +08003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3331 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3332 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003333
3334 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3336 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3337 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003338
3339 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 p[8] = (unsigned char) ticket_nonce_size;
3341 if (ticket_nonce_size > 0) {
3342 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003343 }
3344 p += 9 + ticket_nonce_size;
3345
3346 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003348 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003350 p += ticket_len;
3351
3352 /* Ticket Extensions
3353 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003354 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003355 */
Jerry Yuedab6372022-10-31 14:37:31 +08003356 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3357
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003359 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003360 p += 2;
3361
Jerry Yu01da35e2022-12-12 15:09:22 +08003362#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003363 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003364 size_t output_len;
3365
Jerry Yuf135bac2023-11-23 18:10:51 +08003366 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003367 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003368 MBEDTLS_SSL_DEBUG_RET(
3369 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3370 return ret;
3371 }
3372 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003373 } else {
3374 MBEDTLS_SSL_DEBUG_MSG(
3375 4, ("early_data not allowed, "
3376 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003377 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003378
Jerry Yu01da35e2022-12-12 15:09:22 +08003379#endif /* MBEDTLS_SSL_EARLY_DATA */
3380
3381 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3382
Jerry Yue67bef42022-07-07 07:29:42 +00003383 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3385 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003386
Jerry Yu7de2ff02022-11-08 21:43:46 +08003387 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003391}
3392
3393/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003394 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003397{
3398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003403 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3404 unsigned char *buf;
3405 size_t buf_len, msg_len;
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3408 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003409
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003410 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3411 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3412 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3415 ssl, buf, buf + buf_len, &msg_len,
3416 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3419 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003420
Jerry Yu359e65f2022-09-22 23:47:43 +08003421 /* Limit session tickets count to one when resumption connection.
3422 *
3423 * See document of mbedtls_ssl_conf_new_session_tickets.
3424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003426 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003428 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003430
Jerry Yua8d3c502022-10-30 14:51:23 +08003431 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3433 } else {
3434 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003435 }
3436
Jerry Yue67bef42022-07-07 07:29:42 +00003437cleanup:
3438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003440}
3441#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3442
3443/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003444 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003447{
XiaokangQian08037552022-04-20 07:16:41 +00003448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3452 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003455 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003459 /* start state */
3460 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003462 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003463 break;
3464
XiaokangQian7807f9f2022-02-15 10:04:37 +00003465 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = ssl_tls13_process_client_hello(ssl);
3467 if (ret != 0) {
3468 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3469 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003470 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003471
Jerry Yuf41553b2022-05-09 22:20:30 +08003472 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 ret = ssl_tls13_write_hello_retry_request(ssl);
3474 if (ret != 0) {
3475 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3476 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003477 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003478 break;
3479
Jerry Yu5b64ae92022-03-30 17:15:02 +08003480 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003482 break;
3483
Xiaofei Baicba64af2022-02-15 10:00:56 +00003484 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ret = ssl_tls13_write_encrypted_extensions(ssl);
3486 if (ret != 0) {
3487 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3488 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003489 }
Jerry Yu48330562022-05-06 21:35:44 +08003490 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003491
Ronald Cron928cbd32022-10-04 16:14:26 +02003492#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003493 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003495 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003496
Jerry Yu83da34e2022-04-16 13:59:52 +08003497 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003499 break;
3500
3501 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003503 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003504#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 /*
3507 * Injection of dummy-CCS's for middlebox compatibility
3508 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003509#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003510 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3512 if (ret == 0) {
3513 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3514 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003515 break;
3516
3517 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003518 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3519 if (ret != 0) {
3520 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003522 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003523 break;
3524#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3525
Jerry Yu69dd8d42022-04-16 12:51:26 +08003526 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003528 break;
3529
Jerry Yu87b5ed42022-12-12 13:07:07 +08003530#if defined(MBEDTLS_SSL_EARLY_DATA)
3531 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003532 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003533 break;
3534#endif /* MBEDTLS_SSL_EARLY_DATA */
3535
Jerry Yu69dd8d42022-04-16 12:51:26 +08003536 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003538 break;
3539
Jerry Yu69dd8d42022-04-16 12:51:26 +08003540 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003542 break;
3543
Ronald Cron766c0cd2022-10-18 12:17:11 +02003544#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003545 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3547 if (ret == 0) {
3548 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003549 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3551 } else {
3552 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003553 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003555 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003556 }
3557 break;
3558
3559 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3561 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003562 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003564 }
3565 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003566#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003567
Jerry Yue67bef42022-07-07 07:29:42 +00003568#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003569 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 ret = ssl_tls13_write_new_session_ticket(ssl);
3571 if (ret != 0) {
3572 MBEDTLS_SSL_DEBUG_RET(1,
3573 "ssl_tls13_write_new_session_ticket ",
3574 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003575 }
3576 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003577 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003578 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003579 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003580 * as part of ssl_prepare_handshake_step.
3581 */
3582 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 if (ssl->handshake->new_session_tickets_count == 0) {
3585 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3586 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003587 mbedtls_ssl_handshake_set_state(
3588 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 }
Jerry Yue67bef42022-07-07 07:29:42 +00003590 break;
3591
3592#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3593
XiaokangQian7807f9f2022-02-15 10:04:37 +00003594 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3596 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003597 }
3598
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003600}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003601
Jerry Yufb4b6472022-01-27 15:03:26 +08003602#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */