blob: 7273eb939270102c1448abe5ffff28bedf9b69a8 [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08009
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_tls13_keys.h"
20#include "ssl_debug_helpers.h"
21
Jerry Yuf35ba382022-08-23 17:58:26 +080022
Jerry Yuc5a23a02022-08-25 10:51:44 +080023static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010024 mbedtls_ssl_context *ssl,
25 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080026{
27 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010028 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
29 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080030 }
Gilles Peskine449bd832023-01-11 14:50:10 +010031
32 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
33 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
34 ssl->tls_version,
35 ssl->tls_version) != 0)) {
36 return NULL;
37 }
38 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080039}
40
Ronald Cron89089cc2024-02-14 18:18:08 +010041static void ssl_tls13_select_ciphersuite(
42 mbedtls_ssl_context *ssl,
43 const unsigned char *cipher_suites,
44 const unsigned char *cipher_suites_end,
45 int psk_ciphersuite_id,
46 psa_algorithm_t psk_hash_alg,
47 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
48{
49 *selected_ciphersuite_info = NULL;
50
51 /*
Ronald Cron38117652024-03-05 09:05:46 +010052 * In a compliant ClientHello the byte-length of the list of ciphersuites
53 * is even and this function relies on this fact. This should have been
54 * checked in the main ClientHello parsing function. Double check here.
Ronald Cron89089cc2024-02-14 18:18:08 +010055 */
56 if ((cipher_suites_end - cipher_suites) & 1) {
57 return;
58 }
59
60 for (const unsigned char *p = cipher_suites;
61 p < cipher_suites_end; p += 2) {
62 /*
63 * "cipher_suites_end - p is even" is an invariant of the loop. As
64 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
65 * is thus safe to read two bytes.
66 */
67 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
68
69 const mbedtls_ssl_ciphersuite_t *info =
70 ssl_tls13_validate_peer_ciphersuite(ssl, id);
71 if (info == NULL) {
72 continue;
73 }
74
75 /*
Ronald Cron7cab4f82024-03-07 15:09:09 +010076 * If a valid PSK ciphersuite identifier has been passed in, we want
77 * an exact match.
Ronald Cron89089cc2024-02-14 18:18:08 +010078 */
79 if (psk_ciphersuite_id != 0) {
80 if (id != psk_ciphersuite_id) {
81 continue;
82 }
83 } else if (psk_hash_alg != PSA_ALG_NONE) {
84 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
85 psk_hash_alg) {
86 continue;
87 }
88 }
89
90 *selected_ciphersuite_info = info;
91 return;
92 }
93
Gilles Peskinea9d4ef02024-06-03 22:16:23 +020094 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
95 (unsigned) psk_ciphersuite_id,
96 (unsigned long) psk_hash_alg));
Ronald Cron89089cc2024-02-14 18:18:08 +010097}
98
Ronald Cron41a443a2022-10-04 16:38:25 +020099#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +0000100/* From RFC 8446:
101 *
102 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
103 * struct {
104 * PskKeyExchangeMode ke_modes<1..255>;
105 * } PskKeyExchangeModes;
106 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800107MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100108static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
109 const unsigned char *buf,
110 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000111{
Jerry Yu299e31f2022-07-13 23:06:36 +0800112 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000113 size_t ke_modes_len;
114 int ke_modes = 0;
115
Jerry Yu854dd9e2022-07-15 14:28:27 +0800116 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800118 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000119 /* Currently, there are only two PSK modes, so even without looking
120 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (ke_modes_len > 2) {
122 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
123 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
124 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800125 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 while (ke_modes_len-- != 0) {
130 switch (*p++) {
131 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
132 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
133 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
134 break;
135 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
136 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
137 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
138 break;
139 default:
140 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
141 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
142 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000143 }
144 }
145
146 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000148}
Jerry Yu1c105562022-07-10 06:32:38 +0000149
Ronald Cron38117652024-03-05 09:05:46 +0100150/*
151 * Non-error return values of
152 * ssl_tls13_offered_psks_check_identity_match_ticket() and
153 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
154 * not collide with error codes that are negative. Zero
155 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
156 * up by the callers of this function as a generic success condition.
157 *
158 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
Ronald Cron7cab4f82024-03-07 15:09:09 +0100159 * that the pre-shared-key identity matches that of a ticket or an externally-
Ronald Cron38117652024-03-05 09:05:46 +0100160 * provisioned pre-shared-key. We have thus been able to retrieve the
161 * attributes of the pre-shared-key but at least one of them does not meet
162 * some criteria and the pre-shared-key cannot be used. For example, a ticket
163 * is expired or its version is not TLS 1.3. Note eventually that the return
164 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
165 * anything to do with binder check. A binder check is done only when a
166 * suitable pre-shared-key has been selected and only for that selected
167 * pre-shared-key: if the binder check fails, we fail the handshake and we do
168 * not try to find another pre-shared-key for which the binder check would
169 * succeed as recommended by the specification.
170 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100171#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
172#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
173#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800174
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800176static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800177MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Ronald Cron1f045f32024-03-25 13:37:07 +0100180#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu95699e72022-08-21 19:22:23 +0800181MBEDTLS_CHECK_RETURN_CRITICAL
182static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_context *ssl,
184 const unsigned char *identity,
185 size_t identity_len,
186 uint32_t obfuscated_ticket_age,
187 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800188{
Jerry Yufd310eb2022-09-06 09:16:35 +0800189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800190 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800191#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800192 mbedtls_ms_time_t now;
193 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800194 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800195 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800196#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800197
198 ((void) obfuscated_ticket_age);
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800201
Jerry Yufd310eb2022-09-06 09:16:35 +0800202 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100204 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Jerry Yu95699e72022-08-21 19:22:23 +0800206
Jerry Yu4746b102022-09-13 11:11:48 +0800207 /* We create a copy of the encrypted ticket since the ticket parsing
208 * function is allowed to use its input buffer as an output buffer
209 * (in-place decryption). We do, however, need the original buffer for
210 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ticket_buffer = mbedtls_calloc(1, identity_len);
213 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100221 switch (ret) {
222 case 0:
223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
224 break;
225
226 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100229 break;
230
231 case MBEDTLS_ERR_SSL_INVALID_MAC:
232 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100234 break;
235
236 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100238 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800239 }
240
241 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800243
Ronald Cronfbae94a2023-12-05 18:15:14 +0100244 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 goto exit;
246 }
Jerry Yu95699e72022-08-21 19:22:23 +0800247
Ronald Cron38117652024-03-05 09:05:46 +0100248 /*
249 * The identity matches that of a ticket. Now check that it has suitable
250 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800251 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100252 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
255 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800256 goto exit;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800260 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100261
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800264 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
265 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 goto exit;
268 }
269
Jerry Yu25ba4d42023-11-10 14:12:20 +0800270 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800271
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 /* RFC 8446 section 4.6.1
273 *
274 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
275 *
276 * RFC 8446 section 4.2.11.1
277 *
278 * Clients MUST NOT attempt to use tickets which have ages greater than
279 * the "ticket_lifetime" value which was provided with the ticket.
280 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800282 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800284 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800285 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 goto exit;
287 }
Jerry Yu95699e72022-08-21 19:22:23 +0800288
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800289 /* RFC 8446 section 4.2.10
290 *
291 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
292 * the ticket age for the selected PSK identity (computed by subtracting
293 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
294 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800295 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
297 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800298 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800299 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800300 */
Jerry Yucebffc32022-12-15 18:00:05 +0800301 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800302 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800303 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800306 3, ("Ticket age outside tolerance window ( diff = %"
307 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800308 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309 goto exit;
310 }
Jerry Yu95699e72022-08-21 19:22:23 +0800311#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312
Ronald Cron38117652024-03-05 09:05:46 +0100313 /*
314 * All good, we have found a suitable ticket.
315 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100316 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
317
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800318exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100319 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ssl_session_free(session);
321 }
Jerry Yu95699e72022-08-21 19:22:23 +0800322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
324 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800325}
326#endif /* MBEDTLS_SSL_SESSION_TICKETS */
327
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000329static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ssl_context *ssl,
331 const unsigned char *identity,
332 size_t identity_len,
333 uint32_t obfuscated_ticket_age,
334 int *psk_type,
335 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000336{
Ronald Cron606671e2023-02-17 11:36:33 +0100337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338
Jerry Yu95699e72022-08-21 19:22:23 +0800339 ((void) session);
340 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800341 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800344
Jerry Yu95699e72022-08-21 19:22:23 +0800345#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100346 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
347 ssl, identity, identity_len, obfuscated_ticket_age, session);
348 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800349 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100350 ret = mbedtls_ssl_set_hs_psk(ssl,
351 session->resumption_key,
352 session->resumption_key_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357
358 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
359 session->resumption_key,
360 session->resumption_key_len);
361 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
362 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100363 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100364 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
365 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800366 }
367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
368
Jerry Yu1c105562022-07-10 06:32:38 +0000369 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->conf->f_psk != NULL) {
371 if (ssl->conf->f_psk(
372 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100373 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100375 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800381 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ct_memcmp(ssl->conf->psk_identity,
383 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100384 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
385 if (ret != 0) {
386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
387 return ret;
388 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100389 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000390 }
391
Ronald Cronfbae94a2023-12-05 18:15:14 +0100392 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000393}
394
Ronald Cron38117652024-03-05 09:05:46 +0100395/*
396 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
397 * They are positive to not collide with error codes that are negative. Zero
398 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
399 * by the callers of this function as a generic success condition.
400 */
Ronald Cron6e311272023-12-05 17:57:01 +0100401#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
402#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800403MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000404static int ssl_tls13_offered_psks_check_binder_match(
405 mbedtls_ssl_context *ssl,
406 const unsigned char *binder, size_t binder_len,
407 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800413 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800414 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800415 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000416
Ronald Crona5c5c582024-03-19 13:54:15 +0100417 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
418 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
419 }
420
Jerry Yu1c105562022-07-10 06:32:38 +0000421 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800422 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200423 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 transcript, sizeof(transcript), &transcript_len);
425 if (ret != 0) {
426 return ret;
427 }
Jerry Yu1c105562022-07-10 06:32:38 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
430 if (ret != 0) {
431 return ret;
432 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
435 psk, psk_len, psk_type,
436 transcript,
437 server_computed_binder);
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_free((void *) psk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (ret != 0) {
440 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
441 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000442 }
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
445 server_computed_binder, transcript_len);
446 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000447
Ronald Crona5c5c582024-03-19 13:54:15 +0100448 if (mbedtls_ct_memcmp(server_computed_binder,
449 binder,
450 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100451 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_platform_zeroize(server_computed_binder,
455 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100456 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800457}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800458
Jerry Yu82534862022-08-30 10:42:33 +0800459#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800460MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100461static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
462 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800463{
Jerry Yu82534862022-08-30 10:42:33 +0800464 dst->ticket_age_add = src->ticket_age_add;
465 dst->ticket_flags = src->ticket_flags;
466 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (src->resumption_key_len == 0) {
468 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
469 }
470 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800471
Jerry Yu33bf2402022-12-12 16:01:43 +0800472#if defined(MBEDTLS_SSL_EARLY_DATA)
473 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000474
475#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000476 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000477 if (ret != 0) {
478 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000479 }
480#endif /* MBEDTLS_SSL_ALPN */
481#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800484}
485#endif /* MBEDTLS_SSL_SESSION_TICKETS */
486
Ronald Cron79cdd412024-02-20 17:19:28 +0100487struct psk_attributes {
488 int type;
489 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100490 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100491};
Ronald Cron16cc3702024-03-07 15:04:56 +0100492#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100493
Jerry Yu1c105562022-07-10 06:32:38 +0000494/* Parser for pre_shared_key extension in client hello
495 * struct {
496 * opaque identity<1..2^16-1>;
497 * uint32 obfuscated_ticket_age;
498 * } PskIdentity;
499 *
500 * opaque PskBinderEntry<32..255>;
501 *
502 * struct {
503 * PskIdentity identities<7..2^16-1>;
504 * PskBinderEntry binders<33..2^16-1>;
505 * } OfferedPsks;
506 *
507 * struct {
508 * select (Handshake.msg_type) {
509 * case client_hello: OfferedPsks;
510 * ....
511 * };
512 * } PreSharedKeyExtension;
513 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800514MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000515static int ssl_tls13_parse_pre_shared_key_ext(
516 mbedtls_ssl_context *ssl,
517 const unsigned char *pre_shared_key_ext,
518 const unsigned char *pre_shared_key_ext_end,
519 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100520 const unsigned char *ciphersuites_end,
521 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000522{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800524 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800525 const unsigned char *p_identity_len;
526 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000527 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800528 const unsigned char *binders;
529 const unsigned char *p_binder_len;
530 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000531 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800532 int matched_identity = -1;
533 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
536 pre_shared_key_ext,
537 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000538
Jerry Yu96a2e362022-07-21 15:11:34 +0800539 /* identities_len 2 bytes
540 * identities_data >= 7 bytes
541 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
543 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
546 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800547 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000548
Jerry Yu96a2e362022-07-21 15:11:34 +0800549 /* binders_len 2 bytes
550 * binders >= 33 bytes
551 */
Jerry Yu568ec252022-07-22 21:27:34 +0800552 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
554 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800555 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000558
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100559 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
560 identities_end - pre_shared_key_ext);
561 if (0 != ret) {
562 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
563 return ret;
564 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000567 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800568 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800569 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800570 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800571 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100572 int psk_ciphersuite_id;
573 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100574 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100575
Jerry Yu82534862022-08-30 10:42:33 +0800576 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
580 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
583 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800587 binder_len = *p_binder_len;
588 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800590 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800591
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000594 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Jerry Yu1c105562022-07-10 06:32:38 +0000596
Jerry Yu96a2e362022-07-21 15:11:34 +0800597 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100599 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100600 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800601 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100605
Ronald Cron79cdd412024-02-20 17:19:28 +0100606 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800607 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100608 psk_ciphersuite_id = 0;
609 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100610 allowed_key_exchange_modes =
611 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800612 break;
Jerry Yu82534862022-08-30 10:42:33 +0800613#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100614 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100615 psk_ciphersuite_id = session.ciphersuite;
616 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100617 ssl->session_negotiate->ticket_flags = session.ticket_flags;
618 allowed_key_exchange_modes =
619 session.ticket_flags &
620 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800621 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100622#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100626
Ronald Cron79cdd412024-02-20 17:19:28 +0100627 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
628
Ronald Croncf284562024-02-16 18:54:10 +0100629 if ((allowed_key_exchange_modes &
630 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
631 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100632 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100633 } else if ((allowed_key_exchange_modes &
634 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
635 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100636 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100637 }
638
Ronald Cron79cdd412024-02-20 17:19:28 +0100639 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100640 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
641 continue;
642 }
643
Ronald Cron89089cc2024-02-14 18:18:08 +0100644 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
645 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100646 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100647
Ronald Cron74a16292024-02-23 17:07:41 +0100648 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100649#if defined(MBEDTLS_SSL_SESSION_TICKETS)
650 mbedtls_ssl_session_free(&session);
651#endif
652 /*
653 * We consider finding a ciphersuite suitable for the PSK as part
654 * of the validation of its binder. Thus if we do not find one, we
655 * abort the handshake with a decrypt_error alert.
656 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800657 MBEDTLS_SSL_PEND_FATAL_ALERT(
658 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100660 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800661 }
662
Jerry Yu96a2e362022-07-21 15:11:34 +0800663 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100664 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100665 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100666 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800667 /* For security reasons, the handshake should be aborted when we
668 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
669 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800670#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800672#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000674 MBEDTLS_SSL_DEBUG_RET(
675 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800676 MBEDTLS_SSL_PEND_FATAL_ALERT(
677 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
679 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800680 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800681
682 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800683
Jerry Yu82534862022-08-30 10:42:33 +0800684#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100685 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
687 &session);
688 mbedtls_ssl_session_free(&session);
689 if (ret != 0) {
690 return ret;
691 }
Jerry Yu82534862022-08-30 10:42:33 +0800692 }
693#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (p_identity_len != identities_end || p_binder_len != binders_end) {
697 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
698 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
699 MBEDTLS_ERR_SSL_DECODE_ERROR);
700 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000701 }
702
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800703 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000704 ret = ssl->handshake->update_checksum(
705 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100706 if (0 != ret) {
707 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
708 return ret;
709 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100711 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000713 }
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 ssl->handshake->selected_identity = (uint16_t) matched_identity;
716 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000719}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720
721/*
722 * struct {
723 * select ( Handshake.msg_type ) {
724 * ....
725 * case server_hello:
726 * uint16 selected_identity;
727 * }
728 * } PreSharedKeyExtension;
729 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
731 unsigned char *buf,
732 unsigned char *end,
733 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734{
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736
737 *olen = 0;
738
David Horstmann21b89762022-10-06 18:34:28 +0100739 int not_using_psk = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000742 /* We shouldn't have called this extension writer unless we've
743 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745 }
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
748 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
751 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000754
755 *olen = 6;
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
758 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000763}
764
Ronald Cron41a443a2022-10-04 16:38:25 +0200765#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000766
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767/* From RFC 8446:
768 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000769 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770 * } SupportedVersions;
771 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200772MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100773static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
774 const unsigned char *buf,
775 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000778 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000779 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000780 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000784 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 p += 1;
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000788 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 while (p < versions_end) {
790 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
791 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000792 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793
Ronald Cron3bd2b022023-04-03 16:45:39 +0200794 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100795 found_supported_version = 1;
796 break;
797 }
798
Ronald Cron3bd2b022023-04-03 16:45:39 +0200799 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
800 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100801 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 break;
803 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 }
805
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100806 if (!found_supported_version) {
807 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
810 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
811 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812 }
813
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100814 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100817 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818}
819
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200820#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000821/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822 *
823 * From RFC 8446:
824 * enum {
825 * ... (0xFFFF)
826 * } NamedGroup;
827 * struct {
828 * NamedGroup named_group_list<2..2^16-1>;
829 * } NamedGroupList;
830 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
833 const unsigned char *buf,
834 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000837 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000838 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
841 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
842 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000845 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000846 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000849 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
851 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000852 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_SSL_DEBUG_MSG(2,
855 ("got named group: %s(%04x)",
856 mbedtls_ssl_named_group_to_str(named_group),
857 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
860 !mbedtls_ssl_named_group_is_supported(named_group) ||
861 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000862 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863 }
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 MBEDTLS_SSL_DEBUG_MSG(2,
866 ("add named group %s(%04x) into received list.",
867 mbedtls_ssl_named_group_to_str(named_group),
868 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800869
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000870 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874
875}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200876#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
XiaokangQian08037552022-04-20 07:16:41 +0000878#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
879
Valerio Settic9ae8622023-07-25 11:23:50 +0200880#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881/*
882 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000883 * extension is correct and stores the first acceptable key share and its
884 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885 *
886 * Possible return values are:
887 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000888 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
889 * the client does not match a group supported by the server. A
890 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000891 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800892 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200893MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100894static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
895 const unsigned char *buf,
896 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897{
XiaokangQianb67384d2022-04-19 00:02:38 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000900 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800901 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /* From RFC 8446:
904 *
905 * struct {
906 * KeyShareEntry client_shares<0..2^16-1>;
907 * } KeyShareClientHello;
908 *
909 */
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
912 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915
916 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000917 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000918
919 /* We try to find a suitable key share entry and copy it to the
920 * handshake context. Later, we have to find out whether we can do
921 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000922 * dismiss it and send a HelloRetryRequest message.
923 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000926 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800927 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800928 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929
930 /*
931 * struct {
932 * NamedGroup group;
933 * opaque key_exchange<1..2^16-1>;
934 * } KeyShareEntry;
935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
937 group = MBEDTLS_GET_UINT16_BE(p, 0);
938 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800939 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800940 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800942 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943
944 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000945 * for input validation purposes.
946 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
948 !mbedtls_ssl_named_group_is_supported(group) ||
949 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000950 continue;
951 }
XiaokangQian060d8672022-04-21 09:24:56 +0000952
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200954 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200956 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200957 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200958 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_ssl_named_group_to_str(group),
960 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200961 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ssl, key_exchange - 2, key_exchange_len + 2);
963 if (ret != 0) {
964 return ret;
965 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 } else {
968 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
969 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970 continue;
971 }
972
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000973 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000974 }
975
Jerry Yuc1be19f2022-04-23 16:11:39 +0800976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (ssl->handshake->offered_group_id == 0) {
978 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
979 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982}
Valerio Settic9ae8622023-07-25 11:23:50 +0200983#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000984
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
987 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988{
Jerry Yu0c354a22022-08-29 15:25:36 +0800989 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000991}
992
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000995static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return ssl_tls13_client_hello_has_exts(
999 ssl,
1000 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1001 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1002 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001003}
Jerry Yue5991322022-11-07 14:03:44 +08001004#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001005
Jerry Yue5991322022-11-07 14:03:44 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001007MBEDTLS_CHECK_RETURN_CRITICAL
1008static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001010{
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return ssl_tls13_client_hello_has_exts(
1012 ssl,
1013 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1014 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001015}
Jerry Yue5991322022-11-07 14:03:44 +08001016#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001017
Jerry Yue5991322022-11-07 14:03:44 +08001018#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001019MBEDTLS_CHECK_RETURN_CRITICAL
1020static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001022{
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return ssl_tls13_client_hello_has_exts(
1024 ssl,
1025 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1026 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1027 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1028 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001029}
Jerry Yue5991322022-11-07 14:03:44 +08001030#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001031
Pengyu Lv306a01d2023-02-02 16:35:47 +08001032#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1033MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001034static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001035{
Jerry Yue5991322022-11-07 14:03:44 +08001036#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001037 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001038 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001040#else
1041 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001044}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045
Jerry Yu77f01482022-07-11 07:03:24 +00001046MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001047static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001048{
Jerry Yue5991322022-11-07 14:03:44 +08001049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001050 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001051 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001053#else
1054 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001056#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001057}
1058#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1059
1060MBEDTLS_CHECK_RETURN_CRITICAL
1061static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1062{
1063#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1064 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1065 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1066#else
1067 ((void) ssl);
1068 return 0;
1069#endif
1070}
1071
XiaokangQian81802f42022-06-10 13:25:22 +00001072#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001073 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001076{
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001096 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001098 }
1099}
Ronald Cron67ea2542022-09-15 17:34:42 +02001100
XiaokangQian23c5be62022-06-07 02:04:34 +00001101/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001102 * Pick best ( private key, certificate chain ) pair based on the signature
1103 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001104 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001105MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001106static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001107{
XiaokangQianfb665a82022-06-15 03:57:21 +00001108 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001109 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001110
1111#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001113 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001115#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (key_cert_list == NULL) {
1119 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1120 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001121 }
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1124 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 for (key_cert = key_cert_list; key_cert != NULL;
1133 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 psa_algorithm_t psa_alg = PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1137 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001138
1139 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 * This avoids sending the client a cert it'll reject based on
1141 * keyUsage or other extensions.
1142 */
1143 if (mbedtls_x509_crt_check_key_usage(
1144 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001145 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001146 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1148 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1149 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001150 continue;
1151 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_SSL_DEBUG_MSG(3,
1154 ("ssl_tls13_pick_key_cert:"
1155 "check signature algorithm %s [%04x]",
1156 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1157 *sig_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1161 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001162 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1164 PSA_KEY_USAGE_SIGN_HASH) == 1
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001166 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_SSL_DEBUG_MSG(3,
1168 ("ssl_tls13_pick_key_cert:"
1169 "selected signature algorithm"
1170 " %s [%04x]",
1171 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1172 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001173 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 3, "selected certificate (chain)",
1175 ssl->handshake->key_cert->cert);
1176 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001177 }
XiaokangQian81802f42022-06-10 13:25:22 +00001178 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1182 "no suitable certificate found"));
1183 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001184}
XiaokangQian81802f42022-06-10 13:25:22 +00001185#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001186 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001187
XiaokangQian4080a7f2022-04-11 09:55:18 +00001188/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001189 *
1190 * STATE HANDLING: ClientHello
1191 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001192 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001193 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001194 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001195 *
1196 * In this case, the server progresses to sending its ServerHello.
1197 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001198 * 2) The ClientHello was well-formed but didn't match the server's
1199 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001200 *
1201 * For example, the client might not have offered a key share which
1202 * the server supports, or the server might require a cookie.
1203 *
1204 * In this case, the server sends a HelloRetryRequest.
1205 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001206 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 *
1208 * In this case, we abort the handshake.
1209 *
1210 */
1211
1212/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001213 * Structure of this message:
1214 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001215 * uint16 ProtocolVersion;
1216 * opaque Random[32];
1217 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001218 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001219 * struct {
1220 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1221 * Random random;
1222 * opaque legacy_session_id<0..32>;
1223 * CipherSuite cipher_suites<2..2^16-2>;
1224 * opaque legacy_compression_methods<1..2^8-1>;
1225 * Extension extensions<8..2^16-1>;
1226 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001227 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001228
1229#define SSL_CLIENT_HELLO_OK 0
1230#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001231#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001232
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001233MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001234static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1235 const unsigned char *buf,
1236 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001237{
XiaokangQianb67384d2022-04-19 00:02:38 +00001238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1239 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001240 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001241 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001242 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001243 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001244 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001245 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001248 const unsigned char *supported_versions_data;
1249 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001250 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001251 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001252 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001253
Ronald Cron41a443a2022-10-04 16:38:25 +02001254#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001255 int got_psk = 0;
1256 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001257 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001258 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001259#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001260
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001262 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001263 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001264 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001265 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001266 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001267 * .. . .. ciphersuite list length ( 2 bytes )
1268 * .. . .. ciphersuite list
1269 * .. . .. compression alg. list length ( 1 byte )
1270 * .. . .. compression alg. list
1271 * .. . .. extensions length ( 2 bytes, optional )
1272 * .. . .. extensions ( optional )
1273 */
1274
XiaokangQianb67384d2022-04-19 00:02:38 +00001275 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001276 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1278 * read at least up to session id length without worrying.
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281
1282 /* ...
1283 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1284 * ...
1285 * with ProtocolVersion defined as:
1286 * uint16 ProtocolVersion;
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1289 MBEDTLS_SSL_VERSION_TLS1_2) {
1290 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1291 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1292 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1293 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294 }
1295 p += 2;
1296
Jerry Yuc1be19f2022-04-23 16:11:39 +08001297 /* ...
1298 * Random random;
1299 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001300 * with Random defined as:
1301 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 */
Ronald Crond540d992023-03-07 09:41:48 +01001303 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001304 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001305
Jerry Yuc1be19f2022-04-23 16:11:39 +08001306 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001307 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001308 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 */
Ronald Croncada4102023-03-07 09:51:39 +01001310 legacy_session_id_len = *(p++);
1311 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
XiaokangQianb67384d2022-04-19 00:02:38 +00001313 /*
1314 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001315 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001318 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001320 /* ...
1321 * CipherSuite cipher_suites<2..2^16-2>;
1322 * ...
1323 * with CipherSuite defined as:
1324 * uint8 CipherSuite[2];
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001328 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329
Ronald Cronfc7ae872023-02-16 15:32:19 +01001330 /*
1331 * The length of the ciphersuite list has to be even.
1332 */
1333 if (cipher_suites_len & 1) {
1334 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1335 MBEDTLS_ERR_SSL_DECODE_ERROR);
1336 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1337 }
1338
XiaokangQianb67384d2022-04-19 00:02:38 +00001339 /* Check we have enough data for the ciphersuite list, the legacy
1340 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001341 *
1342 * cipher_suites cipher_suites_len bytes
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001343 * legacy_compression_methods length 1 byte
XiaokangQianb67384d2022-04-19 00:02:38 +00001344 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001345 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 1);
Ronald Cron8c527d02023-03-07 15:47:47 +01001346 p += cipher_suites_len;
1347 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001348
Waleed Elmelegy0a9e8a32024-06-25 10:22:49 +01001349 /* Check if we have enough data for legacy_compression_methods
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001350 * and the length of the extensions (2 bytes).
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001351 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001352 MBEDTLS_SSL_CHK_BUF_READ_PTR(p + 1, end, p[0] + 2);
Waleed Elmelegyb6e73312024-06-11 18:44:48 +01001353
Jerry Yu5725f1c2022-08-21 17:27:16 +08001354 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001355 * Search for the supported versions extension and parse it to determine
1356 * if the client supports TLS 1.3.
1357 */
1358 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001359 ssl, p + 1 + p[0], end,
Ronald Croneff56732023-04-03 17:36:31 +02001360 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001361 if (ret < 0) {
1362 MBEDTLS_SSL_DEBUG_RET(1,
1363 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1364 return ret;
1365 }
1366
1367 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001368 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001369 }
1370
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001371 if (ret == 1) {
1372 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001373 supported_versions_data,
1374 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001375 if (ret < 0) {
1376 MBEDTLS_SSL_DEBUG_RET(1,
1377 ("ssl_tls13_parse_supported_versions_ext"), ret);
1378 return ret;
1379 }
1380
Ronald Cronb828c7d2023-04-03 16:37:22 +02001381 /*
1382 * The supported versions extension was parsed successfully as the
1383 * value returned by ssl_tls13_parse_supported_versions_ext() is
1384 * positive. The return value is then equal to
1385 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1386 * the TLS version to negotiate.
1387 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001388 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1389 return SSL_CLIENT_HELLO_TLS1_2;
1390 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001391 }
1392
1393 /*
1394 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001395 */
1396 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001397 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1398 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001399
1400 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001401 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001402 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001403 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001404 */
1405 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1406 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1407 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1408
Ronald Croncada4102023-03-07 09:51:39 +01001409 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1411 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1412 }
1413 ssl->session_negotiate->id_len = legacy_session_id_len;
1414 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1415 legacy_session_id, legacy_session_id_len);
1416 memcpy(&ssl->session_negotiate->id[0],
1417 legacy_session_id, legacy_session_id_len);
1418
Ronald Crond540d992023-03-07 09:41:48 +01001419 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001420 * Search for a matching ciphersuite
1421 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001422 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1423 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001424
Ronald Cron89089cc2024-02-14 18:18:08 +01001425 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1426 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (handshake->ciphersuite_info == NULL) {
1429 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1430 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1431 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001432 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001433 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1434
1435 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1436 ((unsigned) handshake->ciphersuite_info->id),
1437 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001438
XiaokangQian4080a7f2022-04-11 09:55:18 +00001439 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001440 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001441 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1444 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1445 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1446 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1447 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001448 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001449 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001450
Jerry Yuc1be19f2022-04-23 16:11:39 +08001451 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001452 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001453 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001454 * with Extension defined as:
1455 * struct {
1456 * ExtensionType extension_type;
1457 * opaque extension_data<0..2^16-1>;
1458 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001461 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001463 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001466 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469 unsigned int extension_type;
1470 size_t extension_data_len;
1471 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001472 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1473
Ronald Cron5fbd2702024-02-14 10:03:36 +01001474 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001475 /* Do not accept early data extension in 2nd ClientHello */
1476 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1477 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001478
Jerry Yu0c354a22022-08-29 15:25:36 +08001479 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001480 *
1481 * The "pre_shared_key" extension MUST be the last extension in the
1482 * ClientHello (this facilitates implementation as described below).
1483 * Servers MUST check that it is the last extension and otherwise fail
1484 * the handshake with an "illegal_parameter" alert.
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001487 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001489 MBEDTLS_SSL_PEND_FATAL_ALERT(
1490 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1492 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001493 }
1494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1496 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1497 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001498 p += 4;
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501 extension_data_end = p + extension_data_len;
1502
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001503 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001505 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (ret != 0) {
1507 return ret;
1508 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001511#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1512 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1514 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1515 extension_data_end);
1516 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001517 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 1, "mbedtls_ssl_parse_servername_ext", ret);
1519 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001520 }
XiaokangQian40a35232022-05-07 09:02:40 +00001521 break;
1522#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1523
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001524#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001525 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527
1528 /* Supported Groups Extension
1529 *
1530 * When sent by the client, the "supported_groups" extension
1531 * indicates the named groups which the client supports,
1532 * ordered from most preferred to least preferred.
1533 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001534 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 ssl, p, extension_data_end);
1536 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001537 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001538 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001540 }
1541
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001543#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001544
Valerio Settic9ae8622023-07-25 11:23:50 +02001545#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548
1549 /*
1550 * Key Share Extension
1551 *
1552 * When sent by the client, the "key_share" extension
1553 * contains the endpoint's cryptographic parameters for
1554 * ECDHE/DHE key establishment methods.
1555 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001556 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 ssl, p, extension_data_end);
1558 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001559 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1560 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001564 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 1, "ssl_tls13_parse_key_shares_ext", ret);
1566 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001567 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001570#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571
1572 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001573 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001574 break;
1575
Ronald Cron41a443a2022-10-04 16:38:25 +02001576#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001577 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001578 MBEDTLS_SSL_DEBUG_MSG(
1579 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001580
1581 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 ssl, p, extension_data_end);
1583 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001584 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1586 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001587 }
1588
Jerry Yue19e3b92022-07-08 12:04:51 +00001589 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001590#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001591
Jerry Yu1c105562022-07-10 06:32:38 +00001592 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1594 if ((handshake->received_extensions &
1595 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001596 MBEDTLS_SSL_PEND_FATAL_ALERT(
1597 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1599 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001600 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001601#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001602 /* Delay processing of the PSK identity once we have
1603 * found out which algorithms to use. We keep a pointer
1604 * to the buffer and the size for later processing.
1605 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001606 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001607 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001608#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001609 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001610
XiaokangQianacb39922022-06-17 10:18:48 +00001611#if defined(MBEDTLS_SSL_ALPN)
1612 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1616 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001617 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1619 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001620 }
XiaokangQianacb39922022-06-17 10:18:48 +00001621 break;
1622#endif /* MBEDTLS_SSL_ALPN */
1623
Ronald Cron928cbd32022-10-04 16:14:26 +02001624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001625 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001627
Gabor Mezei078e8032022-04-27 21:17:56 +02001628 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 ssl, p, extension_data_end);
1630 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001631 MBEDTLS_SSL_DEBUG_RET(
1632 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001635 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001636#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637
Jan Bruckner151f6422023-02-10 12:45:19 +01001638#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1639 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1640 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1641
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001642 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1643 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001644 if (ret != 0) {
1645 MBEDTLS_SSL_DEBUG_RET(
1646 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1647 return ret;
1648 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001649 break;
1650#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1651
XiaokangQian7807f9f2022-02-15 10:04:37 +00001652 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001653 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001654 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001656 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001657 }
1658
1659 p += extension_data_len;
1660 }
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1663 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001664
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001665 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1666 MBEDTLS_SSL_HS_CLIENT_HELLO,
1667 p - buf);
1668 if (0 != ret) {
1669 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1670 return ret;
1671 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001672
Ronald Cron41a443a2022-10-04 16:38:25 +02001673#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001674 /* Update checksum with either
1675 * - The entire content of the CH message, if no PSK extension is present
1676 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001677 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001678 * ClientHello even if some pre-requisites for PSK key exchange modes are
1679 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001681 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001682 ret = handshake->update_checksum(ssl, buf,
1683 pre_shared_key_ext - buf);
1684 if (0 != ret) {
1685 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1686 return ret;
1687 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1689 pre_shared_key_ext,
1690 pre_shared_key_ext_end,
1691 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001692 cipher_suites_end,
1693 &psk);
1694 if (ret == 0) {
1695 got_psk = 1;
1696 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001697 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1699 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001700 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001702#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001703 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001704 ret = handshake->update_checksum(ssl, buf, p - buf);
1705 if (0 != ret) {
1706 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1707 return ret;
1708 }
Jerry Yu1c105562022-07-10 06:32:38 +00001709 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001710
Ronald Cron79cdd412024-02-20 17:19:28 +01001711 /*
1712 * Determine the key exchange algorithm to use.
1713 * There are three types of key exchanges supported in TLS 1.3:
1714 * - (EC)DH with ECDSA,
1715 * - (EC)DH with PSK,
1716 * - plain PSK.
1717 *
1718 * The PSK-based key exchanges may additionally be used with 0-RTT.
1719 *
1720 * Our built-in order of preference is
1721 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1722 * 2 ) Certificate Mode ( ephemeral )
1723 * 3 ) Plain PSK Mode ( psk )
1724 */
1725#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1726 if (got_psk && (psk.key_exchange_mode ==
1727 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1728 handshake->key_exchange_mode =
1729 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1730 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1731
1732 } else
1733#endif
1734 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1735 handshake->key_exchange_mode =
1736 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1737 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1738
1739 }
1740#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1741 else if (got_psk && (psk.key_exchange_mode ==
1742 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1743 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1744 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1745 }
1746#endif
1747 else {
1748 MBEDTLS_SSL_DEBUG_MSG(
1749 1,
1750 ("ClientHello message misses mandatory extensions."));
1751 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1752 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1753 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001755
Ronald Cron3e47eec2024-02-21 10:29:24 +01001756#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001757 if (handshake->key_exchange_mode &
1758 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1759 handshake->ciphersuite_info = psk.ciphersuite_info;
1760 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1761
1762 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1763 ((unsigned) psk.ciphersuite_info->id),
1764 psk.ciphersuite_info->name));
1765
1766 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1767 handshake->resume = 1;
1768 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001769 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001770#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001771
1772 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001773 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1774 hrr_required = (no_usable_share_for_key_agreement != 0);
1775 }
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001780}
1781
Jerry Yuab0da372023-02-08 13:55:24 +08001782#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001783static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001784{
1785 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1786
Jerry Yu985c9672022-12-04 14:06:30 +08001787 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1788 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001789 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001790 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001791 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001792 }
1793
Jerry Yu454dda32023-10-31 15:13:54 +08001794 if (!handshake->resume) {
1795 /* We currently support early data only in the case of PSKs established
1796 via a NewSessionTicket message thus in the case of a session
1797 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001798 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001799 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001800 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001801 }
1802
Jerry Yu82fd6c12023-10-31 16:32:19 +08001803 /* RFC 8446 4.2.10
1804 *
1805 * In order to accept early data, the server MUST have accepted a PSK cipher
1806 * suite and selected the first key offered in the client's "pre_shared_key"
1807 * extension. In addition, it MUST verify that the following values are the
1808 * same as those associated with the selected PSK:
1809 * - The TLS version number
1810 * - The selected cipher suite
1811 * - The selected ALPN [RFC7301] protocol, if any
1812 *
1813 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001814 * - The TLS version number is checked in
1815 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001816 */
1817
1818 if (handshake->selected_identity != 0) {
1819 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001820 1, ("EarlyData: rejected, the selected key in "
1821 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001822 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001823 }
1824
1825 if (handshake->ciphersuite_info->id !=
1826 ssl->session_negotiate->ciphersuite) {
1827 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001828 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1829 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001830 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001831
1832 }
Jerry Yu985c9672022-12-04 14:06:30 +08001833
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001834 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001835 MBEDTLS_SSL_DEBUG_MSG(
1836 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001837 ("EarlyData: rejected, early_data not allowed in ticket "
1838 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001839 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001840 }
Jerry Yu985c9672022-12-04 14:06:30 +08001841
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001842#if defined(MBEDTLS_SSL_ALPN)
1843 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1844 size_t alpn_len;
1845
1846 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1847 return 0;
1848 }
1849
1850 if (alpn != NULL) {
1851 alpn_len = strlen(alpn);
1852 }
1853
1854 if (alpn == NULL ||
1855 ssl->session_negotiate->ticket_alpn == NULL ||
1856 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1857 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1858 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1859 "from the one associated with the pre-shared key."));
1860 return -1;
1861 }
1862#endif
1863
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001864 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001865}
1866#endif /* MBEDTLS_SSL_EARLY_DATA */
1867
XiaokangQian75fe8c72022-06-15 09:42:45 +00001868/* Update the handshake state machine */
1869
Ronald Cronce7d76e2022-07-08 18:56:49 +02001870MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001871static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1872 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001873{
1874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1875
XiaokangQian7807f9f2022-02-15 10:04:37 +00001876 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001877 * Server certificate selection
1878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1880 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1881 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001882 }
1883#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1884 ssl->handshake->sni_name = NULL;
1885 ssl->handshake->sni_name_len = 0;
1886#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1889 if (ret != 0) {
1890 MBEDTLS_SSL_DEBUG_RET(1,
1891 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1892 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001893 }
1894
Jerry Yuab0da372023-02-08 13:55:24 +08001895#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001896 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001897 ssl->handshake->early_data_accepted =
1898 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001899
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001900 if (ssl->handshake->early_data_accepted) {
1901 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1902 if (ret != 0) {
1903 MBEDTLS_SSL_DEBUG_RET(
1904 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1905 return ret;
1906 }
1907 } else {
1908 ssl->discard_early_data_record =
1909 hrr_required ?
1910 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1911 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001912 }
1913 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001914#else
1915 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001916#endif /* MBEDTLS_SSL_EARLY_DATA */
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001919}
1920
1921/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001922 * Main entry point from the state machine; orchestrates the otherfunctions.
1923 */
1924
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001925MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001926static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001927{
1928
XiaokangQian08037552022-04-20 07:16:41 +00001929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001931 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001932 int parse_client_hello_ret;
1933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1937 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1938 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1941 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001942 parse_client_hello_ret = ret; /* Store positive return value of
1943 * parse_client_hello,
1944 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001945 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001946
Ronald Cronb828c7d2023-04-03 16:37:22 +02001947 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001948 * Version 1.2 of the protocol has to be used for the handshake.
1949 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001950 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1951 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1952 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1953 * will dispatch to the TLS 1.2 state machine.
1954 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001955 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001956 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001957 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001958 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001959 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001960 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001961 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1962 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1963 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001964 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001965 ssl->keep_current_message = 1;
1966 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1967 return 0;
1968 }
1969
Ronald Cron7b6ee942024-01-12 10:29:55 +01001970 MBEDTLS_SSL_PROC_CHK(
1971 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1972 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001973
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001974 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1976 } else {
1977 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1978 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001979
1980cleanup:
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1983 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001984}
1985
1986/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001987 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001988 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001989MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001990static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001991{
Jerry Yu637a3f12022-04-20 21:37:58 +08001992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1993 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1997 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1998 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1999 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002000 }
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2003 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002004
2005#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002006 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002007#endif /* MBEDTLS_HAVE_TIME */
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002010}
2011
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002013 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002014 *
2015 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002016 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002017 * } SupportedVersions;
2018 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002019MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002020static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 mbedtls_ssl_context *ssl,
2022 unsigned char *buf,
2023 unsigned char *end,
2024 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026 *out_len = 0;
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029
2030 /* Check if we have space to write the extension:
2031 * - extension_type (2 bytes)
2032 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002033 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 mbedtls_ssl_write_version(buf + 4,
2042 ssl->conf->transport,
2043 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2046 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047
Jerry Yu349a6132022-04-14 20:52:56 +08002048 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049
Jerry Yub95dd362022-11-08 21:19:34 +08002050 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002054}
2055
Jerry Yud9436a12022-04-20 22:28:09 +08002056
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057
2058/* Generate and export a single key share. For hybrid KEMs, this can
2059 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002061static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2062 uint16_t named_group,
2063 unsigned char *buf,
2064 unsigned char *end,
2065 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002066{
2067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002068
2069 *out_len = 0;
2070
Valerio Settic9ae8622023-07-25 11:23:50 +02002071#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002072 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002073 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002074 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 ssl, named_group, buf, end, out_len);
2076 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002077 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002078 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 ret);
2080 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002083#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (0 /* Other kinds of KEMs */) {
2085 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002086 ((void) ssl);
2087 ((void) named_group);
2088 ((void) buf);
2089 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2091 }
2092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094}
2095
2096/*
2097 * ssl_tls13_write_key_share_ext
2098 *
2099 * Structure of key_share extension in ServerHello:
2100 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002101 * struct {
2102 * NamedGroup group;
2103 * opaque key_exchange<1..2^16-1>;
2104 * } KeyShareEntry;
2105 * struct {
2106 * KeyShareEntry server_share;
2107 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002108 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002110static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2111 unsigned char *buf,
2112 unsigned char *end,
2113 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114{
Jerry Yu955ddd72022-04-22 22:27:33 +08002115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002116 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002117 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002119 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120
2121 *out_len = 0;
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2126 mbedtls_ssl_named_group_to_str(group),
2127 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002128
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129 /* Check if we have space for header and length fields:
2130 * - extension_type (2 bytes)
2131 * - extension_data_length (2 bytes)
2132 * - group (2 bytes)
2133 * - key_exchange_length (2 bytes)
2134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2136 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2137 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002139
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2141 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002142 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 ssl, group, server_share + 4, end, &key_exchange_length);
2144 if (ret != 0) {
2145 return ret;
2146 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002147 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152
Jerry Yu57d48412022-04-20 21:50:42 +08002153 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002158}
Jerry Yud9436a12022-04-20 22:28:09 +08002159
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002161static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2162 unsigned char *buf,
2163 unsigned char *end,
2164 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002165{
2166 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2167 /* key_share Extension
2168 *
2169 * struct {
2170 * select (Handshake.msg_type) {
2171 * ...
2172 * case hello_retry_request:
2173 * NamedGroup selected_group;
2174 * ...
2175 * };
2176 * } KeyShare;
2177 */
2178
2179 *out_len = 0;
2180
Jerry Yub0ac10b2022-05-05 11:10:08 +08002181 /*
2182 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2183 * of the HRR is then to transmit a cookie to force the client to demonstrate
2184 * reachability at their apparent network address (primarily useful for DTLS).
2185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2187 return 0;
2188 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002189
2190 /* We should only send the key_share extension if the client's initial
2191 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (ssl->handshake->offered_group_id != 0) {
2193 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2194 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195 }
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (selected_group == 0) {
2198 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2199 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002200 }
2201
Jerry Yub0ac10b2022-05-05 11:10:08 +08002202 /* Check if we have enough space:
2203 * - extension_type (2 bytes)
2204 * - extension_data_length (2 bytes)
2205 * - selected_group (2 bytes)
2206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2210 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2211 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_DEBUG_MSG(3,
2214 ("HRR selected_group: %s (%x)",
2215 mbedtls_ssl_named_group_to_str(selected_group),
2216 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002217
2218 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002223}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002224
2225/*
2226 * Structure of ServerHello message:
2227 *
2228 * struct {
2229 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2230 * Random random;
2231 * opaque legacy_session_id_echo<0..32>;
2232 * CipherSuite cipher_suite;
2233 * uint8 legacy_compression_method = 0;
2234 * Extension extensions<6..2^16-1>;
2235 * } ServerHello;
2236 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002237MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002238static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2239 unsigned char *buf,
2240 unsigned char *end,
2241 size_t *out_len,
2242 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002243{
Jerry Yu955ddd72022-04-22 22:27:33 +08002244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002246 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002247 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248
2249 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002250 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251
Jerry Yucfc04b32022-04-21 09:31:58 +08002252 /* ...
2253 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2254 * ...
2255 * with ProtocolVersion defined as:
2256 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2259 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260 p += 2;
2261
Jerry Yu1c3e6882022-04-20 21:23:40 +08002262 /* ...
2263 * Random random;
2264 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002265 * with Random defined as:
2266 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2269 if (is_hrr) {
2270 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2271 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2272 } else {
2273 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2274 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2277 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2279
Jerry Yucfc04b32022-04-21 09:31:58 +08002280 /* ...
2281 * opaque legacy_session_id_echo<0..32>;
2282 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2285 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2286 if (ssl->session_negotiate->id_len > 0) {
2287 memcpy(p, &ssl->session_negotiate->id[0],
2288 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2292 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 }
2294
Jerry Yucfc04b32022-04-21 09:31:58 +08002295 /* ...
2296 * CipherSuite cipher_suite;
2297 * ...
2298 * with CipherSuite defined as:
2299 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2302 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 MBEDTLS_SSL_DEBUG_MSG(3,
2305 ("server hello, chosen ciphersuite: %s ( id=%d )",
2306 mbedtls_ssl_get_ciphersuite_name(
2307 ssl->session_negotiate->ciphersuite),
2308 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309
Jerry Yucfc04b32022-04-21 09:31:58 +08002310 /* ...
2311 * uint8 legacy_compression_method = 0;
2312 * ...
2313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002315 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002316
Jerry Yucfc04b32022-04-21 09:31:58 +08002317 /* ...
2318 * Extension extensions<6..2^16-1>;
2319 * ...
2320 * struct {
2321 * ExtensionType extension_type; (2 bytes)
2322 * opaque extension_data<0..2^16-1>;
2323 * } Extension;
2324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002326 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327 p += 2;
2328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2330 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002331 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2333 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002334 }
2335 p += output_len;
2336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2338 if (is_hrr) {
2339 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2340 } else {
2341 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2342 }
2343 if (ret != 0) {
2344 return ret;
2345 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002346 p += output_len;
2347 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002348
Ronald Cron41a443a2022-10-04 16:38:25 +02002349#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2351 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2352 if (ret != 0) {
2353 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2354 ret);
2355 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002356 }
2357 p += output_len;
2358 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002359#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2364 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002365
Jerry Yud9436a12022-04-20 22:28:09 +08002366 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002369
Jerry Yu7de2ff02022-11-08 21:43:46 +08002370 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002371 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_HS_SERVER_HELLO,
2373 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002376}
2377
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002378MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002379static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002380{
2381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2383 if (ret != 0) {
2384 MBEDTLS_SSL_DEBUG_RET(1,
2385 "mbedtls_ssl_tls13_compute_handshake_transform",
2386 ret);
2387 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002388 }
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002391}
2392
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002393MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002394static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002395{
Jerry Yu637a3f12022-04-20 21:37:58 +08002396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002397 unsigned char *buf;
2398 size_t buf_len, msg_len;
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002403
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002404 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2405 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2408 buf + buf_len,
2409 &msg_len,
2410 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002411
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002412 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002413 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2416 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002417
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002418 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002419
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002420#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2421 /* The server sends a dummy change_cipher_spec record immediately
2422 * after its first handshake message. This may either be after
2423 * a ServerHello or a HelloRetryRequest.
2424 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002425 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002427#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002429#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002430
Jerry Yuf4b27e42022-03-30 17:32:21 +08002431cleanup:
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2434 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002435}
2436
Jerry Yu23d1a252022-05-12 18:08:59 +08002437
2438/*
2439 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2440 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002441MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002442static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002443{
2444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002445 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2447 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2448 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2449 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002450 }
2451
2452 /*
2453 * Create stateless transcript hash for HRR
2454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2456 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2457 if (ret != 0) {
2458 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2459 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002460 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002464}
2465
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002467static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002468{
2469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2470 unsigned char *buf;
2471 size_t buf_len, msg_len;
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2478 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2479 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2482 buf + buf_len,
2483 &msg_len,
2484 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002485 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002486 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002487
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2490 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002491
Ronald Cron5fbd2702024-02-14 10:03:36 +01002492 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002493
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002494#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2495 /* The server sends a dummy change_cipher_spec record immediately
2496 * after its first handshake message. This may either be after
2497 * a ServerHello or a HelloRetryRequest.
2498 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002499 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002501#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002503#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002504
2505cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2507 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002508}
2509
Jerry Yu5b64ae92022-03-30 17:15:02 +08002510/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002511 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2512 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002513
2514/*
2515 * struct {
2516 * Extension extensions<0..2 ^ 16 - 1>;
2517 * } EncryptedExtensions;
2518 *
2519 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002520MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002521static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2522 unsigned char *buf,
2523 unsigned char *end,
2524 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002525{
XiaokangQianacb39922022-06-17 10:18:48 +00002526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527 unsigned char *p = buf;
2528 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002529 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002530 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002531
Jerry Yu4d3841a2022-04-16 12:37:19 +08002532 *out_len = 0;
2533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002535 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002536 p += 2;
2537
Jerry Yu8937eb42022-05-03 12:12:14 +08002538 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002539 ((void) ret);
2540 ((void) output_len);
2541
2542#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2544 if (ret != 0) {
2545 return ret;
2546 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002547 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002548#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549
Jerry Yu71c14f12022-12-12 11:10:35 +08002550#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002551 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002552 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002553 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002554 if (ret != 0) {
2555 return ret;
2556 }
2557 p += output_len;
2558 }
2559#endif /* MBEDTLS_SSL_EARLY_DATA */
2560
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002561#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002562 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002563 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2564 ssl, p, end, &output_len);
2565 if (ret != 0) {
2566 return ret;
2567 }
2568 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002569 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002570#endif
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 extensions_len = (p - p_extensions_len) - 2;
2573 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002574
2575 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578
Jerry Yu7de2ff02022-11-08 21:43:46 +08002579 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002583}
2584
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002586static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587{
2588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2589 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002590 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 mbedtls_ssl_set_outbound_transform(ssl,
2593 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002594 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002599 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2600 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2601 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2604 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002606 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002607 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2608 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2611 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002612
Ronald Cron928cbd32022-10-04 16:14:26 +02002613#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2615 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2616 } else {
2617 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2618 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002619#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002621#endif
2622
Jerry Yu4d3841a2022-04-16 12:37:19 +08002623cleanup:
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2626 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002627}
2628
Ronald Cron928cbd32022-10-04 16:14:26 +02002629#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002630#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2631#define SSL_CERTIFICATE_REQUEST_SKIP 1
2632/* Coordination:
2633 * Check whether a CertificateRequest message should be written.
2634 * Returns a negative code on failure, or
2635 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2636 * - SSL_CERTIFICATE_REQUEST_SKIP
2637 * indicating if the writing of the CertificateRequest
2638 * should be skipped or not.
2639 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002640MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002641static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002642{
2643 int authmode;
2644
XiaokangQian40a35232022-05-07 09:02:40 +00002645#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002647 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002649#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002650 authmode = ssl->conf->authmode;
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002653 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002655 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002656
XiaokangQianc3017f62022-05-13 05:55:41 +00002657 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002660}
2661
XiaokangQiancec9ae62022-05-06 07:28:50 +00002662/*
2663 * struct {
2664 * opaque certificate_request_context<0..2^8-1>;
2665 * Extension extensions<2..2^16-1>;
2666 * } CertificateRequest;
2667 *
2668 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002669MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002670static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2671 unsigned char *buf,
2672 const unsigned char *end,
2673 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674{
2675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2676 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002677 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002678 unsigned char *p_extensions_len;
2679
2680 *out_len = 0;
2681
2682 /* Check if we have enough space:
2683 * - certificate_request_context (1 byte)
2684 * - extensions length (2 bytes)
2685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002687
2688 /*
2689 * Write certificate_request_context
2690 */
2691 /*
2692 * We use a zero length context for the normal handshake
2693 * messages. For post-authentication handshake messages
2694 * this request context would be set to a non-zero value.
2695 */
2696 *p++ = 0x0;
2697
2698 /*
2699 * Write extensions
2700 */
2701 /* The extensions must contain the signature_algorithms. */
2702 p_extensions_len = p;
2703 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2705 if (ret != 0) {
2706 return ret;
2707 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708
XiaokangQianec6efb92022-05-06 09:53:10 +00002709 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711
2712 *out_len = p - buf;
2713
Jerry Yu7de2ff02022-11-08 21:43:46 +08002714 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718}
2719
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002720MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002721static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722{
2723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730 unsigned char *buf;
2731 size_t buf_len, msg_len;
2732
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002733 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2734 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2735 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2738 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002740 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002741 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2742 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2745 ssl, buf_len, msg_len));
2746 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2747 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002748 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 } else {
2750 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2752 goto cleanup;
2753 }
2754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002756cleanup:
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2759 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002760}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002761
Jerry Yu4d3841a2022-04-16 12:37:19 +08002762/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002763 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002764 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002765MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002766static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002767{
Jerry Yu5a26f302022-05-10 20:46:40 +08002768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002769
2770#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2772 mbedtls_ssl_own_cert(ssl) == NULL) {
2773 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2774 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2775 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2776 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002777 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002778#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2781 if (ret != 0) {
2782 return ret;
2783 }
2784 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2785 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002786}
2787
2788/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002789 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002792static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002793{
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2795 if (ret != 0) {
2796 return ret;
2797 }
2798 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2799 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002800}
Ronald Cron928cbd32022-10-04 16:14:26 +02002801#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002802
Jerry Yu9b72e392023-12-01 16:27:08 +08002803/*
2804 * RFC 8446 section A.2
2805 *
2806 * | Send ServerHello
2807 * | K_send = handshake
2808 * | Send EncryptedExtensions
2809 * | [Send CertificateRequest]
2810 * Can send | [Send Certificate + CertificateVerify]
2811 * app data | Send Finished
2812 * after --> | K_send = application
2813 * here +--------+--------+
2814 * No 0-RTT | | 0-RTT
2815 * | |
2816 * K_recv = handshake | | K_recv = early data
2817 * [Skip decrypt errors] | +------> WAIT_EOED -+
2818 * | | Recv | | Recv EndOfEarlyData
2819 * | | early data | | K_recv = handshake
2820 * | +------------+ |
2821 * | |
2822 * +> WAIT_FLIGHT2 <--------+
2823 * |
2824 * +--------+--------+
2825 * No auth | | Client auth
2826 * | |
2827 * | v
2828 * | WAIT_CERT
2829 * | Recv | | Recv Certificate
2830 * | empty | v
2831 * | Certificate | WAIT_CV
2832 * | | | Recv
2833 * | v | CertificateVerify
2834 * +-> WAIT_FINISHED <---+
2835 * | Recv Finished
2836 *
2837 *
2838 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002839 * above diagram. We are not going to receive early data related messages
2840 * anymore, prepare to receive the first handshake message of the client
2841 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002842 */
Jerry Yu3be85072023-12-04 09:58:54 +08002843static void ssl_tls13_prepare_for_handshake_second_flight(
2844 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002845{
Jerry Yu9b72e392023-12-01 16:27:08 +08002846 if (ssl->handshake->certificate_request_sent) {
2847 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2848 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002849 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002850 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002851
Jerry Yu9b72e392023-12-01 16:27:08 +08002852 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2853 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002854}
2855
Jerry Yu83da34e2022-04-16 13:59:52 +08002856/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002857 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002858 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002859MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002860static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002861{
Jerry Yud6e253d2022-05-18 13:59:24 +08002862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2865 if (ret != 0) {
2866 return ret;
2867 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2870 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002871 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2873 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2874 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002875 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002876
Jerry Yu87b5ed42022-12-12 13:07:07 +08002877#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002878 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002879 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002880 MBEDTLS_SSL_DEBUG_MSG(
2881 1, ("Switch to early keys for inbound traffic. "
2882 "( K_recv = early data )"));
2883 mbedtls_ssl_set_inbound_transform(
2884 ssl, ssl->handshake->transform_earlydata);
2885 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2886 return 0;
2887 }
2888#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002889 MBEDTLS_SSL_DEBUG_MSG(
2890 1, ("Switch to handshake keys for inbound traffic "
2891 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002893
Jerry Yu3be85072023-12-04 09:58:54 +08002894 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002895
2896 return 0;
2897}
2898
Jerry Yu87b5ed42022-12-12 13:07:07 +08002899#if defined(MBEDTLS_SSL_EARLY_DATA)
2900/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002901 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002902 */
Jerry Yu3be85072023-12-04 09:58:54 +08002903#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002904#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002905/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002906 * Deals with the ambiguity of not knowing if the next message is an
2907 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002908 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002909 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002910 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002911 * indicating which message is received.
2912 */
2913MBEDTLS_CHECK_RETURN_CRITICAL
2914static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2915{
2916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002917
2918 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2919 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2920 return ret;
2921 }
2922 ssl->keep_current_message = 1;
2923
2924 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2925 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002926 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002927 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002928 }
2929
2930 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002931 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002932 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002933 /* Set the reading pointer */
2934 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002935 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2936 if (ret != 0) {
2937 return ret;
2938 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002939 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002940 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002941 }
2942
Jerry Yu7bb40a32023-12-04 10:04:15 +08002943 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2944 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002945 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002946}
2947
2948MBEDTLS_CHECK_RETURN_CRITICAL
2949static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2950 const unsigned char *buf,
2951 const unsigned char *end)
2952{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002953 /* RFC 8446 section 4.5
2954 *
2955 * struct {} EndOfEarlyData;
2956 */
Jerry Yufbf03992023-12-04 10:00:37 +08002957 if (buf != end) {
2958 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2959 MBEDTLS_ERR_SSL_DECODE_ERROR);
2960 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2961 }
2962 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002963}
2964
Jerry Yud5c34962023-12-01 16:32:31 +08002965/*
2966 * RFC 8446 section A.2
2967 *
2968 * | Send ServerHello
2969 * | K_send = handshake
2970 * | Send EncryptedExtensions
2971 * | [Send CertificateRequest]
2972 * Can send | [Send Certificate + CertificateVerify]
2973 * app data | Send Finished
2974 * after --> | K_send = application
2975 * here +--------+--------+
2976 * No 0-RTT | | 0-RTT
2977 * | |
2978 * K_recv = handshake | | K_recv = early data
2979 * [Skip decrypt errors] | +------> WAIT_EOED -+
2980 * | | Recv | | Recv EndOfEarlyData
2981 * | | early data | | K_recv = handshake
2982 * | +------------+ |
2983 * | |
2984 * +> WAIT_FLIGHT2 <--------+
2985 * |
2986 * +--------+--------+
2987 * No auth | | Client auth
2988 * | |
2989 * | v
2990 * | WAIT_CERT
2991 * | Recv | | Recv Certificate
2992 * | empty | v
2993 * | Certificate | WAIT_CV
2994 * | | | Recv
2995 * | v | CertificateVerify
2996 * +-> WAIT_FINISHED <---+
2997 * | Recv Finished
2998 *
2999 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3000 * the above diagram.
3001 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003002MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003003static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003004{
3005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003006
3007 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3008
3009 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3010
Jerry Yu3be85072023-12-04 09:58:54 +08003011 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003012 unsigned char *buf;
3013 size_t buf_len;
3014
3015 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3016 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3017 &buf, &buf_len));
3018
3019 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3020 ssl, buf, buf + buf_len));
3021
Jerry Yue9655122023-12-01 16:44:40 +08003022 MBEDTLS_SSL_DEBUG_MSG(
3023 1, ("Switch to handshake keys for inbound traffic"
3024 "( K_recv = handshake )"));
3025 mbedtls_ssl_set_inbound_transform(
3026 ssl, ssl->handshake->transform_handshake);
3027
Jerry Yud5c34962023-12-01 16:32:31 +08003028 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3029 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3030 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003031
Jerry Yu3be85072023-12-04 09:58:54 +08003032 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003033
Jerry Yub55f9eb2023-12-05 10:27:17 +08003034 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003035 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3036 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003037 } else {
3038 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3039 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3040 goto cleanup;
3041 }
3042
Jerry Yud5c34962023-12-01 16:32:31 +08003043cleanup:
3044 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003045 return ret;
3046}
3047#endif /* MBEDTLS_SSL_EARLY_DATA */
3048
Jerry Yu69dd8d42022-04-16 12:51:26 +08003049/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003050 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003051 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003052MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003053static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003054{
Jerry Yud6e253d2022-05-18 13:59:24 +08003055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3058 if (ret != 0) {
3059 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003060 }
3061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3063 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003064 MBEDTLS_SSL_DEBUG_RET(
3065 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 }
3067
3068 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3069 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003070}
3071
3072/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003073 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003074 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003075MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003076static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003077{
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003081
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003082#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3083 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003084/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3085 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3086 * expected to be resolved with issue#6395.
3087 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003088 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003089 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003090 mbedtls_ssl_handshake_set_state(
3091 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003092 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003093#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003094 {
3095 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003098}
3099
Norbert Fabritius96eed722023-01-23 15:24:59 +01003100#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003101/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003102 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003103 */
3104#define SSL_NEW_SESSION_TICKET_SKIP 0
3105#define SSL_NEW_SESSION_TICKET_WRITE 1
3106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003107static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003108{
3109 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (ssl->conf->f_ticket_write == NULL) {
3111 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3112 " callback is not set"));
3113 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 if (ssl->conf->new_session_tickets_count == 0) {
3116 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3117 " configured count is zero"));
3118 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003119 }
3120
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (ssl->handshake->new_session_tickets_count == 0) {
3122 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3123 "been sent."));
3124 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003125 }
3126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003128}
3129
Jerry Yue67bef42022-07-07 07:29:42 +00003130MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003131static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3132 unsigned char *ticket_nonce,
3133 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003134{
3135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3136 mbedtls_ssl_session *session = ssl->session;
3137 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3138 psa_algorithm_t psa_hash_alg;
3139 int hash_length;
3140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003142
Pengyu Lv9f926952022-11-17 15:22:33 +08003143 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003144 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003145 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003146#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003147 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003148 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003149#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003150
3151#if defined(MBEDTLS_SSL_EARLY_DATA)
3152 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3153 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003154 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003155 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003156 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003157 }
3158#endif /* MBEDTLS_SSL_EARLY_DATA */
3159
Pengyu Lv4938a562023-01-16 11:28:49 +08003160 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003161
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003162#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003163 if (session->ticket_alpn == NULL) {
3164 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3165 if (ret != 0) {
3166 return ret;
3167 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003168 }
3169#endif
3170
Jerry Yue67bef42022-07-07 07:29:42 +00003171 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3173 (unsigned char *) &session->ticket_age_add,
3174 sizeof(session->ticket_age_add)) != 0)) {
3175 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3176 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3179 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003180
3181 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3183 if (ret != 0) {
3184 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3185 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003186 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3188 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003189
3190 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003192 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3194 if (hash_length == -1 ||
3195 (size_t) hash_length > sizeof(session->resumption_key)) {
3196 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003197 }
3198
Jerry Yue67bef42022-07-07 07:29:42 +00003199 /* In this code the psk key length equals the length of the hash */
3200 session->resumption_key_len = hash_length;
3201 session->ciphersuite = ciphersuite_info->id;
3202
3203 /* Compute resumption key
3204 *
3205 * HKDF-Expand-Label( resumption_master_secret,
3206 * "resumption", ticket_nonce, Hash.length )
3207 */
3208 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 psa_hash_alg,
3210 session->app_secrets.resumption_master_secret,
3211 hash_length,
3212 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3213 ticket_nonce,
3214 ticket_nonce_size,
3215 session->resumption_key,
3216 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 if (ret != 0) {
3219 MBEDTLS_SSL_DEBUG_RET(2,
3220 "Creating the ticket-resumed PSK failed",
3221 ret);
3222 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3225 session->resumption_key,
3226 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3229 session->app_secrets.resumption_master_secret,
3230 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003233}
3234
3235/* This function creates a NewSessionTicket message in the following format:
3236 *
3237 * struct {
3238 * uint32 ticket_lifetime;
3239 * uint32 ticket_age_add;
3240 * opaque ticket_nonce<0..255>;
3241 * opaque ticket<1..2^16-1>;
3242 * Extension extensions<0..2^16-2>;
3243 * } NewSessionTicket;
3244 *
3245 * The ticket inside the NewSessionTicket message is an encrypted container
3246 * carrying the necessary information so that the server is later able to
3247 * re-start the communication.
3248 *
3249 * The following fields are placed inside the ticket by the
3250 * f_ticket_write() function:
3251 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003252 * - creation time (ticket_creation_time)
3253 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003254 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003255 * - key (resumption_key)
3256 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003257 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003258 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003259 */
3260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003261static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3262 unsigned char *buf,
3263 unsigned char *end,
3264 size_t *out_len,
3265 unsigned char *ticket_nonce,
3266 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003267{
3268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3269 unsigned char *p = buf;
3270 mbedtls_ssl_session *session = ssl->session;
3271 size_t ticket_len;
3272 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003273 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003274
3275 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003277
3278 /*
3279 * ticket_lifetime 4 bytes
3280 * ticket_age_add 4 bytes
3281 * ticket_nonce 1 + ticket_nonce_size bytes
3282 * ticket >=2 bytes
3283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003285
3286 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003287#if defined(MBEDTLS_HAVE_TIME)
3288 session->ticket_creation_time = mbedtls_ms_time();
3289#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3291 session,
3292 p + 9 + ticket_nonce_size + 2,
3293 end,
3294 &ticket_len,
3295 &ticket_lifetime);
3296 if (ret != 0) {
3297 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3298 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003299 }
Jerry Yuce794882023-11-22 15:01:18 +08003300
3301 /* RFC 8446 section 4.6.1
3302 *
Jerry Yue67bef42022-07-07 07:29:42 +00003303 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003304 * unsigned integer in network byte order from the time of ticket
3305 * issuance. Servers MUST NOT use any value greater than
3306 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003307 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003308 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003309 MBEDTLS_SSL_DEBUG_MSG(
3310 1, ("Ticket lifetime (%u) is greater than 7 days.",
3311 (unsigned int) ticket_lifetime));
3312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 }
Jerry Yuce794882023-11-22 15:01:18 +08003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3316 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3317 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003318
3319 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3321 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3322 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003323
3324 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 p[8] = (unsigned char) ticket_nonce_size;
3326 if (ticket_nonce_size > 0) {
3327 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003328 }
3329 p += 9 + ticket_nonce_size;
3330
3331 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003333 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003335 p += ticket_len;
3336
3337 /* Ticket Extensions
3338 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003339 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003340 */
Jerry Yuedab6372022-10-31 14:37:31 +08003341 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003344 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003345 p += 2;
3346
Jerry Yu01da35e2022-12-12 15:09:22 +08003347#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003348 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003349 size_t output_len;
3350
Jerry Yuf135bac2023-11-23 18:10:51 +08003351 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003352 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003353 MBEDTLS_SSL_DEBUG_RET(
3354 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3355 return ret;
3356 }
3357 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003358 } else {
3359 MBEDTLS_SSL_DEBUG_MSG(
3360 4, ("early_data not allowed, "
3361 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003362 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003363
Jerry Yu01da35e2022-12-12 15:09:22 +08003364#endif /* MBEDTLS_SSL_EARLY_DATA */
3365
3366 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3367
Jerry Yue67bef42022-07-07 07:29:42 +00003368 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3370 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003371
Jerry Yu7de2ff02022-11-08 21:43:46 +08003372 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003376}
3377
3378/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003379 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003381static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003382{
3383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003388 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3389 unsigned char *buf;
3390 size_t buf_len, msg_len;
3391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3393 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003394
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003395 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3396 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3397 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3400 ssl, buf, buf + buf_len, &msg_len,
3401 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3404 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003405
Jerry Yu359e65f2022-09-22 23:47:43 +08003406 /* Limit session tickets count to one when resumption connection.
3407 *
3408 * See document of mbedtls_ssl_conf_new_session_tickets.
3409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003411 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003413 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003415
Jerry Yua8d3c502022-10-30 14:51:23 +08003416 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3418 } else {
3419 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003420 }
3421
Jerry Yue67bef42022-07-07 07:29:42 +00003422cleanup:
3423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003425}
3426#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3427
3428/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003429 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003431int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003432{
XiaokangQian08037552022-04-20 07:16:41 +00003433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3436 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3437 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003440 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444 /* start state */
3445 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003447 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003448 break;
3449
XiaokangQian7807f9f2022-02-15 10:04:37 +00003450 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ret = ssl_tls13_process_client_hello(ssl);
3452 if (ret != 0) {
3453 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3454 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003455 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003456
Jerry Yuf41553b2022-05-09 22:20:30 +08003457 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_write_hello_retry_request(ssl);
3459 if (ret != 0) {
3460 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3461 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003462 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003463 break;
3464
Jerry Yu5b64ae92022-03-30 17:15:02 +08003465 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003467 break;
3468
Xiaofei Baicba64af2022-02-15 10:00:56 +00003469 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = ssl_tls13_write_encrypted_extensions(ssl);
3471 if (ret != 0) {
3472 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3473 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003474 }
Jerry Yu48330562022-05-06 21:35:44 +08003475 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003476
Ronald Cron928cbd32022-10-04 16:14:26 +02003477#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003478 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003480 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003481
Jerry Yu83da34e2022-04-16 13:59:52 +08003482 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003484 break;
3485
3486 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003488 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003489#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003490
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 /*
3492 * Injection of dummy-CCS's for middlebox compatibility
3493 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003494#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003495 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3497 if (ret == 0) {
3498 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3499 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003500 break;
3501
3502 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003503 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3504 if (ret != 0) {
3505 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003507 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003508 break;
3509#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3510
Jerry Yu69dd8d42022-04-16 12:51:26 +08003511 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003513 break;
3514
Jerry Yu87b5ed42022-12-12 13:07:07 +08003515#if defined(MBEDTLS_SSL_EARLY_DATA)
3516 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003517 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003518 break;
3519#endif /* MBEDTLS_SSL_EARLY_DATA */
3520
Jerry Yu69dd8d42022-04-16 12:51:26 +08003521 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003523 break;
3524
Jerry Yu69dd8d42022-04-16 12:51:26 +08003525 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003527 break;
3528
Ronald Cron766c0cd2022-10-18 12:17:11 +02003529#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003530 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3532 if (ret == 0) {
3533 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003534 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3536 } else {
3537 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003538 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003540 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003541 }
3542 break;
3543
3544 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3546 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003547 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003549 }
3550 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003551#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003552
Jerry Yue67bef42022-07-07 07:29:42 +00003553#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003554 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 ret = ssl_tls13_write_new_session_ticket(ssl);
3556 if (ret != 0) {
3557 MBEDTLS_SSL_DEBUG_RET(1,
3558 "ssl_tls13_write_new_session_ticket ",
3559 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003560 }
3561 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003562 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003563 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003564 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003565 * as part of ssl_prepare_handshake_step.
3566 */
3567 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003568
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 if (ssl->handshake->new_session_tickets_count == 0) {
3570 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3571 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003572 mbedtls_ssl_handshake_set_state(
3573 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003574 }
Jerry Yue67bef42022-07-07 07:29:42 +00003575 break;
3576
3577#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3578
XiaokangQian7807f9f2022-02-15 10:04:37 +00003579 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3581 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003582 }
3583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003585}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003586
Jerry Yufb4b6472022-01-27 15:03:26 +08003587#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */