blob: 8a76fbd32e580efb692658874d4b514dad6277e6 [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
Jerry Yu1c105562022-07-10 06:32:38 +0000417 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800418 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200419 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 transcript, sizeof(transcript), &transcript_len);
421 if (ret != 0) {
422 return ret;
423 }
Jerry Yu1c105562022-07-10 06:32:38 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
426 if (ret != 0) {
427 return ret;
428 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
431 psk, psk_len, psk_type,
432 transcript,
433 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800434#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (ret != 0) {
438 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
439 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
443 server_computed_binder, transcript_len);
444 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100447 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_platform_zeroize(server_computed_binder,
451 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100452 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800453}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800454
Jerry Yu82534862022-08-30 10:42:33 +0800455#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
458 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800459{
Jerry Yu82534862022-08-30 10:42:33 +0800460 dst->ticket_age_add = src->ticket_age_add;
461 dst->ticket_flags = src->ticket_flags;
462 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (src->resumption_key_len == 0) {
464 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
465 }
466 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800467
Jerry Yu33bf2402022-12-12 16:01:43 +0800468#if defined(MBEDTLS_SSL_EARLY_DATA)
469 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000470
471#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000472 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000473 if (ret != 0) {
474 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000475 }
476#endif /* MBEDTLS_SSL_ALPN */
477#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800480}
481#endif /* MBEDTLS_SSL_SESSION_TICKETS */
482
Ronald Cron79cdd412024-02-20 17:19:28 +0100483struct psk_attributes {
484 int type;
485 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100486 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100487};
Ronald Cron16cc3702024-03-07 15:04:56 +0100488#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100489
Jerry Yu1c105562022-07-10 06:32:38 +0000490/* Parser for pre_shared_key extension in client hello
491 * struct {
492 * opaque identity<1..2^16-1>;
493 * uint32 obfuscated_ticket_age;
494 * } PskIdentity;
495 *
496 * opaque PskBinderEntry<32..255>;
497 *
498 * struct {
499 * PskIdentity identities<7..2^16-1>;
500 * PskBinderEntry binders<33..2^16-1>;
501 * } OfferedPsks;
502 *
503 * struct {
504 * select (Handshake.msg_type) {
505 * case client_hello: OfferedPsks;
506 * ....
507 * };
508 * } PreSharedKeyExtension;
509 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800510MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000511static int ssl_tls13_parse_pre_shared_key_ext(
512 mbedtls_ssl_context *ssl,
513 const unsigned char *pre_shared_key_ext,
514 const unsigned char *pre_shared_key_ext_end,
515 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100516 const unsigned char *ciphersuites_end,
517 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000518{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800520 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800521 const unsigned char *p_identity_len;
522 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000523 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800524 const unsigned char *binders;
525 const unsigned char *p_binder_len;
526 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000527 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800528 int matched_identity = -1;
529 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
532 pre_shared_key_ext,
533 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Jerry Yu96a2e362022-07-21 15:11:34 +0800535 /* identities_len 2 bytes
536 * identities_data >= 7 bytes
537 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
539 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800540 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
542 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800543 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000544
Jerry Yu96a2e362022-07-21 15:11:34 +0800545 /* binders_len 2 bytes
546 * binders >= 33 bytes
547 */
Jerry Yu568ec252022-07-22 21:27:34 +0800548 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
550 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800551 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800553 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000554
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100555 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
556 identities_end - pre_shared_key_ext);
557 if (0 != ret) {
558 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
559 return ret;
560 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000563 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800564 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800565 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800566 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800567 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100568 int psk_ciphersuite_id;
569 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100570 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100571
Jerry Yu82534862022-08-30 10:42:33 +0800572 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
576 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800577 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
579 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800580 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800583 binder_len = *p_binder_len;
584 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800586 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800587
Jerry Yu96a2e362022-07-21 15:11:34 +0800588 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000590 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 }
Jerry Yu1c105562022-07-10 06:32:38 +0000592
Jerry Yu96a2e362022-07-21 15:11:34 +0800593 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100595 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100596 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800597 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100601
Ronald Cron79cdd412024-02-20 17:19:28 +0100602 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800603 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100604 psk_ciphersuite_id = 0;
605 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100606 allowed_key_exchange_modes =
607 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800608 break;
Jerry Yu82534862022-08-30 10:42:33 +0800609#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100610 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100611 psk_ciphersuite_id = session.ciphersuite;
612 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100613 ssl->session_negotiate->ticket_flags = session.ticket_flags;
614 allowed_key_exchange_modes =
615 session.ticket_flags &
616 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800617 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100618#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800619 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800621 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100622
Ronald Cron79cdd412024-02-20 17:19:28 +0100623 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
624
Ronald Croncf284562024-02-16 18:54:10 +0100625 if ((allowed_key_exchange_modes &
626 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
627 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100628 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100629 } else if ((allowed_key_exchange_modes &
630 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
631 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100632 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100633 }
634
Ronald Cron79cdd412024-02-20 17:19:28 +0100635 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100636 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
637 continue;
638 }
639
Ronald Cron89089cc2024-02-14 18:18:08 +0100640 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
641 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100642 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100643
Ronald Cron74a16292024-02-23 17:07:41 +0100644 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100645#if defined(MBEDTLS_SSL_SESSION_TICKETS)
646 mbedtls_ssl_session_free(&session);
647#endif
648 /*
649 * We consider finding a ciphersuite suitable for the PSK as part
650 * of the validation of its binder. Thus if we do not find one, we
651 * abort the handshake with a decrypt_error alert.
652 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800653 MBEDTLS_SSL_PEND_FATAL_ALERT(
654 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100656 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800657 }
658
Jerry Yu96a2e362022-07-21 15:11:34 +0800659 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100660 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100661 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100662 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800663 /* For security reasons, the handshake should be aborted when we
664 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
665 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800666#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800668#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000670 MBEDTLS_SSL_DEBUG_RET(
671 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800672 MBEDTLS_SSL_PEND_FATAL_ALERT(
673 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
675 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800676 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800677
678 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800679
Jerry Yu82534862022-08-30 10:42:33 +0800680#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100681 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
683 &session);
684 mbedtls_ssl_session_free(&session);
685 if (ret != 0) {
686 return ret;
687 }
Jerry Yu82534862022-08-30 10:42:33 +0800688 }
689#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000690 }
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (p_identity_len != identities_end || p_binder_len != binders_end) {
693 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
694 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
695 MBEDTLS_ERR_SSL_DECODE_ERROR);
696 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000697 }
698
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800699 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000700 ret = ssl->handshake->update_checksum(
701 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100702 if (0 != ret) {
703 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
704 return ret;
705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 ssl->handshake->selected_identity = (uint16_t) matched_identity;
712 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000715}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
717/*
718 * struct {
719 * select ( Handshake.msg_type ) {
720 * ....
721 * case server_hello:
722 * uint16 selected_identity;
723 * }
724 * } PreSharedKeyExtension;
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
727 unsigned char *buf,
728 unsigned char *end,
729 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000730{
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732
733 *olen = 0;
734
David Horstmann21b89762022-10-06 18:34:28 +0100735 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000742 /* We shouldn't have called this extension writer unless we've
743 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745 }
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
748 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
751 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000754
755 *olen = 6;
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
758 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000763}
764
Ronald Cron41a443a2022-10-04 16:38:25 +0200765#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000766
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767/* From RFC 8446:
768 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000769 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770 * } SupportedVersions;
771 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200772MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100773static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
774 const unsigned char *buf,
775 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000778 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000779 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000780 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000784 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 p += 1;
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000788 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 while (p < versions_end) {
790 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
791 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000792 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793
Ronald Cron3bd2b022023-04-03 16:45:39 +0200794 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100795 found_supported_version = 1;
796 break;
797 }
798
Ronald Cron3bd2b022023-04-03 16:45:39 +0200799 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
800 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100801 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 break;
803 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 }
805
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100806 if (!found_supported_version) {
807 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
810 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
811 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812 }
813
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100814 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100817 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818}
819
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200820#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000821/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822 *
823 * From RFC 8446:
824 * enum {
825 * ... (0xFFFF)
826 * } NamedGroup;
827 * struct {
828 * NamedGroup named_group_list<2..2^16-1>;
829 * } NamedGroupList;
830 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
833 const unsigned char *buf,
834 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000837 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000838 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
841 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
842 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000845 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000846 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000849 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
851 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000852 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_SSL_DEBUG_MSG(2,
855 ("got named group: %s(%04x)",
856 mbedtls_ssl_named_group_to_str(named_group),
857 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
860 !mbedtls_ssl_named_group_is_supported(named_group) ||
861 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000862 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863 }
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 MBEDTLS_SSL_DEBUG_MSG(2,
866 ("add named group %s(%04x) into received list.",
867 mbedtls_ssl_named_group_to_str(named_group),
868 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800869
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000870 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874
875}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200876#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
XiaokangQian08037552022-04-20 07:16:41 +0000878#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
879
Valerio Settic9ae8622023-07-25 11:23:50 +0200880#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881/*
882 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000883 * extension is correct and stores the first acceptable key share and its
884 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885 *
886 * Possible return values are:
887 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000888 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
889 * the client does not match a group supported by the server. A
890 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000891 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800892 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200893MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100894static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
895 const unsigned char *buf,
896 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897{
XiaokangQianb67384d2022-04-19 00:02:38 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000900 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800901 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /* From RFC 8446:
904 *
905 * struct {
906 * KeyShareEntry client_shares<0..2^16-1>;
907 * } KeyShareClientHello;
908 *
909 */
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
912 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915
916 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000917 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000918
919 /* We try to find a suitable key share entry and copy it to the
920 * handshake context. Later, we have to find out whether we can do
921 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000922 * dismiss it and send a HelloRetryRequest message.
923 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000926 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800927 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800928 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929
930 /*
931 * struct {
932 * NamedGroup group;
933 * opaque key_exchange<1..2^16-1>;
934 * } KeyShareEntry;
935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
937 group = MBEDTLS_GET_UINT16_BE(p, 0);
938 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800939 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800940 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800942 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943
944 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000945 * for input validation purposes.
946 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
948 !mbedtls_ssl_named_group_is_supported(group) ||
949 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000950 continue;
951 }
XiaokangQian060d8672022-04-21 09:24:56 +0000952
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200954 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200956 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200957 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200958 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_ssl_named_group_to_str(group),
960 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200961 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ssl, key_exchange - 2, key_exchange_len + 2);
963 if (ret != 0) {
964 return ret;
965 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 } else {
968 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
969 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970 continue;
971 }
972
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000973 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000974 }
975
Jerry Yuc1be19f2022-04-23 16:11:39 +0800976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (ssl->handshake->offered_group_id == 0) {
978 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
979 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982}
Valerio Settic9ae8622023-07-25 11:23:50 +0200983#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000984
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
987 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988{
Jerry Yu0c354a22022-08-29 15:25:36 +0800989 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000991}
992
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000995static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return ssl_tls13_client_hello_has_exts(
999 ssl,
1000 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1001 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1002 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001003}
Jerry Yue5991322022-11-07 14:03:44 +08001004#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001005
Jerry Yue5991322022-11-07 14:03:44 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001007MBEDTLS_CHECK_RETURN_CRITICAL
1008static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001010{
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return ssl_tls13_client_hello_has_exts(
1012 ssl,
1013 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1014 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001015}
Jerry Yue5991322022-11-07 14:03:44 +08001016#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001017
Jerry Yue5991322022-11-07 14:03:44 +08001018#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001019MBEDTLS_CHECK_RETURN_CRITICAL
1020static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001022{
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return ssl_tls13_client_hello_has_exts(
1024 ssl,
1025 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1026 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1027 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1028 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001029}
Jerry Yue5991322022-11-07 14:03:44 +08001030#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001031
Pengyu Lv306a01d2023-02-02 16:35:47 +08001032#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1033MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001034static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001035{
Jerry Yue5991322022-11-07 14:03:44 +08001036#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001037 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001038 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001040#else
1041 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001044}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045
Jerry Yu77f01482022-07-11 07:03:24 +00001046MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001047static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001048{
Jerry Yue5991322022-11-07 14:03:44 +08001049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001050 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001051 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001053#else
1054 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001056#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001057}
1058#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1059
1060MBEDTLS_CHECK_RETURN_CRITICAL
1061static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1062{
1063#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1064 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1065 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1066#else
1067 ((void) ssl);
1068 return 0;
1069#endif
1070}
1071
XiaokangQian81802f42022-06-10 13:25:22 +00001072#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001073 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001074
1075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001076static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001077{
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001079 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001081 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001083 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 }
1100}
1101#endif /* MBEDTLS_USE_PSA_CRYPTO */
1102
XiaokangQian23c5be62022-06-07 02:04:34 +00001103/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001104 * Pick best ( private key, certificate chain ) pair based on the signature
1105 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001106 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001107MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001108static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001109{
XiaokangQianfb665a82022-06-15 03:57:21 +00001110 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001111 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001112
1113#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001115 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001117#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (key_cert_list == NULL) {
1121 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1122 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001123 }
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1126 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 for (key_cert = key_cert_list; key_cert != NULL;
1135 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001136#if defined(MBEDTLS_USE_PSA_CRYPTO)
1137 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1138#endif /* MBEDTLS_USE_PSA_CRYPTO */
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1141 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001142
1143 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 * This avoids sending the client a cert it'll reject based on
1145 * keyUsage or other extensions.
1146 */
1147 if (mbedtls_x509_crt_check_key_usage(
1148 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001149 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001150 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1152 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1153 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001154 continue;
1155 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_SSL_DEBUG_MSG(3,
1158 ("ssl_tls13_pick_key_cert:"
1159 "check signature algorithm %s [%04x]",
1160 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1161 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001162#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001164#endif /* MBEDTLS_USE_PSA_CRYPTO */
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1167 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
1169 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1171 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001172#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001174 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_SSL_DEBUG_MSG(3,
1176 ("ssl_tls13_pick_key_cert:"
1177 "selected signature algorithm"
1178 " %s [%04x]",
1179 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1180 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001181 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 3, "selected certificate (chain)",
1183 ssl->handshake->key_cert->cert);
1184 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001185 }
XiaokangQian81802f42022-06-10 13:25:22 +00001186 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001187 }
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1190 "no suitable certificate found"));
1191 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001192}
XiaokangQian81802f42022-06-10 13:25:22 +00001193#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001194 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001195
XiaokangQian4080a7f2022-04-11 09:55:18 +00001196/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001197 *
1198 * STATE HANDLING: ClientHello
1199 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001200 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001201 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001202 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
1204 * In this case, the server progresses to sending its ServerHello.
1205 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001206 * 2) The ClientHello was well-formed but didn't match the server's
1207 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001208 *
1209 * For example, the client might not have offered a key share which
1210 * the server supports, or the server might require a cookie.
1211 *
1212 * In this case, the server sends a HelloRetryRequest.
1213 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001214 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001215 *
1216 * In this case, we abort the handshake.
1217 *
1218 */
1219
1220/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001221 * Structure of this message:
1222 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001223 * uint16 ProtocolVersion;
1224 * opaque Random[32];
1225 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001226 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001227 * struct {
1228 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1229 * Random random;
1230 * opaque legacy_session_id<0..32>;
1231 * CipherSuite cipher_suites<2..2^16-2>;
1232 * opaque legacy_compression_methods<1..2^8-1>;
1233 * Extension extensions<8..2^16-1>;
1234 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001235 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001236
1237#define SSL_CLIENT_HELLO_OK 0
1238#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001239#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001240
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001241MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001242static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1243 const unsigned char *buf,
1244 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001245{
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1247 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001248 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001249 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001250 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001251 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001252 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001253 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001254 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001255 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001256 const unsigned char *supported_versions_data;
1257 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001258 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001259 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001260 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261
Ronald Cron41a443a2022-10-04 16:38:25 +02001262#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001263 int got_psk = 0;
1264 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001265 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001266 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001267#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268
XiaokangQian7807f9f2022-02-15 10:04:37 +00001269 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001270 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001271 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001272 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001273 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 * .. . .. ciphersuite list length ( 2 bytes )
1276 * .. . .. ciphersuite list
1277 * .. . .. compression alg. list length ( 1 byte )
1278 * .. . .. compression alg. list
1279 * .. . .. extensions length ( 2 bytes, optional )
1280 * .. . .. extensions ( optional )
1281 */
1282
XiaokangQianb67384d2022-04-19 00:02:38 +00001283 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001284 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001285 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1286 * read at least up to session id length without worrying.
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001289
1290 /* ...
1291 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1292 * ...
1293 * with ProtocolVersion defined as:
1294 * uint16 ProtocolVersion;
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1297 MBEDTLS_SSL_VERSION_TLS1_2) {
1298 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1299 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1300 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1301 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 }
1303 p += 2;
1304
Jerry Yuc1be19f2022-04-23 16:11:39 +08001305 /* ...
1306 * Random random;
1307 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001308 * with Random defined as:
1309 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 */
Ronald Crond540d992023-03-07 09:41:48 +01001311 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001312 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313
Jerry Yuc1be19f2022-04-23 16:11:39 +08001314 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001316 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 */
Ronald Croncada4102023-03-07 09:51:39 +01001318 legacy_session_id_len = *(p++);
1319 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320
XiaokangQianb67384d2022-04-19 00:02:38 +00001321 /*
1322 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001323 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001326 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001328 /* ...
1329 * CipherSuite cipher_suites<2..2^16-2>;
1330 * ...
1331 * with CipherSuite defined as:
1332 * uint8 CipherSuite[2];
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001335 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001336 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001337
Ronald Cronfc7ae872023-02-16 15:32:19 +01001338 /*
1339 * The length of the ciphersuite list has to be even.
1340 */
1341 if (cipher_suites_len & 1) {
1342 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1343 MBEDTLS_ERR_SSL_DECODE_ERROR);
1344 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1345 }
1346
XiaokangQianb67384d2022-04-19 00:02:38 +00001347 /* Check we have enough data for the ciphersuite list, the legacy
1348 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001349 *
1350 * cipher_suites cipher_suites_len bytes
1351 * legacy_compression_methods 2 bytes
1352 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001355 p += cipher_suites_len;
1356 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001357
1358 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001359 * Search for the supported versions extension and parse it to determine
1360 * if the client supports TLS 1.3.
1361 */
1362 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1363 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001364 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001365 if (ret < 0) {
1366 MBEDTLS_SSL_DEBUG_RET(1,
1367 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1368 return ret;
1369 }
1370
1371 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001372 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001373 }
1374
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001375 if (ret == 1) {
1376 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001377 supported_versions_data,
1378 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001379 if (ret < 0) {
1380 MBEDTLS_SSL_DEBUG_RET(1,
1381 ("ssl_tls13_parse_supported_versions_ext"), ret);
1382 return ret;
1383 }
1384
Ronald Cronb828c7d2023-04-03 16:37:22 +02001385 /*
1386 * The supported versions extension was parsed successfully as the
1387 * value returned by ssl_tls13_parse_supported_versions_ext() is
1388 * positive. The return value is then equal to
1389 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1390 * the TLS version to negotiate.
1391 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001392 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1393 return SSL_CLIENT_HELLO_TLS1_2;
1394 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001395 }
1396
1397 /*
1398 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001399 */
1400 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001401 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1402 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001403
1404 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001405 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001406 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001407 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001408 */
1409 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1410 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1411 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1412
Ronald Croncada4102023-03-07 09:51:39 +01001413 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1414 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1415 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1416 }
1417 ssl->session_negotiate->id_len = legacy_session_id_len;
1418 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1419 legacy_session_id, legacy_session_id_len);
1420 memcpy(&ssl->session_negotiate->id[0],
1421 legacy_session_id, legacy_session_id_len);
1422
Ronald Crond540d992023-03-07 09:41:48 +01001423 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001424 * Search for a matching ciphersuite
1425 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001426 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1427 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001428
Ronald Cron89089cc2024-02-14 18:18:08 +01001429 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1430 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (handshake->ciphersuite_info == NULL) {
1433 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1434 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1435 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001436 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001437 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1438
1439 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1440 ((unsigned) handshake->ciphersuite_info->id),
1441 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001442
XiaokangQian4080a7f2022-04-11 09:55:18 +00001443 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001444 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001445 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1448 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1449 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1450 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1451 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001452 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001453 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454
Jerry Yuc1be19f2022-04-23 16:11:39 +08001455 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001456 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001457 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001458 * with Extension defined as:
1459 * struct {
1460 * ExtensionType extension_type;
1461 * opaque extension_data<0..2^16-1>;
1462 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001467 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001470 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473 unsigned int extension_type;
1474 size_t extension_data_len;
1475 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001476 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1477
Ronald Cron5fbd2702024-02-14 10:03:36 +01001478 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001479 /* Do not accept early data extension in 2nd ClientHello */
1480 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1481 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001482
Jerry Yu0c354a22022-08-29 15:25:36 +08001483 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001484 *
1485 * The "pre_shared_key" extension MUST be the last extension in the
1486 * ClientHello (this facilitates implementation as described below).
1487 * Servers MUST check that it is the last extension and otherwise fail
1488 * the handshake with an "illegal_parameter" alert.
1489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001491 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001493 MBEDTLS_SSL_PEND_FATAL_ALERT(
1494 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1496 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001497 }
1498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1500 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1501 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001502 p += 4;
1503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001505 extension_data_end = p + extension_data_len;
1506
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001507 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001509 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (ret != 0) {
1511 return ret;
1512 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001515#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1516 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1518 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1519 extension_data_end);
1520 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001521 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 1, "mbedtls_ssl_parse_servername_ext", ret);
1523 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001524 }
XiaokangQian40a35232022-05-07 09:02:40 +00001525 break;
1526#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1527
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001528#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531
1532 /* Supported Groups Extension
1533 *
1534 * When sent by the client, the "supported_groups" extension
1535 * indicates the named groups which the client supports,
1536 * ordered from most preferred to least preferred.
1537 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001538 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 ssl, p, extension_data_end);
1540 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001541 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001542 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001544 }
1545
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001547#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548
Valerio Settic9ae8622023-07-25 11:23:50 +02001549#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001550 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001552
1553 /*
1554 * Key Share Extension
1555 *
1556 * When sent by the client, the "key_share" extension
1557 * contains the endpoint's cryptographic parameters for
1558 * ECDHE/DHE key establishment methods.
1559 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001560 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 ssl, p, extension_data_end);
1562 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001563 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1564 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001565 }
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001568 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 1, "ssl_tls13_parse_key_shares_ext", ret);
1570 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001571 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001572
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001574#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575
1576 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001577 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001578 break;
1579
Ronald Cron41a443a2022-10-04 16:38:25 +02001580#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001581 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001582 MBEDTLS_SSL_DEBUG_MSG(
1583 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001584
1585 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 ssl, p, extension_data_end);
1587 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001588 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1590 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001591 }
1592
Jerry Yue19e3b92022-07-08 12:04:51 +00001593 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001594#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001595
Jerry Yu1c105562022-07-10 06:32:38 +00001596 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1598 if ((handshake->received_extensions &
1599 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001600 MBEDTLS_SSL_PEND_FATAL_ALERT(
1601 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1603 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001604 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001605#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001606 /* Delay processing of the PSK identity once we have
1607 * found out which algorithms to use. We keep a pointer
1608 * to the buffer and the size for later processing.
1609 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001610 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001611 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001612#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001613 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001614
XiaokangQianacb39922022-06-17 10:18:48 +00001615#if defined(MBEDTLS_SSL_ALPN)
1616 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1620 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001621 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1623 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001624 }
XiaokangQianacb39922022-06-17 10:18:48 +00001625 break;
1626#endif /* MBEDTLS_SSL_ALPN */
1627
Ronald Cron928cbd32022-10-04 16:14:26 +02001628#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
Gabor Mezei078e8032022-04-27 21:17:56 +02001632 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 ssl, p, extension_data_end);
1634 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001635 MBEDTLS_SSL_DEBUG_RET(
1636 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001638 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001640#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641
Jan Bruckner151f6422023-02-10 12:45:19 +01001642#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1643 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1644 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1645
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001646 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1647 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001648 if (ret != 0) {
1649 MBEDTLS_SSL_DEBUG_RET(
1650 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1651 return ret;
1652 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001653 break;
1654#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1655
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001657 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001658 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001660 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001661 }
1662
1663 p += extension_data_len;
1664 }
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1667 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001668
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001669 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1670 MBEDTLS_SSL_HS_CLIENT_HELLO,
1671 p - buf);
1672 if (0 != ret) {
1673 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1674 return ret;
1675 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001676
Ronald Cron41a443a2022-10-04 16:38:25 +02001677#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001678 /* Update checksum with either
1679 * - The entire content of the CH message, if no PSK extension is present
1680 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001681 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001682 * ClientHello even if some pre-requisites for PSK key exchange modes are
1683 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001684 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001685 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001686 ret = handshake->update_checksum(ssl, buf,
1687 pre_shared_key_ext - buf);
1688 if (0 != ret) {
1689 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1690 return ret;
1691 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1693 pre_shared_key_ext,
1694 pre_shared_key_ext_end,
1695 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001696 cipher_suites_end,
1697 &psk);
1698 if (ret == 0) {
1699 got_psk = 1;
1700 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001701 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1703 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001704 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001706#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001707 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001708 ret = handshake->update_checksum(ssl, buf, p - buf);
1709 if (0 != ret) {
1710 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1711 return ret;
1712 }
Jerry Yu1c105562022-07-10 06:32:38 +00001713 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001714
Ronald Cron79cdd412024-02-20 17:19:28 +01001715 /*
1716 * Determine the key exchange algorithm to use.
1717 * There are three types of key exchanges supported in TLS 1.3:
1718 * - (EC)DH with ECDSA,
1719 * - (EC)DH with PSK,
1720 * - plain PSK.
1721 *
1722 * The PSK-based key exchanges may additionally be used with 0-RTT.
1723 *
1724 * Our built-in order of preference is
1725 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1726 * 2 ) Certificate Mode ( ephemeral )
1727 * 3 ) Plain PSK Mode ( psk )
1728 */
1729#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1730 if (got_psk && (psk.key_exchange_mode ==
1731 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1732 handshake->key_exchange_mode =
1733 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1734 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1735
1736 } else
1737#endif
1738 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1739 handshake->key_exchange_mode =
1740 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1741 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1742
1743 }
1744#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1745 else if (got_psk && (psk.key_exchange_mode ==
1746 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1747 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1748 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1749 }
1750#endif
1751 else {
1752 MBEDTLS_SSL_DEBUG_MSG(
1753 1,
1754 ("ClientHello message misses mandatory extensions."));
1755 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1756 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1757 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001759
Ronald Cron3e47eec2024-02-21 10:29:24 +01001760#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001761 if (handshake->key_exchange_mode &
1762 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1763 handshake->ciphersuite_info = psk.ciphersuite_info;
1764 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1765
1766 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1767 ((unsigned) psk.ciphersuite_info->id),
1768 psk.ciphersuite_info->name));
1769
1770 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1771 handshake->resume = 1;
1772 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001773 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001774#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001775
1776 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001777 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1778 hrr_required = (no_usable_share_for_key_agreement != 0);
1779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001784}
1785
Jerry Yuab0da372023-02-08 13:55:24 +08001786#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001787static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001788{
1789 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1790
Jerry Yu985c9672022-12-04 14:06:30 +08001791 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1792 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001793 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001794 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001796 }
1797
Jerry Yu454dda32023-10-31 15:13:54 +08001798 if (!handshake->resume) {
1799 /* We currently support early data only in the case of PSKs established
1800 via a NewSessionTicket message thus in the case of a session
1801 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001802 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001803 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001804 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001805 }
1806
Jerry Yu82fd6c12023-10-31 16:32:19 +08001807 /* RFC 8446 4.2.10
1808 *
1809 * In order to accept early data, the server MUST have accepted a PSK cipher
1810 * suite and selected the first key offered in the client's "pre_shared_key"
1811 * extension. In addition, it MUST verify that the following values are the
1812 * same as those associated with the selected PSK:
1813 * - The TLS version number
1814 * - The selected cipher suite
1815 * - The selected ALPN [RFC7301] protocol, if any
1816 *
1817 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001818 * - The TLS version number is checked in
1819 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001820 */
1821
1822 if (handshake->selected_identity != 0) {
1823 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001824 1, ("EarlyData: rejected, the selected key in "
1825 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001826 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001827 }
1828
1829 if (handshake->ciphersuite_info->id !=
1830 ssl->session_negotiate->ciphersuite) {
1831 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001832 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1833 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001834 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835
1836 }
Jerry Yu985c9672022-12-04 14:06:30 +08001837
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001838 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001839 MBEDTLS_SSL_DEBUG_MSG(
1840 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001841 ("EarlyData: rejected, early_data not allowed in ticket "
1842 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001843 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001844 }
Jerry Yu985c9672022-12-04 14:06:30 +08001845
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001846#if defined(MBEDTLS_SSL_ALPN)
1847 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1848 size_t alpn_len;
1849
1850 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1851 return 0;
1852 }
1853
1854 if (alpn != NULL) {
1855 alpn_len = strlen(alpn);
1856 }
1857
1858 if (alpn == NULL ||
1859 ssl->session_negotiate->ticket_alpn == NULL ||
1860 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1861 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1862 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1863 "from the one associated with the pre-shared key."));
1864 return -1;
1865 }
1866#endif
1867
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001868 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001869}
1870#endif /* MBEDTLS_SSL_EARLY_DATA */
1871
XiaokangQian75fe8c72022-06-15 09:42:45 +00001872/* Update the handshake state machine */
1873
Ronald Cronce7d76e2022-07-08 18:56:49 +02001874MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001875static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1876 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001877{
1878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1879
XiaokangQian7807f9f2022-02-15 10:04:37 +00001880 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001881 * Server certificate selection
1882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1884 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1885 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001886 }
1887#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1888 ssl->handshake->sni_name = NULL;
1889 ssl->handshake->sni_name_len = 0;
1890#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1893 if (ret != 0) {
1894 MBEDTLS_SSL_DEBUG_RET(1,
1895 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1896 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001897 }
1898
Jerry Yuab0da372023-02-08 13:55:24 +08001899#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001900 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001901 ssl->handshake->early_data_accepted =
1902 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001903
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001904 if (ssl->handshake->early_data_accepted) {
1905 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1906 if (ret != 0) {
1907 MBEDTLS_SSL_DEBUG_RET(
1908 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1909 return ret;
1910 }
1911 } else {
1912 ssl->discard_early_data_record =
1913 hrr_required ?
1914 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1915 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001916 }
1917 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001918#else
1919 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001920#endif /* MBEDTLS_SSL_EARLY_DATA */
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001923}
1924
1925/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001926 * Main entry point from the state machine; orchestrates the otherfunctions.
1927 */
1928
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001929MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001930static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001931{
1932
XiaokangQian08037552022-04-20 07:16:41 +00001933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001935 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001936 int parse_client_hello_ret;
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1941 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1942 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1945 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001946 parse_client_hello_ret = ret; /* Store positive return value of
1947 * parse_client_hello,
1948 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001949 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001950
Ronald Cronb828c7d2023-04-03 16:37:22 +02001951 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001952 * Version 1.2 of the protocol has to be used for the handshake.
1953 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001954 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1955 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1956 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1957 * will dispatch to the TLS 1.2 state machine.
1958 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001959 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001960 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001961 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001962 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001963 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001964 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001965 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1966 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1967 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001968 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001969 ssl->keep_current_message = 1;
1970 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1971 return 0;
1972 }
1973
Ronald Cron7b6ee942024-01-12 10:29:55 +01001974 MBEDTLS_SSL_PROC_CHK(
1975 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1976 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001977
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001978 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1980 } else {
1981 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1982 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001983
1984cleanup:
1985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1987 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001988}
1989
1990/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001991 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001992 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001993MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001994static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001995{
Jerry Yu637a3f12022-04-20 21:37:58 +08001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1997 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2001 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2002 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2003 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002004 }
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2007 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002008
2009#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002010 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002011#endif /* MBEDTLS_HAVE_TIME */
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002014}
2015
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002017 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018 *
2019 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002020 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021 * } SupportedVersions;
2022 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002023MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002024static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 mbedtls_ssl_context *ssl,
2026 unsigned char *buf,
2027 unsigned char *end,
2028 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030 *out_len = 0;
2031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033
2034 /* Check if we have space to write the extension:
2035 * - extension_type (2 bytes)
2036 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002037 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 mbedtls_ssl_write_version(buf + 4,
2046 ssl->conf->transport,
2047 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2050 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051
Jerry Yu349a6132022-04-14 20:52:56 +08002052 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053
Jerry Yub95dd362022-11-08 21:19:34 +08002054 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058}
2059
Jerry Yud9436a12022-04-20 22:28:09 +08002060
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
2062/* Generate and export a single key share. For hybrid KEMs, this can
2063 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002065static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2066 uint16_t named_group,
2067 unsigned char *buf,
2068 unsigned char *end,
2069 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070{
2071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002072
2073 *out_len = 0;
2074
Valerio Settic9ae8622023-07-25 11:23:50 +02002075#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002076 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002077 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002078 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 ssl, named_group, buf, end, out_len);
2080 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002081 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002082 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 ret);
2084 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002087#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (0 /* Other kinds of KEMs */) {
2089 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002090 ((void) ssl);
2091 ((void) named_group);
2092 ((void) buf);
2093 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2095 }
2096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098}
2099
2100/*
2101 * ssl_tls13_write_key_share_ext
2102 *
2103 * Structure of key_share extension in ServerHello:
2104 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002105 * struct {
2106 * NamedGroup group;
2107 * opaque key_exchange<1..2^16-1>;
2108 * } KeyShareEntry;
2109 * struct {
2110 * KeyShareEntry server_share;
2111 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002113MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002114static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2115 unsigned char *buf,
2116 unsigned char *end,
2117 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118{
Jerry Yu955ddd72022-04-22 22:27:33 +08002119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002121 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002123 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124
2125 *out_len = 0;
2126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2130 mbedtls_ssl_named_group_to_str(group),
2131 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002132
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 /* Check if we have space for header and length fields:
2134 * - extension_type (2 bytes)
2135 * - extension_data_length (2 bytes)
2136 * - group (2 bytes)
2137 * - key_exchange_length (2 bytes)
2138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2140 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2141 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002142 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002143
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2145 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002146 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 ssl, group, server_share + 4, end, &key_exchange_length);
2148 if (ret != 0) {
2149 return ret;
2150 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002151 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002156
Jerry Yu57d48412022-04-20 21:50:42 +08002157 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002162}
Jerry Yud9436a12022-04-20 22:28:09 +08002163
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002164MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002165static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2166 unsigned char *buf,
2167 unsigned char *end,
2168 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002169{
2170 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2171 /* key_share Extension
2172 *
2173 * struct {
2174 * select (Handshake.msg_type) {
2175 * ...
2176 * case hello_retry_request:
2177 * NamedGroup selected_group;
2178 * ...
2179 * };
2180 * } KeyShare;
2181 */
2182
2183 *out_len = 0;
2184
Jerry Yub0ac10b2022-05-05 11:10:08 +08002185 /*
2186 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2187 * of the HRR is then to transmit a cookie to force the client to demonstrate
2188 * reachability at their apparent network address (primarily useful for DTLS).
2189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2191 return 0;
2192 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002193
2194 /* We should only send the key_share extension if the client's initial
2195 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (ssl->handshake->offered_group_id != 0) {
2197 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2198 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002199 }
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (selected_group == 0) {
2202 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2203 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002204 }
2205
Jerry Yub0ac10b2022-05-05 11:10:08 +08002206 /* Check if we have enough space:
2207 * - extension_type (2 bytes)
2208 * - extension_data_length (2 bytes)
2209 * - selected_group (2 bytes)
2210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2214 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2215 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_SSL_DEBUG_MSG(3,
2218 ("HRR selected_group: %s (%x)",
2219 mbedtls_ssl_named_group_to_str(selected_group),
2220 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002221
2222 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002227}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002228
2229/*
2230 * Structure of ServerHello message:
2231 *
2232 * struct {
2233 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2234 * Random random;
2235 * opaque legacy_session_id_echo<0..32>;
2236 * CipherSuite cipher_suite;
2237 * uint8 legacy_compression_method = 0;
2238 * Extension extensions<6..2^16-1>;
2239 * } ServerHello;
2240 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002241MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002242static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2243 unsigned char *buf,
2244 unsigned char *end,
2245 size_t *out_len,
2246 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002247{
Jerry Yu955ddd72022-04-22 22:27:33 +08002248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002249 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002250 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002251 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002252
2253 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002254 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255
Jerry Yucfc04b32022-04-21 09:31:58 +08002256 /* ...
2257 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2258 * ...
2259 * with ProtocolVersion defined as:
2260 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2263 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264 p += 2;
2265
Jerry Yu1c3e6882022-04-20 21:23:40 +08002266 /* ...
2267 * Random random;
2268 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002269 * with Random defined as:
2270 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2273 if (is_hrr) {
2274 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2275 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2276 } else {
2277 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2278 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002279 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2281 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002282 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2283
Jerry Yucfc04b32022-04-21 09:31:58 +08002284 /* ...
2285 * opaque legacy_session_id_echo<0..32>;
2286 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2289 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2290 if (ssl->session_negotiate->id_len > 0) {
2291 memcpy(p, &ssl->session_negotiate->id[0],
2292 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2296 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002297 }
2298
Jerry Yucfc04b32022-04-21 09:31:58 +08002299 /* ...
2300 * CipherSuite cipher_suite;
2301 * ...
2302 * with CipherSuite defined as:
2303 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2306 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 MBEDTLS_SSL_DEBUG_MSG(3,
2309 ("server hello, chosen ciphersuite: %s ( id=%d )",
2310 mbedtls_ssl_get_ciphersuite_name(
2311 ssl->session_negotiate->ciphersuite),
2312 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002313
Jerry Yucfc04b32022-04-21 09:31:58 +08002314 /* ...
2315 * uint8 legacy_compression_method = 0;
2316 * ...
2317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002319 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320
Jerry Yucfc04b32022-04-21 09:31:58 +08002321 /* ...
2322 * Extension extensions<6..2^16-1>;
2323 * ...
2324 * struct {
2325 * ExtensionType extension_type; (2 bytes)
2326 * opaque extension_data<0..2^16-1>;
2327 * } Extension;
2328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002330 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002331 p += 2;
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2334 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002335 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2337 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002338 }
2339 p += output_len;
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2342 if (is_hrr) {
2343 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2344 } else {
2345 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2346 }
2347 if (ret != 0) {
2348 return ret;
2349 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002350 p += output_len;
2351 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002352
Ronald Cron41a443a2022-10-04 16:38:25 +02002353#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2355 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2356 if (ret != 0) {
2357 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2358 ret);
2359 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002360 }
2361 p += output_len;
2362 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002363#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2368 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002369
Jerry Yud9436a12022-04-20 22:28:09 +08002370 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002373
Jerry Yu7de2ff02022-11-08 21:43:46 +08002374 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002375 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_SSL_HS_SERVER_HELLO,
2377 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002380}
2381
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002382MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002383static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002384{
2385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2387 if (ret != 0) {
2388 MBEDTLS_SSL_DEBUG_RET(1,
2389 "mbedtls_ssl_tls13_compute_handshake_transform",
2390 ret);
2391 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002392 }
2393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002395}
2396
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002397MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002398static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002399{
Jerry Yu637a3f12022-04-20 21:37:58 +08002400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002401 unsigned char *buf;
2402 size_t buf_len, msg_len;
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002407
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002408 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2409 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2412 buf + buf_len,
2413 &msg_len,
2414 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002415
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002416 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002417 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2420 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002421
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002422 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002423
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002424#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2425 /* The server sends a dummy change_cipher_spec record immediately
2426 * after its first handshake message. This may either be after
2427 * a ServerHello or a HelloRetryRequest.
2428 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002429 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002431#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002433#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002434
Jerry Yuf4b27e42022-03-30 17:32:21 +08002435cleanup:
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2438 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002439}
2440
Jerry Yu23d1a252022-05-12 18:08:59 +08002441
2442/*
2443 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2444 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002445MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002446static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002447{
2448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002449 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2451 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2452 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2453 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002454 }
2455
2456 /*
2457 * Create stateless transcript hash for HRR
2458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2460 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2461 if (ret != 0) {
2462 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2463 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002468}
2469
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002470MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002471static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002472{
2473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2474 unsigned char *buf;
2475 size_t buf_len, msg_len;
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2482 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2483 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2486 buf + buf_len,
2487 &msg_len,
2488 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002489 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002490 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002491
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2494 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002495
Ronald Cron5fbd2702024-02-14 10:03:36 +01002496 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002497
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002498#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2499 /* The server sends a dummy change_cipher_spec record immediately
2500 * after its first handshake message. This may either be after
2501 * a ServerHello or a HelloRetryRequest.
2502 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002503 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002505#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002507#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002508
2509cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2511 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002512}
2513
Jerry Yu5b64ae92022-03-30 17:15:02 +08002514/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002515 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2516 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002517
2518/*
2519 * struct {
2520 * Extension extensions<0..2 ^ 16 - 1>;
2521 * } EncryptedExtensions;
2522 *
2523 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002524MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002525static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2526 unsigned char *buf,
2527 unsigned char *end,
2528 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002529{
XiaokangQianacb39922022-06-17 10:18:48 +00002530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531 unsigned char *p = buf;
2532 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002533 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002534 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002535
Jerry Yu4d3841a2022-04-16 12:37:19 +08002536 *out_len = 0;
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002539 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002540 p += 2;
2541
Jerry Yu8937eb42022-05-03 12:12:14 +08002542 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002543 ((void) ret);
2544 ((void) output_len);
2545
2546#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2548 if (ret != 0) {
2549 return ret;
2550 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002551 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002552#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553
Jerry Yu71c14f12022-12-12 11:10:35 +08002554#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002555 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002556 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002557 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002558 if (ret != 0) {
2559 return ret;
2560 }
2561 p += output_len;
2562 }
2563#endif /* MBEDTLS_SSL_EARLY_DATA */
2564
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002565#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002566 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002567 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2568 ssl, p, end, &output_len);
2569 if (ret != 0) {
2570 return ret;
2571 }
2572 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002573 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002574#endif
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 extensions_len = (p - p_extensions_len) - 2;
2577 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002578
2579 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002582
Jerry Yu7de2ff02022-11-08 21:43:46 +08002583 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587}
2588
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002589MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002590static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591{
2592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2593 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002594 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 mbedtls_ssl_set_outbound_transform(ssl,
2597 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002598 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002603 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2604 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2605 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2608 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002609
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002610 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002611 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2612 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2615 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002616
Ronald Cron928cbd32022-10-04 16:14:26 +02002617#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2619 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2620 } else {
2621 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2622 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002623#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002625#endif
2626
Jerry Yu4d3841a2022-04-16 12:37:19 +08002627cleanup:
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2630 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002631}
2632
Ronald Cron928cbd32022-10-04 16:14:26 +02002633#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002634#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2635#define SSL_CERTIFICATE_REQUEST_SKIP 1
2636/* Coordination:
2637 * Check whether a CertificateRequest message should be written.
2638 * Returns a negative code on failure, or
2639 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2640 * - SSL_CERTIFICATE_REQUEST_SKIP
2641 * indicating if the writing of the CertificateRequest
2642 * should be skipped or not.
2643 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002644MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002645static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002646{
2647 int authmode;
2648
XiaokangQian40a35232022-05-07 09:02:40 +00002649#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002651 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002653#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002654 authmode = ssl->conf->authmode;
2655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002657 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002659 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002660
XiaokangQianc3017f62022-05-13 05:55:41 +00002661 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002664}
2665
XiaokangQiancec9ae62022-05-06 07:28:50 +00002666/*
2667 * struct {
2668 * opaque certificate_request_context<0..2^8-1>;
2669 * Extension extensions<2..2^16-1>;
2670 * } CertificateRequest;
2671 *
2672 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002673MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002674static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2675 unsigned char *buf,
2676 const unsigned char *end,
2677 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002678{
2679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2680 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002681 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002682 unsigned char *p_extensions_len;
2683
2684 *out_len = 0;
2685
2686 /* Check if we have enough space:
2687 * - certificate_request_context (1 byte)
2688 * - extensions length (2 bytes)
2689 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002691
2692 /*
2693 * Write certificate_request_context
2694 */
2695 /*
2696 * We use a zero length context for the normal handshake
2697 * messages. For post-authentication handshake messages
2698 * this request context would be set to a non-zero value.
2699 */
2700 *p++ = 0x0;
2701
2702 /*
2703 * Write extensions
2704 */
2705 /* The extensions must contain the signature_algorithms. */
2706 p_extensions_len = p;
2707 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2709 if (ret != 0) {
2710 return ret;
2711 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002712
XiaokangQianec6efb92022-05-06 09:53:10 +00002713 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002715
2716 *out_len = p - buf;
2717
Jerry Yu7de2ff02022-11-08 21:43:46 +08002718 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722}
2723
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002724MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002725static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726{
2727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734 unsigned char *buf;
2735 size_t buf_len, msg_len;
2736
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002737 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2738 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2739 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2742 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002744 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002745 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2746 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2749 ssl, buf_len, msg_len));
2750 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2751 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002752 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 } else {
2754 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002755 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2756 goto cleanup;
2757 }
2758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002760cleanup:
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2763 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002764}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002765
Jerry Yu4d3841a2022-04-16 12:37:19 +08002766/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002767 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002768 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002769MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002770static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002771{
Jerry Yu5a26f302022-05-10 20:46:40 +08002772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002773
2774#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2776 mbedtls_ssl_own_cert(ssl) == NULL) {
2777 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2778 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2779 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2780 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002781 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002782#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2785 if (ret != 0) {
2786 return ret;
2787 }
2788 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2789 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002790}
2791
2792/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002793 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002794 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002796static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002797{
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2799 if (ret != 0) {
2800 return ret;
2801 }
2802 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2803 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002804}
Ronald Cron928cbd32022-10-04 16:14:26 +02002805#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002806
Jerry Yu9b72e392023-12-01 16:27:08 +08002807/*
2808 * RFC 8446 section A.2
2809 *
2810 * | Send ServerHello
2811 * | K_send = handshake
2812 * | Send EncryptedExtensions
2813 * | [Send CertificateRequest]
2814 * Can send | [Send Certificate + CertificateVerify]
2815 * app data | Send Finished
2816 * after --> | K_send = application
2817 * here +--------+--------+
2818 * No 0-RTT | | 0-RTT
2819 * | |
2820 * K_recv = handshake | | K_recv = early data
2821 * [Skip decrypt errors] | +------> WAIT_EOED -+
2822 * | | Recv | | Recv EndOfEarlyData
2823 * | | early data | | K_recv = handshake
2824 * | +------------+ |
2825 * | |
2826 * +> WAIT_FLIGHT2 <--------+
2827 * |
2828 * +--------+--------+
2829 * No auth | | Client auth
2830 * | |
2831 * | v
2832 * | WAIT_CERT
2833 * | Recv | | Recv Certificate
2834 * | empty | v
2835 * | Certificate | WAIT_CV
2836 * | | | Recv
2837 * | v | CertificateVerify
2838 * +-> WAIT_FINISHED <---+
2839 * | Recv Finished
2840 *
2841 *
2842 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002843 * above diagram. We are not going to receive early data related messages
2844 * anymore, prepare to receive the first handshake message of the client
2845 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002846 */
Jerry Yu3be85072023-12-04 09:58:54 +08002847static void ssl_tls13_prepare_for_handshake_second_flight(
2848 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002849{
Jerry Yu9b72e392023-12-01 16:27:08 +08002850 if (ssl->handshake->certificate_request_sent) {
2851 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2852 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002853 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002854 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002855
Jerry Yu9b72e392023-12-01 16:27:08 +08002856 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2857 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002858}
2859
Jerry Yu83da34e2022-04-16 13:59:52 +08002860/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002861 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002862 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002864static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002865{
Jerry Yud6e253d2022-05-18 13:59:24 +08002866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2869 if (ret != 0) {
2870 return ret;
2871 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2874 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002875 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2877 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2878 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002879 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002880
Jerry Yu87b5ed42022-12-12 13:07:07 +08002881#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002882 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002883 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002884 MBEDTLS_SSL_DEBUG_MSG(
2885 1, ("Switch to early keys for inbound traffic. "
2886 "( K_recv = early data )"));
2887 mbedtls_ssl_set_inbound_transform(
2888 ssl, ssl->handshake->transform_earlydata);
2889 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2890 return 0;
2891 }
2892#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002893 MBEDTLS_SSL_DEBUG_MSG(
2894 1, ("Switch to handshake keys for inbound traffic "
2895 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002897
Jerry Yu3be85072023-12-04 09:58:54 +08002898 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002899
2900 return 0;
2901}
2902
Jerry Yu87b5ed42022-12-12 13:07:07 +08002903#if defined(MBEDTLS_SSL_EARLY_DATA)
2904/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002905 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002906 */
Jerry Yu3be85072023-12-04 09:58:54 +08002907#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002908#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002909/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002910 * Deals with the ambiguity of not knowing if the next message is an
2911 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002912 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002913 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002914 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002915 * indicating which message is received.
2916 */
2917MBEDTLS_CHECK_RETURN_CRITICAL
2918static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2919{
2920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002921
2922 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2923 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2924 return ret;
2925 }
2926 ssl->keep_current_message = 1;
2927
2928 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2929 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002930 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002931 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002932 }
2933
2934 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002935 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002936 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002937 /* Set the reading pointer */
2938 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002939 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2940 if (ret != 0) {
2941 return ret;
2942 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002943 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002944 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002945 }
2946
Jerry Yu7bb40a32023-12-04 10:04:15 +08002947 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2948 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002949 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002950}
2951
2952MBEDTLS_CHECK_RETURN_CRITICAL
2953static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2954 const unsigned char *buf,
2955 const unsigned char *end)
2956{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002957 /* RFC 8446 section 4.5
2958 *
2959 * struct {} EndOfEarlyData;
2960 */
Jerry Yufbf03992023-12-04 10:00:37 +08002961 if (buf != end) {
2962 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2963 MBEDTLS_ERR_SSL_DECODE_ERROR);
2964 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2965 }
2966 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002967}
2968
Jerry Yud5c34962023-12-01 16:32:31 +08002969/*
2970 * RFC 8446 section A.2
2971 *
2972 * | Send ServerHello
2973 * | K_send = handshake
2974 * | Send EncryptedExtensions
2975 * | [Send CertificateRequest]
2976 * Can send | [Send Certificate + CertificateVerify]
2977 * app data | Send Finished
2978 * after --> | K_send = application
2979 * here +--------+--------+
2980 * No 0-RTT | | 0-RTT
2981 * | |
2982 * K_recv = handshake | | K_recv = early data
2983 * [Skip decrypt errors] | +------> WAIT_EOED -+
2984 * | | Recv | | Recv EndOfEarlyData
2985 * | | early data | | K_recv = handshake
2986 * | +------------+ |
2987 * | |
2988 * +> WAIT_FLIGHT2 <--------+
2989 * |
2990 * +--------+--------+
2991 * No auth | | Client auth
2992 * | |
2993 * | v
2994 * | WAIT_CERT
2995 * | Recv | | Recv Certificate
2996 * | empty | v
2997 * | Certificate | WAIT_CV
2998 * | | | Recv
2999 * | v | CertificateVerify
3000 * +-> WAIT_FINISHED <---+
3001 * | Recv Finished
3002 *
3003 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3004 * the above diagram.
3005 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003006MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003007static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003008{
3009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003010
3011 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3012
3013 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3014
Jerry Yu3be85072023-12-04 09:58:54 +08003015 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003016 unsigned char *buf;
3017 size_t buf_len;
3018
3019 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3020 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3021 &buf, &buf_len));
3022
3023 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3024 ssl, buf, buf + buf_len));
3025
Jerry Yue9655122023-12-01 16:44:40 +08003026 MBEDTLS_SSL_DEBUG_MSG(
3027 1, ("Switch to handshake keys for inbound traffic"
3028 "( K_recv = handshake )"));
3029 mbedtls_ssl_set_inbound_transform(
3030 ssl, ssl->handshake->transform_handshake);
3031
Jerry Yud5c34962023-12-01 16:32:31 +08003032 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3033 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3034 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003035
Jerry Yu3be85072023-12-04 09:58:54 +08003036 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003037
Jerry Yub55f9eb2023-12-05 10:27:17 +08003038 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003039 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3040 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003041 } else {
3042 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3043 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3044 goto cleanup;
3045 }
3046
Jerry Yud5c34962023-12-01 16:32:31 +08003047cleanup:
3048 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003049 return ret;
3050}
3051#endif /* MBEDTLS_SSL_EARLY_DATA */
3052
Jerry Yu69dd8d42022-04-16 12:51:26 +08003053/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003054 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003055 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003057static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003058{
Jerry Yud6e253d2022-05-18 13:59:24 +08003059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3062 if (ret != 0) {
3063 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003064 }
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3067 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003068 MBEDTLS_SSL_DEBUG_RET(
3069 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 }
3071
3072 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3073 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003074}
3075
3076/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003077 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003078 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003080static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003081{
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003085
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003086#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3087 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003088/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3089 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3090 * expected to be resolved with issue#6395.
3091 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003092 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003093 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003094 mbedtls_ssl_handshake_set_state(
3095 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003096 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003097#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003098 {
3099 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3100 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003102}
3103
Norbert Fabritius96eed722023-01-23 15:24:59 +01003104#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003105/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003106 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003107 */
3108#define SSL_NEW_SESSION_TICKET_SKIP 0
3109#define SSL_NEW_SESSION_TICKET_WRITE 1
3110MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003111static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003112{
3113 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 if (ssl->conf->f_ticket_write == NULL) {
3115 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3116 " callback is not set"));
3117 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (ssl->conf->new_session_tickets_count == 0) {
3120 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3121 " configured count is zero"));
3122 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003123 }
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (ssl->handshake->new_session_tickets_count == 0) {
3126 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3127 "been sent."));
3128 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003129 }
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003132}
3133
Jerry Yue67bef42022-07-07 07:29:42 +00003134MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003135static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3136 unsigned char *ticket_nonce,
3137 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003138{
3139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3140 mbedtls_ssl_session *session = ssl->session;
3141 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3142 psa_algorithm_t psa_hash_alg;
3143 int hash_length;
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003146
Pengyu Lv9f926952022-11-17 15:22:33 +08003147 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003148 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003149 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003150#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003151 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003152 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003153#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003154
3155#if defined(MBEDTLS_SSL_EARLY_DATA)
3156 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3157 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003158 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003159 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003160 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003161 }
3162#endif /* MBEDTLS_SSL_EARLY_DATA */
3163
Pengyu Lv4938a562023-01-16 11:28:49 +08003164 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003165
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003166#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003167 if (session->ticket_alpn == NULL) {
3168 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3169 if (ret != 0) {
3170 return ret;
3171 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003172 }
3173#endif
3174
Jerry Yue67bef42022-07-07 07:29:42 +00003175 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3177 (unsigned char *) &session->ticket_age_add,
3178 sizeof(session->ticket_age_add)) != 0)) {
3179 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3180 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3183 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003184
3185 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3187 if (ret != 0) {
3188 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3189 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003190 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3192 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003193
3194 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003196 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3198 if (hash_length == -1 ||
3199 (size_t) hash_length > sizeof(session->resumption_key)) {
3200 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003201 }
3202
Jerry Yue67bef42022-07-07 07:29:42 +00003203 /* In this code the psk key length equals the length of the hash */
3204 session->resumption_key_len = hash_length;
3205 session->ciphersuite = ciphersuite_info->id;
3206
3207 /* Compute resumption key
3208 *
3209 * HKDF-Expand-Label( resumption_master_secret,
3210 * "resumption", ticket_nonce, Hash.length )
3211 */
3212 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 psa_hash_alg,
3214 session->app_secrets.resumption_master_secret,
3215 hash_length,
3216 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3217 ticket_nonce,
3218 ticket_nonce_size,
3219 session->resumption_key,
3220 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 if (ret != 0) {
3223 MBEDTLS_SSL_DEBUG_RET(2,
3224 "Creating the ticket-resumed PSK failed",
3225 ret);
3226 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3229 session->resumption_key,
3230 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3233 session->app_secrets.resumption_master_secret,
3234 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003237}
3238
3239/* This function creates a NewSessionTicket message in the following format:
3240 *
3241 * struct {
3242 * uint32 ticket_lifetime;
3243 * uint32 ticket_age_add;
3244 * opaque ticket_nonce<0..255>;
3245 * opaque ticket<1..2^16-1>;
3246 * Extension extensions<0..2^16-2>;
3247 * } NewSessionTicket;
3248 *
3249 * The ticket inside the NewSessionTicket message is an encrypted container
3250 * carrying the necessary information so that the server is later able to
3251 * re-start the communication.
3252 *
3253 * The following fields are placed inside the ticket by the
3254 * f_ticket_write() function:
3255 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003256 * - creation time (ticket_creation_time)
3257 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003258 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003259 * - key (resumption_key)
3260 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003261 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003262 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003263 */
3264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003265static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3266 unsigned char *buf,
3267 unsigned char *end,
3268 size_t *out_len,
3269 unsigned char *ticket_nonce,
3270 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003271{
3272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3273 unsigned char *p = buf;
3274 mbedtls_ssl_session *session = ssl->session;
3275 size_t ticket_len;
3276 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003277 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003278
3279 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003281
3282 /*
3283 * ticket_lifetime 4 bytes
3284 * ticket_age_add 4 bytes
3285 * ticket_nonce 1 + ticket_nonce_size bytes
3286 * ticket >=2 bytes
3287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003289
3290 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003291#if defined(MBEDTLS_HAVE_TIME)
3292 session->ticket_creation_time = mbedtls_ms_time();
3293#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3295 session,
3296 p + 9 + ticket_nonce_size + 2,
3297 end,
3298 &ticket_len,
3299 &ticket_lifetime);
3300 if (ret != 0) {
3301 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3302 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003303 }
Jerry Yuce794882023-11-22 15:01:18 +08003304
3305 /* RFC 8446 section 4.6.1
3306 *
Jerry Yue67bef42022-07-07 07:29:42 +00003307 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003308 * unsigned integer in network byte order from the time of ticket
3309 * issuance. Servers MUST NOT use any value greater than
3310 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003311 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003312 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003313 MBEDTLS_SSL_DEBUG_MSG(
3314 1, ("Ticket lifetime (%u) is greater than 7 days.",
3315 (unsigned int) ticket_lifetime));
3316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 }
Jerry Yuce794882023-11-22 15:01:18 +08003318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3320 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3321 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003322
3323 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3325 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3326 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003327
3328 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 p[8] = (unsigned char) ticket_nonce_size;
3330 if (ticket_nonce_size > 0) {
3331 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003332 }
3333 p += 9 + ticket_nonce_size;
3334
3335 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003337 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003339 p += ticket_len;
3340
3341 /* Ticket Extensions
3342 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003343 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003344 */
Jerry Yuedab6372022-10-31 14:37:31 +08003345 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003348 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003349 p += 2;
3350
Jerry Yu01da35e2022-12-12 15:09:22 +08003351#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003352 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003353 size_t output_len;
3354
Jerry Yuf135bac2023-11-23 18:10:51 +08003355 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003356 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003357 MBEDTLS_SSL_DEBUG_RET(
3358 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3359 return ret;
3360 }
3361 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003362 } else {
3363 MBEDTLS_SSL_DEBUG_MSG(
3364 4, ("early_data not allowed, "
3365 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003366 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003367
Jerry Yu01da35e2022-12-12 15:09:22 +08003368#endif /* MBEDTLS_SSL_EARLY_DATA */
3369
3370 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3371
Jerry Yue67bef42022-07-07 07:29:42 +00003372 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3374 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003375
Jerry Yu7de2ff02022-11-08 21:43:46 +08003376 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003380}
3381
3382/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003383 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003385static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003386{
3387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003392 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3393 unsigned char *buf;
3394 size_t buf_len, msg_len;
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3397 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003398
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003399 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3400 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3401 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3404 ssl, buf, buf + buf_len, &msg_len,
3405 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3408 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003409
Jerry Yu359e65f2022-09-22 23:47:43 +08003410 /* Limit session tickets count to one when resumption connection.
3411 *
3412 * See document of mbedtls_ssl_conf_new_session_tickets.
3413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003415 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003417 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003419
Jerry Yua8d3c502022-10-30 14:51:23 +08003420 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3422 } else {
3423 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003424 }
3425
Jerry Yue67bef42022-07-07 07:29:42 +00003426cleanup:
3427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003429}
3430#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3431
3432/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003433 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003435int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003436{
XiaokangQian08037552022-04-20 07:16:41 +00003437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3440 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3441 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003444 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003448 /* start state */
3449 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003451 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003452 break;
3453
XiaokangQian7807f9f2022-02-15 10:04:37 +00003454 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 ret = ssl_tls13_process_client_hello(ssl);
3456 if (ret != 0) {
3457 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3458 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003459 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003460
Jerry Yuf41553b2022-05-09 22:20:30 +08003461 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ret = ssl_tls13_write_hello_retry_request(ssl);
3463 if (ret != 0) {
3464 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3465 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003466 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003467 break;
3468
Jerry Yu5b64ae92022-03-30 17:15:02 +08003469 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003471 break;
3472
Xiaofei Baicba64af2022-02-15 10:00:56 +00003473 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 ret = ssl_tls13_write_encrypted_extensions(ssl);
3475 if (ret != 0) {
3476 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3477 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003478 }
Jerry Yu48330562022-05-06 21:35:44 +08003479 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003480
Ronald Cron928cbd32022-10-04 16:14:26 +02003481#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003482 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003484 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003485
Jerry Yu83da34e2022-04-16 13:59:52 +08003486 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003488 break;
3489
3490 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003492 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003493#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003494
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 /*
3496 * Injection of dummy-CCS's for middlebox compatibility
3497 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003498#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003499 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3501 if (ret == 0) {
3502 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3503 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003504 break;
3505
3506 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003507 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3508 if (ret != 0) {
3509 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003511 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003512 break;
3513#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3514
Jerry Yu69dd8d42022-04-16 12:51:26 +08003515 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003517 break;
3518
Jerry Yu87b5ed42022-12-12 13:07:07 +08003519#if defined(MBEDTLS_SSL_EARLY_DATA)
3520 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003521 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003522 break;
3523#endif /* MBEDTLS_SSL_EARLY_DATA */
3524
Jerry Yu69dd8d42022-04-16 12:51:26 +08003525 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003527 break;
3528
Jerry Yu69dd8d42022-04-16 12:51:26 +08003529 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003531 break;
3532
Ronald Cron766c0cd2022-10-18 12:17:11 +02003533#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003534 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3536 if (ret == 0) {
3537 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003538 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3540 } else {
3541 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003542 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003544 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003545 }
3546 break;
3547
3548 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3550 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003551 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003553 }
3554 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003555#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003556
Jerry Yue67bef42022-07-07 07:29:42 +00003557#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003558 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 ret = ssl_tls13_write_new_session_ticket(ssl);
3560 if (ret != 0) {
3561 MBEDTLS_SSL_DEBUG_RET(1,
3562 "ssl_tls13_write_new_session_ticket ",
3563 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003564 }
3565 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003566 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003567 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003568 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003569 * as part of ssl_prepare_handshake_step.
3570 */
3571 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003572
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 if (ssl->handshake->new_session_tickets_count == 0) {
3574 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3575 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003576 mbedtls_ssl_handshake_set_state(
3577 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 }
Jerry Yue67bef42022-07-07 07:29:42 +00003579 break;
3580
3581#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3582
XiaokangQian7807f9f2022-02-15 10:04:37 +00003583 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3585 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003586 }
3587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003589}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003590
Jerry Yufb4b6472022-01-27 15:03:26 +08003591#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */