blob: 6fe8caeca4a906b908d9a3f136f3108ee194d7ee [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
Ronald Cron19521dd2024-03-07 15:09:41 +010095 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%x",
96 (unsigned) psk_ciphersuite_id, psk_hash_alg));
Ronald Cron89089cc2024-02-14 18:18:08 +010097}
98
Ronald Cron41a443a2022-10-04 16:38:25 +020099#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +0000100/* From RFC 8446:
101 *
102 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
103 * struct {
104 * PskKeyExchangeMode ke_modes<1..255>;
105 * } PskKeyExchangeModes;
106 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800107MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100108static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
109 const unsigned char *buf,
110 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000111{
Jerry Yu299e31f2022-07-13 23:06:36 +0800112 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000113 size_t ke_modes_len;
114 int ke_modes = 0;
115
Jerry Yu854dd9e2022-07-15 14:28:27 +0800116 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800118 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000119 /* Currently, there are only two PSK modes, so even without looking
120 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (ke_modes_len > 2) {
122 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
123 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
124 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800125 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 while (ke_modes_len-- != 0) {
130 switch (*p++) {
131 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
132 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
133 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
134 break;
135 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
136 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
137 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
138 break;
139 default:
140 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
141 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
142 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000143 }
144 }
145
146 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000148}
Jerry Yu1c105562022-07-10 06:32:38 +0000149
Ronald Cron38117652024-03-05 09:05:46 +0100150/*
151 * Non-error return values of
152 * ssl_tls13_offered_psks_check_identity_match_ticket() and
153 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
154 * not collide with error codes that are negative. Zero
155 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
156 * up by the callers of this function as a generic success condition.
157 *
158 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
Ronald Cron7cab4f82024-03-07 15:09:09 +0100159 * that the pre-shared-key identity matches that of a ticket or an externally-
Ronald Cron38117652024-03-05 09:05:46 +0100160 * provisioned pre-shared-key. We have thus been able to retrieve the
161 * attributes of the pre-shared-key but at least one of them does not meet
162 * some criteria and the pre-shared-key cannot be used. For example, a ticket
163 * is expired or its version is not TLS 1.3. Note eventually that the return
164 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
165 * anything to do with binder check. A binder check is done only when a
166 * suitable pre-shared-key has been selected and only for that selected
167 * pre-shared-key: if the binder check fails, we fail the handshake and we do
168 * not try to find another pre-shared-key for which the binder check would
169 * succeed as recommended by the specification.
170 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100171#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
172#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
173#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800174
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800176static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800177MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Ronald Cron1f045f32024-03-25 13:37:07 +0100180#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu95699e72022-08-21 19:22:23 +0800181MBEDTLS_CHECK_RETURN_CRITICAL
182static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_context *ssl,
184 const unsigned char *identity,
185 size_t identity_len,
186 uint32_t obfuscated_ticket_age,
187 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800188{
Jerry Yufd310eb2022-09-06 09:16:35 +0800189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800190 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800191#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800192 mbedtls_ms_time_t now;
193 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800194 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800195 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800196#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800197
198 ((void) obfuscated_ticket_age);
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800201
Jerry Yufd310eb2022-09-06 09:16:35 +0800202 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100204 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Jerry Yu95699e72022-08-21 19:22:23 +0800206
Jerry Yu4746b102022-09-13 11:11:48 +0800207 /* We create a copy of the encrypted ticket since the ticket parsing
208 * function is allowed to use its input buffer as an output buffer
209 * (in-place decryption). We do, however, need the original buffer for
210 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ticket_buffer = mbedtls_calloc(1, identity_len);
213 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100221 switch (ret) {
222 case 0:
223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
224 break;
225
226 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100229 break;
230
231 case MBEDTLS_ERR_SSL_INVALID_MAC:
232 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100234 break;
235
236 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100238 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800239 }
240
241 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800243
Ronald Cronfbae94a2023-12-05 18:15:14 +0100244 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 goto exit;
246 }
Jerry Yu95699e72022-08-21 19:22:23 +0800247
Ronald Cron38117652024-03-05 09:05:46 +0100248 /*
249 * The identity matches that of a ticket. Now check that it has suitable
250 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800251 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100252 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
255 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800256 goto exit;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800260 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100261
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800264 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
265 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 goto exit;
268 }
269
Jerry Yu25ba4d42023-11-10 14:12:20 +0800270 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800271
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 /* RFC 8446 section 4.6.1
273 *
274 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
275 *
276 * RFC 8446 section 4.2.11.1
277 *
278 * Clients MUST NOT attempt to use tickets which have ages greater than
279 * the "ticket_lifetime" value which was provided with the ticket.
280 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800282 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800284 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800285 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 goto exit;
287 }
Jerry Yu95699e72022-08-21 19:22:23 +0800288
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800289 /* RFC 8446 section 4.2.10
290 *
291 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
292 * the ticket age for the selected PSK identity (computed by subtracting
293 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
294 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800295 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
297 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800298 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800299 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800300 */
Jerry Yucebffc32022-12-15 18:00:05 +0800301 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800302 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800303 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800306 3, ("Ticket age outside tolerance window ( diff = %"
307 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800308 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309 goto exit;
310 }
Jerry Yu95699e72022-08-21 19:22:23 +0800311#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312
Ronald Cron38117652024-03-05 09:05:46 +0100313 /*
314 * All good, we have found a suitable ticket.
315 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100316 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
317
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800318exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100319 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ssl_session_free(session);
321 }
Jerry Yu95699e72022-08-21 19:22:23 +0800322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
324 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800325}
326#endif /* MBEDTLS_SSL_SESSION_TICKETS */
327
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000329static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ssl_context *ssl,
331 const unsigned char *identity,
332 size_t identity_len,
333 uint32_t obfuscated_ticket_age,
334 int *psk_type,
335 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000336{
Ronald Cron606671e2023-02-17 11:36:33 +0100337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338
Jerry Yu95699e72022-08-21 19:22:23 +0800339 ((void) session);
340 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800341 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800344
Jerry Yu95699e72022-08-21 19:22:23 +0800345#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100346 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
347 ssl, identity, identity_len, obfuscated_ticket_age, session);
348 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800349 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100350 ret = mbedtls_ssl_set_hs_psk(ssl,
351 session->resumption_key,
352 session->resumption_key_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357
358 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
359 session->resumption_key,
360 session->resumption_key_len);
361 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
362 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100363 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100364 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
365 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800366 }
367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
368
Jerry Yu1c105562022-07-10 06:32:38 +0000369 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->conf->f_psk != NULL) {
371 if (ssl->conf->f_psk(
372 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100373 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100375 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800381 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ct_memcmp(ssl->conf->psk_identity,
383 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100384 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
385 if (ret != 0) {
386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
387 return ret;
388 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100389 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000390 }
391
Ronald Cronfbae94a2023-12-05 18:15:14 +0100392 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000393}
394
Ronald Cron38117652024-03-05 09:05:46 +0100395/*
396 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
397 * They are positive to not collide with error codes that are negative. Zero
398 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
399 * by the callers of this function as a generic success condition.
400 */
Ronald Cron6e311272023-12-05 17:57:01 +0100401#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
402#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800403MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000404static int ssl_tls13_offered_psks_check_binder_match(
405 mbedtls_ssl_context *ssl,
406 const unsigned char *binder, size_t binder_len,
407 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800413 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800414 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800415 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000416
Ronald Crona5c5c582024-03-19 13:54:15 +0100417 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
418 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
419 }
420
Jerry Yu1c105562022-07-10 06:32:38 +0000421 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800422 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200423 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 transcript, sizeof(transcript), &transcript_len);
425 if (ret != 0) {
426 return ret;
427 }
Jerry Yu1c105562022-07-10 06:32:38 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
430 if (ret != 0) {
431 return ret;
432 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
435 psk, psk_len, psk_type,
436 transcript,
437 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (ret != 0) {
442 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
447 server_computed_binder, transcript_len);
448 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000449
Ronald Crona5c5c582024-03-19 13:54:15 +0100450 if (mbedtls_ct_memcmp(server_computed_binder,
451 binder,
452 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100453 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000454 }
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_platform_zeroize(server_computed_binder,
457 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100458 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800459}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800460
Jerry Yu82534862022-08-30 10:42:33 +0800461#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100463static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
464 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800465{
Jerry Yu82534862022-08-30 10:42:33 +0800466 dst->ticket_age_add = src->ticket_age_add;
467 dst->ticket_flags = src->ticket_flags;
468 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (src->resumption_key_len == 0) {
470 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
471 }
472 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800473
Jerry Yu33bf2402022-12-12 16:01:43 +0800474#if defined(MBEDTLS_SSL_EARLY_DATA)
475 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000476
477#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000478 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000479 if (ret != 0) {
480 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000481 }
482#endif /* MBEDTLS_SSL_ALPN */
483#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800486}
487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
488
Ronald Cron79cdd412024-02-20 17:19:28 +0100489struct psk_attributes {
490 int type;
491 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100492 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100493};
Ronald Cron16cc3702024-03-07 15:04:56 +0100494#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100495
Jerry Yu1c105562022-07-10 06:32:38 +0000496/* Parser for pre_shared_key extension in client hello
497 * struct {
498 * opaque identity<1..2^16-1>;
499 * uint32 obfuscated_ticket_age;
500 * } PskIdentity;
501 *
502 * opaque PskBinderEntry<32..255>;
503 *
504 * struct {
505 * PskIdentity identities<7..2^16-1>;
506 * PskBinderEntry binders<33..2^16-1>;
507 * } OfferedPsks;
508 *
509 * struct {
510 * select (Handshake.msg_type) {
511 * case client_hello: OfferedPsks;
512 * ....
513 * };
514 * } PreSharedKeyExtension;
515 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800516MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000517static int ssl_tls13_parse_pre_shared_key_ext(
518 mbedtls_ssl_context *ssl,
519 const unsigned char *pre_shared_key_ext,
520 const unsigned char *pre_shared_key_ext_end,
521 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100522 const unsigned char *ciphersuites_end,
523 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000524{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800526 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800527 const unsigned char *p_identity_len;
528 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000529 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800530 const unsigned char *binders;
531 const unsigned char *p_binder_len;
532 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000533 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800534 int matched_identity = -1;
535 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
538 pre_shared_key_ext,
539 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000540
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 /* identities_len 2 bytes
542 * identities_data >= 7 bytes
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
545 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
548 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800549 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000550
Jerry Yu96a2e362022-07-21 15:11:34 +0800551 /* binders_len 2 bytes
552 * binders >= 33 bytes
553 */
Jerry Yu568ec252022-07-22 21:27:34 +0800554 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
556 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800559 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000560
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100561 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
562 identities_end - pre_shared_key_ext);
563 if (0 != ret) {
564 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
565 return ret;
566 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000569 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800570 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800571 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800572 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800573 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100574 int psk_ciphersuite_id;
575 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100576 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100577
Jerry Yu82534862022-08-30 10:42:33 +0800578 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
582 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800583 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
585 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800586 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800589 binder_len = *p_binder_len;
590 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800592 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800593
Jerry Yu96a2e362022-07-21 15:11:34 +0800594 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000596 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Jerry Yu1c105562022-07-10 06:32:38 +0000598
Jerry Yu96a2e362022-07-21 15:11:34 +0800599 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100601 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100602 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800603 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100607
Ronald Cron79cdd412024-02-20 17:19:28 +0100608 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800609 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100610 psk_ciphersuite_id = 0;
611 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100612 allowed_key_exchange_modes =
613 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 break;
Jerry Yu82534862022-08-30 10:42:33 +0800615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100616 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100617 psk_ciphersuite_id = session.ciphersuite;
618 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100619 ssl->session_negotiate->ticket_flags = session.ticket_flags;
620 allowed_key_exchange_modes =
621 session.ticket_flags &
622 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100624#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800627 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100628
Ronald Cron79cdd412024-02-20 17:19:28 +0100629 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
630
Ronald Croncf284562024-02-16 18:54:10 +0100631 if ((allowed_key_exchange_modes &
632 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
633 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100634 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100635 } else if ((allowed_key_exchange_modes &
636 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
637 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100638 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100639 }
640
Ronald Cron79cdd412024-02-20 17:19:28 +0100641 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100642 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
643 continue;
644 }
645
Ronald Cron89089cc2024-02-14 18:18:08 +0100646 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
647 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100648 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100649
Ronald Cron74a16292024-02-23 17:07:41 +0100650 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100651#if defined(MBEDTLS_SSL_SESSION_TICKETS)
652 mbedtls_ssl_session_free(&session);
653#endif
654 /*
655 * We consider finding a ciphersuite suitable for the PSK as part
656 * of the validation of its binder. Thus if we do not find one, we
657 * abort the handshake with a decrypt_error alert.
658 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800659 MBEDTLS_SSL_PEND_FATAL_ALERT(
660 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100662 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800663 }
664
Jerry Yu96a2e362022-07-21 15:11:34 +0800665 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100666 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100667 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100668 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800669 /* For security reasons, the handshake should be aborted when we
670 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
671 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800672#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800674#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000676 MBEDTLS_SSL_DEBUG_RET(
677 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800678 MBEDTLS_SSL_PEND_FATAL_ALERT(
679 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
681 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800682 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800683
684 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800685
Jerry Yu82534862022-08-30 10:42:33 +0800686#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100687 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
689 &session);
690 mbedtls_ssl_session_free(&session);
691 if (ret != 0) {
692 return ret;
693 }
Jerry Yu82534862022-08-30 10:42:33 +0800694 }
695#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000696 }
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (p_identity_len != identities_end || p_binder_len != binders_end) {
699 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
700 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
701 MBEDTLS_ERR_SSL_DECODE_ERROR);
702 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000703 }
704
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800705 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000706 ret = ssl->handshake->update_checksum(
707 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100708 if (0 != ret) {
709 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
710 return ret;
711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100713 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 ssl->handshake->selected_identity = (uint16_t) matched_identity;
718 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000721}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722
723/*
724 * struct {
725 * select ( Handshake.msg_type ) {
726 * ....
727 * case server_hello:
728 * uint16 selected_identity;
729 * }
730 * } PreSharedKeyExtension;
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
733 unsigned char *buf,
734 unsigned char *end,
735 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736{
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738
739 *olen = 0;
740
David Horstmann21b89762022-10-06 18:34:28 +0100741 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000744#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000746#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000748 /* We shouldn't have called this extension writer unless we've
749 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
754 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
757 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000760
761 *olen = 6;
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
764 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000769}
770
Ronald Cron41a443a2022-10-04 16:38:25 +0200771#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000772
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773/* From RFC 8446:
774 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000775 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 * } SupportedVersions;
777 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100779static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
780 const unsigned char *buf,
781 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000784 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000785 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000786 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100787 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000790 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791 p += 1;
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000794 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 while (p < versions_end) {
796 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
797 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000798 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Ronald Cron3bd2b022023-04-03 16:45:39 +0200800 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100801 found_supported_version = 1;
802 break;
803 }
804
Ronald Cron3bd2b022023-04-03 16:45:39 +0200805 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
806 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100807 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808 break;
809 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 }
811
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100812 if (!found_supported_version) {
813 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
816 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
817 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 }
819
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100820 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100823 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824}
825
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200826#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000827/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828 *
829 * From RFC 8446:
830 * enum {
831 * ... (0xFFFF)
832 * } NamedGroup;
833 * struct {
834 * NamedGroup named_group_list<2..2^16-1>;
835 * } NamedGroupList;
836 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
839 const unsigned char *buf,
840 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000843 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000844 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
847 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
848 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000851 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000852 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000855 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
857 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000858 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_SSL_DEBUG_MSG(2,
861 ("got named group: %s(%04x)",
862 mbedtls_ssl_named_group_to_str(named_group),
863 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
866 !mbedtls_ssl_named_group_is_supported(named_group) ||
867 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000868 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869 }
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_DEBUG_MSG(2,
872 ("add named group %s(%04x) into received list.",
873 mbedtls_ssl_named_group_to_str(named_group),
874 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800875
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000876 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877 }
878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880
881}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200882#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883
XiaokangQian08037552022-04-20 07:16:41 +0000884#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
885
Valerio Settic9ae8622023-07-25 11:23:50 +0200886#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887/*
888 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000889 * extension is correct and stores the first acceptable key share and its
890 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891 *
892 * Possible return values are:
893 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000894 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
895 * the client does not match a group supported by the server. A
896 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000897 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800898 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200899MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100900static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
901 const unsigned char *buf,
902 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903{
XiaokangQianb67384d2022-04-19 00:02:38 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000905 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000906 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800907 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000908
909 /* From RFC 8446:
910 *
911 * struct {
912 * KeyShareEntry client_shares<0..2^16-1>;
913 * } KeyShareClientHello;
914 *
915 */
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
918 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000921
922 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000923 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924
925 /* We try to find a suitable key share entry and copy it to the
926 * handshake context. Later, we have to find out whether we can do
927 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000928 * dismiss it and send a HelloRetryRequest message.
929 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000932 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800933 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800934 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935
936 /*
937 * struct {
938 * NamedGroup group;
939 * opaque key_exchange<1..2^16-1>;
940 * } KeyShareEntry;
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
943 group = MBEDTLS_GET_UINT16_BE(p, 0);
944 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800945 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800946 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800948 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949
950 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000951 * for input validation purposes.
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
954 !mbedtls_ssl_named_group_is_supported(group) ||
955 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000956 continue;
957 }
XiaokangQian060d8672022-04-21 09:24:56 +0000958
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200960 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200962 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200963 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200964 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_ssl_named_group_to_str(group),
966 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200967 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 ssl, key_exchange - 2, key_exchange_len + 2);
969 if (ret != 0) {
970 return ret;
971 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 } else {
974 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
975 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976 continue;
977 }
978
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000979 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980 }
981
Jerry Yuc1be19f2022-04-23 16:11:39 +0800982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (ssl->handshake->offered_group_id == 0) {
984 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
985 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988}
Valerio Settic9ae8622023-07-25 11:23:50 +0200989#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000990
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200991MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100992static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
993 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000994{
Jerry Yu0c354a22022-08-29 15:25:36 +0800995 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000997}
998
Jerry Yue5991322022-11-07 14:03:44 +0800999#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001000MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +00001001static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return ssl_tls13_client_hello_has_exts(
1005 ssl,
1006 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1007 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1008 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001009}
Jerry Yue5991322022-11-07 14:03:44 +08001010#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011
Jerry Yue5991322022-11-07 14:03:44 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001013MBEDTLS_CHECK_RETURN_CRITICAL
1014static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001016{
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return ssl_tls13_client_hello_has_exts(
1018 ssl,
1019 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1020 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001021}
Jerry Yue5991322022-11-07 14:03:44 +08001022#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001023
Jerry Yue5991322022-11-07 14:03:44 +08001024#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001025MBEDTLS_CHECK_RETURN_CRITICAL
1026static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001028{
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return ssl_tls13_client_hello_has_exts(
1030 ssl,
1031 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1032 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1033 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1034 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001035}
Jerry Yue5991322022-11-07 14:03:44 +08001036#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001037
Pengyu Lv306a01d2023-02-02 16:35:47 +08001038#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1039MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001040static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001041{
Jerry Yue5991322022-11-07 14:03:44 +08001042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001043 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001044 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001046#else
1047 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001049#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001050}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051
Jerry Yu77f01482022-07-11 07:03:24 +00001052MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001053static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001054{
Jerry Yue5991322022-11-07 14:03:44 +08001055#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001056 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001057 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001059#else
1060 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001062#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001063}
1064#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1065
1066MBEDTLS_CHECK_RETURN_CRITICAL
1067static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1068{
1069#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1070 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1071 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1072#else
1073 ((void) ssl);
1074 return 0;
1075#endif
1076}
1077
XiaokangQian81802f42022-06-10 13:25:22 +00001078#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001079 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001080
1081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001082static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001083{
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001103 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001105 }
1106}
1107#endif /* MBEDTLS_USE_PSA_CRYPTO */
1108
XiaokangQian23c5be62022-06-07 02:04:34 +00001109/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001110 * Pick best ( private key, certificate chain ) pair based on the signature
1111 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001112 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001113MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001114static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001115{
XiaokangQianfb665a82022-06-15 03:57:21 +00001116 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001117 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001118
1119#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001121 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (key_cert_list == NULL) {
1127 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1128 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001129 }
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1132 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 for (key_cert = key_cert_list; key_cert != NULL;
1141 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001142#if defined(MBEDTLS_USE_PSA_CRYPTO)
1143 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1144#endif /* MBEDTLS_USE_PSA_CRYPTO */
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1147 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001148
1149 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 * This avoids sending the client a cert it'll reject based on
1151 * keyUsage or other extensions.
1152 */
1153 if (mbedtls_x509_crt_check_key_usage(
1154 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001155 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001156 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1158 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1159 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001160 continue;
1161 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_SSL_DEBUG_MSG(3,
1164 ("ssl_tls13_pick_key_cert:"
1165 "check signature algorithm %s [%04x]",
1166 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1167 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001170#endif /* MBEDTLS_USE_PSA_CRYPTO */
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1173 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001174#if defined(MBEDTLS_USE_PSA_CRYPTO)
1175 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1177 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001180 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(3,
1182 ("ssl_tls13_pick_key_cert:"
1183 "selected signature algorithm"
1184 " %s [%04x]",
1185 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1186 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001187 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 3, "selected certificate (chain)",
1189 ssl->handshake->key_cert->cert);
1190 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001191 }
XiaokangQian81802f42022-06-10 13:25:22 +00001192 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001193 }
1194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1196 "no suitable certificate found"));
1197 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001198}
XiaokangQian81802f42022-06-10 13:25:22 +00001199#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001200 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001201
XiaokangQian4080a7f2022-04-11 09:55:18 +00001202/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
1204 * STATE HANDLING: ClientHello
1205 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001206 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001209 *
1210 * In this case, the server progresses to sending its ServerHello.
1211 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001212 * 2) The ClientHello was well-formed but didn't match the server's
1213 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001214 *
1215 * For example, the client might not have offered a key share which
1216 * the server supports, or the server might require a cookie.
1217 *
1218 * In this case, the server sends a HelloRetryRequest.
1219 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001220 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001221 *
1222 * In this case, we abort the handshake.
1223 *
1224 */
1225
1226/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001227 * Structure of this message:
1228 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001229 * uint16 ProtocolVersion;
1230 * opaque Random[32];
1231 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001232 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001233 * struct {
1234 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1235 * Random random;
1236 * opaque legacy_session_id<0..32>;
1237 * CipherSuite cipher_suites<2..2^16-2>;
1238 * opaque legacy_compression_methods<1..2^8-1>;
1239 * Extension extensions<8..2^16-1>;
1240 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001241 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001242
1243#define SSL_CLIENT_HELLO_OK 0
1244#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001245#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001246
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001248static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1249 const unsigned char *buf,
1250 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001251{
XiaokangQianb67384d2022-04-19 00:02:38 +00001252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1253 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001254 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001255 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001256 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001257 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001258 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001259 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001260 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001262 const unsigned char *supported_versions_data;
1263 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001264 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001265 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001266 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001267
Ronald Cron41a443a2022-10-04 16:38:25 +02001268#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001269 int got_psk = 0;
1270 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001271 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001272 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001273#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001276 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001278 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001279 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001280 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 * .. . .. ciphersuite list length ( 2 bytes )
1282 * .. . .. ciphersuite list
1283 * .. . .. compression alg. list length ( 1 byte )
1284 * .. . .. compression alg. list
1285 * .. . .. extensions length ( 2 bytes, optional )
1286 * .. . .. extensions ( optional )
1287 */
1288
XiaokangQianb67384d2022-04-19 00:02:38 +00001289 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001290 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1292 * read at least up to session id length without worrying.
1293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295
1296 /* ...
1297 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1298 * ...
1299 * with ProtocolVersion defined as:
1300 * uint16 ProtocolVersion;
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1303 MBEDTLS_SSL_VERSION_TLS1_2) {
1304 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1305 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1306 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1307 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308 }
1309 p += 2;
1310
Jerry Yuc1be19f2022-04-23 16:11:39 +08001311 /* ...
1312 * Random random;
1313 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001314 * with Random defined as:
1315 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 */
Ronald Crond540d992023-03-07 09:41:48 +01001317 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001318 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319
Jerry Yuc1be19f2022-04-23 16:11:39 +08001320 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001321 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001322 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323 */
Ronald Croncada4102023-03-07 09:51:39 +01001324 legacy_session_id_len = *(p++);
1325 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326
XiaokangQianb67384d2022-04-19 00:02:38 +00001327 /*
1328 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001329 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001332 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001333
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001334 /* ...
1335 * CipherSuite cipher_suites<2..2^16-2>;
1336 * ...
1337 * with CipherSuite defined as:
1338 * uint8 CipherSuite[2];
1339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001341 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001342 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343
Ronald Cronfc7ae872023-02-16 15:32:19 +01001344 /*
1345 * The length of the ciphersuite list has to be even.
1346 */
1347 if (cipher_suites_len & 1) {
1348 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1349 MBEDTLS_ERR_SSL_DECODE_ERROR);
1350 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1351 }
1352
XiaokangQianb67384d2022-04-19 00:02:38 +00001353 /* Check we have enough data for the ciphersuite list, the legacy
1354 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001355 *
1356 * cipher_suites cipher_suites_len bytes
1357 * legacy_compression_methods 2 bytes
1358 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001361 p += cipher_suites_len;
1362 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001363
1364 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001365 * Search for the supported versions extension and parse it to determine
1366 * if the client supports TLS 1.3.
1367 */
1368 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1369 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001370 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001371 if (ret < 0) {
1372 MBEDTLS_SSL_DEBUG_RET(1,
1373 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1374 return ret;
1375 }
1376
1377 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001378 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001379 }
1380
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001381 if (ret == 1) {
1382 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001383 supported_versions_data,
1384 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001385 if (ret < 0) {
1386 MBEDTLS_SSL_DEBUG_RET(1,
1387 ("ssl_tls13_parse_supported_versions_ext"), ret);
1388 return ret;
1389 }
1390
Ronald Cronb828c7d2023-04-03 16:37:22 +02001391 /*
1392 * The supported versions extension was parsed successfully as the
1393 * value returned by ssl_tls13_parse_supported_versions_ext() is
1394 * positive. The return value is then equal to
1395 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1396 * the TLS version to negotiate.
1397 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001398 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1399 return SSL_CLIENT_HELLO_TLS1_2;
1400 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001401 }
1402
1403 /*
1404 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001405 */
1406 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001407 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1408 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001409
1410 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001411 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001412 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001413 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001414 */
1415 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1416 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1417 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1418
Ronald Croncada4102023-03-07 09:51:39 +01001419 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1420 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1421 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1422 }
1423 ssl->session_negotiate->id_len = legacy_session_id_len;
1424 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1425 legacy_session_id, legacy_session_id_len);
1426 memcpy(&ssl->session_negotiate->id[0],
1427 legacy_session_id, legacy_session_id_len);
1428
Ronald Crond540d992023-03-07 09:41:48 +01001429 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001430 * Search for a matching ciphersuite
1431 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001432 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1433 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001434
Ronald Cron89089cc2024-02-14 18:18:08 +01001435 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1436 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (handshake->ciphersuite_info == NULL) {
1439 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1440 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1441 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001442 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001443 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1444
1445 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1446 ((unsigned) handshake->ciphersuite_info->id),
1447 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001448
XiaokangQian4080a7f2022-04-11 09:55:18 +00001449 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001450 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001451 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1454 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1455 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1456 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1457 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001458 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001459 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001460
Jerry Yuc1be19f2022-04-23 16:11:39 +08001461 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001462 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001463 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001464 * with Extension defined as:
1465 * struct {
1466 * ExtensionType extension_type;
1467 * opaque extension_data<0..2^16-1>;
1468 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001473 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001476 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001479 unsigned int extension_type;
1480 size_t extension_data_len;
1481 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001482 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1483
Ronald Cron5fbd2702024-02-14 10:03:36 +01001484 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001485 /* Do not accept early data extension in 2nd ClientHello */
1486 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1487 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001488
Jerry Yu0c354a22022-08-29 15:25:36 +08001489 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001490 *
1491 * The "pre_shared_key" extension MUST be the last extension in the
1492 * ClientHello (this facilitates implementation as described below).
1493 * Servers MUST check that it is the last extension and otherwise fail
1494 * the handshake with an "illegal_parameter" alert.
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001497 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001499 MBEDTLS_SSL_PEND_FATAL_ALERT(
1500 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1502 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001503 }
1504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1506 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1507 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 p += 4;
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511 extension_data_end = p + extension_data_len;
1512
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001513 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001515 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (ret != 0) {
1517 return ret;
1518 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001521#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1522 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1524 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1525 extension_data_end);
1526 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001527 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 1, "mbedtls_ssl_parse_servername_ext", ret);
1529 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001530 }
XiaokangQian40a35232022-05-07 09:02:40 +00001531 break;
1532#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1533
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001534#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537
1538 /* Supported Groups Extension
1539 *
1540 * When sent by the client, the "supported_groups" extension
1541 * indicates the named groups which the client supports,
1542 * ordered from most preferred to least preferred.
1543 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001544 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 ssl, p, extension_data_end);
1546 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001547 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001548 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001550 }
1551
XiaokangQian7807f9f2022-02-15 10:04:37 +00001552 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001553#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554
Valerio Settic9ae8622023-07-25 11:23:50 +02001555#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558
1559 /*
1560 * Key Share Extension
1561 *
1562 * When sent by the client, the "key_share" extension
1563 * contains the endpoint's cryptographic parameters for
1564 * ECDHE/DHE key establishment methods.
1565 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001566 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 ssl, p, extension_data_end);
1568 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001569 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1570 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 }
1572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001574 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 1, "ssl_tls13_parse_key_shares_ext", ret);
1576 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001577 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001578
XiaokangQian7807f9f2022-02-15 10:04:37 +00001579 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001580#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001581
1582 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001583 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 break;
1585
Ronald Cron41a443a2022-10-04 16:38:25 +02001586#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001587 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001588 MBEDTLS_SSL_DEBUG_MSG(
1589 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001590
1591 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 ssl, p, extension_data_end);
1593 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001594 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1596 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001597 }
1598
Jerry Yue19e3b92022-07-08 12:04:51 +00001599 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001600#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001601
Jerry Yu1c105562022-07-10 06:32:38 +00001602 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1604 if ((handshake->received_extensions &
1605 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001606 MBEDTLS_SSL_PEND_FATAL_ALERT(
1607 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1609 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001610 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001611#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001612 /* Delay processing of the PSK identity once we have
1613 * found out which algorithms to use. We keep a pointer
1614 * to the buffer and the size for later processing.
1615 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001616 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001617 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001618#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001619 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001620
XiaokangQianacb39922022-06-17 10:18:48 +00001621#if defined(MBEDTLS_SSL_ALPN)
1622 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1626 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001627 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1629 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001630 }
XiaokangQianacb39922022-06-17 10:18:48 +00001631 break;
1632#endif /* MBEDTLS_SSL_ALPN */
1633
Ronald Cron928cbd32022-10-04 16:14:26 +02001634#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001635 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637
Gabor Mezei078e8032022-04-27 21:17:56 +02001638 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 ssl, p, extension_data_end);
1640 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001641 MBEDTLS_SSL_DEBUG_RET(
1642 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001644 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001645 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001646#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001647
Jan Bruckner151f6422023-02-10 12:45:19 +01001648#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1649 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1650 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1651
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001652 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1653 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001654 if (ret != 0) {
1655 MBEDTLS_SSL_DEBUG_RET(
1656 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1657 return ret;
1658 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001659 break;
1660#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1661
XiaokangQian7807f9f2022-02-15 10:04:37 +00001662 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001663 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001664 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001666 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001667 }
1668
1669 p += extension_data_len;
1670 }
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1673 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001674
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001675 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1676 MBEDTLS_SSL_HS_CLIENT_HELLO,
1677 p - buf);
1678 if (0 != ret) {
1679 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1680 return ret;
1681 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001682
Ronald Cron41a443a2022-10-04 16:38:25 +02001683#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001684 /* Update checksum with either
1685 * - The entire content of the CH message, if no PSK extension is present
1686 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001687 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001688 * ClientHello even if some pre-requisites for PSK key exchange modes are
1689 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001690 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001691 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001692 ret = handshake->update_checksum(ssl, buf,
1693 pre_shared_key_ext - buf);
1694 if (0 != ret) {
1695 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1696 return ret;
1697 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1699 pre_shared_key_ext,
1700 pre_shared_key_ext_end,
1701 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001702 cipher_suites_end,
1703 &psk);
1704 if (ret == 0) {
1705 got_psk = 1;
1706 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001707 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1709 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001712#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001713 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001714 ret = handshake->update_checksum(ssl, buf, p - buf);
1715 if (0 != ret) {
1716 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1717 return ret;
1718 }
Jerry Yu1c105562022-07-10 06:32:38 +00001719 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720
Ronald Cron79cdd412024-02-20 17:19:28 +01001721 /*
1722 * Determine the key exchange algorithm to use.
1723 * There are three types of key exchanges supported in TLS 1.3:
1724 * - (EC)DH with ECDSA,
1725 * - (EC)DH with PSK,
1726 * - plain PSK.
1727 *
1728 * The PSK-based key exchanges may additionally be used with 0-RTT.
1729 *
1730 * Our built-in order of preference is
1731 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1732 * 2 ) Certificate Mode ( ephemeral )
1733 * 3 ) Plain PSK Mode ( psk )
1734 */
1735#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1736 if (got_psk && (psk.key_exchange_mode ==
1737 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1738 handshake->key_exchange_mode =
1739 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1740 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1741
1742 } else
1743#endif
1744 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1745 handshake->key_exchange_mode =
1746 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1747 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1748
1749 }
1750#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1751 else if (got_psk && (psk.key_exchange_mode ==
1752 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1753 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1754 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1755 }
1756#endif
1757 else {
1758 MBEDTLS_SSL_DEBUG_MSG(
1759 1,
1760 ("ClientHello message misses mandatory extensions."));
1761 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1762 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1763 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001765
Ronald Cron3e47eec2024-02-21 10:29:24 +01001766#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001767 if (handshake->key_exchange_mode &
1768 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1769 handshake->ciphersuite_info = psk.ciphersuite_info;
1770 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1771
1772 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1773 ((unsigned) psk.ciphersuite_info->id),
1774 psk.ciphersuite_info->name));
1775
1776 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1777 handshake->resume = 1;
1778 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001779 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001780#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001781
1782 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001783 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1784 hrr_required = (no_usable_share_for_key_agreement != 0);
1785 }
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001790}
1791
Jerry Yuab0da372023-02-08 13:55:24 +08001792#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001793static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001794{
1795 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1796
Jerry Yu985c9672022-12-04 14:06:30 +08001797 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1798 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001799 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001800 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001801 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001802 }
1803
Jerry Yu454dda32023-10-31 15:13:54 +08001804 if (!handshake->resume) {
1805 /* We currently support early data only in the case of PSKs established
1806 via a NewSessionTicket message thus in the case of a session
1807 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001808 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001809 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001810 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001811 }
1812
Jerry Yu82fd6c12023-10-31 16:32:19 +08001813 /* RFC 8446 4.2.10
1814 *
1815 * In order to accept early data, the server MUST have accepted a PSK cipher
1816 * suite and selected the first key offered in the client's "pre_shared_key"
1817 * extension. In addition, it MUST verify that the following values are the
1818 * same as those associated with the selected PSK:
1819 * - The TLS version number
1820 * - The selected cipher suite
1821 * - The selected ALPN [RFC7301] protocol, if any
1822 *
1823 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001824 * - The TLS version number is checked in
1825 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001826 */
1827
1828 if (handshake->selected_identity != 0) {
1829 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001830 1, ("EarlyData: rejected, the selected key in "
1831 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001832 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001833 }
1834
1835 if (handshake->ciphersuite_info->id !=
1836 ssl->session_negotiate->ciphersuite) {
1837 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001838 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1839 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001840 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001841
1842 }
Jerry Yu985c9672022-12-04 14:06:30 +08001843
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001844 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001845 MBEDTLS_SSL_DEBUG_MSG(
1846 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001847 ("EarlyData: rejected, early_data not allowed in ticket "
1848 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001849 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001850 }
Jerry Yu985c9672022-12-04 14:06:30 +08001851
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001852#if defined(MBEDTLS_SSL_ALPN)
1853 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1854 size_t alpn_len;
1855
1856 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1857 return 0;
1858 }
1859
1860 if (alpn != NULL) {
1861 alpn_len = strlen(alpn);
1862 }
1863
1864 if (alpn == NULL ||
1865 ssl->session_negotiate->ticket_alpn == NULL ||
1866 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1867 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1868 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1869 "from the one associated with the pre-shared key."));
1870 return -1;
1871 }
1872#endif
1873
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001874 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001875}
1876#endif /* MBEDTLS_SSL_EARLY_DATA */
1877
XiaokangQian75fe8c72022-06-15 09:42:45 +00001878/* Update the handshake state machine */
1879
Ronald Cronce7d76e2022-07-08 18:56:49 +02001880MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001881static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1882 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001883{
1884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1885
XiaokangQian7807f9f2022-02-15 10:04:37 +00001886 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001887 * Server certificate selection
1888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1890 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1891 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001892 }
1893#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1894 ssl->handshake->sni_name = NULL;
1895 ssl->handshake->sni_name_len = 0;
1896#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1899 if (ret != 0) {
1900 MBEDTLS_SSL_DEBUG_RET(1,
1901 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1902 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001903 }
1904
Jerry Yuab0da372023-02-08 13:55:24 +08001905#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001906 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001907 ssl->handshake->early_data_accepted =
1908 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001909
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001910 if (ssl->handshake->early_data_accepted) {
1911 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1912 if (ret != 0) {
1913 MBEDTLS_SSL_DEBUG_RET(
1914 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1915 return ret;
1916 }
1917 } else {
1918 ssl->discard_early_data_record =
1919 hrr_required ?
1920 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1921 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001922 }
1923 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001924#else
1925 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001926#endif /* MBEDTLS_SSL_EARLY_DATA */
1927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001929}
1930
1931/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001932 * Main entry point from the state machine; orchestrates the otherfunctions.
1933 */
1934
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001935MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001936static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001937{
1938
XiaokangQian08037552022-04-20 07:16:41 +00001939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001941 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001942 int parse_client_hello_ret;
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1947 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1948 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1951 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001952 parse_client_hello_ret = ret; /* Store positive return value of
1953 * parse_client_hello,
1954 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001955 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001956
Ronald Cronb828c7d2023-04-03 16:37:22 +02001957 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001958 * Version 1.2 of the protocol has to be used for the handshake.
1959 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001960 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1961 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1962 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1963 * will dispatch to the TLS 1.2 state machine.
1964 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001965 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001966 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001967 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001968 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001969 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001970 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001971 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1972 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1973 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001974 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001975 ssl->keep_current_message = 1;
1976 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1977 return 0;
1978 }
1979
Ronald Cron7b6ee942024-01-12 10:29:55 +01001980 MBEDTLS_SSL_PROC_CHK(
1981 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1982 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001983
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001984 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1986 } else {
1987 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1988 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001989
1990cleanup:
1991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1993 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001994}
1995
1996/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001997 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001998 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001999MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002000static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002001{
Jerry Yu637a3f12022-04-20 21:37:58 +08002002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2003 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2007 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2008 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2009 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2013 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002014
2015#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002016 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002017#endif /* MBEDTLS_HAVE_TIME */
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002020}
2021
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002023 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002024 *
2025 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002026 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027 * } SupportedVersions;
2028 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002029MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002030static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 mbedtls_ssl_context *ssl,
2032 unsigned char *buf,
2033 unsigned char *end,
2034 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036 *out_len = 0;
2037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039
2040 /* Check if we have space to write the extension:
2041 * - extension_type (2 bytes)
2042 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002043 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 mbedtls_ssl_write_version(buf + 4,
2052 ssl->conf->transport,
2053 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2056 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057
Jerry Yu349a6132022-04-14 20:52:56 +08002058 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059
Jerry Yub95dd362022-11-08 21:19:34 +08002060 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064}
2065
Jerry Yud9436a12022-04-20 22:28:09 +08002066
Jerry Yu3bf2c642022-03-30 22:02:12 +08002067
2068/* Generate and export a single key share. For hybrid KEMs, this can
2069 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002070MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002071static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2072 uint16_t named_group,
2073 unsigned char *buf,
2074 unsigned char *end,
2075 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076{
2077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002078
2079 *out_len = 0;
2080
Valerio Settic9ae8622023-07-25 11:23:50 +02002081#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002082 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002083 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002084 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 ssl, named_group, buf, end, out_len);
2086 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002087 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002088 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 ret);
2090 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002093#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (0 /* Other kinds of KEMs */) {
2095 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002096 ((void) ssl);
2097 ((void) named_group);
2098 ((void) buf);
2099 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2101 }
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104}
2105
2106/*
2107 * ssl_tls13_write_key_share_ext
2108 *
2109 * Structure of key_share extension in ServerHello:
2110 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002111 * struct {
2112 * NamedGroup group;
2113 * opaque key_exchange<1..2^16-1>;
2114 * } KeyShareEntry;
2115 * struct {
2116 * KeyShareEntry server_share;
2117 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002119MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002120static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2121 unsigned char *buf,
2122 unsigned char *end,
2123 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124{
Jerry Yu955ddd72022-04-22 22:27:33 +08002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002127 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130
2131 *out_len = 0;
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2136 mbedtls_ssl_named_group_to_str(group),
2137 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002138
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139 /* Check if we have space for header and length fields:
2140 * - extension_type (2 bytes)
2141 * - extension_data_length (2 bytes)
2142 * - group (2 bytes)
2143 * - key_exchange_length (2 bytes)
2144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2146 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2147 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002148 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002149
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2151 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002152 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 ssl, group, server_share + 4, end, &key_exchange_length);
2154 if (ret != 0) {
2155 return ret;
2156 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002157 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002162
Jerry Yu57d48412022-04-20 21:50:42 +08002163 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002168}
Jerry Yud9436a12022-04-20 22:28:09 +08002169
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002170MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002171static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2172 unsigned char *buf,
2173 unsigned char *end,
2174 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002175{
2176 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2177 /* key_share Extension
2178 *
2179 * struct {
2180 * select (Handshake.msg_type) {
2181 * ...
2182 * case hello_retry_request:
2183 * NamedGroup selected_group;
2184 * ...
2185 * };
2186 * } KeyShare;
2187 */
2188
2189 *out_len = 0;
2190
Jerry Yub0ac10b2022-05-05 11:10:08 +08002191 /*
2192 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2193 * of the HRR is then to transmit a cookie to force the client to demonstrate
2194 * reachability at their apparent network address (primarily useful for DTLS).
2195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2197 return 0;
2198 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002199
2200 /* We should only send the key_share extension if the client's initial
2201 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (ssl->handshake->offered_group_id != 0) {
2203 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2204 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002205 }
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (selected_group == 0) {
2208 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2209 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002210 }
2211
Jerry Yub0ac10b2022-05-05 11:10:08 +08002212 /* Check if we have enough space:
2213 * - extension_type (2 bytes)
2214 * - extension_data_length (2 bytes)
2215 * - selected_group (2 bytes)
2216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2220 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2221 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_SSL_DEBUG_MSG(3,
2224 ("HRR selected_group: %s (%x)",
2225 mbedtls_ssl_named_group_to_str(selected_group),
2226 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002227
2228 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002233}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002234
2235/*
2236 * Structure of ServerHello message:
2237 *
2238 * struct {
2239 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2240 * Random random;
2241 * opaque legacy_session_id_echo<0..32>;
2242 * CipherSuite cipher_suite;
2243 * uint8 legacy_compression_method = 0;
2244 * Extension extensions<6..2^16-1>;
2245 * } ServerHello;
2246 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002248static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2249 unsigned char *buf,
2250 unsigned char *end,
2251 size_t *out_len,
2252 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002253{
Jerry Yu955ddd72022-04-22 22:27:33 +08002254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002256 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002257 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002258
2259 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002260 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002261
Jerry Yucfc04b32022-04-21 09:31:58 +08002262 /* ...
2263 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2264 * ...
2265 * with ProtocolVersion defined as:
2266 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2269 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002270 p += 2;
2271
Jerry Yu1c3e6882022-04-20 21:23:40 +08002272 /* ...
2273 * Random random;
2274 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002275 * with Random defined as:
2276 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2279 if (is_hrr) {
2280 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2281 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2282 } else {
2283 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2284 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2287 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002288 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2289
Jerry Yucfc04b32022-04-21 09:31:58 +08002290 /* ...
2291 * opaque legacy_session_id_echo<0..32>;
2292 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2295 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2296 if (ssl->session_negotiate->id_len > 0) {
2297 memcpy(p, &ssl->session_negotiate->id[0],
2298 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002299 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2302 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303 }
2304
Jerry Yucfc04b32022-04-21 09:31:58 +08002305 /* ...
2306 * CipherSuite cipher_suite;
2307 * ...
2308 * with CipherSuite defined as:
2309 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2312 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002313 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_DEBUG_MSG(3,
2315 ("server hello, chosen ciphersuite: %s ( id=%d )",
2316 mbedtls_ssl_get_ciphersuite_name(
2317 ssl->session_negotiate->ciphersuite),
2318 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002319
Jerry Yucfc04b32022-04-21 09:31:58 +08002320 /* ...
2321 * uint8 legacy_compression_method = 0;
2322 * ...
2323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002325 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326
Jerry Yucfc04b32022-04-21 09:31:58 +08002327 /* ...
2328 * Extension extensions<6..2^16-1>;
2329 * ...
2330 * struct {
2331 * ExtensionType extension_type; (2 bytes)
2332 * opaque extension_data<0..2^16-1>;
2333 * } Extension;
2334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002336 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002337 p += 2;
2338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2340 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002341 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2343 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002344 }
2345 p += output_len;
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2348 if (is_hrr) {
2349 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2350 } else {
2351 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2352 }
2353 if (ret != 0) {
2354 return ret;
2355 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002356 p += output_len;
2357 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002358
Ronald Cron41a443a2022-10-04 16:38:25 +02002359#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2361 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2362 if (ret != 0) {
2363 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2364 ret);
2365 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002366 }
2367 p += output_len;
2368 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002369#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2374 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002375
Jerry Yud9436a12022-04-20 22:28:09 +08002376 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002379
Jerry Yu7de2ff02022-11-08 21:43:46 +08002380 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002381 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_SSL_HS_SERVER_HELLO,
2383 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002386}
2387
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002388MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002389static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002390{
2391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2393 if (ret != 0) {
2394 MBEDTLS_SSL_DEBUG_RET(1,
2395 "mbedtls_ssl_tls13_compute_handshake_transform",
2396 ret);
2397 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002398 }
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002401}
2402
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002403MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002404static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002405{
Jerry Yu637a3f12022-04-20 21:37:58 +08002406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002407 unsigned char *buf;
2408 size_t buf_len, msg_len;
2409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002413
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002414 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2415 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2418 buf + buf_len,
2419 &msg_len,
2420 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002421
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002422 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002423 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2426 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002427
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002428 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002429
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002430#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2431 /* The server sends a dummy change_cipher_spec record immediately
2432 * after its first handshake message. This may either be after
2433 * a ServerHello or a HelloRetryRequest.
2434 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002435 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002437#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002439#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002440
Jerry Yuf4b27e42022-03-30 17:32:21 +08002441cleanup:
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2444 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002445}
2446
Jerry Yu23d1a252022-05-12 18:08:59 +08002447
2448/*
2449 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2450 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002451MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002452static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002453{
2454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002455 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2457 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2458 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2459 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002460 }
2461
2462 /*
2463 * Create stateless transcript hash for HRR
2464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2466 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2467 if (ret != 0) {
2468 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2469 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002474}
2475
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002476MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002477static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002478{
2479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2480 unsigned char *buf;
2481 size_t buf_len, msg_len;
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2488 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2489 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2492 buf + buf_len,
2493 &msg_len,
2494 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002495 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002496 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002497
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2500 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002501
Ronald Cron5fbd2702024-02-14 10:03:36 +01002502 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002503
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002504#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2505 /* The server sends a dummy change_cipher_spec record immediately
2506 * after its first handshake message. This may either be after
2507 * a ServerHello or a HelloRetryRequest.
2508 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002509 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002511#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002513#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002514
2515cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2517 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002518}
2519
Jerry Yu5b64ae92022-03-30 17:15:02 +08002520/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002521 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2522 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523
2524/*
2525 * struct {
2526 * Extension extensions<0..2 ^ 16 - 1>;
2527 * } EncryptedExtensions;
2528 *
2529 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002530MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002531static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2532 unsigned char *buf,
2533 unsigned char *end,
2534 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535{
XiaokangQianacb39922022-06-17 10:18:48 +00002536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002537 unsigned char *p = buf;
2538 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002539 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002540 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002541
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542 *out_len = 0;
2543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002545 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546 p += 2;
2547
Jerry Yu8937eb42022-05-03 12:12:14 +08002548 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002549 ((void) ret);
2550 ((void) output_len);
2551
2552#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2554 if (ret != 0) {
2555 return ret;
2556 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002557 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002558#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Jerry Yu71c14f12022-12-12 11:10:35 +08002560#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002561 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002562 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002563 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002564 if (ret != 0) {
2565 return ret;
2566 }
2567 p += output_len;
2568 }
2569#endif /* MBEDTLS_SSL_EARLY_DATA */
2570
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002571#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002572 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002573 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2574 ssl, p, end, &output_len);
2575 if (ret != 0) {
2576 return ret;
2577 }
2578 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002579 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002580#endif
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 extensions_len = (p - p_extensions_len) - 2;
2583 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002584
2585 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002588
Jerry Yu7de2ff02022-11-08 21:43:46 +08002589 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593}
2594
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002596static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002597{
2598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2599 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002600 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 mbedtls_ssl_set_outbound_transform(ssl,
2603 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002604 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002609 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2610 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2611 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2614 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002615
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002616 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002617 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2618 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2621 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002622
Ronald Cron928cbd32022-10-04 16:14:26 +02002623#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2625 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2626 } else {
2627 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2628 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002629#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002631#endif
2632
Jerry Yu4d3841a2022-04-16 12:37:19 +08002633cleanup:
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2636 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002637}
2638
Ronald Cron928cbd32022-10-04 16:14:26 +02002639#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002640#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2641#define SSL_CERTIFICATE_REQUEST_SKIP 1
2642/* Coordination:
2643 * Check whether a CertificateRequest message should be written.
2644 * Returns a negative code on failure, or
2645 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2646 * - SSL_CERTIFICATE_REQUEST_SKIP
2647 * indicating if the writing of the CertificateRequest
2648 * should be skipped or not.
2649 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002652{
2653 int authmode;
2654
XiaokangQian40a35232022-05-07 09:02:40 +00002655#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002657 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002659#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002660 authmode = ssl->conf->authmode;
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002663 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002665 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002666
XiaokangQianc3017f62022-05-13 05:55:41 +00002667 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002670}
2671
XiaokangQiancec9ae62022-05-06 07:28:50 +00002672/*
2673 * struct {
2674 * opaque certificate_request_context<0..2^8-1>;
2675 * Extension extensions<2..2^16-1>;
2676 * } CertificateRequest;
2677 *
2678 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002679MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002680static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2681 unsigned char *buf,
2682 const unsigned char *end,
2683 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684{
2685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2686 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002687 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002688 unsigned char *p_extensions_len;
2689
2690 *out_len = 0;
2691
2692 /* Check if we have enough space:
2693 * - certificate_request_context (1 byte)
2694 * - extensions length (2 bytes)
2695 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002697
2698 /*
2699 * Write certificate_request_context
2700 */
2701 /*
2702 * We use a zero length context for the normal handshake
2703 * messages. For post-authentication handshake messages
2704 * this request context would be set to a non-zero value.
2705 */
2706 *p++ = 0x0;
2707
2708 /*
2709 * Write extensions
2710 */
2711 /* The extensions must contain the signature_algorithms. */
2712 p_extensions_len = p;
2713 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2715 if (ret != 0) {
2716 return ret;
2717 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718
XiaokangQianec6efb92022-05-06 09:53:10 +00002719 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721
2722 *out_len = p - buf;
2723
Jerry Yu7de2ff02022-11-08 21:43:46 +08002724 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002728}
2729
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002730MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002731static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732{
2733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002740 unsigned char *buf;
2741 size_t buf_len, msg_len;
2742
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002743 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2744 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2745 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2748 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002749
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002750 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002751 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2752 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2755 ssl, buf_len, msg_len));
2756 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2757 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002758 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 } else {
2760 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002761 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2762 goto cleanup;
2763 }
2764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002766cleanup:
2767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2769 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002770}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002771
Jerry Yu4d3841a2022-04-16 12:37:19 +08002772/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002773 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002774 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002775MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002776static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002777{
Jerry Yu5a26f302022-05-10 20:46:40 +08002778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002779
2780#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2782 mbedtls_ssl_own_cert(ssl) == NULL) {
2783 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2784 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2785 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2786 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002787 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002788#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2791 if (ret != 0) {
2792 return ret;
2793 }
2794 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2795 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002796}
2797
2798/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002799 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002800 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002802static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002803{
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2805 if (ret != 0) {
2806 return ret;
2807 }
2808 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2809 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002810}
Ronald Cron928cbd32022-10-04 16:14:26 +02002811#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002812
Jerry Yu9b72e392023-12-01 16:27:08 +08002813/*
2814 * RFC 8446 section A.2
2815 *
2816 * | Send ServerHello
2817 * | K_send = handshake
2818 * | Send EncryptedExtensions
2819 * | [Send CertificateRequest]
2820 * Can send | [Send Certificate + CertificateVerify]
2821 * app data | Send Finished
2822 * after --> | K_send = application
2823 * here +--------+--------+
2824 * No 0-RTT | | 0-RTT
2825 * | |
2826 * K_recv = handshake | | K_recv = early data
2827 * [Skip decrypt errors] | +------> WAIT_EOED -+
2828 * | | Recv | | Recv EndOfEarlyData
2829 * | | early data | | K_recv = handshake
2830 * | +------------+ |
2831 * | |
2832 * +> WAIT_FLIGHT2 <--------+
2833 * |
2834 * +--------+--------+
2835 * No auth | | Client auth
2836 * | |
2837 * | v
2838 * | WAIT_CERT
2839 * | Recv | | Recv Certificate
2840 * | empty | v
2841 * | Certificate | WAIT_CV
2842 * | | | Recv
2843 * | v | CertificateVerify
2844 * +-> WAIT_FINISHED <---+
2845 * | Recv Finished
2846 *
2847 *
2848 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002849 * above diagram. We are not going to receive early data related messages
2850 * anymore, prepare to receive the first handshake message of the client
2851 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002852 */
Jerry Yu3be85072023-12-04 09:58:54 +08002853static void ssl_tls13_prepare_for_handshake_second_flight(
2854 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002855{
Jerry Yu9b72e392023-12-01 16:27:08 +08002856 if (ssl->handshake->certificate_request_sent) {
2857 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2858 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002859 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002860 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002861
Jerry Yu9b72e392023-12-01 16:27:08 +08002862 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2863 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002864}
2865
Jerry Yu83da34e2022-04-16 13:59:52 +08002866/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002867 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002868 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002869MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002870static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002871{
Jerry Yud6e253d2022-05-18 13:59:24 +08002872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2875 if (ret != 0) {
2876 return ret;
2877 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2880 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002881 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2883 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2884 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002885 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002886
Jerry Yu87b5ed42022-12-12 13:07:07 +08002887#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002888 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002889 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002890 MBEDTLS_SSL_DEBUG_MSG(
2891 1, ("Switch to early keys for inbound traffic. "
2892 "( K_recv = early data )"));
2893 mbedtls_ssl_set_inbound_transform(
2894 ssl, ssl->handshake->transform_earlydata);
2895 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2896 return 0;
2897 }
2898#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002899 MBEDTLS_SSL_DEBUG_MSG(
2900 1, ("Switch to handshake keys for inbound traffic "
2901 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002903
Jerry Yu3be85072023-12-04 09:58:54 +08002904 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002905
2906 return 0;
2907}
2908
Jerry Yu87b5ed42022-12-12 13:07:07 +08002909#if defined(MBEDTLS_SSL_EARLY_DATA)
2910/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002911 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002912 */
Jerry Yu3be85072023-12-04 09:58:54 +08002913#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002914#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002915/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002916 * Deals with the ambiguity of not knowing if the next message is an
2917 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002918 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002919 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002920 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002921 * indicating which message is received.
2922 */
2923MBEDTLS_CHECK_RETURN_CRITICAL
2924static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2925{
2926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002927
2928 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2929 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2930 return ret;
2931 }
2932 ssl->keep_current_message = 1;
2933
2934 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2935 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002936 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002937 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002938 }
2939
2940 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002941 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002942 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002943 /* Set the reading pointer */
2944 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002945 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2946 if (ret != 0) {
2947 return ret;
2948 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002949 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002950 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002951 }
2952
Jerry Yu7bb40a32023-12-04 10:04:15 +08002953 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2954 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002955 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002956}
2957
2958MBEDTLS_CHECK_RETURN_CRITICAL
2959static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2960 const unsigned char *buf,
2961 const unsigned char *end)
2962{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002963 /* RFC 8446 section 4.5
2964 *
2965 * struct {} EndOfEarlyData;
2966 */
Jerry Yufbf03992023-12-04 10:00:37 +08002967 if (buf != end) {
2968 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2969 MBEDTLS_ERR_SSL_DECODE_ERROR);
2970 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2971 }
2972 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002973}
2974
Jerry Yud5c34962023-12-01 16:32:31 +08002975/*
2976 * RFC 8446 section A.2
2977 *
2978 * | Send ServerHello
2979 * | K_send = handshake
2980 * | Send EncryptedExtensions
2981 * | [Send CertificateRequest]
2982 * Can send | [Send Certificate + CertificateVerify]
2983 * app data | Send Finished
2984 * after --> | K_send = application
2985 * here +--------+--------+
2986 * No 0-RTT | | 0-RTT
2987 * | |
2988 * K_recv = handshake | | K_recv = early data
2989 * [Skip decrypt errors] | +------> WAIT_EOED -+
2990 * | | Recv | | Recv EndOfEarlyData
2991 * | | early data | | K_recv = handshake
2992 * | +------------+ |
2993 * | |
2994 * +> WAIT_FLIGHT2 <--------+
2995 * |
2996 * +--------+--------+
2997 * No auth | | Client auth
2998 * | |
2999 * | v
3000 * | WAIT_CERT
3001 * | Recv | | Recv Certificate
3002 * | empty | v
3003 * | Certificate | WAIT_CV
3004 * | | | Recv
3005 * | v | CertificateVerify
3006 * +-> WAIT_FINISHED <---+
3007 * | Recv Finished
3008 *
3009 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3010 * the above diagram.
3011 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003013static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003014{
3015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003016
3017 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3018
3019 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3020
Jerry Yu3be85072023-12-04 09:58:54 +08003021 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003022 unsigned char *buf;
3023 size_t buf_len;
3024
3025 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3026 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3027 &buf, &buf_len));
3028
3029 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3030 ssl, buf, buf + buf_len));
3031
Jerry Yue9655122023-12-01 16:44:40 +08003032 MBEDTLS_SSL_DEBUG_MSG(
3033 1, ("Switch to handshake keys for inbound traffic"
3034 "( K_recv = handshake )"));
3035 mbedtls_ssl_set_inbound_transform(
3036 ssl, ssl->handshake->transform_handshake);
3037
Jerry Yud5c34962023-12-01 16:32:31 +08003038 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3039 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3040 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003041
Jerry Yu3be85072023-12-04 09:58:54 +08003042 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003043
Jerry Yub55f9eb2023-12-05 10:27:17 +08003044 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003045 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3046 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003047 } else {
3048 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3049 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3050 goto cleanup;
3051 }
3052
Jerry Yud5c34962023-12-01 16:32:31 +08003053cleanup:
3054 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003055 return ret;
3056}
3057#endif /* MBEDTLS_SSL_EARLY_DATA */
3058
Jerry Yu69dd8d42022-04-16 12:51:26 +08003059/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003060 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003061 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003063static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003064{
Jerry Yud6e253d2022-05-18 13:59:24 +08003065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3068 if (ret != 0) {
3069 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003070 }
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3073 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003074 MBEDTLS_SSL_DEBUG_RET(
3075 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 }
3077
3078 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3079 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003080}
3081
3082/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003083 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003084 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003085MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003086static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003087{
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003091
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003092#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3093 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003094/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3095 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3096 * expected to be resolved with issue#6395.
3097 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003098 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003099 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003100 mbedtls_ssl_handshake_set_state(
3101 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003102 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003103#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003104 {
3105 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3106 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003108}
3109
Norbert Fabritius96eed722023-01-23 15:24:59 +01003110#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003111/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003112 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003113 */
3114#define SSL_NEW_SESSION_TICKET_SKIP 0
3115#define SSL_NEW_SESSION_TICKET_WRITE 1
3116MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003117static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003118{
3119 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 if (ssl->conf->f_ticket_write == NULL) {
3121 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3122 " callback is not set"));
3123 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (ssl->conf->new_session_tickets_count == 0) {
3126 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3127 " configured count is zero"));
3128 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003129 }
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ssl->handshake->new_session_tickets_count == 0) {
3132 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3133 "been sent."));
3134 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003135 }
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003138}
3139
Jerry Yue67bef42022-07-07 07:29:42 +00003140MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003141static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3142 unsigned char *ticket_nonce,
3143 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003144{
3145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3146 mbedtls_ssl_session *session = ssl->session;
3147 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3148 psa_algorithm_t psa_hash_alg;
3149 int hash_length;
3150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003152
Pengyu Lv9f926952022-11-17 15:22:33 +08003153 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003154 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003155 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003156#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003157 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003158 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003159#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003160
3161#if defined(MBEDTLS_SSL_EARLY_DATA)
3162 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3163 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003164 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003165 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003166 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003167 }
3168#endif /* MBEDTLS_SSL_EARLY_DATA */
3169
Pengyu Lv4938a562023-01-16 11:28:49 +08003170 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003171
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003172#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003173 if (session->ticket_alpn == NULL) {
3174 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3175 if (ret != 0) {
3176 return ret;
3177 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003178 }
3179#endif
3180
Jerry Yue67bef42022-07-07 07:29:42 +00003181 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3183 (unsigned char *) &session->ticket_age_add,
3184 sizeof(session->ticket_age_add)) != 0)) {
3185 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3186 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3189 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003190
3191 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3193 if (ret != 0) {
3194 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3195 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3198 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003199
3200 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003202 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3204 if (hash_length == -1 ||
3205 (size_t) hash_length > sizeof(session->resumption_key)) {
3206 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003207 }
3208
Jerry Yue67bef42022-07-07 07:29:42 +00003209 /* In this code the psk key length equals the length of the hash */
3210 session->resumption_key_len = hash_length;
3211 session->ciphersuite = ciphersuite_info->id;
3212
3213 /* Compute resumption key
3214 *
3215 * HKDF-Expand-Label( resumption_master_secret,
3216 * "resumption", ticket_nonce, Hash.length )
3217 */
3218 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 psa_hash_alg,
3220 session->app_secrets.resumption_master_secret,
3221 hash_length,
3222 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3223 ticket_nonce,
3224 ticket_nonce_size,
3225 session->resumption_key,
3226 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (ret != 0) {
3229 MBEDTLS_SSL_DEBUG_RET(2,
3230 "Creating the ticket-resumed PSK failed",
3231 ret);
3232 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3235 session->resumption_key,
3236 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3239 session->app_secrets.resumption_master_secret,
3240 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003243}
3244
3245/* This function creates a NewSessionTicket message in the following format:
3246 *
3247 * struct {
3248 * uint32 ticket_lifetime;
3249 * uint32 ticket_age_add;
3250 * opaque ticket_nonce<0..255>;
3251 * opaque ticket<1..2^16-1>;
3252 * Extension extensions<0..2^16-2>;
3253 * } NewSessionTicket;
3254 *
3255 * The ticket inside the NewSessionTicket message is an encrypted container
3256 * carrying the necessary information so that the server is later able to
3257 * re-start the communication.
3258 *
3259 * The following fields are placed inside the ticket by the
3260 * f_ticket_write() function:
3261 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003262 * - creation time (ticket_creation_time)
3263 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003264 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003265 * - key (resumption_key)
3266 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003267 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003268 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003269 */
3270MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003271static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3272 unsigned char *buf,
3273 unsigned char *end,
3274 size_t *out_len,
3275 unsigned char *ticket_nonce,
3276 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003277{
3278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3279 unsigned char *p = buf;
3280 mbedtls_ssl_session *session = ssl->session;
3281 size_t ticket_len;
3282 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003283 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003284
3285 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003287
3288 /*
3289 * ticket_lifetime 4 bytes
3290 * ticket_age_add 4 bytes
3291 * ticket_nonce 1 + ticket_nonce_size bytes
3292 * ticket >=2 bytes
3293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003295
3296 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003297#if defined(MBEDTLS_HAVE_TIME)
3298 session->ticket_creation_time = mbedtls_ms_time();
3299#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3301 session,
3302 p + 9 + ticket_nonce_size + 2,
3303 end,
3304 &ticket_len,
3305 &ticket_lifetime);
3306 if (ret != 0) {
3307 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3308 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003309 }
Jerry Yuce794882023-11-22 15:01:18 +08003310
3311 /* RFC 8446 section 4.6.1
3312 *
Jerry Yue67bef42022-07-07 07:29:42 +00003313 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003314 * unsigned integer in network byte order from the time of ticket
3315 * issuance. Servers MUST NOT use any value greater than
3316 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003317 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003318 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003319 MBEDTLS_SSL_DEBUG_MSG(
3320 1, ("Ticket lifetime (%u) is greater than 7 days.",
3321 (unsigned int) ticket_lifetime));
3322 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 }
Jerry Yuce794882023-11-22 15:01:18 +08003324
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3326 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3327 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003328
3329 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3331 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3332 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003333
3334 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 p[8] = (unsigned char) ticket_nonce_size;
3336 if (ticket_nonce_size > 0) {
3337 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003338 }
3339 p += 9 + ticket_nonce_size;
3340
3341 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003343 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003345 p += ticket_len;
3346
3347 /* Ticket Extensions
3348 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003349 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003350 */
Jerry Yuedab6372022-10-31 14:37:31 +08003351 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003354 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003355 p += 2;
3356
Jerry Yu01da35e2022-12-12 15:09:22 +08003357#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003358 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003359 size_t output_len;
3360
Jerry Yuf135bac2023-11-23 18:10:51 +08003361 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003362 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003363 MBEDTLS_SSL_DEBUG_RET(
3364 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3365 return ret;
3366 }
3367 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003368 } else {
3369 MBEDTLS_SSL_DEBUG_MSG(
3370 4, ("early_data not allowed, "
3371 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003372 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003373
Jerry Yu01da35e2022-12-12 15:09:22 +08003374#endif /* MBEDTLS_SSL_EARLY_DATA */
3375
3376 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3377
Jerry Yue67bef42022-07-07 07:29:42 +00003378 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3380 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003381
Jerry Yu7de2ff02022-11-08 21:43:46 +08003382 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003386}
3387
3388/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003389 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003391static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003392{
3393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003398 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3399 unsigned char *buf;
3400 size_t buf_len, msg_len;
3401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3403 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003404
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003405 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3406 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3407 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3410 ssl, buf, buf + buf_len, &msg_len,
3411 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3414 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003415
Jerry Yu359e65f2022-09-22 23:47:43 +08003416 /* Limit session tickets count to one when resumption connection.
3417 *
3418 * See document of mbedtls_ssl_conf_new_session_tickets.
3419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003421 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003423 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003425
Jerry Yua8d3c502022-10-30 14:51:23 +08003426 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3428 } else {
3429 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003430 }
3431
Jerry Yue67bef42022-07-07 07:29:42 +00003432cleanup:
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003435}
3436#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3437
3438/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003439 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003441int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003442{
XiaokangQian08037552022-04-20 07:16:41 +00003443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3446 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3447 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003450 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003454 /* start state */
3455 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003457 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003458 break;
3459
XiaokangQian7807f9f2022-02-15 10:04:37 +00003460 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_process_client_hello(ssl);
3462 if (ret != 0) {
3463 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3464 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003465 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003466
Jerry Yuf41553b2022-05-09 22:20:30 +08003467 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ret = ssl_tls13_write_hello_retry_request(ssl);
3469 if (ret != 0) {
3470 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3471 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003472 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003473 break;
3474
Jerry Yu5b64ae92022-03-30 17:15:02 +08003475 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003477 break;
3478
Xiaofei Baicba64af2022-02-15 10:00:56 +00003479 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ret = ssl_tls13_write_encrypted_extensions(ssl);
3481 if (ret != 0) {
3482 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3483 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003484 }
Jerry Yu48330562022-05-06 21:35:44 +08003485 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003486
Ronald Cron928cbd32022-10-04 16:14:26 +02003487#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003488 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003490 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003491
Jerry Yu83da34e2022-04-16 13:59:52 +08003492 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003494 break;
3495
3496 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003498 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003499#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 /*
3502 * Injection of dummy-CCS's for middlebox compatibility
3503 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003504#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003505 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3507 if (ret == 0) {
3508 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3509 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003510 break;
3511
3512 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003513 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3514 if (ret != 0) {
3515 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003517 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003518 break;
3519#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3520
Jerry Yu69dd8d42022-04-16 12:51:26 +08003521 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003523 break;
3524
Jerry Yu87b5ed42022-12-12 13:07:07 +08003525#if defined(MBEDTLS_SSL_EARLY_DATA)
3526 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003527 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003528 break;
3529#endif /* MBEDTLS_SSL_EARLY_DATA */
3530
Jerry Yu69dd8d42022-04-16 12:51:26 +08003531 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003533 break;
3534
Jerry Yu69dd8d42022-04-16 12:51:26 +08003535 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003537 break;
3538
Ronald Cron766c0cd2022-10-18 12:17:11 +02003539#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003540 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3542 if (ret == 0) {
3543 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003544 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3546 } else {
3547 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003548 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003550 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003551 }
3552 break;
3553
3554 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3556 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003557 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003559 }
3560 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003561#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003562
Jerry Yue67bef42022-07-07 07:29:42 +00003563#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003564 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 ret = ssl_tls13_write_new_session_ticket(ssl);
3566 if (ret != 0) {
3567 MBEDTLS_SSL_DEBUG_RET(1,
3568 "ssl_tls13_write_new_session_ticket ",
3569 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003570 }
3571 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003572 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003573 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003574 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003575 * as part of ssl_prepare_handshake_step.
3576 */
3577 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 if (ssl->handshake->new_session_tickets_count == 0) {
3580 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3581 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003582 mbedtls_ssl_handshake_set_state(
3583 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 }
Jerry Yue67bef42022-07-07 07:29:42 +00003585 break;
3586
3587#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3588
XiaokangQian7807f9f2022-02-15 10:04:37 +00003589 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3591 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003592 }
3593
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003595}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003596
Jerry Yufb4b6472022-01-27 15:03:26 +08003597#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */