blob: 92e8add6c2004a60005721499d9629d5674afaaf [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#if defined(MBEDTLS_SSL_SESSION_TICKETS)
573 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800575#endif
Jerry Yubb852022022-07-20 21:10:44 +0800576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
578 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
581 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800582 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800585 binder_len = *p_binder_len;
586 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800588 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800589
Jerry Yu96a2e362022-07-21 15:11:34 +0800590 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jerry Yu1c105562022-07-10 06:32:38 +0000594
Jerry Yu96a2e362022-07-21 15:11:34 +0800595 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100597 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100598 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800599 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100603
Ronald Cron79cdd412024-02-20 17:19:28 +0100604 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800605 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100606 psk_ciphersuite_id = 0;
607 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100608 allowed_key_exchange_modes =
609 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 break;
Jerry Yu82534862022-08-30 10:42:33 +0800611#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100612 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100613 psk_ciphersuite_id = session.ciphersuite;
614 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100615 ssl->session_negotiate->ticket_flags = session.ticket_flags;
616 allowed_key_exchange_modes =
617 session.ticket_flags &
618 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800619 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100620#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800621 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100624
Ronald Cron79cdd412024-02-20 17:19:28 +0100625 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
626
Ronald Croncf284562024-02-16 18:54:10 +0100627 if ((allowed_key_exchange_modes &
628 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
629 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100630 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100631 } else if ((allowed_key_exchange_modes &
632 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
633 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100634 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100635 }
636
Ronald Cron79cdd412024-02-20 17:19:28 +0100637 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100638 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
639 continue;
640 }
641
Ronald Cron89089cc2024-02-14 18:18:08 +0100642 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
643 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100644 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100645
Ronald Cron74a16292024-02-23 17:07:41 +0100646 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100647#if defined(MBEDTLS_SSL_SESSION_TICKETS)
648 mbedtls_ssl_session_free(&session);
649#endif
650 /*
651 * We consider finding a ciphersuite suitable for the PSK as part
652 * of the validation of its binder. Thus if we do not find one, we
653 * abort the handshake with a decrypt_error alert.
654 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800655 MBEDTLS_SSL_PEND_FATAL_ALERT(
656 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100658 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800659 }
660
Jerry Yu96a2e362022-07-21 15:11:34 +0800661 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100662 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100663 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100664 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800665 /* For security reasons, the handshake should be aborted when we
666 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
667 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800668#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800670#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000672 MBEDTLS_SSL_DEBUG_RET(
673 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800674 MBEDTLS_SSL_PEND_FATAL_ALERT(
675 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
677 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800678 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800679
680 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800681
Jerry Yu82534862022-08-30 10:42:33 +0800682#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100683 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
685 &session);
686 mbedtls_ssl_session_free(&session);
687 if (ret != 0) {
688 return ret;
689 }
Jerry Yu82534862022-08-30 10:42:33 +0800690 }
691#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000692 }
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (p_identity_len != identities_end || p_binder_len != binders_end) {
695 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
696 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
697 MBEDTLS_ERR_SSL_DECODE_ERROR);
698 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000699 }
700
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800701 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000702 ret = ssl->handshake->update_checksum(
703 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100704 if (0 != ret) {
705 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
706 return ret;
707 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100709 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ssl->handshake->selected_identity = (uint16_t) matched_identity;
714 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000717}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718
719/*
720 * struct {
721 * select ( Handshake.msg_type ) {
722 * ....
723 * case server_hello:
724 * uint16 selected_identity;
725 * }
726 * } PreSharedKeyExtension;
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
729 unsigned char *buf,
730 unsigned char *end,
731 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732{
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
735 *olen = 0;
736
David Horstmann21b89762022-10-06 18:34:28 +0100737 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000742#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000744 /* We shouldn't have called this extension writer unless we've
745 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000747 }
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
750 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
753 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000756
757 *olen = 6;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
760 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000765}
766
Ronald Cron41a443a2022-10-04 16:38:25 +0200767#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000768
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769/* From RFC 8446:
770 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000771 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772 * } SupportedVersions;
773 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200774MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100775static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
776 const unsigned char *buf,
777 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000780 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000781 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000782 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100783 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000786 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000787 p += 1;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000790 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 while (p < versions_end) {
792 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
793 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000794 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795
Ronald Cron3bd2b022023-04-03 16:45:39 +0200796 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100797 found_supported_version = 1;
798 break;
799 }
800
Ronald Cron3bd2b022023-04-03 16:45:39 +0200801 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
802 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100803 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 break;
805 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000806 }
807
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100808 if (!found_supported_version) {
809 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
812 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
813 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814 }
815
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100816 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100819 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820}
821
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200822#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000823/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824 *
825 * From RFC 8446:
826 * enum {
827 * ... (0xFFFF)
828 * } NamedGroup;
829 * struct {
830 * NamedGroup named_group_list<2..2^16-1>;
831 * } NamedGroupList;
832 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200833MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100834static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
835 const unsigned char *buf,
836 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000839 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000840 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
843 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
844 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000847 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000848 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000851 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
853 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000854 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MBEDTLS_SSL_DEBUG_MSG(2,
857 ("got named group: %s(%04x)",
858 mbedtls_ssl_named_group_to_str(named_group),
859 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
862 !mbedtls_ssl_named_group_is_supported(named_group) ||
863 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000864 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000865 }
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 MBEDTLS_SSL_DEBUG_MSG(2,
868 ("add named group %s(%04x) into received list.",
869 mbedtls_ssl_named_group_to_str(named_group),
870 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800871
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000872 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873 }
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876
877}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200878#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879
XiaokangQian08037552022-04-20 07:16:41 +0000880#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
881
Valerio Settic9ae8622023-07-25 11:23:50 +0200882#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883/*
884 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000885 * extension is correct and stores the first acceptable key share and its
886 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887 *
888 * Possible return values are:
889 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000890 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
891 * the client does not match a group supported by the server. A
892 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000893 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800894 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200895MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100896static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
897 const unsigned char *buf,
898 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899{
XiaokangQianb67384d2022-04-19 00:02:38 +0000900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000901 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000902 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800903 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904
905 /* From RFC 8446:
906 *
907 * struct {
908 * KeyShareEntry client_shares<0..2^16-1>;
909 * } KeyShareClientHello;
910 *
911 */
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
914 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917
918 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000919 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920
921 /* We try to find a suitable key share entry and copy it to the
922 * handshake context. Later, we have to find out whether we can do
923 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000924 * dismiss it and send a HelloRetryRequest message.
925 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000928 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800929 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800930 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931
932 /*
933 * struct {
934 * NamedGroup group;
935 * opaque key_exchange<1..2^16-1>;
936 * } KeyShareEntry;
937 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
939 group = MBEDTLS_GET_UINT16_BE(p, 0);
940 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800941 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800942 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800944 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945
946 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000947 * for input validation purposes.
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
950 !mbedtls_ssl_named_group_is_supported(group) ||
951 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000952 continue;
953 }
XiaokangQian060d8672022-04-21 09:24:56 +0000954
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200956 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200958 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200959 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200960 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 mbedtls_ssl_named_group_to_str(group),
962 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200963 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 ssl, key_exchange - 2, key_exchange_len + 2);
965 if (ret != 0) {
966 return ret;
967 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 } else {
970 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
971 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000972 continue;
973 }
974
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000975 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976 }
977
Jerry Yuc1be19f2022-04-23 16:11:39 +0800978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (ssl->handshake->offered_group_id == 0) {
980 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
981 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000984}
Valerio Settic9ae8622023-07-25 11:23:50 +0200985#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100988static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
989 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000990{
Jerry Yu0c354a22022-08-29 15:25:36 +0800991 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000993}
994
Jerry Yue5991322022-11-07 14:03:44 +0800995#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200996MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000997static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000999{
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return ssl_tls13_client_hello_has_exts(
1001 ssl,
1002 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1003 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1004 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001005}
Jerry Yue5991322022-11-07 14:03:44 +08001006#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001007
Jerry Yue5991322022-11-07 14:03:44 +08001008#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001009MBEDTLS_CHECK_RETURN_CRITICAL
1010static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001012{
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 return ssl_tls13_client_hello_has_exts(
1014 ssl,
1015 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1016 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001017}
Jerry Yue5991322022-11-07 14:03:44 +08001018#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001019
Jerry Yue5991322022-11-07 14:03:44 +08001020#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001021MBEDTLS_CHECK_RETURN_CRITICAL
1022static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001024{
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 return ssl_tls13_client_hello_has_exts(
1026 ssl,
1027 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1028 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1029 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1030 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001031}
Jerry Yue5991322022-11-07 14:03:44 +08001032#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001033
Pengyu Lv306a01d2023-02-02 16:35:47 +08001034#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1035MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001036static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001037{
Jerry Yue5991322022-11-07 14:03:44 +08001038#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001039 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001040 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001042#else
1043 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001046}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001047
Jerry Yu77f01482022-07-11 07:03:24 +00001048MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001049static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001050{
Jerry Yue5991322022-11-07 14:03:44 +08001051#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001052 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001053 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001055#else
1056 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001058#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001059}
1060#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1061
1062MBEDTLS_CHECK_RETURN_CRITICAL
1063static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1064{
1065#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1066 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1067 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1068#else
1069 ((void) ssl);
1070 return 0;
1071#endif
1072}
1073
XiaokangQian81802f42022-06-10 13:25:22 +00001074#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001075 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001076
1077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001078static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001079{
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001081 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001083 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 }
1102}
1103#endif /* MBEDTLS_USE_PSA_CRYPTO */
1104
XiaokangQian23c5be62022-06-07 02:04:34 +00001105/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001106 * Pick best ( private key, certificate chain ) pair based on the signature
1107 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001108 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001110static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001111{
XiaokangQianfb665a82022-06-15 03:57:21 +00001112 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001113 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001114
1115#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001117 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001119#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (key_cert_list == NULL) {
1123 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1124 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001125 }
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1128 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 for (key_cert = key_cert_list; key_cert != NULL;
1137 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001138#if defined(MBEDTLS_USE_PSA_CRYPTO)
1139 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1140#endif /* MBEDTLS_USE_PSA_CRYPTO */
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1143 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001144
1145 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 * This avoids sending the client a cert it'll reject based on
1147 * keyUsage or other extensions.
1148 */
1149 if (mbedtls_x509_crt_check_key_usage(
1150 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001151 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001152 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1154 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1155 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001156 continue;
1157 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 MBEDTLS_SSL_DEBUG_MSG(3,
1160 ("ssl_tls13_pick_key_cert:"
1161 "check signature algorithm %s [%04x]",
1162 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1163 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001166#endif /* MBEDTLS_USE_PSA_CRYPTO */
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1169 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001170#if defined(MBEDTLS_USE_PSA_CRYPTO)
1171 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1173 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001174#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001176 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_SSL_DEBUG_MSG(3,
1178 ("ssl_tls13_pick_key_cert:"
1179 "selected signature algorithm"
1180 " %s [%04x]",
1181 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1182 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001183 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 3, "selected certificate (chain)",
1185 ssl->handshake->key_cert->cert);
1186 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001187 }
XiaokangQian81802f42022-06-10 13:25:22 +00001188 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001189 }
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1192 "no suitable certificate found"));
1193 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001194}
XiaokangQian81802f42022-06-10 13:25:22 +00001195#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001196 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001197
XiaokangQian4080a7f2022-04-11 09:55:18 +00001198/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001199 *
1200 * STATE HANDLING: ClientHello
1201 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001202 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001204 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001205 *
1206 * In this case, the server progresses to sending its ServerHello.
1207 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 * 2) The ClientHello was well-formed but didn't match the server's
1209 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 *
1211 * For example, the client might not have offered a key share which
1212 * the server supports, or the server might require a cookie.
1213 *
1214 * In this case, the server sends a HelloRetryRequest.
1215 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001216 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001217 *
1218 * In this case, we abort the handshake.
1219 *
1220 */
1221
1222/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001223 * Structure of this message:
1224 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001225 * uint16 ProtocolVersion;
1226 * opaque Random[32];
1227 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001228 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001229 * struct {
1230 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1231 * Random random;
1232 * opaque legacy_session_id<0..32>;
1233 * CipherSuite cipher_suites<2..2^16-2>;
1234 * opaque legacy_compression_methods<1..2^8-1>;
1235 * Extension extensions<8..2^16-1>;
1236 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001237 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001238
1239#define SSL_CLIENT_HELLO_OK 0
1240#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001241#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001242
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001243MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001244static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1245 const unsigned char *buf,
1246 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247{
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1249 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001250 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001251 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001252 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001253 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001254 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001255 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001256 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001257 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001258 const unsigned char *supported_versions_data;
1259 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001260 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001261 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001262 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001263
Ronald Cron41a443a2022-10-04 16:38:25 +02001264#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001265 int got_psk = 0;
1266 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001267 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001268 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001269#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270
XiaokangQian7807f9f2022-02-15 10:04:37 +00001271 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001272 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001273 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001274 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001275 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277 * .. . .. ciphersuite list length ( 2 bytes )
1278 * .. . .. ciphersuite list
1279 * .. . .. compression alg. list length ( 1 byte )
1280 * .. . .. compression alg. list
1281 * .. . .. extensions length ( 2 bytes, optional )
1282 * .. . .. extensions ( optional )
1283 */
1284
XiaokangQianb67384d2022-04-19 00:02:38 +00001285 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001286 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001287 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1288 * read at least up to session id length without worrying.
1289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291
1292 /* ...
1293 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1294 * ...
1295 * with ProtocolVersion defined as:
1296 * uint16 ProtocolVersion;
1297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1299 MBEDTLS_SSL_VERSION_TLS1_2) {
1300 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1301 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1302 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1303 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304 }
1305 p += 2;
1306
Jerry Yuc1be19f2022-04-23 16:11:39 +08001307 /* ...
1308 * Random random;
1309 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 * with Random defined as:
1311 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 */
Ronald Crond540d992023-03-07 09:41:48 +01001313 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001314 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315
Jerry Yuc1be19f2022-04-23 16:11:39 +08001316 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001317 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001318 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319 */
Ronald Croncada4102023-03-07 09:51:39 +01001320 legacy_session_id_len = *(p++);
1321 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001322
XiaokangQianb67384d2022-04-19 00:02:38 +00001323 /*
1324 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001325 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001326 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001328 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001330 /* ...
1331 * CipherSuite cipher_suites<2..2^16-2>;
1332 * ...
1333 * with CipherSuite defined as:
1334 * uint8 CipherSuite[2];
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001337 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001338 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001339
Ronald Cronfc7ae872023-02-16 15:32:19 +01001340 /*
1341 * The length of the ciphersuite list has to be even.
1342 */
1343 if (cipher_suites_len & 1) {
1344 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1345 MBEDTLS_ERR_SSL_DECODE_ERROR);
1346 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1347 }
1348
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 /* Check we have enough data for the ciphersuite list, the legacy
1350 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001351 *
1352 * cipher_suites cipher_suites_len bytes
1353 * legacy_compression_methods 2 bytes
1354 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001357 p += cipher_suites_len;
1358 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001359
1360 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001361 * Search for the supported versions extension and parse it to determine
1362 * if the client supports TLS 1.3.
1363 */
1364 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1365 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001366 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001367 if (ret < 0) {
1368 MBEDTLS_SSL_DEBUG_RET(1,
1369 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1370 return ret;
1371 }
1372
1373 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001374 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001375 }
1376
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001377 if (ret == 1) {
1378 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001379 supported_versions_data,
1380 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001381 if (ret < 0) {
1382 MBEDTLS_SSL_DEBUG_RET(1,
1383 ("ssl_tls13_parse_supported_versions_ext"), ret);
1384 return ret;
1385 }
1386
Ronald Cronb828c7d2023-04-03 16:37:22 +02001387 /*
1388 * The supported versions extension was parsed successfully as the
1389 * value returned by ssl_tls13_parse_supported_versions_ext() is
1390 * positive. The return value is then equal to
1391 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1392 * the TLS version to negotiate.
1393 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001394 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1395 return SSL_CLIENT_HELLO_TLS1_2;
1396 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001397 }
1398
1399 /*
1400 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001401 */
1402 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001403 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1404 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001405
1406 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001407 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001408 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001409 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001410 */
1411 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1412 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1413 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1414
Ronald Croncada4102023-03-07 09:51:39 +01001415 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1417 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1418 }
1419 ssl->session_negotiate->id_len = legacy_session_id_len;
1420 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1421 legacy_session_id, legacy_session_id_len);
1422 memcpy(&ssl->session_negotiate->id[0],
1423 legacy_session_id, legacy_session_id_len);
1424
Ronald Crond540d992023-03-07 09:41:48 +01001425 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001426 * Search for a matching ciphersuite
1427 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001428 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1429 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001430
Ronald Cron89089cc2024-02-14 18:18:08 +01001431 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1432 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (handshake->ciphersuite_info == NULL) {
1435 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1436 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1437 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001438 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001439 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1440
1441 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1442 ((unsigned) handshake->ciphersuite_info->id),
1443 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001444
XiaokangQian4080a7f2022-04-11 09:55:18 +00001445 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001446 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001447 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1450 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1451 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1452 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1453 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001455 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001456
Jerry Yuc1be19f2022-04-23 16:11:39 +08001457 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001458 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001459 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001460 * with Extension defined as:
1461 * struct {
1462 * ExtensionType extension_type;
1463 * opaque extension_data<0..2^16-1>;
1464 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001467 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001469 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001472 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001475 unsigned int extension_type;
1476 size_t extension_data_len;
1477 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001478 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1479
Ronald Cron5fbd2702024-02-14 10:03:36 +01001480 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001481 /* Do not accept early data extension in 2nd ClientHello */
1482 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1483 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001484
Jerry Yu0c354a22022-08-29 15:25:36 +08001485 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001486 *
1487 * The "pre_shared_key" extension MUST be the last extension in the
1488 * ClientHello (this facilitates implementation as described below).
1489 * Servers MUST check that it is the last extension and otherwise fail
1490 * the handshake with an "illegal_parameter" alert.
1491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001493 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001495 MBEDTLS_SSL_PEND_FATAL_ALERT(
1496 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1498 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001499 }
1500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1502 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1503 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001504 p += 4;
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507 extension_data_end = p + extension_data_len;
1508
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001509 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001511 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (ret != 0) {
1513 return ret;
1514 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001517#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1518 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1520 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1521 extension_data_end);
1522 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001523 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 1, "mbedtls_ssl_parse_servername_ext", ret);
1525 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001526 }
XiaokangQian40a35232022-05-07 09:02:40 +00001527 break;
1528#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1529
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001530#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001533
1534 /* Supported Groups Extension
1535 *
1536 * When sent by the client, the "supported_groups" extension
1537 * indicates the named groups which the client supports,
1538 * ordered from most preferred to least preferred.
1539 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001540 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 ssl, p, extension_data_end);
1542 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001543 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001544 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546 }
1547
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001549#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001550
Valerio Settic9ae8622023-07-25 11:23:50 +02001551#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001552 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554
1555 /*
1556 * Key Share Extension
1557 *
1558 * When sent by the client, the "key_share" extension
1559 * contains the endpoint's cryptographic parameters for
1560 * ECDHE/DHE key establishment methods.
1561 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001562 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ssl, p, extension_data_end);
1564 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001565 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1566 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001567 }
1568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001570 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 1, "ssl_tls13_parse_key_shares_ext", ret);
1572 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001573 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001574
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001576#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001577
1578 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001579 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001580 break;
1581
Ronald Cron41a443a2022-10-04 16:38:25 +02001582#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001583 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001584 MBEDTLS_SSL_DEBUG_MSG(
1585 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001586
1587 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 ssl, p, extension_data_end);
1589 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001590 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1592 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001593 }
1594
Jerry Yue19e3b92022-07-08 12:04:51 +00001595 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001596#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001597
Jerry Yu1c105562022-07-10 06:32:38 +00001598 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1600 if ((handshake->received_extensions &
1601 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001602 MBEDTLS_SSL_PEND_FATAL_ALERT(
1603 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1605 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001606 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001607#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001608 /* Delay processing of the PSK identity once we have
1609 * found out which algorithms to use. We keep a pointer
1610 * to the buffer and the size for later processing.
1611 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001612 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001613 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001614#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001615 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001616
XiaokangQianacb39922022-06-17 10:18:48 +00001617#if defined(MBEDTLS_SSL_ALPN)
1618 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1622 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001623 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1625 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001626 }
XiaokangQianacb39922022-06-17 10:18:48 +00001627 break;
1628#endif /* MBEDTLS_SSL_ALPN */
1629
Ronald Cron928cbd32022-10-04 16:14:26 +02001630#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001633
Gabor Mezei078e8032022-04-27 21:17:56 +02001634 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 ssl, p, extension_data_end);
1636 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001637 MBEDTLS_SSL_DEBUG_RET(
1638 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001640 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001642#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001643
Jan Bruckner151f6422023-02-10 12:45:19 +01001644#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1645 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1646 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1647
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001648 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1649 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001650 if (ret != 0) {
1651 MBEDTLS_SSL_DEBUG_RET(
1652 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1653 return ret;
1654 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001655 break;
1656#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1657
XiaokangQian7807f9f2022-02-15 10:04:37 +00001658 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001659 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001660 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001662 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001663 }
1664
1665 p += extension_data_len;
1666 }
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1669 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001670
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001671 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1672 MBEDTLS_SSL_HS_CLIENT_HELLO,
1673 p - buf);
1674 if (0 != ret) {
1675 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1676 return ret;
1677 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001678
Ronald Cron41a443a2022-10-04 16:38:25 +02001679#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680 /* Update checksum with either
1681 * - The entire content of the CH message, if no PSK extension is present
1682 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001683 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001684 * ClientHello even if some pre-requisites for PSK key exchange modes are
1685 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001687 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001688 ret = handshake->update_checksum(ssl, buf,
1689 pre_shared_key_ext - buf);
1690 if (0 != ret) {
1691 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1692 return ret;
1693 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1695 pre_shared_key_ext,
1696 pre_shared_key_ext_end,
1697 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001698 cipher_suites_end,
1699 &psk);
1700 if (ret == 0) {
1701 got_psk = 1;
1702 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001703 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1705 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001708#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001709 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001710 ret = handshake->update_checksum(ssl, buf, p - buf);
1711 if (0 != ret) {
1712 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1713 return ret;
1714 }
Jerry Yu1c105562022-07-10 06:32:38 +00001715 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001716
Ronald Cron79cdd412024-02-20 17:19:28 +01001717 /*
1718 * Determine the key exchange algorithm to use.
1719 * There are three types of key exchanges supported in TLS 1.3:
1720 * - (EC)DH with ECDSA,
1721 * - (EC)DH with PSK,
1722 * - plain PSK.
1723 *
1724 * The PSK-based key exchanges may additionally be used with 0-RTT.
1725 *
1726 * Our built-in order of preference is
1727 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1728 * 2 ) Certificate Mode ( ephemeral )
1729 * 3 ) Plain PSK Mode ( psk )
1730 */
1731#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1732 if (got_psk && (psk.key_exchange_mode ==
1733 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1734 handshake->key_exchange_mode =
1735 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1736 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1737
1738 } else
1739#endif
1740 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1741 handshake->key_exchange_mode =
1742 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1743 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1744
1745 }
1746#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1747 else if (got_psk && (psk.key_exchange_mode ==
1748 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1749 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1750 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1751 }
1752#endif
1753 else {
1754 MBEDTLS_SSL_DEBUG_MSG(
1755 1,
1756 ("ClientHello message misses mandatory extensions."));
1757 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1758 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1759 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001761
Ronald Cron3e47eec2024-02-21 10:29:24 +01001762#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001763 if (handshake->key_exchange_mode &
1764 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1765 handshake->ciphersuite_info = psk.ciphersuite_info;
1766 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1767
1768 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1769 ((unsigned) psk.ciphersuite_info->id),
1770 psk.ciphersuite_info->name));
1771
1772 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1773 handshake->resume = 1;
1774 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001775 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001776#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001777
1778 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001779 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1780 hrr_required = (no_usable_share_for_key_agreement != 0);
1781 }
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001786}
1787
Jerry Yuab0da372023-02-08 13:55:24 +08001788#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001789static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001790{
1791 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1792
Jerry Yu985c9672022-12-04 14:06:30 +08001793 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1794 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001795 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001796 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001797 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001798 }
1799
Jerry Yu454dda32023-10-31 15:13:54 +08001800 if (!handshake->resume) {
1801 /* We currently support early data only in the case of PSKs established
1802 via a NewSessionTicket message thus in the case of a session
1803 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001804 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001805 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001806 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001807 }
1808
Jerry Yu82fd6c12023-10-31 16:32:19 +08001809 /* RFC 8446 4.2.10
1810 *
1811 * In order to accept early data, the server MUST have accepted a PSK cipher
1812 * suite and selected the first key offered in the client's "pre_shared_key"
1813 * extension. In addition, it MUST verify that the following values are the
1814 * same as those associated with the selected PSK:
1815 * - The TLS version number
1816 * - The selected cipher suite
1817 * - The selected ALPN [RFC7301] protocol, if any
1818 *
1819 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001820 * - The TLS version number is checked in
1821 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001822 */
1823
1824 if (handshake->selected_identity != 0) {
1825 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001826 1, ("EarlyData: rejected, the selected key in "
1827 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001828 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001829 }
1830
1831 if (handshake->ciphersuite_info->id !=
1832 ssl->session_negotiate->ciphersuite) {
1833 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001834 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1835 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001836 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001837
1838 }
Jerry Yu985c9672022-12-04 14:06:30 +08001839
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001840 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001841 MBEDTLS_SSL_DEBUG_MSG(
1842 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001843 ("EarlyData: rejected, early_data not allowed in ticket "
1844 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001845 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001846 }
Jerry Yu985c9672022-12-04 14:06:30 +08001847
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001848#if defined(MBEDTLS_SSL_ALPN)
1849 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1850 size_t alpn_len;
1851
1852 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1853 return 0;
1854 }
1855
1856 if (alpn != NULL) {
1857 alpn_len = strlen(alpn);
1858 }
1859
1860 if (alpn == NULL ||
1861 ssl->session_negotiate->ticket_alpn == NULL ||
1862 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1863 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1864 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1865 "from the one associated with the pre-shared key."));
1866 return -1;
1867 }
1868#endif
1869
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001870 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001871}
1872#endif /* MBEDTLS_SSL_EARLY_DATA */
1873
XiaokangQian75fe8c72022-06-15 09:42:45 +00001874/* Update the handshake state machine */
1875
Ronald Cronce7d76e2022-07-08 18:56:49 +02001876MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001877static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1878 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001879{
1880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1881
XiaokangQian7807f9f2022-02-15 10:04:37 +00001882 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001883 * Server certificate selection
1884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1886 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1887 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001888 }
1889#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1890 ssl->handshake->sni_name = NULL;
1891 ssl->handshake->sni_name_len = 0;
1892#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1895 if (ret != 0) {
1896 MBEDTLS_SSL_DEBUG_RET(1,
1897 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1898 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001899 }
1900
Jerry Yuab0da372023-02-08 13:55:24 +08001901#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001902 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001903 ssl->handshake->early_data_accepted =
1904 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001905
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001906 if (ssl->handshake->early_data_accepted) {
1907 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1908 if (ret != 0) {
1909 MBEDTLS_SSL_DEBUG_RET(
1910 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1911 return ret;
1912 }
1913 } else {
1914 ssl->discard_early_data_record =
1915 hrr_required ?
1916 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1917 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001918 }
1919 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001920#else
1921 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001922#endif /* MBEDTLS_SSL_EARLY_DATA */
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001925}
1926
1927/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001928 * Main entry point from the state machine; orchestrates the otherfunctions.
1929 */
1930
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001931MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001932static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001933{
1934
XiaokangQian08037552022-04-20 07:16:41 +00001935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001937 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001938 int parse_client_hello_ret;
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1943 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1944 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1947 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001948 parse_client_hello_ret = ret; /* Store positive return value of
1949 * parse_client_hello,
1950 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001951 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001952
Ronald Cronb828c7d2023-04-03 16:37:22 +02001953 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001954 * Version 1.2 of the protocol has to be used for the handshake.
1955 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001956 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1957 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1958 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1959 * will dispatch to the TLS 1.2 state machine.
1960 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001961 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001962 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001963 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001964 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001965 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001966 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001967 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1968 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1969 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001970 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001971 ssl->keep_current_message = 1;
1972 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1973 return 0;
1974 }
1975
Ronald Cron7b6ee942024-01-12 10:29:55 +01001976 MBEDTLS_SSL_PROC_CHK(
1977 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1978 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001979
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001980 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1982 } else {
1983 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1984 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001985
1986cleanup:
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1989 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001990}
1991
1992/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001993 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001994 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001995MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001996static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001997{
Jerry Yu637a3f12022-04-20 21:37:58 +08001998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1999 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2003 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2004 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2005 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002006 }
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2009 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002010
2011#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002012 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002013#endif /* MBEDTLS_HAVE_TIME */
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002016}
2017
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002019 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020 *
2021 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002022 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023 * } SupportedVersions;
2024 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002025MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002026static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 mbedtls_ssl_context *ssl,
2028 unsigned char *buf,
2029 unsigned char *end,
2030 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032 *out_len = 0;
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
2036 /* Check if we have space to write the extension:
2037 * - extension_type (2 bytes)
2038 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002039 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 mbedtls_ssl_write_version(buf + 4,
2048 ssl->conf->transport,
2049 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2052 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053
Jerry Yu349a6132022-04-14 20:52:56 +08002054 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055
Jerry Yub95dd362022-11-08 21:19:34 +08002056 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060}
2061
Jerry Yud9436a12022-04-20 22:28:09 +08002062
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063
2064/* Generate and export a single key share. For hybrid KEMs, this can
2065 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002067static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2068 uint16_t named_group,
2069 unsigned char *buf,
2070 unsigned char *end,
2071 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072{
2073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002074
2075 *out_len = 0;
2076
Valerio Settic9ae8622023-07-25 11:23:50 +02002077#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002078 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002079 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002080 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 ssl, named_group, buf, end, out_len);
2082 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002083 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002084 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 ret);
2086 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002089#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (0 /* Other kinds of KEMs */) {
2091 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002092 ((void) ssl);
2093 ((void) named_group);
2094 ((void) buf);
2095 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2097 }
2098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100}
2101
2102/*
2103 * ssl_tls13_write_key_share_ext
2104 *
2105 * Structure of key_share extension in ServerHello:
2106 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002107 * struct {
2108 * NamedGroup group;
2109 * opaque key_exchange<1..2^16-1>;
2110 * } KeyShareEntry;
2111 * struct {
2112 * KeyShareEntry server_share;
2113 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002116static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2117 unsigned char *buf,
2118 unsigned char *end,
2119 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120{
Jerry Yu955ddd72022-04-22 22:27:33 +08002121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002123 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126
2127 *out_len = 0;
2128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2132 mbedtls_ssl_named_group_to_str(group),
2133 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002134
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135 /* Check if we have space for header and length fields:
2136 * - extension_type (2 bytes)
2137 * - extension_data_length (2 bytes)
2138 * - group (2 bytes)
2139 * - key_exchange_length (2 bytes)
2140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2142 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2143 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002145
Jerry Yu3bf2c642022-03-30 22:02:12 +08002146 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2147 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002148 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 ssl, group, server_share + 4, end, &key_exchange_length);
2150 if (ret != 0) {
2151 return ret;
2152 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002158
Jerry Yu57d48412022-04-20 21:50:42 +08002159 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164}
Jerry Yud9436a12022-04-20 22:28:09 +08002165
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002166MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002167static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2168 unsigned char *buf,
2169 unsigned char *end,
2170 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002171{
2172 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2173 /* key_share Extension
2174 *
2175 * struct {
2176 * select (Handshake.msg_type) {
2177 * ...
2178 * case hello_retry_request:
2179 * NamedGroup selected_group;
2180 * ...
2181 * };
2182 * } KeyShare;
2183 */
2184
2185 *out_len = 0;
2186
Jerry Yub0ac10b2022-05-05 11:10:08 +08002187 /*
2188 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2189 * of the HRR is then to transmit a cookie to force the client to demonstrate
2190 * reachability at their apparent network address (primarily useful for DTLS).
2191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2193 return 0;
2194 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195
2196 /* We should only send the key_share extension if the client's initial
2197 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (ssl->handshake->offered_group_id != 0) {
2199 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2200 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201 }
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (selected_group == 0) {
2204 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2205 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002206 }
2207
Jerry Yub0ac10b2022-05-05 11:10:08 +08002208 /* Check if we have enough space:
2209 * - extension_type (2 bytes)
2210 * - extension_data_length (2 bytes)
2211 * - selected_group (2 bytes)
2212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2216 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2217 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 MBEDTLS_SSL_DEBUG_MSG(3,
2220 ("HRR selected_group: %s (%x)",
2221 mbedtls_ssl_named_group_to_str(selected_group),
2222 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002223
2224 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002229}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002230
2231/*
2232 * Structure of ServerHello message:
2233 *
2234 * struct {
2235 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2236 * Random random;
2237 * opaque legacy_session_id_echo<0..32>;
2238 * CipherSuite cipher_suite;
2239 * uint8 legacy_compression_method = 0;
2240 * Extension extensions<6..2^16-1>;
2241 * } ServerHello;
2242 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002243MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002244static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2245 unsigned char *buf,
2246 unsigned char *end,
2247 size_t *out_len,
2248 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002249{
Jerry Yu955ddd72022-04-22 22:27:33 +08002250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002252 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002253 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002254
2255 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002256 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257
Jerry Yucfc04b32022-04-21 09:31:58 +08002258 /* ...
2259 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2260 * ...
2261 * with ProtocolVersion defined as:
2262 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2265 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002266 p += 2;
2267
Jerry Yu1c3e6882022-04-20 21:23:40 +08002268 /* ...
2269 * Random random;
2270 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002271 * with Random defined as:
2272 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2275 if (is_hrr) {
2276 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2277 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2278 } else {
2279 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2280 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2283 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002284 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2285
Jerry Yucfc04b32022-04-21 09:31:58 +08002286 /* ...
2287 * opaque legacy_session_id_echo<0..32>;
2288 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2291 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2292 if (ssl->session_negotiate->id_len > 0) {
2293 memcpy(p, &ssl->session_negotiate->id[0],
2294 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002295 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2298 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002299 }
2300
Jerry Yucfc04b32022-04-21 09:31:58 +08002301 /* ...
2302 * CipherSuite cipher_suite;
2303 * ...
2304 * with CipherSuite defined as:
2305 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2308 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 MBEDTLS_SSL_DEBUG_MSG(3,
2311 ("server hello, chosen ciphersuite: %s ( id=%d )",
2312 mbedtls_ssl_get_ciphersuite_name(
2313 ssl->session_negotiate->ciphersuite),
2314 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002315
Jerry Yucfc04b32022-04-21 09:31:58 +08002316 /* ...
2317 * uint8 legacy_compression_method = 0;
2318 * ...
2319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002321 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002322
Jerry Yucfc04b32022-04-21 09:31:58 +08002323 /* ...
2324 * Extension extensions<6..2^16-1>;
2325 * ...
2326 * struct {
2327 * ExtensionType extension_type; (2 bytes)
2328 * opaque extension_data<0..2^16-1>;
2329 * } Extension;
2330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002332 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002333 p += 2;
2334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2336 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002337 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2339 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002340 }
2341 p += output_len;
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2344 if (is_hrr) {
2345 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2346 } else {
2347 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2348 }
2349 if (ret != 0) {
2350 return ret;
2351 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002352 p += output_len;
2353 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002354
Ronald Cron41a443a2022-10-04 16:38:25 +02002355#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2357 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2358 if (ret != 0) {
2359 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2360 ret);
2361 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002362 }
2363 p += output_len;
2364 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002365#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2370 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002371
Jerry Yud9436a12022-04-20 22:28:09 +08002372 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002375
Jerry Yu7de2ff02022-11-08 21:43:46 +08002376 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002377 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 MBEDTLS_SSL_HS_SERVER_HELLO,
2379 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002382}
2383
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002384MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002385static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002386{
2387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2389 if (ret != 0) {
2390 MBEDTLS_SSL_DEBUG_RET(1,
2391 "mbedtls_ssl_tls13_compute_handshake_transform",
2392 ret);
2393 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002394 }
2395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002397}
2398
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002399MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002400static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002401{
Jerry Yu637a3f12022-04-20 21:37:58 +08002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002403 unsigned char *buf;
2404 size_t buf_len, msg_len;
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002410 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2411 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2414 buf + buf_len,
2415 &msg_len,
2416 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002417
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002418 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002419 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2422 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002423
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002424 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002425
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002426#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2427 /* The server sends a dummy change_cipher_spec record immediately
2428 * after its first handshake message. This may either be after
2429 * a ServerHello or a HelloRetryRequest.
2430 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002431 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002433#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002435#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002436
Jerry Yuf4b27e42022-03-30 17:32:21 +08002437cleanup:
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2440 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002441}
2442
Jerry Yu23d1a252022-05-12 18:08:59 +08002443
2444/*
2445 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2446 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002447MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002448static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002449{
2450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002451 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2453 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2454 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2455 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002456 }
2457
2458 /*
2459 * Create stateless transcript hash for HRR
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2462 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2463 if (ret != 0) {
2464 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2465 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002466 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002470}
2471
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002472MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002473static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002474{
2475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2476 unsigned char *buf;
2477 size_t buf_len, msg_len;
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2484 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2485 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2488 buf + buf_len,
2489 &msg_len,
2490 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002491 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002492 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002493
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2496 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002497
Ronald Cron5fbd2702024-02-14 10:03:36 +01002498 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002499
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002500#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2501 /* The server sends a dummy change_cipher_spec record immediately
2502 * after its first handshake message. This may either be after
2503 * a ServerHello or a HelloRetryRequest.
2504 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002505 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002507#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002509#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002510
2511cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2513 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002514}
2515
Jerry Yu5b64ae92022-03-30 17:15:02 +08002516/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002517 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2518 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002519
2520/*
2521 * struct {
2522 * Extension extensions<0..2 ^ 16 - 1>;
2523 * } EncryptedExtensions;
2524 *
2525 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002527static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2528 unsigned char *buf,
2529 unsigned char *end,
2530 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531{
XiaokangQianacb39922022-06-17 10:18:48 +00002532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002533 unsigned char *p = buf;
2534 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002535 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002536 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002537
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538 *out_len = 0;
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002541 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542 p += 2;
2543
Jerry Yu8937eb42022-05-03 12:12:14 +08002544 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002545 ((void) ret);
2546 ((void) output_len);
2547
2548#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2550 if (ret != 0) {
2551 return ret;
2552 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002553 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002554#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002555
Jerry Yu71c14f12022-12-12 11:10:35 +08002556#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002557 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002558 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002559 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002560 if (ret != 0) {
2561 return ret;
2562 }
2563 p += output_len;
2564 }
2565#endif /* MBEDTLS_SSL_EARLY_DATA */
2566
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002567#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002568 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002569 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2570 ssl, p, end, &output_len);
2571 if (ret != 0) {
2572 return ret;
2573 }
2574 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002575 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002576#endif
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 extensions_len = (p - p_extensions_len) - 2;
2579 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002580
2581 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002584
Jerry Yu7de2ff02022-11-08 21:43:46 +08002585 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002589}
2590
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002591MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002592static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593{
2594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2595 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002596 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 mbedtls_ssl_set_outbound_transform(ssl,
2599 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002600 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002604
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002605 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2606 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2607 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2610 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002611
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002612 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002613 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2614 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2617 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002618
Ronald Cron928cbd32022-10-04 16:14:26 +02002619#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2621 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2622 } else {
2623 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2624 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002625#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002627#endif
2628
Jerry Yu4d3841a2022-04-16 12:37:19 +08002629cleanup:
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2632 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002633}
2634
Ronald Cron928cbd32022-10-04 16:14:26 +02002635#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002636#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2637#define SSL_CERTIFICATE_REQUEST_SKIP 1
2638/* Coordination:
2639 * Check whether a CertificateRequest message should be written.
2640 * Returns a negative code on failure, or
2641 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2642 * - SSL_CERTIFICATE_REQUEST_SKIP
2643 * indicating if the writing of the CertificateRequest
2644 * should be skipped or not.
2645 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002646MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002647static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002648{
2649 int authmode;
2650
XiaokangQian40a35232022-05-07 09:02:40 +00002651#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002653 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002655#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002656 authmode = ssl->conf->authmode;
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002659 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002661 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002662
XiaokangQianc3017f62022-05-13 05:55:41 +00002663 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002666}
2667
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668/*
2669 * struct {
2670 * opaque certificate_request_context<0..2^8-1>;
2671 * Extension extensions<2..2^16-1>;
2672 * } CertificateRequest;
2673 *
2674 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002675MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002676static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2677 unsigned char *buf,
2678 const unsigned char *end,
2679 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680{
2681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2682 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002683 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684 unsigned char *p_extensions_len;
2685
2686 *out_len = 0;
2687
2688 /* Check if we have enough space:
2689 * - certificate_request_context (1 byte)
2690 * - extensions length (2 bytes)
2691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693
2694 /*
2695 * Write certificate_request_context
2696 */
2697 /*
2698 * We use a zero length context for the normal handshake
2699 * messages. For post-authentication handshake messages
2700 * this request context would be set to a non-zero value.
2701 */
2702 *p++ = 0x0;
2703
2704 /*
2705 * Write extensions
2706 */
2707 /* The extensions must contain the signature_algorithms. */
2708 p_extensions_len = p;
2709 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2711 if (ret != 0) {
2712 return ret;
2713 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714
XiaokangQianec6efb92022-05-06 09:53:10 +00002715 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717
2718 *out_len = p - buf;
2719
Jerry Yu7de2ff02022-11-08 21:43:46 +08002720 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002724}
2725
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002726MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002727static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002728{
2729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002736 unsigned char *buf;
2737 size_t buf_len, msg_len;
2738
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002739 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2740 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2741 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2744 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002745
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002746 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002747 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2748 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2751 ssl, buf_len, msg_len));
2752 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2753 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 } else {
2756 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002757 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2758 goto cleanup;
2759 }
2760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002762cleanup:
2763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2765 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002766}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002767
Jerry Yu4d3841a2022-04-16 12:37:19 +08002768/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002769 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002770 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002771MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002772static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002773{
Jerry Yu5a26f302022-05-10 20:46:40 +08002774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002775
2776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2778 mbedtls_ssl_own_cert(ssl) == NULL) {
2779 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2780 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2781 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2782 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002783 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002784#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2787 if (ret != 0) {
2788 return ret;
2789 }
2790 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2791 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002792}
2793
2794/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002795 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002796 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002797MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002798static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002799{
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2801 if (ret != 0) {
2802 return ret;
2803 }
2804 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2805 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002806}
Ronald Cron928cbd32022-10-04 16:14:26 +02002807#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002808
Jerry Yu9b72e392023-12-01 16:27:08 +08002809/*
2810 * RFC 8446 section A.2
2811 *
2812 * | Send ServerHello
2813 * | K_send = handshake
2814 * | Send EncryptedExtensions
2815 * | [Send CertificateRequest]
2816 * Can send | [Send Certificate + CertificateVerify]
2817 * app data | Send Finished
2818 * after --> | K_send = application
2819 * here +--------+--------+
2820 * No 0-RTT | | 0-RTT
2821 * | |
2822 * K_recv = handshake | | K_recv = early data
2823 * [Skip decrypt errors] | +------> WAIT_EOED -+
2824 * | | Recv | | Recv EndOfEarlyData
2825 * | | early data | | K_recv = handshake
2826 * | +------------+ |
2827 * | |
2828 * +> WAIT_FLIGHT2 <--------+
2829 * |
2830 * +--------+--------+
2831 * No auth | | Client auth
2832 * | |
2833 * | v
2834 * | WAIT_CERT
2835 * | Recv | | Recv Certificate
2836 * | empty | v
2837 * | Certificate | WAIT_CV
2838 * | | | Recv
2839 * | v | CertificateVerify
2840 * +-> WAIT_FINISHED <---+
2841 * | Recv Finished
2842 *
2843 *
2844 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002845 * above diagram. We are not going to receive early data related messages
2846 * anymore, prepare to receive the first handshake message of the client
2847 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002848 */
Jerry Yu3be85072023-12-04 09:58:54 +08002849static void ssl_tls13_prepare_for_handshake_second_flight(
2850 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002851{
Jerry Yu9b72e392023-12-01 16:27:08 +08002852 if (ssl->handshake->certificate_request_sent) {
2853 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2854 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002855 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002856 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002857
Jerry Yu9b72e392023-12-01 16:27:08 +08002858 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2859 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002860}
2861
Jerry Yu83da34e2022-04-16 13:59:52 +08002862/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002863 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002864 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002866static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002867{
Jerry Yud6e253d2022-05-18 13:59:24 +08002868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2871 if (ret != 0) {
2872 return ret;
2873 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2876 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002877 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2879 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2880 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002881 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002882
Jerry Yu87b5ed42022-12-12 13:07:07 +08002883#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002884 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002885 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002886 MBEDTLS_SSL_DEBUG_MSG(
2887 1, ("Switch to early keys for inbound traffic. "
2888 "( K_recv = early data )"));
2889 mbedtls_ssl_set_inbound_transform(
2890 ssl, ssl->handshake->transform_earlydata);
2891 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2892 return 0;
2893 }
2894#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002895 MBEDTLS_SSL_DEBUG_MSG(
2896 1, ("Switch to handshake keys for inbound traffic "
2897 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002899
Jerry Yu3be85072023-12-04 09:58:54 +08002900 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002901
2902 return 0;
2903}
2904
Jerry Yu87b5ed42022-12-12 13:07:07 +08002905#if defined(MBEDTLS_SSL_EARLY_DATA)
2906/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002907 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002908 */
Jerry Yu3be85072023-12-04 09:58:54 +08002909#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002910#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002911/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002912 * Deals with the ambiguity of not knowing if the next message is an
2913 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002914 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002915 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002916 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002917 * indicating which message is received.
2918 */
2919MBEDTLS_CHECK_RETURN_CRITICAL
2920static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2921{
2922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002923
2924 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2925 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2926 return ret;
2927 }
2928 ssl->keep_current_message = 1;
2929
2930 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2931 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002932 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002933 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002934 }
2935
2936 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002937 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002938 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002939 /* Set the reading pointer */
2940 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002941 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2942 if (ret != 0) {
2943 return ret;
2944 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002945 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002946 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002947 }
2948
Jerry Yu7bb40a32023-12-04 10:04:15 +08002949 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2950 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002951 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002952}
2953
2954MBEDTLS_CHECK_RETURN_CRITICAL
2955static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2956 const unsigned char *buf,
2957 const unsigned char *end)
2958{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002959 /* RFC 8446 section 4.5
2960 *
2961 * struct {} EndOfEarlyData;
2962 */
Jerry Yufbf03992023-12-04 10:00:37 +08002963 if (buf != end) {
2964 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2965 MBEDTLS_ERR_SSL_DECODE_ERROR);
2966 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2967 }
2968 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002969}
2970
Jerry Yud5c34962023-12-01 16:32:31 +08002971/*
2972 * RFC 8446 section A.2
2973 *
2974 * | Send ServerHello
2975 * | K_send = handshake
2976 * | Send EncryptedExtensions
2977 * | [Send CertificateRequest]
2978 * Can send | [Send Certificate + CertificateVerify]
2979 * app data | Send Finished
2980 * after --> | K_send = application
2981 * here +--------+--------+
2982 * No 0-RTT | | 0-RTT
2983 * | |
2984 * K_recv = handshake | | K_recv = early data
2985 * [Skip decrypt errors] | +------> WAIT_EOED -+
2986 * | | Recv | | Recv EndOfEarlyData
2987 * | | early data | | K_recv = handshake
2988 * | +------------+ |
2989 * | |
2990 * +> WAIT_FLIGHT2 <--------+
2991 * |
2992 * +--------+--------+
2993 * No auth | | Client auth
2994 * | |
2995 * | v
2996 * | WAIT_CERT
2997 * | Recv | | Recv Certificate
2998 * | empty | v
2999 * | Certificate | WAIT_CV
3000 * | | | Recv
3001 * | v | CertificateVerify
3002 * +-> WAIT_FINISHED <---+
3003 * | Recv Finished
3004 *
3005 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3006 * the above diagram.
3007 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003008MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003009static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003010{
3011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003012
3013 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3014
3015 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3016
Jerry Yu3be85072023-12-04 09:58:54 +08003017 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003018 unsigned char *buf;
3019 size_t buf_len;
3020
3021 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3022 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3023 &buf, &buf_len));
3024
3025 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3026 ssl, buf, buf + buf_len));
3027
Jerry Yue9655122023-12-01 16:44:40 +08003028 MBEDTLS_SSL_DEBUG_MSG(
3029 1, ("Switch to handshake keys for inbound traffic"
3030 "( K_recv = handshake )"));
3031 mbedtls_ssl_set_inbound_transform(
3032 ssl, ssl->handshake->transform_handshake);
3033
Jerry Yud5c34962023-12-01 16:32:31 +08003034 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3035 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3036 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003037
Jerry Yu3be85072023-12-04 09:58:54 +08003038 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003039
Jerry Yub55f9eb2023-12-05 10:27:17 +08003040 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003041 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3042 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003043 } else {
3044 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3045 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3046 goto cleanup;
3047 }
3048
Jerry Yud5c34962023-12-01 16:32:31 +08003049cleanup:
3050 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003051 return ret;
3052}
3053#endif /* MBEDTLS_SSL_EARLY_DATA */
3054
Jerry Yu69dd8d42022-04-16 12:51:26 +08003055/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003056 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003057 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003059static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003060{
Jerry Yud6e253d2022-05-18 13:59:24 +08003061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3064 if (ret != 0) {
3065 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003066 }
3067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3069 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003070 MBEDTLS_SSL_DEBUG_RET(
3071 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 }
3073
3074 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3075 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003076}
3077
3078/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003079 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003080 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003081MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003082static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003083{
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003087
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003088#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3089 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003090/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3091 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3092 * expected to be resolved with issue#6395.
3093 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003094 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003095 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003096 mbedtls_ssl_handshake_set_state(
3097 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003098 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003099#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003100 {
3101 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003104}
3105
Norbert Fabritius96eed722023-01-23 15:24:59 +01003106#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003107/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003108 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003109 */
3110#define SSL_NEW_SESSION_TICKET_SKIP 0
3111#define SSL_NEW_SESSION_TICKET_WRITE 1
3112MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003113static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003114{
3115 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ssl->conf->f_ticket_write == NULL) {
3117 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3118 " callback is not set"));
3119 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (ssl->conf->new_session_tickets_count == 0) {
3122 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3123 " configured count is zero"));
3124 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003125 }
3126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (ssl->handshake->new_session_tickets_count == 0) {
3128 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3129 "been sent."));
3130 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003131 }
3132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003134}
3135
Jerry Yue67bef42022-07-07 07:29:42 +00003136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003137static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3138 unsigned char *ticket_nonce,
3139 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003140{
3141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3142 mbedtls_ssl_session *session = ssl->session;
3143 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3144 psa_algorithm_t psa_hash_alg;
3145 int hash_length;
3146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003148
Pengyu Lv9f926952022-11-17 15:22:33 +08003149 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003150 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003151 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003152#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003153 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003154 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003155#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003156
3157#if defined(MBEDTLS_SSL_EARLY_DATA)
3158 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3159 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003160 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003161 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003162 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003163 }
3164#endif /* MBEDTLS_SSL_EARLY_DATA */
3165
Pengyu Lv4938a562023-01-16 11:28:49 +08003166 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003167
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003168#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003169 if (session->ticket_alpn == NULL) {
3170 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3171 if (ret != 0) {
3172 return ret;
3173 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003174 }
3175#endif
3176
Jerry Yue67bef42022-07-07 07:29:42 +00003177 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3179 (unsigned char *) &session->ticket_age_add,
3180 sizeof(session->ticket_age_add)) != 0)) {
3181 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3182 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3185 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003186
3187 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3189 if (ret != 0) {
3190 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3191 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3194 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003195
3196 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003198 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3200 if (hash_length == -1 ||
3201 (size_t) hash_length > sizeof(session->resumption_key)) {
3202 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003203 }
3204
Jerry Yue67bef42022-07-07 07:29:42 +00003205 /* In this code the psk key length equals the length of the hash */
3206 session->resumption_key_len = hash_length;
3207 session->ciphersuite = ciphersuite_info->id;
3208
3209 /* Compute resumption key
3210 *
3211 * HKDF-Expand-Label( resumption_master_secret,
3212 * "resumption", ticket_nonce, Hash.length )
3213 */
3214 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 psa_hash_alg,
3216 session->app_secrets.resumption_master_secret,
3217 hash_length,
3218 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3219 ticket_nonce,
3220 ticket_nonce_size,
3221 session->resumption_key,
3222 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003223
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 if (ret != 0) {
3225 MBEDTLS_SSL_DEBUG_RET(2,
3226 "Creating the ticket-resumed PSK failed",
3227 ret);
3228 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3231 session->resumption_key,
3232 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3235 session->app_secrets.resumption_master_secret,
3236 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003239}
3240
3241/* This function creates a NewSessionTicket message in the following format:
3242 *
3243 * struct {
3244 * uint32 ticket_lifetime;
3245 * uint32 ticket_age_add;
3246 * opaque ticket_nonce<0..255>;
3247 * opaque ticket<1..2^16-1>;
3248 * Extension extensions<0..2^16-2>;
3249 * } NewSessionTicket;
3250 *
3251 * The ticket inside the NewSessionTicket message is an encrypted container
3252 * carrying the necessary information so that the server is later able to
3253 * re-start the communication.
3254 *
3255 * The following fields are placed inside the ticket by the
3256 * f_ticket_write() function:
3257 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003258 * - creation time (ticket_creation_time)
3259 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003260 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003261 * - key (resumption_key)
3262 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003263 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003264 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003265 */
3266MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003267static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3268 unsigned char *buf,
3269 unsigned char *end,
3270 size_t *out_len,
3271 unsigned char *ticket_nonce,
3272 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003273{
3274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3275 unsigned char *p = buf;
3276 mbedtls_ssl_session *session = ssl->session;
3277 size_t ticket_len;
3278 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003279 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003280
3281 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003283
3284 /*
3285 * ticket_lifetime 4 bytes
3286 * ticket_age_add 4 bytes
3287 * ticket_nonce 1 + ticket_nonce_size bytes
3288 * ticket >=2 bytes
3289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003291
3292 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003293#if defined(MBEDTLS_HAVE_TIME)
3294 session->ticket_creation_time = mbedtls_ms_time();
3295#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3297 session,
3298 p + 9 + ticket_nonce_size + 2,
3299 end,
3300 &ticket_len,
3301 &ticket_lifetime);
3302 if (ret != 0) {
3303 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3304 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003305 }
Jerry Yuce794882023-11-22 15:01:18 +08003306
3307 /* RFC 8446 section 4.6.1
3308 *
Jerry Yue67bef42022-07-07 07:29:42 +00003309 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003310 * unsigned integer in network byte order from the time of ticket
3311 * issuance. Servers MUST NOT use any value greater than
3312 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003313 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003314 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003315 MBEDTLS_SSL_DEBUG_MSG(
3316 1, ("Ticket lifetime (%u) is greater than 7 days.",
3317 (unsigned int) ticket_lifetime));
3318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 }
Jerry Yuce794882023-11-22 15:01:18 +08003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3322 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3323 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003324
3325 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3327 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3328 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003329
3330 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 p[8] = (unsigned char) ticket_nonce_size;
3332 if (ticket_nonce_size > 0) {
3333 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003334 }
3335 p += 9 + ticket_nonce_size;
3336
3337 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003339 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003341 p += ticket_len;
3342
3343 /* Ticket Extensions
3344 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003345 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003346 */
Jerry Yuedab6372022-10-31 14:37:31 +08003347 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003350 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003351 p += 2;
3352
Jerry Yu01da35e2022-12-12 15:09:22 +08003353#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003354 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003355 size_t output_len;
3356
Jerry Yuf135bac2023-11-23 18:10:51 +08003357 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003358 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003359 MBEDTLS_SSL_DEBUG_RET(
3360 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3361 return ret;
3362 }
3363 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003364 } else {
3365 MBEDTLS_SSL_DEBUG_MSG(
3366 4, ("early_data not allowed, "
3367 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003368 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003369
Jerry Yu01da35e2022-12-12 15:09:22 +08003370#endif /* MBEDTLS_SSL_EARLY_DATA */
3371
3372 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3373
Jerry Yue67bef42022-07-07 07:29:42 +00003374 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3376 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003377
Jerry Yu7de2ff02022-11-08 21:43:46 +08003378 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003382}
3383
3384/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003385 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003387static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003388{
3389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003394 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3395 unsigned char *buf;
3396 size_t buf_len, msg_len;
3397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3399 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003400
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003401 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3402 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3403 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3406 ssl, buf, buf + buf_len, &msg_len,
3407 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3410 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003411
Jerry Yu359e65f2022-09-22 23:47:43 +08003412 /* Limit session tickets count to one when resumption connection.
3413 *
3414 * See document of mbedtls_ssl_conf_new_session_tickets.
3415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003417 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003419 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003421
Jerry Yua8d3c502022-10-30 14:51:23 +08003422 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3424 } else {
3425 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003426 }
3427
Jerry Yue67bef42022-07-07 07:29:42 +00003428cleanup:
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003431}
3432#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3433
3434/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003435 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003438{
XiaokangQian08037552022-04-20 07:16:41 +00003439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3442 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3443 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003446 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003450 /* start state */
3451 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003453 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003454 break;
3455
XiaokangQian7807f9f2022-02-15 10:04:37 +00003456 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_process_client_hello(ssl);
3458 if (ret != 0) {
3459 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3460 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003461 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003462
Jerry Yuf41553b2022-05-09 22:20:30 +08003463 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ret = ssl_tls13_write_hello_retry_request(ssl);
3465 if (ret != 0) {
3466 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3467 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003468 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003469 break;
3470
Jerry Yu5b64ae92022-03-30 17:15:02 +08003471 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003473 break;
3474
Xiaofei Baicba64af2022-02-15 10:00:56 +00003475 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 ret = ssl_tls13_write_encrypted_extensions(ssl);
3477 if (ret != 0) {
3478 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3479 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003480 }
Jerry Yu48330562022-05-06 21:35:44 +08003481 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003482
Ronald Cron928cbd32022-10-04 16:14:26 +02003483#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003484 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003486 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003487
Jerry Yu83da34e2022-04-16 13:59:52 +08003488 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003490 break;
3491
3492 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003494 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003495#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003496
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 /*
3498 * Injection of dummy-CCS's for middlebox compatibility
3499 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003500#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003501 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3503 if (ret == 0) {
3504 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3505 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003506 break;
3507
3508 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003509 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3510 if (ret != 0) {
3511 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003513 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003514 break;
3515#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3516
Jerry Yu69dd8d42022-04-16 12:51:26 +08003517 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003519 break;
3520
Jerry Yu87b5ed42022-12-12 13:07:07 +08003521#if defined(MBEDTLS_SSL_EARLY_DATA)
3522 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003523 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003524 break;
3525#endif /* MBEDTLS_SSL_EARLY_DATA */
3526
Jerry Yu69dd8d42022-04-16 12:51:26 +08003527 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003529 break;
3530
Jerry Yu69dd8d42022-04-16 12:51:26 +08003531 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003533 break;
3534
Ronald Cron766c0cd2022-10-18 12:17:11 +02003535#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003536 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3538 if (ret == 0) {
3539 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003540 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3542 } else {
3543 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003544 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003546 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003547 }
3548 break;
3549
3550 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3552 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003553 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003555 }
3556 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003557#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003558
Jerry Yue67bef42022-07-07 07:29:42 +00003559#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003560 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 ret = ssl_tls13_write_new_session_ticket(ssl);
3562 if (ret != 0) {
3563 MBEDTLS_SSL_DEBUG_RET(1,
3564 "ssl_tls13_write_new_session_ticket ",
3565 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003566 }
3567 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003568 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003569 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003570 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003571 * as part of ssl_prepare_handshake_step.
3572 */
3573 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003574
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 if (ssl->handshake->new_session_tickets_count == 0) {
3576 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3577 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003578 mbedtls_ssl_handshake_set_state(
3579 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 }
Jerry Yue67bef42022-07-07 07:29:42 +00003581 break;
3582
3583#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3584
XiaokangQian7807f9f2022-02-15 10:04:37 +00003585 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3587 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003588 }
3589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003591}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003592
Jerry Yufb4b6472022-01-27 15:03:26 +08003593#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */