blob: c0d1f8d20f03c9741780fcd3b1a5039e5d686cfa [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
175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800176MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800177static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800178MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800179static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800180
181MBEDTLS_CHECK_RETURN_CRITICAL
182static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_context *ssl,
184 const unsigned char *identity,
185 size_t identity_len,
186 uint32_t obfuscated_ticket_age,
187 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800188{
Jerry Yufd310eb2022-09-06 09:16:35 +0800189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800190 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800191#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800192 mbedtls_ms_time_t now;
193 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800194 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800195 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800196#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800197
198 ((void) obfuscated_ticket_age);
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800201
Jerry Yufd310eb2022-09-06 09:16:35 +0800202 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100204 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Jerry Yu95699e72022-08-21 19:22:23 +0800206
Jerry Yu4746b102022-09-13 11:11:48 +0800207 /* We create a copy of the encrypted ticket since the ticket parsing
208 * function is allowed to use its input buffer as an output buffer
209 * (in-place decryption). We do, however, need the original buffer for
210 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ticket_buffer = mbedtls_calloc(1, identity_len);
213 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100221 switch (ret) {
222 case 0:
223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
224 break;
225
226 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100229 break;
230
231 case MBEDTLS_ERR_SSL_INVALID_MAC:
232 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100234 break;
235
236 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100238 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800239 }
240
241 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800243
Ronald Cronfbae94a2023-12-05 18:15:14 +0100244 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 goto exit;
246 }
Jerry Yu95699e72022-08-21 19:22:23 +0800247
Ronald Cron38117652024-03-05 09:05:46 +0100248 /*
249 * The identity matches that of a ticket. Now check that it has suitable
250 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800251 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100252 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
255 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800256 goto exit;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800260 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100261
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800264 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
265 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 goto exit;
268 }
269
Jerry Yu25ba4d42023-11-10 14:12:20 +0800270 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800271
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 /* RFC 8446 section 4.6.1
273 *
274 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
275 *
276 * RFC 8446 section 4.2.11.1
277 *
278 * Clients MUST NOT attempt to use tickets which have ages greater than
279 * the "ticket_lifetime" value which was provided with the ticket.
280 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800282 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800284 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800285 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 goto exit;
287 }
Jerry Yu95699e72022-08-21 19:22:23 +0800288
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800289 /* RFC 8446 section 4.2.10
290 *
291 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
292 * the ticket age for the selected PSK identity (computed by subtracting
293 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
294 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800295 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
297 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800298 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800299 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800300 */
Jerry Yucebffc32022-12-15 18:00:05 +0800301 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800302 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800303 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800306 3, ("Ticket age outside tolerance window ( diff = %"
307 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800308 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309 goto exit;
310 }
Jerry Yu95699e72022-08-21 19:22:23 +0800311#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312
Ronald Cron38117652024-03-05 09:05:46 +0100313 /*
314 * All good, we have found a suitable ticket.
315 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100316 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
317
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800318exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100319 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ssl_session_free(session);
321 }
Jerry Yu95699e72022-08-21 19:22:23 +0800322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
324 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800325}
326#endif /* MBEDTLS_SSL_SESSION_TICKETS */
327
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000329static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ssl_context *ssl,
331 const unsigned char *identity,
332 size_t identity_len,
333 uint32_t obfuscated_ticket_age,
334 int *psk_type,
335 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000336{
Ronald Cron606671e2023-02-17 11:36:33 +0100337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338
Jerry Yu95699e72022-08-21 19:22:23 +0800339 ((void) session);
340 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800341 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800344
Jerry Yu95699e72022-08-21 19:22:23 +0800345#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100346 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
347 ssl, identity, identity_len, obfuscated_ticket_age, session);
348 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800349 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100350 ret = mbedtls_ssl_set_hs_psk(ssl,
351 session->resumption_key,
352 session->resumption_key_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357
358 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
359 session->resumption_key,
360 session->resumption_key_len);
361 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
362 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100363 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100364 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
365 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800366 }
367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
368
Jerry Yu1c105562022-07-10 06:32:38 +0000369 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->conf->f_psk != NULL) {
371 if (ssl->conf->f_psk(
372 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100373 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100375 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800381 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ct_memcmp(ssl->conf->psk_identity,
383 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100384 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
385 if (ret != 0) {
386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
387 return ret;
388 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100389 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000390 }
391
Ronald Cronfbae94a2023-12-05 18:15:14 +0100392 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000393}
394
Ronald Cron38117652024-03-05 09:05:46 +0100395/*
396 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
397 * They are positive to not collide with error codes that are negative. Zero
398 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
399 * by the callers of this function as a generic success condition.
400 */
Ronald Cron6e311272023-12-05 17:57:01 +0100401#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
402#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800403MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000404static int ssl_tls13_offered_psks_check_binder_match(
405 mbedtls_ssl_context *ssl,
406 const unsigned char *binder, size_t binder_len,
407 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800413 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800414 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800415 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000416
Ronald Crona5c5c582024-03-19 13:54:15 +0100417 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
418 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
419 }
420
Jerry Yu1c105562022-07-10 06:32:38 +0000421 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800422 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200423 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 transcript, sizeof(transcript), &transcript_len);
425 if (ret != 0) {
426 return ret;
427 }
Jerry Yu1c105562022-07-10 06:32:38 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
430 if (ret != 0) {
431 return ret;
432 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
435 psk, psk_len, psk_type,
436 transcript,
437 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (ret != 0) {
442 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
447 server_computed_binder, transcript_len);
448 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000449
Ronald Crona5c5c582024-03-19 13:54:15 +0100450 if (mbedtls_ct_memcmp(server_computed_binder,
451 binder,
452 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100453 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000454 }
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_platform_zeroize(server_computed_binder,
457 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100458 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800459}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800460
Jerry Yu82534862022-08-30 10:42:33 +0800461#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100463static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
464 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800465{
Jerry Yu82534862022-08-30 10:42:33 +0800466 dst->ticket_age_add = src->ticket_age_add;
467 dst->ticket_flags = src->ticket_flags;
468 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (src->resumption_key_len == 0) {
470 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
471 }
472 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800473
Jerry Yu33bf2402022-12-12 16:01:43 +0800474#if defined(MBEDTLS_SSL_EARLY_DATA)
475 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000476
477#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000478 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000479 if (ret != 0) {
480 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000481 }
482#endif /* MBEDTLS_SSL_ALPN */
483#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800486}
487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
488
Ronald Cron79cdd412024-02-20 17:19:28 +0100489struct psk_attributes {
490 int type;
491 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100492 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100493};
Ronald Cron16cc3702024-03-07 15:04:56 +0100494#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100495
Jerry Yu1c105562022-07-10 06:32:38 +0000496/* Parser for pre_shared_key extension in client hello
497 * struct {
498 * opaque identity<1..2^16-1>;
499 * uint32 obfuscated_ticket_age;
500 * } PskIdentity;
501 *
502 * opaque PskBinderEntry<32..255>;
503 *
504 * struct {
505 * PskIdentity identities<7..2^16-1>;
506 * PskBinderEntry binders<33..2^16-1>;
507 * } OfferedPsks;
508 *
509 * struct {
510 * select (Handshake.msg_type) {
511 * case client_hello: OfferedPsks;
512 * ....
513 * };
514 * } PreSharedKeyExtension;
515 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800516MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000517static int ssl_tls13_parse_pre_shared_key_ext(
518 mbedtls_ssl_context *ssl,
519 const unsigned char *pre_shared_key_ext,
520 const unsigned char *pre_shared_key_ext_end,
521 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100522 const unsigned char *ciphersuites_end,
523 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000524{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800526 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800527 const unsigned char *p_identity_len;
528 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000529 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800530 const unsigned char *binders;
531 const unsigned char *p_binder_len;
532 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000533 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800534 int matched_identity = -1;
535 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
538 pre_shared_key_ext,
539 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000540
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 /* identities_len 2 bytes
542 * identities_data >= 7 bytes
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
545 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
548 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800549 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000550
Jerry Yu96a2e362022-07-21 15:11:34 +0800551 /* binders_len 2 bytes
552 * binders >= 33 bytes
553 */
Jerry Yu568ec252022-07-22 21:27:34 +0800554 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
556 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800559 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000560
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100561 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
562 identities_end - pre_shared_key_ext);
563 if (0 != ret) {
564 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
565 return ret;
566 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000569 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800570 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800571 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800572 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800573 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100574 int psk_ciphersuite_id;
575 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100576 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100577
Jerry Yu82534862022-08-30 10:42:33 +0800578#if defined(MBEDTLS_SSL_SESSION_TICKETS)
579 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800581#endif
Jerry Yubb852022022-07-20 21:10:44 +0800582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
584 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800585 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
587 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800588 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800591 binder_len = *p_binder_len;
592 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800594 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800595
Jerry Yu96a2e362022-07-21 15:11:34 +0800596 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000598 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 }
Jerry Yu1c105562022-07-10 06:32:38 +0000600
Jerry Yu96a2e362022-07-21 15:11:34 +0800601 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100603 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100604 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800605 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100609
Ronald Cron79cdd412024-02-20 17:19:28 +0100610 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800611 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100612 psk_ciphersuite_id = 0;
613 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100614 allowed_key_exchange_modes =
615 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800616 break;
Jerry Yu82534862022-08-30 10:42:33 +0800617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100618 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100619 psk_ciphersuite_id = session.ciphersuite;
620 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100621 ssl->session_negotiate->ticket_flags = session.ticket_flags;
622 allowed_key_exchange_modes =
623 session.ticket_flags &
624 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100626#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800627 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800629 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100630
Ronald Cron79cdd412024-02-20 17:19:28 +0100631 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
632
Ronald Croncf284562024-02-16 18:54:10 +0100633 if ((allowed_key_exchange_modes &
634 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
635 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100636 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100637 } else if ((allowed_key_exchange_modes &
638 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
639 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100640 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100641 }
642
Ronald Cron79cdd412024-02-20 17:19:28 +0100643 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100644 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
645 continue;
646 }
647
Ronald Cron89089cc2024-02-14 18:18:08 +0100648 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
649 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100650 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100651
Ronald Cron74a16292024-02-23 17:07:41 +0100652 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100653#if defined(MBEDTLS_SSL_SESSION_TICKETS)
654 mbedtls_ssl_session_free(&session);
655#endif
656 /*
657 * We consider finding a ciphersuite suitable for the PSK as part
658 * of the validation of its binder. Thus if we do not find one, we
659 * abort the handshake with a decrypt_error alert.
660 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800661 MBEDTLS_SSL_PEND_FATAL_ALERT(
662 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100664 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800665 }
666
Jerry Yu96a2e362022-07-21 15:11:34 +0800667 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100668 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100669 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100670 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800671 /* For security reasons, the handshake should be aborted when we
672 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
673 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800674#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800676#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000678 MBEDTLS_SSL_DEBUG_RET(
679 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800680 MBEDTLS_SSL_PEND_FATAL_ALERT(
681 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
683 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800684 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800685
686 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800687
Jerry Yu82534862022-08-30 10:42:33 +0800688#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100689 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
691 &session);
692 mbedtls_ssl_session_free(&session);
693 if (ret != 0) {
694 return ret;
695 }
Jerry Yu82534862022-08-30 10:42:33 +0800696 }
697#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (p_identity_len != identities_end || p_binder_len != binders_end) {
701 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
702 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
703 MBEDTLS_ERR_SSL_DECODE_ERROR);
704 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000705 }
706
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800707 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000708 ret = ssl->handshake->update_checksum(
709 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100710 if (0 != ret) {
711 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
712 return ret;
713 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100715 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 ssl->handshake->selected_identity = (uint16_t) matched_identity;
720 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000723}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724
725/*
726 * struct {
727 * select ( Handshake.msg_type ) {
728 * ....
729 * case server_hello:
730 * uint16 selected_identity;
731 * }
732 * } PreSharedKeyExtension;
733 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
735 unsigned char *buf,
736 unsigned char *end,
737 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738{
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740
741 *olen = 0;
742
David Horstmann21b89762022-10-06 18:34:28 +0100743 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000744#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000746#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000748#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000750 /* We shouldn't have called this extension writer unless we've
751 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000753 }
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
756 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
759 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000762
763 *olen = 6;
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
766 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000771}
772
Ronald Cron41a443a2022-10-04 16:38:25 +0200773#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000774
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775/* From RFC 8446:
776 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000777 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778 * } SupportedVersions;
779 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200780MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
782 const unsigned char *buf,
783 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000786 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000787 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000788 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100789 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000792 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793 p += 1;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000796 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 while (p < versions_end) {
798 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
799 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000800 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000801
Ronald Cron3bd2b022023-04-03 16:45:39 +0200802 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100803 found_supported_version = 1;
804 break;
805 }
806
Ronald Cron3bd2b022023-04-03 16:45:39 +0200807 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
808 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100809 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 break;
811 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812 }
813
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100814 if (!found_supported_version) {
815 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
818 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
819 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820 }
821
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100822 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100825 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826}
827
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200828#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000829/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830 *
831 * From RFC 8446:
832 * enum {
833 * ... (0xFFFF)
834 * } NamedGroup;
835 * struct {
836 * NamedGroup named_group_list<2..2^16-1>;
837 * } NamedGroupList;
838 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100840static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
841 const unsigned char *buf,
842 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000845 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000846 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
849 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
850 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000853 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000854 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000857 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
859 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000860 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 MBEDTLS_SSL_DEBUG_MSG(2,
863 ("got named group: %s(%04x)",
864 mbedtls_ssl_named_group_to_str(named_group),
865 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
868 !mbedtls_ssl_named_group_is_supported(named_group) ||
869 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000870 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 MBEDTLS_SSL_DEBUG_MSG(2,
874 ("add named group %s(%04x) into received list.",
875 mbedtls_ssl_named_group_to_str(named_group),
876 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800877
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000878 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879 }
880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200884#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885
XiaokangQian08037552022-04-20 07:16:41 +0000886#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
887
Valerio Settic9ae8622023-07-25 11:23:50 +0200888#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889/*
890 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000891 * extension is correct and stores the first acceptable key share and its
892 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893 *
894 * Possible return values are:
895 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000896 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
897 * the client does not match a group supported by the server. A
898 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000899 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800900 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200901MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100902static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
903 const unsigned char *buf,
904 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000905{
XiaokangQianb67384d2022-04-19 00:02:38 +0000906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000907 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000908 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800909 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910
911 /* From RFC 8446:
912 *
913 * struct {
914 * KeyShareEntry client_shares<0..2^16-1>;
915 * } KeyShareClientHello;
916 *
917 */
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
920 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000921 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923
924 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000925 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000926
927 /* We try to find a suitable key share entry and copy it to the
928 * handshake context. Later, we have to find out whether we can do
929 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000930 * dismiss it and send a HelloRetryRequest message.
931 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000934 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800935 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800936 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937
938 /*
939 * struct {
940 * NamedGroup group;
941 * opaque key_exchange<1..2^16-1>;
942 * } KeyShareEntry;
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
945 group = MBEDTLS_GET_UINT16_BE(p, 0);
946 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800947 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800948 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800950 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000951
952 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000953 * for input validation purposes.
954 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
956 !mbedtls_ssl_named_group_is_supported(group) ||
957 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000958 continue;
959 }
XiaokangQian060d8672022-04-21 09:24:56 +0000960
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200962 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200964 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200965 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200966 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_ssl_named_group_to_str(group),
968 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200969 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 ssl, key_exchange - 2, key_exchange_len + 2);
971 if (ret != 0) {
972 return ret;
973 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 } else {
976 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
977 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000978 continue;
979 }
980
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000981 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982 }
983
Jerry Yuc1be19f2022-04-23 16:11:39 +0800984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (ssl->handshake->offered_group_id == 0) {
986 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
987 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000990}
Valerio Settic9ae8622023-07-25 11:23:50 +0200991#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000992
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200993MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100994static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
995 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000996{
Jerry Yu0c354a22022-08-29 15:25:36 +0800997 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000999}
1000
Jerry Yue5991322022-11-07 14:03:44 +08001001#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001002MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +00001003static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001005{
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return ssl_tls13_client_hello_has_exts(
1007 ssl,
1008 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1009 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1010 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011}
Jerry Yue5991322022-11-07 14:03:44 +08001012#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001013
Jerry Yue5991322022-11-07 14:03:44 +08001014#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001015MBEDTLS_CHECK_RETURN_CRITICAL
1016static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001018{
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return ssl_tls13_client_hello_has_exts(
1020 ssl,
1021 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1022 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001023}
Jerry Yue5991322022-11-07 14:03:44 +08001024#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001025
Jerry Yue5991322022-11-07 14:03:44 +08001026#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001027MBEDTLS_CHECK_RETURN_CRITICAL
1028static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001030{
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return ssl_tls13_client_hello_has_exts(
1032 ssl,
1033 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1034 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1035 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1036 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001037}
Jerry Yue5991322022-11-07 14:03:44 +08001038#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001039
Pengyu Lv306a01d2023-02-02 16:35:47 +08001040#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1041MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001042static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001043{
Jerry Yue5991322022-11-07 14:03:44 +08001044#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001045 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001046 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001048#else
1049 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001052}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001053
Jerry Yu77f01482022-07-11 07:03:24 +00001054MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001055static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001056{
Jerry Yue5991322022-11-07 14:03:44 +08001057#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001058 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001059 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001061#else
1062 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001064#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001065}
1066#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1067
1068MBEDTLS_CHECK_RETURN_CRITICAL
1069static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1070{
1071#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1072 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1073 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1074#else
1075 ((void) ssl);
1076 return 0;
1077#endif
1078}
1079
XiaokangQian81802f42022-06-10 13:25:22 +00001080#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001081 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001082
1083#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001085{
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001103 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001105 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001107 }
1108}
1109#endif /* MBEDTLS_USE_PSA_CRYPTO */
1110
XiaokangQian23c5be62022-06-07 02:04:34 +00001111/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001112 * Pick best ( private key, certificate chain ) pair based on the signature
1113 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001114 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001116static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001117{
XiaokangQianfb665a82022-06-15 03:57:21 +00001118 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001119 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001120
1121#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001123 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001125#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (key_cert_list == NULL) {
1129 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1130 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001131 }
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1134 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 for (key_cert = key_cert_list; key_cert != NULL;
1143 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001144#if defined(MBEDTLS_USE_PSA_CRYPTO)
1145 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1146#endif /* MBEDTLS_USE_PSA_CRYPTO */
1147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1149 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001150
1151 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 * This avoids sending the client a cert it'll reject based on
1153 * keyUsage or other extensions.
1154 */
1155 if (mbedtls_x509_crt_check_key_usage(
1156 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001157 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001158 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1160 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1161 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001162 continue;
1163 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 MBEDTLS_SSL_DEBUG_MSG(3,
1166 ("ssl_tls13_pick_key_cert:"
1167 "check signature algorithm %s [%04x]",
1168 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1169 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001170#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001172#endif /* MBEDTLS_USE_PSA_CRYPTO */
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1175 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001176#if defined(MBEDTLS_USE_PSA_CRYPTO)
1177 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1179 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001180#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001182 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_MSG(3,
1184 ("ssl_tls13_pick_key_cert:"
1185 "selected signature algorithm"
1186 " %s [%04x]",
1187 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1188 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001189 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 3, "selected certificate (chain)",
1191 ssl->handshake->key_cert->cert);
1192 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001193 }
XiaokangQian81802f42022-06-10 13:25:22 +00001194 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001195 }
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1198 "no suitable certificate found"));
1199 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001200}
XiaokangQian81802f42022-06-10 13:25:22 +00001201#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001202 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001203
XiaokangQian4080a7f2022-04-11 09:55:18 +00001204/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001205 *
1206 * STATE HANDLING: ClientHello
1207 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001209 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001210 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001211 *
1212 * In this case, the server progresses to sending its ServerHello.
1213 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001214 * 2) The ClientHello was well-formed but didn't match the server's
1215 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001216 *
1217 * For example, the client might not have offered a key share which
1218 * the server supports, or the server might require a cookie.
1219 *
1220 * In this case, the server sends a HelloRetryRequest.
1221 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001222 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001223 *
1224 * In this case, we abort the handshake.
1225 *
1226 */
1227
1228/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001229 * Structure of this message:
1230 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001231 * uint16 ProtocolVersion;
1232 * opaque Random[32];
1233 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001234 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001235 * struct {
1236 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1237 * Random random;
1238 * opaque legacy_session_id<0..32>;
1239 * CipherSuite cipher_suites<2..2^16-2>;
1240 * opaque legacy_compression_methods<1..2^8-1>;
1241 * Extension extensions<8..2^16-1>;
1242 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001243 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001244
1245#define SSL_CLIENT_HELLO_OK 0
1246#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001247#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001248
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001249MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001250static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1251 const unsigned char *buf,
1252 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001253{
XiaokangQianb67384d2022-04-19 00:02:38 +00001254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1255 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001256 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001257 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001258 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001259 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001260 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001261 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001262 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001263 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001264 const unsigned char *supported_versions_data;
1265 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001266 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001267 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001268 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001269
Ronald Cron41a443a2022-10-04 16:38:25 +02001270#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001271 int got_psk = 0;
1272 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001273 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001274 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001275#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001278 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001279 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001280 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001281 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001282 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001283 * .. . .. ciphersuite list length ( 2 bytes )
1284 * .. . .. ciphersuite list
1285 * .. . .. compression alg. list length ( 1 byte )
1286 * .. . .. compression alg. list
1287 * .. . .. extensions length ( 2 bytes, optional )
1288 * .. . .. extensions ( optional )
1289 */
1290
XiaokangQianb67384d2022-04-19 00:02:38 +00001291 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001292 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001293 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1294 * read at least up to session id length without worrying.
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297
1298 /* ...
1299 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1300 * ...
1301 * with ProtocolVersion defined as:
1302 * uint16 ProtocolVersion;
1303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1305 MBEDTLS_SSL_VERSION_TLS1_2) {
1306 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1307 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1308 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1309 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 }
1311 p += 2;
1312
Jerry Yuc1be19f2022-04-23 16:11:39 +08001313 /* ...
1314 * Random random;
1315 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001316 * with Random defined as:
1317 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 */
Ronald Crond540d992023-03-07 09:41:48 +01001319 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001320 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001321
Jerry Yuc1be19f2022-04-23 16:11:39 +08001322 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001323 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001324 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001325 */
Ronald Croncada4102023-03-07 09:51:39 +01001326 legacy_session_id_len = *(p++);
1327 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001328
XiaokangQianb67384d2022-04-19 00:02:38 +00001329 /*
1330 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001331 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001334 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001335
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001336 /* ...
1337 * CipherSuite cipher_suites<2..2^16-2>;
1338 * ...
1339 * with CipherSuite defined as:
1340 * uint8 CipherSuite[2];
1341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001344 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001345
Ronald Cronfc7ae872023-02-16 15:32:19 +01001346 /*
1347 * The length of the ciphersuite list has to be even.
1348 */
1349 if (cipher_suites_len & 1) {
1350 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1351 MBEDTLS_ERR_SSL_DECODE_ERROR);
1352 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1353 }
1354
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 /* Check we have enough data for the ciphersuite list, the legacy
1356 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001357 *
1358 * cipher_suites cipher_suites_len bytes
1359 * legacy_compression_methods 2 bytes
1360 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001363 p += cipher_suites_len;
1364 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001365
1366 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001367 * Search for the supported versions extension and parse it to determine
1368 * if the client supports TLS 1.3.
1369 */
1370 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1371 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001372 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001373 if (ret < 0) {
1374 MBEDTLS_SSL_DEBUG_RET(1,
1375 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1376 return ret;
1377 }
1378
1379 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001380 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001381 }
1382
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001383 if (ret == 1) {
1384 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001385 supported_versions_data,
1386 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001387 if (ret < 0) {
1388 MBEDTLS_SSL_DEBUG_RET(1,
1389 ("ssl_tls13_parse_supported_versions_ext"), ret);
1390 return ret;
1391 }
1392
Ronald Cronb828c7d2023-04-03 16:37:22 +02001393 /*
1394 * The supported versions extension was parsed successfully as the
1395 * value returned by ssl_tls13_parse_supported_versions_ext() is
1396 * positive. The return value is then equal to
1397 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1398 * the TLS version to negotiate.
1399 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001400 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1401 return SSL_CLIENT_HELLO_TLS1_2;
1402 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001403 }
1404
1405 /*
1406 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001407 */
1408 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001409 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1410 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001411
1412 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001413 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001414 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001415 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001416 */
1417 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1418 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1419 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1420
Ronald Croncada4102023-03-07 09:51:39 +01001421 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1422 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1423 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1424 }
1425 ssl->session_negotiate->id_len = legacy_session_id_len;
1426 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1427 legacy_session_id, legacy_session_id_len);
1428 memcpy(&ssl->session_negotiate->id[0],
1429 legacy_session_id, legacy_session_id_len);
1430
Ronald Crond540d992023-03-07 09:41:48 +01001431 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001432 * Search for a matching ciphersuite
1433 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001434 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1435 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001436
Ronald Cron89089cc2024-02-14 18:18:08 +01001437 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1438 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (handshake->ciphersuite_info == NULL) {
1441 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1442 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001444 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001445 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1446
1447 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1448 ((unsigned) handshake->ciphersuite_info->id),
1449 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001450
XiaokangQian4080a7f2022-04-11 09:55:18 +00001451 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001452 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001453 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1456 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1457 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1458 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1459 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001460 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001461 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001462
Jerry Yuc1be19f2022-04-23 16:11:39 +08001463 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001464 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001465 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001466 * with Extension defined as:
1467 * struct {
1468 * ExtensionType extension_type;
1469 * opaque extension_data<0..2^16-1>;
1470 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001475 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001478 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001481 unsigned int extension_type;
1482 size_t extension_data_len;
1483 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001484 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1485
Ronald Cron5fbd2702024-02-14 10:03:36 +01001486 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001487 /* Do not accept early data extension in 2nd ClientHello */
1488 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1489 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001490
Jerry Yu0c354a22022-08-29 15:25:36 +08001491 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001492 *
1493 * The "pre_shared_key" extension MUST be the last extension in the
1494 * ClientHello (this facilitates implementation as described below).
1495 * Servers MUST check that it is the last extension and otherwise fail
1496 * the handshake with an "illegal_parameter" alert.
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001499 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001501 MBEDTLS_SSL_PEND_FATAL_ALERT(
1502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1504 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001505 }
1506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1508 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1509 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 p += 4;
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513 extension_data_end = p + extension_data_len;
1514
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001515 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001517 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (ret != 0) {
1519 return ret;
1520 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001523#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1524 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1526 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1527 extension_data_end);
1528 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001529 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 1, "mbedtls_ssl_parse_servername_ext", ret);
1531 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001532 }
XiaokangQian40a35232022-05-07 09:02:40 +00001533 break;
1534#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1535
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001536#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539
1540 /* Supported Groups Extension
1541 *
1542 * When sent by the client, the "supported_groups" extension
1543 * indicates the named groups which the client supports,
1544 * ordered from most preferred to least preferred.
1545 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001546 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 ssl, p, extension_data_end);
1548 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001549 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001550 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001552 }
1553
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001555#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556
Valerio Settic9ae8622023-07-25 11:23:50 +02001557#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560
1561 /*
1562 * Key Share Extension
1563 *
1564 * When sent by the client, the "key_share" extension
1565 * contains the endpoint's cryptographic parameters for
1566 * ECDHE/DHE key establishment methods.
1567 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001568 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 ssl, p, extension_data_end);
1570 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001571 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1572 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573 }
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001576 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 1, "ssl_tls13_parse_key_shares_ext", ret);
1578 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001579 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001580
XiaokangQian7807f9f2022-02-15 10:04:37 +00001581 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001582#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001583
1584 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001585 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586 break;
1587
Ronald Cron41a443a2022-10-04 16:38:25 +02001588#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001589 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001590 MBEDTLS_SSL_DEBUG_MSG(
1591 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001592
1593 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 ssl, p, extension_data_end);
1595 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001596 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1598 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001599 }
1600
Jerry Yue19e3b92022-07-08 12:04:51 +00001601 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001602#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001603
Jerry Yu1c105562022-07-10 06:32:38 +00001604 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1606 if ((handshake->received_extensions &
1607 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001608 MBEDTLS_SSL_PEND_FATAL_ALERT(
1609 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1611 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001612 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001613#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001614 /* Delay processing of the PSK identity once we have
1615 * found out which algorithms to use. We keep a pointer
1616 * to the buffer and the size for later processing.
1617 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001618 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001619 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001620#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001621 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001622
XiaokangQianacb39922022-06-17 10:18:48 +00001623#if defined(MBEDTLS_SSL_ALPN)
1624 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1628 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001629 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1631 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001632 }
XiaokangQianacb39922022-06-17 10:18:48 +00001633 break;
1634#endif /* MBEDTLS_SSL_ALPN */
1635
Ronald Cron928cbd32022-10-04 16:14:26 +02001636#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639
Gabor Mezei078e8032022-04-27 21:17:56 +02001640 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 ssl, p, extension_data_end);
1642 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001643 MBEDTLS_SSL_DEBUG_RET(
1644 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001646 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001647 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001648#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001649
Jan Bruckner151f6422023-02-10 12:45:19 +01001650#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1651 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1652 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1653
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001654 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1655 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001656 if (ret != 0) {
1657 MBEDTLS_SSL_DEBUG_RET(
1658 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1659 return ret;
1660 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001661 break;
1662#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1663
XiaokangQian7807f9f2022-02-15 10:04:37 +00001664 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001665 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001666 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001668 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001669 }
1670
1671 p += extension_data_len;
1672 }
1673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1675 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001676
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001677 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1678 MBEDTLS_SSL_HS_CLIENT_HELLO,
1679 p - buf);
1680 if (0 != ret) {
1681 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1682 return ret;
1683 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001684
Ronald Cron41a443a2022-10-04 16:38:25 +02001685#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686 /* Update checksum with either
1687 * - The entire content of the CH message, if no PSK extension is present
1688 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001689 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001690 * ClientHello even if some pre-requisites for PSK key exchange modes are
1691 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001692 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001693 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001694 ret = handshake->update_checksum(ssl, buf,
1695 pre_shared_key_ext - buf);
1696 if (0 != ret) {
1697 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1698 return ret;
1699 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1701 pre_shared_key_ext,
1702 pre_shared_key_ext_end,
1703 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001704 cipher_suites_end,
1705 &psk);
1706 if (ret == 0) {
1707 got_psk = 1;
1708 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001709 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1711 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001714#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001715 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001716 ret = handshake->update_checksum(ssl, buf, p - buf);
1717 if (0 != ret) {
1718 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1719 return ret;
1720 }
Jerry Yu1c105562022-07-10 06:32:38 +00001721 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001722
Ronald Cron79cdd412024-02-20 17:19:28 +01001723 /*
1724 * Determine the key exchange algorithm to use.
1725 * There are three types of key exchanges supported in TLS 1.3:
1726 * - (EC)DH with ECDSA,
1727 * - (EC)DH with PSK,
1728 * - plain PSK.
1729 *
1730 * The PSK-based key exchanges may additionally be used with 0-RTT.
1731 *
1732 * Our built-in order of preference is
1733 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1734 * 2 ) Certificate Mode ( ephemeral )
1735 * 3 ) Plain PSK Mode ( psk )
1736 */
1737#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1738 if (got_psk && (psk.key_exchange_mode ==
1739 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1740 handshake->key_exchange_mode =
1741 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1742 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1743
1744 } else
1745#endif
1746 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1747 handshake->key_exchange_mode =
1748 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1749 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1750
1751 }
1752#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1753 else if (got_psk && (psk.key_exchange_mode ==
1754 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1755 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1756 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1757 }
1758#endif
1759 else {
1760 MBEDTLS_SSL_DEBUG_MSG(
1761 1,
1762 ("ClientHello message misses mandatory extensions."));
1763 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1764 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1765 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001767
Ronald Cron3e47eec2024-02-21 10:29:24 +01001768#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001769 if (handshake->key_exchange_mode &
1770 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1771 handshake->ciphersuite_info = psk.ciphersuite_info;
1772 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1773
1774 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1775 ((unsigned) psk.ciphersuite_info->id),
1776 psk.ciphersuite_info->name));
1777
1778 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1779 handshake->resume = 1;
1780 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001781 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001782#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001783
1784 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001785 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1786 hrr_required = (no_usable_share_for_key_agreement != 0);
1787 }
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001792}
1793
Jerry Yuab0da372023-02-08 13:55:24 +08001794#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001796{
1797 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1798
Jerry Yu985c9672022-12-04 14:06:30 +08001799 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001801 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001802 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001803 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001804 }
1805
Jerry Yu454dda32023-10-31 15:13:54 +08001806 if (!handshake->resume) {
1807 /* We currently support early data only in the case of PSKs established
1808 via a NewSessionTicket message thus in the case of a session
1809 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001810 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001811 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001812 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001813 }
1814
Jerry Yu82fd6c12023-10-31 16:32:19 +08001815 /* RFC 8446 4.2.10
1816 *
1817 * In order to accept early data, the server MUST have accepted a PSK cipher
1818 * suite and selected the first key offered in the client's "pre_shared_key"
1819 * extension. In addition, it MUST verify that the following values are the
1820 * same as those associated with the selected PSK:
1821 * - The TLS version number
1822 * - The selected cipher suite
1823 * - The selected ALPN [RFC7301] protocol, if any
1824 *
1825 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001826 * - The TLS version number is checked in
1827 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001828 */
1829
1830 if (handshake->selected_identity != 0) {
1831 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001832 1, ("EarlyData: rejected, the selected key in "
1833 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001834 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835 }
1836
1837 if (handshake->ciphersuite_info->id !=
1838 ssl->session_negotiate->ciphersuite) {
1839 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001840 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1841 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001842 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001843
1844 }
Jerry Yu985c9672022-12-04 14:06:30 +08001845
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001846 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001847 MBEDTLS_SSL_DEBUG_MSG(
1848 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001849 ("EarlyData: rejected, early_data not allowed in ticket "
1850 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001851 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001852 }
Jerry Yu985c9672022-12-04 14:06:30 +08001853
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001854#if defined(MBEDTLS_SSL_ALPN)
1855 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1856 size_t alpn_len;
1857
1858 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1859 return 0;
1860 }
1861
1862 if (alpn != NULL) {
1863 alpn_len = strlen(alpn);
1864 }
1865
1866 if (alpn == NULL ||
1867 ssl->session_negotiate->ticket_alpn == NULL ||
1868 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1869 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1870 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1871 "from the one associated with the pre-shared key."));
1872 return -1;
1873 }
1874#endif
1875
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001876 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001877}
1878#endif /* MBEDTLS_SSL_EARLY_DATA */
1879
XiaokangQian75fe8c72022-06-15 09:42:45 +00001880/* Update the handshake state machine */
1881
Ronald Cronce7d76e2022-07-08 18:56:49 +02001882MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001883static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1884 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001885{
1886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1887
XiaokangQian7807f9f2022-02-15 10:04:37 +00001888 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001889 * Server certificate selection
1890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1892 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1893 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001894 }
1895#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1896 ssl->handshake->sni_name = NULL;
1897 ssl->handshake->sni_name_len = 0;
1898#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1901 if (ret != 0) {
1902 MBEDTLS_SSL_DEBUG_RET(1,
1903 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1904 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001905 }
1906
Jerry Yuab0da372023-02-08 13:55:24 +08001907#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001908 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001909 ssl->handshake->early_data_accepted =
1910 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001911
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001912 if (ssl->handshake->early_data_accepted) {
1913 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1914 if (ret != 0) {
1915 MBEDTLS_SSL_DEBUG_RET(
1916 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1917 return ret;
1918 }
1919 } else {
1920 ssl->discard_early_data_record =
1921 hrr_required ?
1922 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1923 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001924 }
1925 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001926#else
1927 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001928#endif /* MBEDTLS_SSL_EARLY_DATA */
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001931}
1932
1933/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001934 * Main entry point from the state machine; orchestrates the otherfunctions.
1935 */
1936
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001937MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001938static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001939{
1940
XiaokangQian08037552022-04-20 07:16:41 +00001941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001943 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001944 int parse_client_hello_ret;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1949 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1950 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1953 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001954 parse_client_hello_ret = ret; /* Store positive return value of
1955 * parse_client_hello,
1956 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001957 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001958
Ronald Cronb828c7d2023-04-03 16:37:22 +02001959 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001960 * Version 1.2 of the protocol has to be used for the handshake.
1961 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001962 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1963 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1964 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1965 * will dispatch to the TLS 1.2 state machine.
1966 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001967 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001968 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001969 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001970 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001971 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001972 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001973 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1974 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1975 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001976 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001977 ssl->keep_current_message = 1;
1978 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1979 return 0;
1980 }
1981
Ronald Cron7b6ee942024-01-12 10:29:55 +01001982 MBEDTLS_SSL_PROC_CHK(
1983 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1984 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001985
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001986 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1988 } else {
1989 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1990 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001991
1992cleanup:
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1995 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001996}
1997
1998/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001999 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08002000 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002001MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002002static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002003{
Jerry Yu637a3f12022-04-20 21:37:58 +08002004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2005 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2009 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2010 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2011 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002012 }
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2015 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002016
2017#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002018 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002019#endif /* MBEDTLS_HAVE_TIME */
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002022}
2023
Jerry Yu3bf2c642022-03-30 22:02:12 +08002024/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002025 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026 *
2027 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002028 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029 * } SupportedVersions;
2030 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002031MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002032static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 mbedtls_ssl_context *ssl,
2034 unsigned char *buf,
2035 unsigned char *end,
2036 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 *out_len = 0;
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041
2042 /* Check if we have space to write the extension:
2043 * - extension_type (2 bytes)
2044 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002045 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 mbedtls_ssl_write_version(buf + 4,
2054 ssl->conf->transport,
2055 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2058 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059
Jerry Yu349a6132022-04-14 20:52:56 +08002060 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
Jerry Yub95dd362022-11-08 21:19:34 +08002062 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002066}
2067
Jerry Yud9436a12022-04-20 22:28:09 +08002068
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069
2070/* Generate and export a single key share. For hybrid KEMs, this can
2071 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002072MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002073static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2074 uint16_t named_group,
2075 unsigned char *buf,
2076 unsigned char *end,
2077 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078{
2079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002080
2081 *out_len = 0;
2082
Valerio Settic9ae8622023-07-25 11:23:50 +02002083#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002084 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002085 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002086 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 ssl, named_group, buf, end, out_len);
2088 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002089 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002090 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 ret);
2092 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002095#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (0 /* Other kinds of KEMs */) {
2097 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002098 ((void) ssl);
2099 ((void) named_group);
2100 ((void) buf);
2101 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2103 }
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106}
2107
2108/*
2109 * ssl_tls13_write_key_share_ext
2110 *
2111 * Structure of key_share extension in ServerHello:
2112 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002113 * struct {
2114 * NamedGroup group;
2115 * opaque key_exchange<1..2^16-1>;
2116 * } KeyShareEntry;
2117 * struct {
2118 * KeyShareEntry server_share;
2119 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002121MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002122static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2123 unsigned char *buf,
2124 unsigned char *end,
2125 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126{
Jerry Yu955ddd72022-04-22 22:27:33 +08002127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002129 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132
2133 *out_len = 0;
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2138 mbedtls_ssl_named_group_to_str(group),
2139 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002140
Jerry Yu3bf2c642022-03-30 22:02:12 +08002141 /* Check if we have space for header and length fields:
2142 * - extension_type (2 bytes)
2143 * - extension_data_length (2 bytes)
2144 * - group (2 bytes)
2145 * - key_exchange_length (2 bytes)
2146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2148 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2149 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002151
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2153 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002154 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 ssl, group, server_share + 4, end, &key_exchange_length);
2156 if (ret != 0) {
2157 return ret;
2158 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002159 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164
Jerry Yu57d48412022-04-20 21:50:42 +08002165 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002170}
Jerry Yud9436a12022-04-20 22:28:09 +08002171
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002172MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002173static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2174 unsigned char *buf,
2175 unsigned char *end,
2176 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002177{
2178 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2179 /* key_share Extension
2180 *
2181 * struct {
2182 * select (Handshake.msg_type) {
2183 * ...
2184 * case hello_retry_request:
2185 * NamedGroup selected_group;
2186 * ...
2187 * };
2188 * } KeyShare;
2189 */
2190
2191 *out_len = 0;
2192
Jerry Yub0ac10b2022-05-05 11:10:08 +08002193 /*
2194 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2195 * of the HRR is then to transmit a cookie to force the client to demonstrate
2196 * reachability at their apparent network address (primarily useful for DTLS).
2197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2199 return 0;
2200 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201
2202 /* We should only send the key_share extension if the client's initial
2203 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (ssl->handshake->offered_group_id != 0) {
2205 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2206 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002207 }
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (selected_group == 0) {
2210 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2211 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002212 }
2213
Jerry Yub0ac10b2022-05-05 11:10:08 +08002214 /* Check if we have enough space:
2215 * - extension_type (2 bytes)
2216 * - extension_data_length (2 bytes)
2217 * - selected_group (2 bytes)
2218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2222 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2223 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_DEBUG_MSG(3,
2226 ("HRR selected_group: %s (%x)",
2227 mbedtls_ssl_named_group_to_str(selected_group),
2228 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002229
2230 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002235}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002236
2237/*
2238 * Structure of ServerHello message:
2239 *
2240 * struct {
2241 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2242 * Random random;
2243 * opaque legacy_session_id_echo<0..32>;
2244 * CipherSuite cipher_suite;
2245 * uint8 legacy_compression_method = 0;
2246 * Extension extensions<6..2^16-1>;
2247 * } ServerHello;
2248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002249MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002250static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2251 unsigned char *buf,
2252 unsigned char *end,
2253 size_t *out_len,
2254 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002255{
Jerry Yu955ddd72022-04-22 22:27:33 +08002256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002258 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002259 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260
2261 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002262 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263
Jerry Yucfc04b32022-04-21 09:31:58 +08002264 /* ...
2265 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2266 * ...
2267 * with ProtocolVersion defined as:
2268 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2271 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002272 p += 2;
2273
Jerry Yu1c3e6882022-04-20 21:23:40 +08002274 /* ...
2275 * Random random;
2276 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002277 * with Random defined as:
2278 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2281 if (is_hrr) {
2282 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2283 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2284 } else {
2285 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2286 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002287 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2289 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002290 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2291
Jerry Yucfc04b32022-04-21 09:31:58 +08002292 /* ...
2293 * opaque legacy_session_id_echo<0..32>;
2294 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2297 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2298 if (ssl->session_negotiate->id_len > 0) {
2299 memcpy(p, &ssl->session_negotiate->id[0],
2300 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002301 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2304 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305 }
2306
Jerry Yucfc04b32022-04-21 09:31:58 +08002307 /* ...
2308 * CipherSuite cipher_suite;
2309 * ...
2310 * with CipherSuite defined as:
2311 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2314 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002315 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_DEBUG_MSG(3,
2317 ("server hello, chosen ciphersuite: %s ( id=%d )",
2318 mbedtls_ssl_get_ciphersuite_name(
2319 ssl->session_negotiate->ciphersuite),
2320 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321
Jerry Yucfc04b32022-04-21 09:31:58 +08002322 /* ...
2323 * uint8 legacy_compression_method = 0;
2324 * ...
2325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002327 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002328
Jerry Yucfc04b32022-04-21 09:31:58 +08002329 /* ...
2330 * Extension extensions<6..2^16-1>;
2331 * ...
2332 * struct {
2333 * ExtensionType extension_type; (2 bytes)
2334 * opaque extension_data<0..2^16-1>;
2335 * } Extension;
2336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002338 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002339 p += 2;
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2342 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002343 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2345 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002346 }
2347 p += output_len;
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2350 if (is_hrr) {
2351 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2352 } else {
2353 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2354 }
2355 if (ret != 0) {
2356 return ret;
2357 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002358 p += output_len;
2359 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002360
Ronald Cron41a443a2022-10-04 16:38:25 +02002361#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2363 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2364 if (ret != 0) {
2365 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2366 ret);
2367 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002368 }
2369 p += output_len;
2370 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002371#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2376 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002377
Jerry Yud9436a12022-04-20 22:28:09 +08002378 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002381
Jerry Yu7de2ff02022-11-08 21:43:46 +08002382 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002383 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_SSL_HS_SERVER_HELLO,
2385 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002388}
2389
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002390MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002391static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002392{
2393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2395 if (ret != 0) {
2396 MBEDTLS_SSL_DEBUG_RET(1,
2397 "mbedtls_ssl_tls13_compute_handshake_transform",
2398 ret);
2399 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002400 }
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002403}
2404
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002406static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002407{
Jerry Yu637a3f12022-04-20 21:37:58 +08002408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409 unsigned char *buf;
2410 size_t buf_len, msg_len;
2411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002415
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002416 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2417 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2420 buf + buf_len,
2421 &msg_len,
2422 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002423
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002424 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002425 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2428 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002429
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002430 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002431
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002432#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2433 /* The server sends a dummy change_cipher_spec record immediately
2434 * after its first handshake message. This may either be after
2435 * a ServerHello or a HelloRetryRequest.
2436 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002437 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002439#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002441#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002442
Jerry Yuf4b27e42022-03-30 17:32:21 +08002443cleanup:
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2446 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002447}
2448
Jerry Yu23d1a252022-05-12 18:08:59 +08002449
2450/*
2451 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2452 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002454static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002455{
2456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002457 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2459 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2460 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2461 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002462 }
2463
2464 /*
2465 * Create stateless transcript hash for HRR
2466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2468 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2469 if (ret != 0) {
2470 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2471 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002472 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002476}
2477
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002479static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002480{
2481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2482 unsigned char *buf;
2483 size_t buf_len, msg_len;
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2490 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2491 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2494 buf + buf_len,
2495 &msg_len,
2496 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002497 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002498 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002499
2500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2502 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002503
Ronald Cron5fbd2702024-02-14 10:03:36 +01002504 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002505
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002506#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2507 /* The server sends a dummy change_cipher_spec record immediately
2508 * after its first handshake message. This may either be after
2509 * a ServerHello or a HelloRetryRequest.
2510 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002511 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002513#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002515#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002516
2517cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2519 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002520}
2521
Jerry Yu5b64ae92022-03-30 17:15:02 +08002522/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2524 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002525
2526/*
2527 * struct {
2528 * Extension extensions<0..2 ^ 16 - 1>;
2529 * } EncryptedExtensions;
2530 *
2531 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002532MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002533static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2534 unsigned char *buf,
2535 unsigned char *end,
2536 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002537{
XiaokangQianacb39922022-06-17 10:18:48 +00002538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002539 unsigned char *p = buf;
2540 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002541 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002542 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002543
Jerry Yu4d3841a2022-04-16 12:37:19 +08002544 *out_len = 0;
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002547 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002548 p += 2;
2549
Jerry Yu8937eb42022-05-03 12:12:14 +08002550 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002551 ((void) ret);
2552 ((void) output_len);
2553
2554#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2556 if (ret != 0) {
2557 return ret;
2558 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002559 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002560#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002561
Jerry Yu71c14f12022-12-12 11:10:35 +08002562#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002563 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002564 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002565 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002566 if (ret != 0) {
2567 return ret;
2568 }
2569 p += output_len;
2570 }
2571#endif /* MBEDTLS_SSL_EARLY_DATA */
2572
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002573#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002574 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002575 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2576 ssl, p, end, &output_len);
2577 if (ret != 0) {
2578 return ret;
2579 }
2580 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002581 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002582#endif
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 extensions_len = (p - p_extensions_len) - 2;
2585 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002586
2587 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002590
Jerry Yu7de2ff02022-11-08 21:43:46 +08002591 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002595}
2596
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002597MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002598static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002599{
2600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2601 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002602 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 mbedtls_ssl_set_outbound_transform(ssl,
2605 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002606 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002610
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002611 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2612 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2613 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2616 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002617
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002618 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002619 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2620 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2623 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002624
Ronald Cron928cbd32022-10-04 16:14:26 +02002625#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2627 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2628 } else {
2629 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2630 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002631#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002633#endif
2634
Jerry Yu4d3841a2022-04-16 12:37:19 +08002635cleanup:
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2638 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002639}
2640
Ronald Cron928cbd32022-10-04 16:14:26 +02002641#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002642#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2643#define SSL_CERTIFICATE_REQUEST_SKIP 1
2644/* Coordination:
2645 * Check whether a CertificateRequest message should be written.
2646 * Returns a negative code on failure, or
2647 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2648 * - SSL_CERTIFICATE_REQUEST_SKIP
2649 * indicating if the writing of the CertificateRequest
2650 * should be skipped or not.
2651 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002652MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002653static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002654{
2655 int authmode;
2656
XiaokangQian40a35232022-05-07 09:02:40 +00002657#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002659 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002661#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002662 authmode = ssl->conf->authmode;
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002665 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002667 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002668
XiaokangQianc3017f62022-05-13 05:55:41 +00002669 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002672}
2673
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674/*
2675 * struct {
2676 * opaque certificate_request_context<0..2^8-1>;
2677 * Extension extensions<2..2^16-1>;
2678 * } CertificateRequest;
2679 *
2680 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002681MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002682static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2683 unsigned char *buf,
2684 const unsigned char *end,
2685 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686{
2687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2688 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002689 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690 unsigned char *p_extensions_len;
2691
2692 *out_len = 0;
2693
2694 /* Check if we have enough space:
2695 * - certificate_request_context (1 byte)
2696 * - extensions length (2 bytes)
2697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002699
2700 /*
2701 * Write certificate_request_context
2702 */
2703 /*
2704 * We use a zero length context for the normal handshake
2705 * messages. For post-authentication handshake messages
2706 * this request context would be set to a non-zero value.
2707 */
2708 *p++ = 0x0;
2709
2710 /*
2711 * Write extensions
2712 */
2713 /* The extensions must contain the signature_algorithms. */
2714 p_extensions_len = p;
2715 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2717 if (ret != 0) {
2718 return ret;
2719 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002720
XiaokangQianec6efb92022-05-06 09:53:10 +00002721 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723
2724 *out_len = p - buf;
2725
Jerry Yu7de2ff02022-11-08 21:43:46 +08002726 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730}
2731
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002733static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734{
2735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742 unsigned char *buf;
2743 size_t buf_len, msg_len;
2744
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002745 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2746 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2747 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2750 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002752 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002753 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2754 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2757 ssl, buf_len, msg_len));
2758 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2759 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002760 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 } else {
2762 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002763 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2764 goto cleanup;
2765 }
2766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002768cleanup:
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2771 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002772}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002773
Jerry Yu4d3841a2022-04-16 12:37:19 +08002774/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002775 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002776 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002777MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002778static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002779{
Jerry Yu5a26f302022-05-10 20:46:40 +08002780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002781
2782#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2784 mbedtls_ssl_own_cert(ssl) == NULL) {
2785 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2786 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2787 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2788 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002789 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2793 if (ret != 0) {
2794 return ret;
2795 }
2796 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2797 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002798}
2799
2800/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002801 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002802 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002803MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002804static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002805{
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2807 if (ret != 0) {
2808 return ret;
2809 }
2810 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2811 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002812}
Ronald Cron928cbd32022-10-04 16:14:26 +02002813#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002814
Jerry Yu9b72e392023-12-01 16:27:08 +08002815/*
2816 * RFC 8446 section A.2
2817 *
2818 * | Send ServerHello
2819 * | K_send = handshake
2820 * | Send EncryptedExtensions
2821 * | [Send CertificateRequest]
2822 * Can send | [Send Certificate + CertificateVerify]
2823 * app data | Send Finished
2824 * after --> | K_send = application
2825 * here +--------+--------+
2826 * No 0-RTT | | 0-RTT
2827 * | |
2828 * K_recv = handshake | | K_recv = early data
2829 * [Skip decrypt errors] | +------> WAIT_EOED -+
2830 * | | Recv | | Recv EndOfEarlyData
2831 * | | early data | | K_recv = handshake
2832 * | +------------+ |
2833 * | |
2834 * +> WAIT_FLIGHT2 <--------+
2835 * |
2836 * +--------+--------+
2837 * No auth | | Client auth
2838 * | |
2839 * | v
2840 * | WAIT_CERT
2841 * | Recv | | Recv Certificate
2842 * | empty | v
2843 * | Certificate | WAIT_CV
2844 * | | | Recv
2845 * | v | CertificateVerify
2846 * +-> WAIT_FINISHED <---+
2847 * | Recv Finished
2848 *
2849 *
2850 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002851 * above diagram. We are not going to receive early data related messages
2852 * anymore, prepare to receive the first handshake message of the client
2853 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002854 */
Jerry Yu3be85072023-12-04 09:58:54 +08002855static void ssl_tls13_prepare_for_handshake_second_flight(
2856 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002857{
Jerry Yu9b72e392023-12-01 16:27:08 +08002858 if (ssl->handshake->certificate_request_sent) {
2859 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2860 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002861 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002862 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002863
Jerry Yu9b72e392023-12-01 16:27:08 +08002864 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2865 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002866}
2867
Jerry Yu83da34e2022-04-16 13:59:52 +08002868/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002869 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002870 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002871MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002872static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002873{
Jerry Yud6e253d2022-05-18 13:59:24 +08002874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2877 if (ret != 0) {
2878 return ret;
2879 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2882 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002883 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2885 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2886 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002887 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002888
Jerry Yu87b5ed42022-12-12 13:07:07 +08002889#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002890 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002891 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002892 MBEDTLS_SSL_DEBUG_MSG(
2893 1, ("Switch to early keys for inbound traffic. "
2894 "( K_recv = early data )"));
2895 mbedtls_ssl_set_inbound_transform(
2896 ssl, ssl->handshake->transform_earlydata);
2897 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2898 return 0;
2899 }
2900#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002901 MBEDTLS_SSL_DEBUG_MSG(
2902 1, ("Switch to handshake keys for inbound traffic "
2903 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002905
Jerry Yu3be85072023-12-04 09:58:54 +08002906 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002907
2908 return 0;
2909}
2910
Jerry Yu87b5ed42022-12-12 13:07:07 +08002911#if defined(MBEDTLS_SSL_EARLY_DATA)
2912/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002913 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002914 */
Jerry Yu3be85072023-12-04 09:58:54 +08002915#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002916#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002917/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002918 * Deals with the ambiguity of not knowing if the next message is an
2919 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002920 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002921 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002922 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002923 * indicating which message is received.
2924 */
2925MBEDTLS_CHECK_RETURN_CRITICAL
2926static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2927{
2928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002929
2930 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2931 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2932 return ret;
2933 }
2934 ssl->keep_current_message = 1;
2935
2936 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2937 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002938 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002939 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002940 }
2941
2942 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002943 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002944 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002945 /* Set the reading pointer */
2946 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002947 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2948 if (ret != 0) {
2949 return ret;
2950 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002951 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002952 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002953 }
2954
Jerry Yu7bb40a32023-12-04 10:04:15 +08002955 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2956 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002957 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002958}
2959
2960MBEDTLS_CHECK_RETURN_CRITICAL
2961static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2962 const unsigned char *buf,
2963 const unsigned char *end)
2964{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002965 /* RFC 8446 section 4.5
2966 *
2967 * struct {} EndOfEarlyData;
2968 */
Jerry Yufbf03992023-12-04 10:00:37 +08002969 if (buf != end) {
2970 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2971 MBEDTLS_ERR_SSL_DECODE_ERROR);
2972 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2973 }
2974 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002975}
2976
Jerry Yud5c34962023-12-01 16:32:31 +08002977/*
2978 * RFC 8446 section A.2
2979 *
2980 * | Send ServerHello
2981 * | K_send = handshake
2982 * | Send EncryptedExtensions
2983 * | [Send CertificateRequest]
2984 * Can send | [Send Certificate + CertificateVerify]
2985 * app data | Send Finished
2986 * after --> | K_send = application
2987 * here +--------+--------+
2988 * No 0-RTT | | 0-RTT
2989 * | |
2990 * K_recv = handshake | | K_recv = early data
2991 * [Skip decrypt errors] | +------> WAIT_EOED -+
2992 * | | Recv | | Recv EndOfEarlyData
2993 * | | early data | | K_recv = handshake
2994 * | +------------+ |
2995 * | |
2996 * +> WAIT_FLIGHT2 <--------+
2997 * |
2998 * +--------+--------+
2999 * No auth | | Client auth
3000 * | |
3001 * | v
3002 * | WAIT_CERT
3003 * | Recv | | Recv Certificate
3004 * | empty | v
3005 * | Certificate | WAIT_CV
3006 * | | | Recv
3007 * | v | CertificateVerify
3008 * +-> WAIT_FINISHED <---+
3009 * | Recv Finished
3010 *
3011 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3012 * the above diagram.
3013 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003014MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003015static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003016{
3017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003018
3019 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3020
3021 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3022
Jerry Yu3be85072023-12-04 09:58:54 +08003023 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003024 unsigned char *buf;
3025 size_t buf_len;
3026
3027 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3028 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3029 &buf, &buf_len));
3030
3031 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3032 ssl, buf, buf + buf_len));
3033
Jerry Yue9655122023-12-01 16:44:40 +08003034 MBEDTLS_SSL_DEBUG_MSG(
3035 1, ("Switch to handshake keys for inbound traffic"
3036 "( K_recv = handshake )"));
3037 mbedtls_ssl_set_inbound_transform(
3038 ssl, ssl->handshake->transform_handshake);
3039
Jerry Yud5c34962023-12-01 16:32:31 +08003040 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3041 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3042 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003043
Jerry Yu3be85072023-12-04 09:58:54 +08003044 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003045
Jerry Yub55f9eb2023-12-05 10:27:17 +08003046 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003047 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3048 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003049 } else {
3050 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3051 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3052 goto cleanup;
3053 }
3054
Jerry Yud5c34962023-12-01 16:32:31 +08003055cleanup:
3056 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003057 return ret;
3058}
3059#endif /* MBEDTLS_SSL_EARLY_DATA */
3060
Jerry Yu69dd8d42022-04-16 12:51:26 +08003061/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003062 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003063 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003065static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003066{
Jerry Yud6e253d2022-05-18 13:59:24 +08003067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3070 if (ret != 0) {
3071 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003072 }
3073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3075 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003076 MBEDTLS_SSL_DEBUG_RET(
3077 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 }
3079
3080 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3081 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003082}
3083
3084/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003085 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003086 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003087MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003088static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003089{
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003093
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003094#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3095 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003096/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3097 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3098 * expected to be resolved with issue#6395.
3099 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003100 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003101 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003102 mbedtls_ssl_handshake_set_state(
3103 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003104 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003105#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003106 {
3107 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3108 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003110}
3111
Norbert Fabritiusda0d1692023-01-23 15:24:59 +01003112#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003113/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003114 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003115 */
3116#define SSL_NEW_SESSION_TICKET_SKIP 0
3117#define SSL_NEW_SESSION_TICKET_WRITE 1
3118MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003119static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003120{
3121 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (ssl->conf->f_ticket_write == NULL) {
3123 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3124 " callback is not set"));
3125 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (ssl->conf->new_session_tickets_count == 0) {
3128 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3129 " configured count is zero"));
3130 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003131 }
3132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if (ssl->handshake->new_session_tickets_count == 0) {
3134 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3135 "been sent."));
3136 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003137 }
3138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003140}
3141
Jerry Yue67bef42022-07-07 07:29:42 +00003142MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003143static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3144 unsigned char *ticket_nonce,
3145 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003146{
3147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3148 mbedtls_ssl_session *session = ssl->session;
3149 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3150 psa_algorithm_t psa_hash_alg;
3151 int hash_length;
3152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003154
Pengyu Lv9f926952022-11-17 15:22:33 +08003155 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003156 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003157 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003158#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003159 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003160 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003161#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003162
3163#if defined(MBEDTLS_SSL_EARLY_DATA)
3164 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3165 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003166 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003167 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003168 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003169 }
3170#endif /* MBEDTLS_SSL_EARLY_DATA */
3171
Pengyu Lv4938a562023-01-16 11:28:49 +08003172 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003173
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003174#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003175 if (session->ticket_alpn == NULL) {
3176 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3177 if (ret != 0) {
3178 return ret;
3179 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003180 }
3181#endif
3182
Jerry Yue67bef42022-07-07 07:29:42 +00003183 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3185 (unsigned char *) &session->ticket_age_add,
3186 sizeof(session->ticket_age_add)) != 0)) {
3187 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3188 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3191 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003192
3193 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3195 if (ret != 0) {
3196 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3197 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3200 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003201
3202 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003204 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3206 if (hash_length == -1 ||
3207 (size_t) hash_length > sizeof(session->resumption_key)) {
3208 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003209 }
3210
Jerry Yue67bef42022-07-07 07:29:42 +00003211 /* In this code the psk key length equals the length of the hash */
3212 session->resumption_key_len = hash_length;
3213 session->ciphersuite = ciphersuite_info->id;
3214
3215 /* Compute resumption key
3216 *
3217 * HKDF-Expand-Label( resumption_master_secret,
3218 * "resumption", ticket_nonce, Hash.length )
3219 */
3220 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 psa_hash_alg,
3222 session->app_secrets.resumption_master_secret,
3223 hash_length,
3224 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3225 ticket_nonce,
3226 ticket_nonce_size,
3227 session->resumption_key,
3228 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 if (ret != 0) {
3231 MBEDTLS_SSL_DEBUG_RET(2,
3232 "Creating the ticket-resumed PSK failed",
3233 ret);
3234 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3237 session->resumption_key,
3238 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3241 session->app_secrets.resumption_master_secret,
3242 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003245}
3246
3247/* This function creates a NewSessionTicket message in the following format:
3248 *
3249 * struct {
3250 * uint32 ticket_lifetime;
3251 * uint32 ticket_age_add;
3252 * opaque ticket_nonce<0..255>;
3253 * opaque ticket<1..2^16-1>;
3254 * Extension extensions<0..2^16-2>;
3255 * } NewSessionTicket;
3256 *
3257 * The ticket inside the NewSessionTicket message is an encrypted container
3258 * carrying the necessary information so that the server is later able to
3259 * re-start the communication.
3260 *
3261 * The following fields are placed inside the ticket by the
3262 * f_ticket_write() function:
3263 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003264 * - creation time (ticket_creation_time)
3265 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003266 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003267 * - key (resumption_key)
3268 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003269 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003270 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003271 */
3272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003273static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3274 unsigned char *buf,
3275 unsigned char *end,
3276 size_t *out_len,
3277 unsigned char *ticket_nonce,
3278 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003279{
3280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3281 unsigned char *p = buf;
3282 mbedtls_ssl_session *session = ssl->session;
3283 size_t ticket_len;
3284 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003285 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003286
3287 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003289
3290 /*
3291 * ticket_lifetime 4 bytes
3292 * ticket_age_add 4 bytes
3293 * ticket_nonce 1 + ticket_nonce_size bytes
3294 * ticket >=2 bytes
3295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003297
3298 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003299#if defined(MBEDTLS_HAVE_TIME)
3300 session->ticket_creation_time = mbedtls_ms_time();
3301#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3303 session,
3304 p + 9 + ticket_nonce_size + 2,
3305 end,
3306 &ticket_len,
3307 &ticket_lifetime);
3308 if (ret != 0) {
3309 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3310 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003311 }
Jerry Yuce794882023-11-22 15:01:18 +08003312
3313 /* RFC 8446 section 4.6.1
3314 *
Jerry Yue67bef42022-07-07 07:29:42 +00003315 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003316 * unsigned integer in network byte order from the time of ticket
3317 * issuance. Servers MUST NOT use any value greater than
3318 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003319 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003320 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003321 MBEDTLS_SSL_DEBUG_MSG(
3322 1, ("Ticket lifetime (%u) is greater than 7 days.",
3323 (unsigned int) ticket_lifetime));
3324 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 }
Jerry Yuce794882023-11-22 15:01:18 +08003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3328 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3329 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003330
3331 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3333 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3334 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003335
3336 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 p[8] = (unsigned char) ticket_nonce_size;
3338 if (ticket_nonce_size > 0) {
3339 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003340 }
3341 p += 9 + ticket_nonce_size;
3342
3343 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003345 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003347 p += ticket_len;
3348
3349 /* Ticket Extensions
3350 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003351 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003352 */
Jerry Yuedab6372022-10-31 14:37:31 +08003353 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003356 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003357 p += 2;
3358
Jerry Yu01da35e2022-12-12 15:09:22 +08003359#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003360 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003361 size_t output_len;
3362
Jerry Yuf135bac2023-11-23 18:10:51 +08003363 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003364 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003365 MBEDTLS_SSL_DEBUG_RET(
3366 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3367 return ret;
3368 }
3369 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003370 } else {
3371 MBEDTLS_SSL_DEBUG_MSG(
3372 4, ("early_data not allowed, "
3373 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003374 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003375
Jerry Yu01da35e2022-12-12 15:09:22 +08003376#endif /* MBEDTLS_SSL_EARLY_DATA */
3377
3378 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3379
Jerry Yue67bef42022-07-07 07:29:42 +00003380 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3382 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003383
Jerry Yu7de2ff02022-11-08 21:43:46 +08003384 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003388}
3389
3390/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003391 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003393static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003394{
3395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003400 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3401 unsigned char *buf;
3402 size_t buf_len, msg_len;
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3405 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003406
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003407 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3408 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3409 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3412 ssl, buf, buf + buf_len, &msg_len,
3413 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3416 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003417
Jerry Yu359e65f2022-09-22 23:47:43 +08003418 /* Limit session tickets count to one when resumption connection.
3419 *
3420 * See document of mbedtls_ssl_conf_new_session_tickets.
3421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003423 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003425 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003427
Jerry Yua8d3c502022-10-30 14:51:23 +08003428 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3430 } else {
3431 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003432 }
3433
Jerry Yue67bef42022-07-07 07:29:42 +00003434cleanup:
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003437}
3438#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3439
3440/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003441 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003443int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003444{
XiaokangQian08037552022-04-20 07:16:41 +00003445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3448 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3449 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003452 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003456 /* start state */
3457 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003459 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003460 break;
3461
XiaokangQian7807f9f2022-02-15 10:04:37 +00003462 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 ret = ssl_tls13_process_client_hello(ssl);
3464 if (ret != 0) {
3465 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3466 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003467 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003468
Jerry Yuf41553b2022-05-09 22:20:30 +08003469 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = ssl_tls13_write_hello_retry_request(ssl);
3471 if (ret != 0) {
3472 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3473 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003474 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003475 break;
3476
Jerry Yu5b64ae92022-03-30 17:15:02 +08003477 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003479 break;
3480
Xiaofei Baicba64af2022-02-15 10:00:56 +00003481 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ret = ssl_tls13_write_encrypted_extensions(ssl);
3483 if (ret != 0) {
3484 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3485 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003486 }
Jerry Yu48330562022-05-06 21:35:44 +08003487 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003488
Ronald Cron928cbd32022-10-04 16:14:26 +02003489#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003490 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003492 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003493
Jerry Yu83da34e2022-04-16 13:59:52 +08003494 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003496 break;
3497
3498 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003500 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003501#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 /*
3504 * Injection of dummy-CCS's for middlebox compatibility
3505 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003506#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003507 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3509 if (ret == 0) {
3510 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3511 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003512 break;
3513
3514 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003515 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3516 if (ret != 0) {
3517 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003519 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003520 break;
3521#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3522
Jerry Yu69dd8d42022-04-16 12:51:26 +08003523 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003525 break;
3526
Jerry Yu87b5ed42022-12-12 13:07:07 +08003527#if defined(MBEDTLS_SSL_EARLY_DATA)
3528 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003529 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003530 break;
3531#endif /* MBEDTLS_SSL_EARLY_DATA */
3532
Jerry Yu69dd8d42022-04-16 12:51:26 +08003533 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003535 break;
3536
Jerry Yu69dd8d42022-04-16 12:51:26 +08003537 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003539 break;
3540
Ronald Cron766c0cd2022-10-18 12:17:11 +02003541#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003542 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3544 if (ret == 0) {
3545 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003546 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3548 } else {
3549 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003550 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003552 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003553 }
3554 break;
3555
3556 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3558 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003559 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003561 }
3562 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003563#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003564
Jerry Yue67bef42022-07-07 07:29:42 +00003565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003566 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 ret = ssl_tls13_write_new_session_ticket(ssl);
3568 if (ret != 0) {
3569 MBEDTLS_SSL_DEBUG_RET(1,
3570 "ssl_tls13_write_new_session_ticket ",
3571 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003572 }
3573 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003574 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003575 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003576 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003577 * as part of ssl_prepare_handshake_step.
3578 */
3579 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003580
Gilles Peskine449bd832023-01-11 14:50:10 +01003581 if (ssl->handshake->new_session_tickets_count == 0) {
3582 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3583 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003584 mbedtls_ssl_handshake_set_state(
3585 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 }
Jerry Yue67bef42022-07-07 07:29:42 +00003587 break;
3588
3589#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3590
XiaokangQian7807f9f2022-02-15 10:04:37 +00003591 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3593 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003594 }
3595
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003597}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003598
Jerry Yufb4b6472022-01-27 15:03:26 +08003599#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */