blob: f5ef92032b692fc78b545ed3aa2fc0acfc110668 [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
1358 * legacy_compression_methods 2 bytes
1359 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001362 p += cipher_suites_len;
1363 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001364
1365 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001366 * Search for the supported versions extension and parse it to determine
1367 * if the client supports TLS 1.3.
1368 */
1369 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1370 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001371 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001372 if (ret < 0) {
1373 MBEDTLS_SSL_DEBUG_RET(1,
1374 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1375 return ret;
1376 }
1377
1378 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001379 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001380 }
1381
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001382 if (ret == 1) {
1383 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001384 supported_versions_data,
1385 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001386 if (ret < 0) {
1387 MBEDTLS_SSL_DEBUG_RET(1,
1388 ("ssl_tls13_parse_supported_versions_ext"), ret);
1389 return ret;
1390 }
1391
Ronald Cronb828c7d2023-04-03 16:37:22 +02001392 /*
1393 * The supported versions extension was parsed successfully as the
1394 * value returned by ssl_tls13_parse_supported_versions_ext() is
1395 * positive. The return value is then equal to
1396 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1397 * the TLS version to negotiate.
1398 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001399 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1400 return SSL_CLIENT_HELLO_TLS1_2;
1401 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001402 }
1403
1404 /*
1405 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001406 */
1407 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001408 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1409 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001410
1411 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001412 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001413 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001414 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001415 */
1416 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1417 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1418 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1419
Ronald Croncada4102023-03-07 09:51:39 +01001420 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1421 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1422 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1423 }
1424 ssl->session_negotiate->id_len = legacy_session_id_len;
1425 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1426 legacy_session_id, legacy_session_id_len);
1427 memcpy(&ssl->session_negotiate->id[0],
1428 legacy_session_id, legacy_session_id_len);
1429
Ronald Crond540d992023-03-07 09:41:48 +01001430 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001431 * Search for a matching ciphersuite
1432 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001433 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1434 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001435
Ronald Cron89089cc2024-02-14 18:18:08 +01001436 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1437 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (handshake->ciphersuite_info == NULL) {
1440 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1441 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1442 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001443 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001444 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1445
1446 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1447 ((unsigned) handshake->ciphersuite_info->id),
1448 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001449
XiaokangQian4080a7f2022-04-11 09:55:18 +00001450 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001451 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001452 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1456 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1457 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1458 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001460 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001461
Jerry Yuc1be19f2022-04-23 16:11:39 +08001462 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001463 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001464 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001465 * with Extension defined as:
1466 * struct {
1467 * ExtensionType extension_type;
1468 * opaque extension_data<0..2^16-1>;
1469 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001472 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001474 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001477 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001480 unsigned int extension_type;
1481 size_t extension_data_len;
1482 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001483 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1484
Ronald Cron5fbd2702024-02-14 10:03:36 +01001485 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001486 /* Do not accept early data extension in 2nd ClientHello */
1487 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1488 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001489
Jerry Yu0c354a22022-08-29 15:25:36 +08001490 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001491 *
1492 * The "pre_shared_key" extension MUST be the last extension in the
1493 * ClientHello (this facilitates implementation as described below).
1494 * Servers MUST check that it is the last extension and otherwise fail
1495 * the handshake with an "illegal_parameter" alert.
1496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001498 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001500 MBEDTLS_SSL_PEND_FATAL_ALERT(
1501 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1503 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001504 }
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1507 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1508 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509 p += 4;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 extension_data_end = p + extension_data_len;
1513
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001514 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001516 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (ret != 0) {
1518 return ret;
1519 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001522#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1523 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1525 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1526 extension_data_end);
1527 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001528 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 1, "mbedtls_ssl_parse_servername_ext", ret);
1530 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001531 }
XiaokangQian40a35232022-05-07 09:02:40 +00001532 break;
1533#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1534
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001535#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001536 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001538
1539 /* Supported Groups Extension
1540 *
1541 * When sent by the client, the "supported_groups" extension
1542 * indicates the named groups which the client supports,
1543 * ordered from most preferred to least preferred.
1544 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001545 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 ssl, p, extension_data_end);
1547 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001548 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001549 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001551 }
1552
XiaokangQian7807f9f2022-02-15 10:04:37 +00001553 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001554#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001555
Valerio Settic9ae8622023-07-25 11:23:50 +02001556#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001557 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559
1560 /*
1561 * Key Share Extension
1562 *
1563 * When sent by the client, the "key_share" extension
1564 * contains the endpoint's cryptographic parameters for
1565 * ECDHE/DHE key establishment methods.
1566 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001567 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 ssl, p, extension_data_end);
1569 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001570 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1571 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001572 }
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001575 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 1, "ssl_tls13_parse_key_shares_ext", ret);
1577 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001578 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001579
XiaokangQian7807f9f2022-02-15 10:04:37 +00001580 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001581#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001582
1583 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001584 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585 break;
1586
Ronald Cron41a443a2022-10-04 16:38:25 +02001587#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001588 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001589 MBEDTLS_SSL_DEBUG_MSG(
1590 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001591
1592 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 ssl, p, extension_data_end);
1594 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001595 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1597 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001598 }
1599
Jerry Yue19e3b92022-07-08 12:04:51 +00001600 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001601#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001602
Jerry Yu1c105562022-07-10 06:32:38 +00001603 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1605 if ((handshake->received_extensions &
1606 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001607 MBEDTLS_SSL_PEND_FATAL_ALERT(
1608 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1610 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001611 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001612#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001613 /* Delay processing of the PSK identity once we have
1614 * found out which algorithms to use. We keep a pointer
1615 * to the buffer and the size for later processing.
1616 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001617 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001618 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001619#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001620 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001621
XiaokangQianacb39922022-06-17 10:18:48 +00001622#if defined(MBEDTLS_SSL_ALPN)
1623 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1627 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001628 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1630 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001631 }
XiaokangQianacb39922022-06-17 10:18:48 +00001632 break;
1633#endif /* MBEDTLS_SSL_ALPN */
1634
Ronald Cron928cbd32022-10-04 16:14:26 +02001635#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001638
Gabor Mezei078e8032022-04-27 21:17:56 +02001639 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 ssl, p, extension_data_end);
1641 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001642 MBEDTLS_SSL_DEBUG_RET(
1643 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001645 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001646 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001647#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001648
Jan Bruckner151f6422023-02-10 12:45:19 +01001649#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1650 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1651 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1652
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001653 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1654 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001655 if (ret != 0) {
1656 MBEDTLS_SSL_DEBUG_RET(
1657 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1658 return ret;
1659 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001660 break;
1661#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1662
XiaokangQian7807f9f2022-02-15 10:04:37 +00001663 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001664 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001665 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001667 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001668 }
1669
1670 p += extension_data_len;
1671 }
1672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1674 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001675
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001676 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1677 MBEDTLS_SSL_HS_CLIENT_HELLO,
1678 p - buf);
1679 if (0 != ret) {
1680 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1681 return ret;
1682 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001683
Ronald Cron41a443a2022-10-04 16:38:25 +02001684#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685 /* Update checksum with either
1686 * - The entire content of the CH message, if no PSK extension is present
1687 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001688 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001689 * ClientHello even if some pre-requisites for PSK key exchange modes are
1690 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001691 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001692 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001693 ret = handshake->update_checksum(ssl, buf,
1694 pre_shared_key_ext - buf);
1695 if (0 != ret) {
1696 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1697 return ret;
1698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1700 pre_shared_key_ext,
1701 pre_shared_key_ext_end,
1702 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001703 cipher_suites_end,
1704 &psk);
1705 if (ret == 0) {
1706 got_psk = 1;
1707 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001708 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1710 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001711 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001713#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001714 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001715 ret = handshake->update_checksum(ssl, buf, p - buf);
1716 if (0 != ret) {
1717 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1718 return ret;
1719 }
Jerry Yu1c105562022-07-10 06:32:38 +00001720 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001721
Ronald Cron79cdd412024-02-20 17:19:28 +01001722 /*
1723 * Determine the key exchange algorithm to use.
1724 * There are three types of key exchanges supported in TLS 1.3:
1725 * - (EC)DH with ECDSA,
1726 * - (EC)DH with PSK,
1727 * - plain PSK.
1728 *
1729 * The PSK-based key exchanges may additionally be used with 0-RTT.
1730 *
1731 * Our built-in order of preference is
1732 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1733 * 2 ) Certificate Mode ( ephemeral )
1734 * 3 ) Plain PSK Mode ( psk )
1735 */
1736#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1737 if (got_psk && (psk.key_exchange_mode ==
1738 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1739 handshake->key_exchange_mode =
1740 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1741 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1742
1743 } else
1744#endif
1745 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1746 handshake->key_exchange_mode =
1747 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1748 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1749
1750 }
1751#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1752 else if (got_psk && (psk.key_exchange_mode ==
1753 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1754 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1755 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1756 }
1757#endif
1758 else {
1759 MBEDTLS_SSL_DEBUG_MSG(
1760 1,
1761 ("ClientHello message misses mandatory extensions."));
1762 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1763 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1764 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001766
Ronald Cron3e47eec2024-02-21 10:29:24 +01001767#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001768 if (handshake->key_exchange_mode &
1769 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1770 handshake->ciphersuite_info = psk.ciphersuite_info;
1771 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1772
1773 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1774 ((unsigned) psk.ciphersuite_info->id),
1775 psk.ciphersuite_info->name));
1776
1777 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1778 handshake->resume = 1;
1779 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001780 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001781#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001782
1783 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001784 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1785 hrr_required = (no_usable_share_for_key_agreement != 0);
1786 }
1787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001791}
1792
Jerry Yuab0da372023-02-08 13:55:24 +08001793#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001794static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001795{
1796 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1797
Jerry Yu985c9672022-12-04 14:06:30 +08001798 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1799 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001800 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001801 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001803 }
1804
Jerry Yu454dda32023-10-31 15:13:54 +08001805 if (!handshake->resume) {
1806 /* We currently support early data only in the case of PSKs established
1807 via a NewSessionTicket message thus in the case of a session
1808 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001809 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001810 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001811 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001812 }
1813
Jerry Yu82fd6c12023-10-31 16:32:19 +08001814 /* RFC 8446 4.2.10
1815 *
1816 * In order to accept early data, the server MUST have accepted a PSK cipher
1817 * suite and selected the first key offered in the client's "pre_shared_key"
1818 * extension. In addition, it MUST verify that the following values are the
1819 * same as those associated with the selected PSK:
1820 * - The TLS version number
1821 * - The selected cipher suite
1822 * - The selected ALPN [RFC7301] protocol, if any
1823 *
1824 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001825 * - The TLS version number is checked in
1826 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001827 */
1828
1829 if (handshake->selected_identity != 0) {
1830 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001831 1, ("EarlyData: rejected, the selected key in "
1832 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001833 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001834 }
1835
1836 if (handshake->ciphersuite_info->id !=
1837 ssl->session_negotiate->ciphersuite) {
1838 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001839 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1840 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001841 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001842
1843 }
Jerry Yu985c9672022-12-04 14:06:30 +08001844
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001845 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001846 MBEDTLS_SSL_DEBUG_MSG(
1847 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001848 ("EarlyData: rejected, early_data not allowed in ticket "
1849 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001850 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001851 }
Jerry Yu985c9672022-12-04 14:06:30 +08001852
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001853#if defined(MBEDTLS_SSL_ALPN)
1854 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1855 size_t alpn_len;
1856
1857 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1858 return 0;
1859 }
1860
1861 if (alpn != NULL) {
1862 alpn_len = strlen(alpn);
1863 }
1864
1865 if (alpn == NULL ||
1866 ssl->session_negotiate->ticket_alpn == NULL ||
1867 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1868 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1869 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1870 "from the one associated with the pre-shared key."));
1871 return -1;
1872 }
1873#endif
1874
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001875 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001876}
1877#endif /* MBEDTLS_SSL_EARLY_DATA */
1878
XiaokangQian75fe8c72022-06-15 09:42:45 +00001879/* Update the handshake state machine */
1880
Ronald Cronce7d76e2022-07-08 18:56:49 +02001881MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001882static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1883 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001884{
1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1886
XiaokangQian7807f9f2022-02-15 10:04:37 +00001887 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001888 * Server certificate selection
1889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1891 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1892 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001893 }
1894#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1895 ssl->handshake->sni_name = NULL;
1896 ssl->handshake->sni_name_len = 0;
1897#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1900 if (ret != 0) {
1901 MBEDTLS_SSL_DEBUG_RET(1,
1902 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1903 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001904 }
1905
Jerry Yuab0da372023-02-08 13:55:24 +08001906#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001907 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001908 ssl->handshake->early_data_accepted =
1909 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001910
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001911 if (ssl->handshake->early_data_accepted) {
1912 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1913 if (ret != 0) {
1914 MBEDTLS_SSL_DEBUG_RET(
1915 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1916 return ret;
1917 }
1918 } else {
1919 ssl->discard_early_data_record =
1920 hrr_required ?
1921 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1922 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001923 }
1924 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001925#else
1926 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001927#endif /* MBEDTLS_SSL_EARLY_DATA */
1928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001930}
1931
1932/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001933 * Main entry point from the state machine; orchestrates the otherfunctions.
1934 */
1935
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001936MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001937static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001938{
1939
XiaokangQian08037552022-04-20 07:16:41 +00001940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001942 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001943 int parse_client_hello_ret;
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1948 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1949 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1952 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001953 parse_client_hello_ret = ret; /* Store positive return value of
1954 * parse_client_hello,
1955 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001956 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001957
Ronald Cronb828c7d2023-04-03 16:37:22 +02001958 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001959 * Version 1.2 of the protocol has to be used for the handshake.
1960 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001961 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1962 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1963 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1964 * will dispatch to the TLS 1.2 state machine.
1965 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001966 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001967 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001968 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001969 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001970 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001971 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001972 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1973 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1974 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001975 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001976 ssl->keep_current_message = 1;
1977 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1978 return 0;
1979 }
1980
Ronald Cron7b6ee942024-01-12 10:29:55 +01001981 MBEDTLS_SSL_PROC_CHK(
1982 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1983 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001984
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001985 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1987 } else {
1988 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1989 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001990
1991cleanup:
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1994 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001995}
1996
1997/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001998 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001999 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002001static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002002{
Jerry Yu637a3f12022-04-20 21:37:58 +08002003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2004 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2008 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2009 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2010 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002011 }
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2014 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002015
2016#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002017 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002018#endif /* MBEDTLS_HAVE_TIME */
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002021}
2022
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002024 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025 *
2026 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002027 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028 * } SupportedVersions;
2029 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002030MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002031static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 mbedtls_ssl_context *ssl,
2033 unsigned char *buf,
2034 unsigned char *end,
2035 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037 *out_len = 0;
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
2041 /* Check if we have space to write the extension:
2042 * - extension_type (2 bytes)
2043 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002044 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 mbedtls_ssl_write_version(buf + 4,
2053 ssl->conf->transport,
2054 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2057 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058
Jerry Yu349a6132022-04-14 20:52:56 +08002059 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060
Jerry Yub95dd362022-11-08 21:19:34 +08002061 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002065}
2066
Jerry Yud9436a12022-04-20 22:28:09 +08002067
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068
2069/* Generate and export a single key share. For hybrid KEMs, this can
2070 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002072static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2073 uint16_t named_group,
2074 unsigned char *buf,
2075 unsigned char *end,
2076 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077{
2078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002079
2080 *out_len = 0;
2081
Valerio Settic9ae8622023-07-25 11:23:50 +02002082#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002083 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002084 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002085 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 ssl, named_group, buf, end, out_len);
2087 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002088 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002089 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 ret);
2091 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002092 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002094#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (0 /* Other kinds of KEMs */) {
2096 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002097 ((void) ssl);
2098 ((void) named_group);
2099 ((void) buf);
2100 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2102 }
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105}
2106
2107/*
2108 * ssl_tls13_write_key_share_ext
2109 *
2110 * Structure of key_share extension in ServerHello:
2111 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002112 * struct {
2113 * NamedGroup group;
2114 * opaque key_exchange<1..2^16-1>;
2115 * } KeyShareEntry;
2116 * struct {
2117 * KeyShareEntry server_share;
2118 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002119 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002120MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002121static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2122 unsigned char *buf,
2123 unsigned char *end,
2124 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125{
Jerry Yu955ddd72022-04-22 22:27:33 +08002126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002127 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002128 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131
2132 *out_len = 0;
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2137 mbedtls_ssl_named_group_to_str(group),
2138 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002139
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 /* Check if we have space for header and length fields:
2141 * - extension_type (2 bytes)
2142 * - extension_data_length (2 bytes)
2143 * - group (2 bytes)
2144 * - key_exchange_length (2 bytes)
2145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2147 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2148 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002149 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002150
Jerry Yu3bf2c642022-03-30 22:02:12 +08002151 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2152 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002153 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 ssl, group, server_share + 4, end, &key_exchange_length);
2155 if (ret != 0) {
2156 return ret;
2157 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002158 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002163
Jerry Yu57d48412022-04-20 21:50:42 +08002164 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002169}
Jerry Yud9436a12022-04-20 22:28:09 +08002170
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002171MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002172static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2173 unsigned char *buf,
2174 unsigned char *end,
2175 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002176{
2177 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2178 /* key_share Extension
2179 *
2180 * struct {
2181 * select (Handshake.msg_type) {
2182 * ...
2183 * case hello_retry_request:
2184 * NamedGroup selected_group;
2185 * ...
2186 * };
2187 * } KeyShare;
2188 */
2189
2190 *out_len = 0;
2191
Jerry Yub0ac10b2022-05-05 11:10:08 +08002192 /*
2193 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2194 * of the HRR is then to transmit a cookie to force the client to demonstrate
2195 * reachability at their apparent network address (primarily useful for DTLS).
2196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2198 return 0;
2199 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002200
2201 /* We should only send the key_share extension if the client's initial
2202 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (ssl->handshake->offered_group_id != 0) {
2204 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2205 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002206 }
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (selected_group == 0) {
2209 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2210 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002211 }
2212
Jerry Yub0ac10b2022-05-05 11:10:08 +08002213 /* Check if we have enough space:
2214 * - extension_type (2 bytes)
2215 * - extension_data_length (2 bytes)
2216 * - selected_group (2 bytes)
2217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2221 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2222 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_SSL_DEBUG_MSG(3,
2225 ("HRR selected_group: %s (%x)",
2226 mbedtls_ssl_named_group_to_str(selected_group),
2227 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002228
2229 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002234}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235
2236/*
2237 * Structure of ServerHello message:
2238 *
2239 * struct {
2240 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2241 * Random random;
2242 * opaque legacy_session_id_echo<0..32>;
2243 * CipherSuite cipher_suite;
2244 * uint8 legacy_compression_method = 0;
2245 * Extension extensions<6..2^16-1>;
2246 * } ServerHello;
2247 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002249static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2250 unsigned char *buf,
2251 unsigned char *end,
2252 size_t *out_len,
2253 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002254{
Jerry Yu955ddd72022-04-22 22:27:33 +08002255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002257 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002258 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002259
2260 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002261 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002262
Jerry Yucfc04b32022-04-21 09:31:58 +08002263 /* ...
2264 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2265 * ...
2266 * with ProtocolVersion defined as:
2267 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2270 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 p += 2;
2272
Jerry Yu1c3e6882022-04-20 21:23:40 +08002273 /* ...
2274 * Random random;
2275 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002276 * with Random defined as:
2277 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2280 if (is_hrr) {
2281 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2282 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2283 } else {
2284 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2285 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002286 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2288 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2290
Jerry Yucfc04b32022-04-21 09:31:58 +08002291 /* ...
2292 * opaque legacy_session_id_echo<0..32>;
2293 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2296 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2297 if (ssl->session_negotiate->id_len > 0) {
2298 memcpy(p, &ssl->session_negotiate->id[0],
2299 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002300 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2303 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002304 }
2305
Jerry Yucfc04b32022-04-21 09:31:58 +08002306 /* ...
2307 * CipherSuite cipher_suite;
2308 * ...
2309 * with CipherSuite defined as:
2310 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2313 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002314 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_DEBUG_MSG(3,
2316 ("server hello, chosen ciphersuite: %s ( id=%d )",
2317 mbedtls_ssl_get_ciphersuite_name(
2318 ssl->session_negotiate->ciphersuite),
2319 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320
Jerry Yucfc04b32022-04-21 09:31:58 +08002321 /* ...
2322 * uint8 legacy_compression_method = 0;
2323 * ...
2324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002326 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327
Jerry Yucfc04b32022-04-21 09:31:58 +08002328 /* ...
2329 * Extension extensions<6..2^16-1>;
2330 * ...
2331 * struct {
2332 * ExtensionType extension_type; (2 bytes)
2333 * opaque extension_data<0..2^16-1>;
2334 * } Extension;
2335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002337 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002338 p += 2;
2339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2341 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002342 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2344 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002345 }
2346 p += output_len;
2347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2349 if (is_hrr) {
2350 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2351 } else {
2352 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2353 }
2354 if (ret != 0) {
2355 return ret;
2356 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002357 p += output_len;
2358 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002359
Ronald Cron41a443a2022-10-04 16:38:25 +02002360#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2362 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2363 if (ret != 0) {
2364 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2365 ret);
2366 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002367 }
2368 p += output_len;
2369 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002370#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2375 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002376
Jerry Yud9436a12022-04-20 22:28:09 +08002377 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002380
Jerry Yu7de2ff02022-11-08 21:43:46 +08002381 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002382 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_SSL_HS_SERVER_HELLO,
2384 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002387}
2388
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002389MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002390static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002391{
2392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2394 if (ret != 0) {
2395 MBEDTLS_SSL_DEBUG_RET(1,
2396 "mbedtls_ssl_tls13_compute_handshake_transform",
2397 ret);
2398 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002399 }
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002402}
2403
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002404MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002405static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002406{
Jerry Yu637a3f12022-04-20 21:37:58 +08002407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002408 unsigned char *buf;
2409 size_t buf_len, msg_len;
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002414
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002415 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2416 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2419 buf + buf_len,
2420 &msg_len,
2421 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002422
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002423 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002424 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2427 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002428
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002429 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002430
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002431#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2432 /* The server sends a dummy change_cipher_spec record immediately
2433 * after its first handshake message. This may either be after
2434 * a ServerHello or a HelloRetryRequest.
2435 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002436 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002438#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002440#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002441
Jerry Yuf4b27e42022-03-30 17:32:21 +08002442cleanup:
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2445 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002446}
2447
Jerry Yu23d1a252022-05-12 18:08:59 +08002448
2449/*
2450 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2451 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002452MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002453static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002454{
2455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002456 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2458 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2459 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2460 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002461 }
2462
2463 /*
2464 * Create stateless transcript hash for HRR
2465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2467 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2468 if (ret != 0) {
2469 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2470 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002471 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002475}
2476
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002478static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002479{
2480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2481 unsigned char *buf;
2482 size_t buf_len, msg_len;
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2489 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2490 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2493 buf + buf_len,
2494 &msg_len,
2495 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002496 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002497 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002498
2499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2501 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002502
Ronald Cron5fbd2702024-02-14 10:03:36 +01002503 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002504
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002505#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2506 /* The server sends a dummy change_cipher_spec record immediately
2507 * after its first handshake message. This may either be after
2508 * a ServerHello or a HelloRetryRequest.
2509 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002510 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002512#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002514#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002515
2516cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2518 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002519}
2520
Jerry Yu5b64ae92022-03-30 17:15:02 +08002521/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002522 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2523 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002524
2525/*
2526 * struct {
2527 * Extension extensions<0..2 ^ 16 - 1>;
2528 * } EncryptedExtensions;
2529 *
2530 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002531MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002532static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2533 unsigned char *buf,
2534 unsigned char *end,
2535 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002536{
XiaokangQianacb39922022-06-17 10:18:48 +00002537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538 unsigned char *p = buf;
2539 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002540 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002541 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002542
Jerry Yu4d3841a2022-04-16 12:37:19 +08002543 *out_len = 0;
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002546 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002547 p += 2;
2548
Jerry Yu8937eb42022-05-03 12:12:14 +08002549 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002550 ((void) ret);
2551 ((void) output_len);
2552
2553#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2555 if (ret != 0) {
2556 return ret;
2557 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002558 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002559#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560
Jerry Yu71c14f12022-12-12 11:10:35 +08002561#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002562 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002563 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002564 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002565 if (ret != 0) {
2566 return ret;
2567 }
2568 p += output_len;
2569 }
2570#endif /* MBEDTLS_SSL_EARLY_DATA */
2571
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002572#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002573 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002574 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2575 ssl, p, end, &output_len);
2576 if (ret != 0) {
2577 return ret;
2578 }
2579 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002580 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002581#endif
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 extensions_len = (p - p_extensions_len) - 2;
2584 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002585
2586 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002589
Jerry Yu7de2ff02022-11-08 21:43:46 +08002590 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002594}
2595
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002596MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002597static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598{
2599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2600 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002601 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 mbedtls_ssl_set_outbound_transform(ssl,
2604 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002605 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002609
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002610 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2611 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2612 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2615 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002616
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002617 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002618 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2619 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2622 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002623
Ronald Cron928cbd32022-10-04 16:14:26 +02002624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2626 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2627 } else {
2628 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2629 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002630#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002632#endif
2633
Jerry Yu4d3841a2022-04-16 12:37:19 +08002634cleanup:
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2637 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002638}
2639
Ronald Cron928cbd32022-10-04 16:14:26 +02002640#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002641#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2642#define SSL_CERTIFICATE_REQUEST_SKIP 1
2643/* Coordination:
2644 * Check whether a CertificateRequest message should be written.
2645 * Returns a negative code on failure, or
2646 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2647 * - SSL_CERTIFICATE_REQUEST_SKIP
2648 * indicating if the writing of the CertificateRequest
2649 * should be skipped or not.
2650 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002651MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002652static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002653{
2654 int authmode;
2655
XiaokangQian40a35232022-05-07 09:02:40 +00002656#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002658 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002660#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002661 authmode = ssl->conf->authmode;
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002664 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002666 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002667
XiaokangQianc3017f62022-05-13 05:55:41 +00002668 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002671}
2672
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673/*
2674 * struct {
2675 * opaque certificate_request_context<0..2^8-1>;
2676 * Extension extensions<2..2^16-1>;
2677 * } CertificateRequest;
2678 *
2679 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002680MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002681static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2682 unsigned char *buf,
2683 const unsigned char *end,
2684 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002685{
2686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2687 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002688 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689 unsigned char *p_extensions_len;
2690
2691 *out_len = 0;
2692
2693 /* Check if we have enough space:
2694 * - certificate_request_context (1 byte)
2695 * - extensions length (2 bytes)
2696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698
2699 /*
2700 * Write certificate_request_context
2701 */
2702 /*
2703 * We use a zero length context for the normal handshake
2704 * messages. For post-authentication handshake messages
2705 * this request context would be set to a non-zero value.
2706 */
2707 *p++ = 0x0;
2708
2709 /*
2710 * Write extensions
2711 */
2712 /* The extensions must contain the signature_algorithms. */
2713 p_extensions_len = p;
2714 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2716 if (ret != 0) {
2717 return ret;
2718 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002719
XiaokangQianec6efb92022-05-06 09:53:10 +00002720 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722
2723 *out_len = p - buf;
2724
Jerry Yu7de2ff02022-11-08 21:43:46 +08002725 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729}
2730
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002731MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002732static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002733{
2734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002741 unsigned char *buf;
2742 size_t buf_len, msg_len;
2743
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002744 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2745 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2746 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2749 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002750
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002751 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002752 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2753 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2756 ssl, buf_len, msg_len));
2757 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2758 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002759 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 } else {
2761 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002762 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2763 goto cleanup;
2764 }
2765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002767cleanup:
2768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2770 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002771}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002772
Jerry Yu4d3841a2022-04-16 12:37:19 +08002773/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002774 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002775 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002776MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002777static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002778{
Jerry Yu5a26f302022-05-10 20:46:40 +08002779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002780
2781#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2783 mbedtls_ssl_own_cert(ssl) == NULL) {
2784 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2785 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2786 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2787 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002788 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002789#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2792 if (ret != 0) {
2793 return ret;
2794 }
2795 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2796 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002797}
2798
2799/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002800 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002801 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002802MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002803static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002804{
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2806 if (ret != 0) {
2807 return ret;
2808 }
2809 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2810 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002811}
Ronald Cron928cbd32022-10-04 16:14:26 +02002812#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002813
Jerry Yu9b72e392023-12-01 16:27:08 +08002814/*
2815 * RFC 8446 section A.2
2816 *
2817 * | Send ServerHello
2818 * | K_send = handshake
2819 * | Send EncryptedExtensions
2820 * | [Send CertificateRequest]
2821 * Can send | [Send Certificate + CertificateVerify]
2822 * app data | Send Finished
2823 * after --> | K_send = application
2824 * here +--------+--------+
2825 * No 0-RTT | | 0-RTT
2826 * | |
2827 * K_recv = handshake | | K_recv = early data
2828 * [Skip decrypt errors] | +------> WAIT_EOED -+
2829 * | | Recv | | Recv EndOfEarlyData
2830 * | | early data | | K_recv = handshake
2831 * | +------------+ |
2832 * | |
2833 * +> WAIT_FLIGHT2 <--------+
2834 * |
2835 * +--------+--------+
2836 * No auth | | Client auth
2837 * | |
2838 * | v
2839 * | WAIT_CERT
2840 * | Recv | | Recv Certificate
2841 * | empty | v
2842 * | Certificate | WAIT_CV
2843 * | | | Recv
2844 * | v | CertificateVerify
2845 * +-> WAIT_FINISHED <---+
2846 * | Recv Finished
2847 *
2848 *
2849 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002850 * above diagram. We are not going to receive early data related messages
2851 * anymore, prepare to receive the first handshake message of the client
2852 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002853 */
Jerry Yu3be85072023-12-04 09:58:54 +08002854static void ssl_tls13_prepare_for_handshake_second_flight(
2855 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002856{
Jerry Yu9b72e392023-12-01 16:27:08 +08002857 if (ssl->handshake->certificate_request_sent) {
2858 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2859 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002860 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002861 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002862
Jerry Yu9b72e392023-12-01 16:27:08 +08002863 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2864 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002865}
2866
Jerry Yu83da34e2022-04-16 13:59:52 +08002867/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002868 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002869 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002870MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002871static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002872{
Jerry Yud6e253d2022-05-18 13:59:24 +08002873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2876 if (ret != 0) {
2877 return ret;
2878 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2881 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002882 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2884 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2885 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002886 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002887
Jerry Yu87b5ed42022-12-12 13:07:07 +08002888#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002889 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002890 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002891 MBEDTLS_SSL_DEBUG_MSG(
2892 1, ("Switch to early keys for inbound traffic. "
2893 "( K_recv = early data )"));
2894 mbedtls_ssl_set_inbound_transform(
2895 ssl, ssl->handshake->transform_earlydata);
2896 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2897 return 0;
2898 }
2899#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002900 MBEDTLS_SSL_DEBUG_MSG(
2901 1, ("Switch to handshake keys for inbound traffic "
2902 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002904
Jerry Yu3be85072023-12-04 09:58:54 +08002905 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002906
2907 return 0;
2908}
2909
Jerry Yu87b5ed42022-12-12 13:07:07 +08002910#if defined(MBEDTLS_SSL_EARLY_DATA)
2911/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002912 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002913 */
Jerry Yu3be85072023-12-04 09:58:54 +08002914#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002915#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002916/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002917 * Deals with the ambiguity of not knowing if the next message is an
2918 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002919 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002920 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002921 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002922 * indicating which message is received.
2923 */
2924MBEDTLS_CHECK_RETURN_CRITICAL
2925static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2926{
2927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002928
2929 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2930 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2931 return ret;
2932 }
2933 ssl->keep_current_message = 1;
2934
2935 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2936 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002937 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002938 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002939 }
2940
2941 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002942 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002943 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002944 /* Set the reading pointer */
2945 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002946 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2947 if (ret != 0) {
2948 return ret;
2949 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002950 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002951 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002952 }
2953
Jerry Yu7bb40a32023-12-04 10:04:15 +08002954 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2955 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002956 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002957}
2958
2959MBEDTLS_CHECK_RETURN_CRITICAL
2960static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2961 const unsigned char *buf,
2962 const unsigned char *end)
2963{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002964 /* RFC 8446 section 4.5
2965 *
2966 * struct {} EndOfEarlyData;
2967 */
Jerry Yufbf03992023-12-04 10:00:37 +08002968 if (buf != end) {
2969 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2970 MBEDTLS_ERR_SSL_DECODE_ERROR);
2971 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2972 }
2973 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002974}
2975
Jerry Yud5c34962023-12-01 16:32:31 +08002976/*
2977 * RFC 8446 section A.2
2978 *
2979 * | Send ServerHello
2980 * | K_send = handshake
2981 * | Send EncryptedExtensions
2982 * | [Send CertificateRequest]
2983 * Can send | [Send Certificate + CertificateVerify]
2984 * app data | Send Finished
2985 * after --> | K_send = application
2986 * here +--------+--------+
2987 * No 0-RTT | | 0-RTT
2988 * | |
2989 * K_recv = handshake | | K_recv = early data
2990 * [Skip decrypt errors] | +------> WAIT_EOED -+
2991 * | | Recv | | Recv EndOfEarlyData
2992 * | | early data | | K_recv = handshake
2993 * | +------------+ |
2994 * | |
2995 * +> WAIT_FLIGHT2 <--------+
2996 * |
2997 * +--------+--------+
2998 * No auth | | Client auth
2999 * | |
3000 * | v
3001 * | WAIT_CERT
3002 * | Recv | | Recv Certificate
3003 * | empty | v
3004 * | Certificate | WAIT_CV
3005 * | | | Recv
3006 * | v | CertificateVerify
3007 * +-> WAIT_FINISHED <---+
3008 * | Recv Finished
3009 *
3010 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3011 * the above diagram.
3012 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003013MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003014static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003015{
3016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003017
3018 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3019
3020 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3021
Jerry Yu3be85072023-12-04 09:58:54 +08003022 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003023 unsigned char *buf;
3024 size_t buf_len;
3025
3026 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3027 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3028 &buf, &buf_len));
3029
3030 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3031 ssl, buf, buf + buf_len));
3032
Jerry Yue9655122023-12-01 16:44:40 +08003033 MBEDTLS_SSL_DEBUG_MSG(
3034 1, ("Switch to handshake keys for inbound traffic"
3035 "( K_recv = handshake )"));
3036 mbedtls_ssl_set_inbound_transform(
3037 ssl, ssl->handshake->transform_handshake);
3038
Jerry Yud5c34962023-12-01 16:32:31 +08003039 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3040 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3041 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003042
Jerry Yu3be85072023-12-04 09:58:54 +08003043 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003044
Jerry Yub55f9eb2023-12-05 10:27:17 +08003045 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003046 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3047 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003048 } else {
3049 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3050 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3051 goto cleanup;
3052 }
3053
Jerry Yud5c34962023-12-01 16:32:31 +08003054cleanup:
3055 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003056 return ret;
3057}
3058#endif /* MBEDTLS_SSL_EARLY_DATA */
3059
Jerry Yu69dd8d42022-04-16 12:51:26 +08003060/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003061 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003062 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003064static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003065{
Jerry Yud6e253d2022-05-18 13:59:24 +08003066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3069 if (ret != 0) {
3070 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003071 }
3072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3074 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003075 MBEDTLS_SSL_DEBUG_RET(
3076 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 }
3078
3079 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3080 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003081}
3082
3083/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003084 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003085 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003086MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003087static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003088{
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003092
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003093#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3094 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003095/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3096 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3097 * expected to be resolved with issue#6395.
3098 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003099 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003100 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003101 mbedtls_ssl_handshake_set_state(
3102 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003103 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003104#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003105 {
3106 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003109}
3110
Norbert Fabritius96eed722023-01-23 15:24:59 +01003111#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003112/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003113 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003114 */
3115#define SSL_NEW_SESSION_TICKET_SKIP 0
3116#define SSL_NEW_SESSION_TICKET_WRITE 1
3117MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003118static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003119{
3120 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (ssl->conf->f_ticket_write == NULL) {
3122 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3123 " callback is not set"));
3124 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (ssl->conf->new_session_tickets_count == 0) {
3127 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3128 " configured count is zero"));
3129 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003130 }
3131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (ssl->handshake->new_session_tickets_count == 0) {
3133 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3134 "been sent."));
3135 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003136 }
3137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003139}
3140
Jerry Yue67bef42022-07-07 07:29:42 +00003141MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003142static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3143 unsigned char *ticket_nonce,
3144 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003145{
3146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3147 mbedtls_ssl_session *session = ssl->session;
3148 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3149 psa_algorithm_t psa_hash_alg;
3150 int hash_length;
3151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003153
Pengyu Lv9f926952022-11-17 15:22:33 +08003154 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003155 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003156 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003157#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003158 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003159 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003160#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003161
3162#if defined(MBEDTLS_SSL_EARLY_DATA)
3163 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3164 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003165 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003166 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003167 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003168 }
3169#endif /* MBEDTLS_SSL_EARLY_DATA */
3170
Pengyu Lv4938a562023-01-16 11:28:49 +08003171 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003172
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003173#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003174 if (session->ticket_alpn == NULL) {
3175 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3176 if (ret != 0) {
3177 return ret;
3178 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003179 }
3180#endif
3181
Jerry Yue67bef42022-07-07 07:29:42 +00003182 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3184 (unsigned char *) &session->ticket_age_add,
3185 sizeof(session->ticket_age_add)) != 0)) {
3186 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3187 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3190 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003191
3192 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3194 if (ret != 0) {
3195 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3196 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3199 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003200
3201 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003203 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3205 if (hash_length == -1 ||
3206 (size_t) hash_length > sizeof(session->resumption_key)) {
3207 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003208 }
3209
Jerry Yue67bef42022-07-07 07:29:42 +00003210 /* In this code the psk key length equals the length of the hash */
3211 session->resumption_key_len = hash_length;
3212 session->ciphersuite = ciphersuite_info->id;
3213
3214 /* Compute resumption key
3215 *
3216 * HKDF-Expand-Label( resumption_master_secret,
3217 * "resumption", ticket_nonce, Hash.length )
3218 */
3219 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 psa_hash_alg,
3221 session->app_secrets.resumption_master_secret,
3222 hash_length,
3223 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3224 ticket_nonce,
3225 ticket_nonce_size,
3226 session->resumption_key,
3227 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 if (ret != 0) {
3230 MBEDTLS_SSL_DEBUG_RET(2,
3231 "Creating the ticket-resumed PSK failed",
3232 ret);
3233 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003234 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3236 session->resumption_key,
3237 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3240 session->app_secrets.resumption_master_secret,
3241 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003244}
3245
3246/* This function creates a NewSessionTicket message in the following format:
3247 *
3248 * struct {
3249 * uint32 ticket_lifetime;
3250 * uint32 ticket_age_add;
3251 * opaque ticket_nonce<0..255>;
3252 * opaque ticket<1..2^16-1>;
3253 * Extension extensions<0..2^16-2>;
3254 * } NewSessionTicket;
3255 *
3256 * The ticket inside the NewSessionTicket message is an encrypted container
3257 * carrying the necessary information so that the server is later able to
3258 * re-start the communication.
3259 *
3260 * The following fields are placed inside the ticket by the
3261 * f_ticket_write() function:
3262 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003263 * - creation time (ticket_creation_time)
3264 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003265 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003266 * - key (resumption_key)
3267 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003268 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003269 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003270 */
3271MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003272static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3273 unsigned char *buf,
3274 unsigned char *end,
3275 size_t *out_len,
3276 unsigned char *ticket_nonce,
3277 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003278{
3279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3280 unsigned char *p = buf;
3281 mbedtls_ssl_session *session = ssl->session;
3282 size_t ticket_len;
3283 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003284 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003285
3286 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003288
3289 /*
3290 * ticket_lifetime 4 bytes
3291 * ticket_age_add 4 bytes
3292 * ticket_nonce 1 + ticket_nonce_size bytes
3293 * ticket >=2 bytes
3294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003296
3297 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003298#if defined(MBEDTLS_HAVE_TIME)
3299 session->ticket_creation_time = mbedtls_ms_time();
3300#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3302 session,
3303 p + 9 + ticket_nonce_size + 2,
3304 end,
3305 &ticket_len,
3306 &ticket_lifetime);
3307 if (ret != 0) {
3308 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3309 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003310 }
Jerry Yuce794882023-11-22 15:01:18 +08003311
3312 /* RFC 8446 section 4.6.1
3313 *
Jerry Yue67bef42022-07-07 07:29:42 +00003314 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003315 * unsigned integer in network byte order from the time of ticket
3316 * issuance. Servers MUST NOT use any value greater than
3317 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003318 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003319 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003320 MBEDTLS_SSL_DEBUG_MSG(
3321 1, ("Ticket lifetime (%u) is greater than 7 days.",
3322 (unsigned int) ticket_lifetime));
3323 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 }
Jerry Yuce794882023-11-22 15:01:18 +08003325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3327 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3328 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003329
3330 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3332 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3333 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003334
3335 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 p[8] = (unsigned char) ticket_nonce_size;
3337 if (ticket_nonce_size > 0) {
3338 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003339 }
3340 p += 9 + ticket_nonce_size;
3341
3342 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003344 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003346 p += ticket_len;
3347
3348 /* Ticket Extensions
3349 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003350 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003351 */
Jerry Yuedab6372022-10-31 14:37:31 +08003352 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003355 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003356 p += 2;
3357
Jerry Yu01da35e2022-12-12 15:09:22 +08003358#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003359 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003360 size_t output_len;
3361
Jerry Yuf135bac2023-11-23 18:10:51 +08003362 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003363 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003364 MBEDTLS_SSL_DEBUG_RET(
3365 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3366 return ret;
3367 }
3368 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003369 } else {
3370 MBEDTLS_SSL_DEBUG_MSG(
3371 4, ("early_data not allowed, "
3372 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003373 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003374
Jerry Yu01da35e2022-12-12 15:09:22 +08003375#endif /* MBEDTLS_SSL_EARLY_DATA */
3376
3377 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3378
Jerry Yue67bef42022-07-07 07:29:42 +00003379 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3381 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003382
Jerry Yu7de2ff02022-11-08 21:43:46 +08003383 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003387}
3388
3389/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003390 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003393{
3394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003399 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3400 unsigned char *buf;
3401 size_t buf_len, msg_len;
3402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3404 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003405
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003406 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3407 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3408 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3411 ssl, buf, buf + buf_len, &msg_len,
3412 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3415 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003416
Jerry Yu359e65f2022-09-22 23:47:43 +08003417 /* Limit session tickets count to one when resumption connection.
3418 *
3419 * See document of mbedtls_ssl_conf_new_session_tickets.
3420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003422 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003424 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003426
Jerry Yua8d3c502022-10-30 14:51:23 +08003427 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3429 } else {
3430 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003431 }
3432
Jerry Yue67bef42022-07-07 07:29:42 +00003433cleanup:
3434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003436}
3437#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3438
3439/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003440 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003442int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003443{
XiaokangQian08037552022-04-20 07:16:41 +00003444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3447 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3448 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003451 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003455 /* start state */
3456 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003458 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003459 break;
3460
XiaokangQian7807f9f2022-02-15 10:04:37 +00003461 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ret = ssl_tls13_process_client_hello(ssl);
3463 if (ret != 0) {
3464 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3465 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003466 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003467
Jerry Yuf41553b2022-05-09 22:20:30 +08003468 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = ssl_tls13_write_hello_retry_request(ssl);
3470 if (ret != 0) {
3471 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3472 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003473 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003474 break;
3475
Jerry Yu5b64ae92022-03-30 17:15:02 +08003476 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003478 break;
3479
Xiaofei Baicba64af2022-02-15 10:00:56 +00003480 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ret = ssl_tls13_write_encrypted_extensions(ssl);
3482 if (ret != 0) {
3483 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3484 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003485 }
Jerry Yu48330562022-05-06 21:35:44 +08003486 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003487
Ronald Cron928cbd32022-10-04 16:14:26 +02003488#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003489 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003491 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003492
Jerry Yu83da34e2022-04-16 13:59:52 +08003493 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003495 break;
3496
3497 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003499 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003500#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 /*
3503 * Injection of dummy-CCS's for middlebox compatibility
3504 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003505#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003506 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3508 if (ret == 0) {
3509 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3510 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003511 break;
3512
3513 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003514 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3515 if (ret != 0) {
3516 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003518 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003519 break;
3520#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3521
Jerry Yu69dd8d42022-04-16 12:51:26 +08003522 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003524 break;
3525
Jerry Yu87b5ed42022-12-12 13:07:07 +08003526#if defined(MBEDTLS_SSL_EARLY_DATA)
3527 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003528 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003529 break;
3530#endif /* MBEDTLS_SSL_EARLY_DATA */
3531
Jerry Yu69dd8d42022-04-16 12:51:26 +08003532 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003534 break;
3535
Jerry Yu69dd8d42022-04-16 12:51:26 +08003536 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003538 break;
3539
Ronald Cron766c0cd2022-10-18 12:17:11 +02003540#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003541 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3543 if (ret == 0) {
3544 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003545 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3547 } else {
3548 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
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_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003551 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003552 }
3553 break;
3554
3555 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3557 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003558 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003560 }
3561 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003562#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003563
Jerry Yue67bef42022-07-07 07:29:42 +00003564#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003565 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003566 ret = ssl_tls13_write_new_session_ticket(ssl);
3567 if (ret != 0) {
3568 MBEDTLS_SSL_DEBUG_RET(1,
3569 "ssl_tls13_write_new_session_ticket ",
3570 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003571 }
3572 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003573 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003574 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003575 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003576 * as part of ssl_prepare_handshake_step.
3577 */
3578 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 if (ssl->handshake->new_session_tickets_count == 0) {
3581 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3582 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003583 mbedtls_ssl_handshake_set_state(
3584 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 }
Jerry Yue67bef42022-07-07 07:29:42 +00003586 break;
3587
3588#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3589
XiaokangQian7807f9f2022-02-15 10:04:37 +00003590 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3592 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003593 }
3594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003596}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003597
Jerry Yufb4b6472022-01-27 15:03:26 +08003598#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */